# ai_controller.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_controller.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    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
    00017 #include "ai_controller.hpp"
    00018 #include "../ai_storage.hpp"
    00019 #include "../ai_instance.hpp"
    00020 #include "../ai_config.hpp"
    00021 #include "ai_log.hpp"
    00022
    00023 /* static */ void AIController::SetCommandDelay(int ticks)
    00024 {
    00025   if (ticks <= 0) return;
    00026   AIObject::SetDoCommandDelay(ticks);
    00027 }
    00028
    00029 /* static */ 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 /* static */ 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 /* static */ uint AIController::GetTick()
    00065 {
    00066   return ::Company::Get(_current_company)->ai_instance->GetController()->ticks;
    00067 }
    00068
    00069 /* static */ int AIController::GetSetting(const char *name)
    00070 {
    00071   return AIConfig::GetConfig(_current_company)->GetSetting(name);
    00072 }
    00073
    00074 /* static */ 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 }