ai_controller.cpp

Go to the documentation of this file.
00001 /* $Id: ai_controller.cpp 17500 2009-09-11 17:18:06Z yexo $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "../../string_func.h"
00014 #include "../../company_base.h"
00015 #include "../../rev.h"
00016 #include "table/strings.h"
00017 
00018 #include "../ai.hpp"
00019 #include "ai_controller.hpp"
00020 #include "../ai_storage.hpp"
00021 #include "../ai_instance.hpp"
00022 #include "../ai_config.hpp"
00023 #include "ai_log.hpp"
00024 
00025 /* static */ void AIController::SetCommandDelay(int ticks)
00026 {
00027   if (ticks <= 0) return;
00028   AIObject::SetDoCommandDelay(ticks);
00029 }
00030 
00031 /* static */ void AIController::Sleep(int ticks)
00032 {
00033   if (!AIObject::CanSuspend()) {
00034     throw AI_FatalError("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator.");
00035   }
00036 
00037   if (ticks <= 0) {
00038     AILog::Warning("Sleep() value should be > 0. Assuming value 1.");
00039     ticks = 1;
00040   }
00041 
00042   throw AI_VMSuspend(ticks, NULL);
00043 }
00044 
00045 /* static */ void AIController::Print(bool error_msg, const char *message)
00046 {
00047   AILog::Log(error_msg ? AILog::LOG_SQ_ERROR : AILog::LOG_SQ_INFO, message);
00048 }
00049 
00050 AIController::AIController() :
00051   ticks(0),
00052   loaded_library_count(0)
00053 {
00054 }
00055 
00056 AIController::~AIController()
00057 {
00058   for (LoadedLibraryList::iterator iter = this->loaded_library.begin(); iter != this->loaded_library.end(); iter++) {
00059     free((void *)(*iter).second);
00060     free((void *)(*iter).first);
00061   }
00062 
00063   this->loaded_library.clear();
00064 }
00065 
00066 /* static */ uint AIController::GetTick()
00067 {
00068   return ::Company::Get(_current_company)->ai_instance->GetController()->ticks;
00069 }
00070 
00071 /* static */ int AIController::GetSetting(const char *name)
00072 {
00073   return AIConfig::GetConfig(_current_company)->GetSetting(name);
00074 }
00075 
00076 /* static */ uint AIController::GetVersion()
00077 {
00078   return _openttd_newgrf_version;
00079 }
00080 
00081 bool AIController::LoadedLibrary(const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len)
00082 {
00083   LoadedLibraryList::iterator iter = this->loaded_library.find(library_name);
00084   if (iter == this->loaded_library.end()) {
00085     *next_number = ++this->loaded_library_count;
00086     return false;
00087   }
00088 
00089   ttd_strlcpy(fake_class_name, (*iter).second, fake_class_name_len);
00090   return true;
00091 }
00092 
00093 void AIController::AddLoadedLibrary(const char *library_name, const char *fake_class_name)
00094 {
00095   this->loaded_library[strdup(library_name)] = strdup(fake_class_name);
00096 }

Generated on Wed Dec 23 23:27:48 2009 for OpenTTD by  doxygen 1.5.6