# ai_controller.cpp

Go to the documentation of this file.

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