# ai_controller.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_controller.cpp 15462 2009-02-13 00:18:56Z yexo $ */
    00002
    00005 #include "../../stdafx.h"
    00006 #include "../../string_func.h"
    00007 #include "../../company_base.h"
    00008 #include "table/strings.h"
    00009
    00010 #include "../ai.hpp"
    00011 #include "ai_controller.hpp"
    00012 #include "../ai_storage.hpp"
    00013 #include "../ai_instance.hpp"
    00014 #include "../ai_config.hpp"
    00015 #include "ai_log.hpp"
    00016
    00017 /* static */ void AIController::SetCommandDelay(int ticks)
    00018 {
    00019   if (ticks <= 0) return;
    00020   AIObject::SetDoCommandDelay(ticks);
    00021 }
    00022
    00023 /* static */ void AIController::Sleep(int ticks)
    00024 {
    00025   if (!AIObject::GetAllowDoCommand()) {
    00026     AILog::Error("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator.\n");
    00027     return;
    00028   }
    00029
    00030   if (ticks <= 0) {
    00031     AILog::Warning("Sleep() value should be > 0. Assuming value 1.");
    00032     ticks = 1;
    00033   }
    00034
    00035   throw AI_VMSuspend(ticks, NULL);
    00036 }
    00037
    00038 /* static */ void AIController::Print(bool error_msg, const char *message)
    00039 {
    00040   AILog::Log(error_msg ? AILog::LOG_SQ_ERROR : AILog::LOG_SQ_INFO, message);
    00041 }
    00042
    00043 AIController::AIController() :
    00044   ticks(0),
    00045   loaded_library_count(0)
    00046 {
    00047 }
    00048
    00049 AIController::~AIController()
    00050 {
    00051   for (LoadedLibraryList::iterator iter = this->loaded_library.begin(); iter != this->loaded_library.end(); iter++) {
    00052     free((void *)(*iter).second);
    00053     free((void *)(*iter).first);
    00054   }
    00055
    00056   this->loaded_library.clear();
    00057 }
    00058
    00059 /* static */ uint AIController::GetTick()
    00060 {
    00061   return ::GetCompany(_current_company)->ai_instance->GetController()->ticks;
    00062 }
    00063
    00064 /* static */ int AIController::GetSetting(const char *name)
    00065 {
    00066   return AIConfig::GetConfig(_current_company)->GetSetting(name);
    00067 }
    00068
    00069 bool AIController::LoadedLibrary(const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len)
    00070 {
    00071   LoadedLibraryList::iterator iter = this->loaded_library.find(library_name);
    00072   if (iter == this->loaded_library.end()) {
    00073     *next_number = ++this->loaded_library_count;
    00074     return false;
    00075   }
    00076
    00077   ttd_strlcpy(fake_class_name, (*iter).second, fake_class_name_len);
    00078   return true;
    00079 }
    00080
    00081 void AIController::AddLoadedLibrary(const char *library_name, const char *fake_class_name)
    00082 {
    00083   this->loaded_library[strdup(library_name)] = strdup(fake_class_name);
    00084 }