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