ai_controller.cpp
Go to the documentation of this file.00001
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 void AIController::SetCommandDelay(int ticks)
00019 {
00020 if (ticks <= 0) return;
00021 AIObject::SetDoCommandDelay(ticks);
00022 }
00023
00024 void AIController::Sleep(int ticks)
00025 {
00026 if (!AIObject::CanSuspend()) {
00027 throw AI_FatalError("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator.");
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 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 uint AIController::GetTick()
00060 {
00061 return ::GetCompany(_current_company)->ai_instance->GetController()->ticks;
00062 }
00063
00064 int AIController::GetSetting(const char *name)
00065 {
00066 return AIConfig::GetConfig(_current_company)->GetSetting(name);
00067 }
00068
00069 uint AIController::GetVersion()
00070 {
00071 return _openttd_newgrf_version;
00072 }
00073
00074 bool AIController::LoadedLibrary(const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len)
00075 {
00076 LoadedLibraryList::iterator iter = this->loaded_library.find(library_name);
00077 if (iter == this->loaded_library.end()) {
00078 *next_number = ++this->loaded_library_count;
00079 return false;
00080 }
00081
00082 ttd_strlcpy(fake_class_name, (*iter).second, fake_class_name_len);
00083 return true;
00084 }
00085
00086 void AIController::AddLoadedLibrary(const char *library_name, const char *fake_class_name)
00087 {
00088 this->loaded_library[strdup(library_name)] = strdup(fake_class_name);
00089 }