# ai_core.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "../core/backup_type.hpp"
    00014 #include "../core/bitmath_func.hpp"
    00015 #include "../company_base.h"
    00016 #include "../company_func.h"
    00017 #include "../network/network.h"
    00018 #include "../window_func.h"
    00019 #include "ai_scanner.hpp"
    00020 #include "ai_instance.hpp"
    00021 #include "ai_config.hpp"
    00022 #include "ai_info.hpp"
    00023 #include "ai.hpp"
    00024
    00025 /* static */ uint AI::frame_counter = 0;
    00026 /* static */ AIScannerInfo *AI::scanner_info = NULL;
    00027 /* static */ AIScannerLibrary *AI::scanner_library = NULL;
    00028
    00029 /* static */ bool AI::CanStartNew()
    00030 {
    00031   /* Only allow new AIs on the server and only when that is allowed in multiplayer */
    00032   return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
    00033 }
    00034
    00035 /* static */ void AI::StartNew(CompanyID company, bool rerandomise_ai)
    00036 {
    00037   assert(Company::IsValidID(company));
    00038
    00039   /* Clients shouldn't start AIs */
    00040   if (_networking && !_network_server) return;
    00041
    00042   AIConfig *config = AIConfig::GetConfig(company);
    00043   AIInfo *info = config->GetInfo();
    00044   if (info == NULL || (rerandomise_ai && config->IsRandom())) {
    00045     info = AI::scanner_info->SelectRandomAI();
    00046     assert(info != NULL);
    00047     /* Load default data and store the name in the settings */
    00048     config->Change(info->GetName(), -1, false, true);
    00049   }
    00050
    00051   Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00052   Company *c = Company::Get(company);
    00053
    00054   c->ai_info = info;
    00055   assert(c->ai_instance == NULL);
    00056   c->ai_instance = new AIInstance();
    00057   c->ai_instance->Initialize(info);
    00058
    00059   cur_company.Restore();
    00060
    00061   InvalidateWindowData(WC_AI_DEBUG, 0, -1);
    00062   return;
    00063 }
    00064
    00065 /* static */ void AI::GameLoop()
    00066 {
    00067   /* If we are in networking, only servers run this function, and that only if it is allowed */
    00068   if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
    00069
    00070   /* The speed with which AIs go, is limited by the 'competitor_speed' */
    00071   AI::frame_counter++;
    00072   assert(_settings_game.difficulty.competitor_speed <= 4);
    00073   if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
    00074
    00075   Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
    00076   const Company *c;
    00077   FOR_ALL_COMPANIES(c) {
    00078     if (c->is_ai) {
    00079       cur_company.Change(c->index);
    00080       c->ai_instance->GameLoop();
    00081     }
    00082   }
    00083   cur_company.Restore();
    00084
    00085   /* Occasionally collect garbage; every 255 ticks do one company.
    00086    * Effectively collecting garbage once every two months per AI. */
    00087   if ((AI::frame_counter & 255) == 0) {
    00088     CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
    00089     if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
    00090   }
    00091 }
    00092
    00093 /* static */ uint AI::GetTick()
    00094 {
    00095   return AI::frame_counter;
    00096 }
    00097
    00098 /* static */ void AI::Stop(CompanyID company)
    00099 {
    00100   if (_networking && !_network_server) return;
    00101
    00102   Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00103   Company *c = Company::Get(company);
    00104
    00105   delete c->ai_instance;
    00106   c->ai_instance = NULL;
    00107   c->ai_info = NULL;
    00108
    00109   cur_company.Restore();
    00110
    00111   InvalidateWindowData(WC_AI_DEBUG, 0, -1);
    00112   DeleteWindowById(WC_AI_SETTINGS, company);
    00113 }
    00114
    00115 /* static */ void AI::Suspend(CompanyID company)
    00116 {
    00117   if (_networking && !_network_server) return;
    00118
    00119   Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00120   Company::Get(company)->ai_instance->Suspend();
    00121
    00122   cur_company.Restore();
    00123 }
    00124
    00125 /* static */ void AI::KillAll()
    00126 {
    00127   /* It might happen there are no companies .. than we have nothing to loop */
    00128   if (Company::GetPoolSize() == 0) return;
    00129
    00130   const Company *c;
    00131   FOR_ALL_COMPANIES(c) {
    00132     if (c->is_ai) AI::Stop(c->index);
    00133   }
    00134 }
    00135
    00136 /* static */ void AI::Initialize()
    00137 {
    00138   if (AI::scanner_info != NULL) AI::Uninitialize(true);
    00139
    00140   AI::frame_counter = 0;
    00141   if (AI::scanner_info == NULL) {
    00142     TarScanner::DoScan(TarScanner::AI);
    00143     AI::scanner_info = new AIScannerInfo();
    00144     AI::scanner_info->Initialize();
    00145     AI::scanner_library = new AIScannerLibrary();
    00146     AI::scanner_library->Initialize();
    00147   }
    00148 }
    00149
    00150 /* static */ void AI::Uninitialize(bool keepConfig)
    00151 {
    00152   AI::KillAll();
    00153
    00154   if (keepConfig) {
    00155     /* Run a rescan, which indexes all AIInfos again, and check if we can
    00156      *  still load all the AIS, while keeping the configs in place */
    00157     Rescan();
    00158   } else {
    00159     delete AI::scanner_info;
    00160     delete AI::scanner_library;
    00161     AI::scanner_info = NULL;
    00162     AI::scanner_library = NULL;
    00163
    00164     for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    00165       if (_settings_game.ai_config[c] != NULL) {
    00166         delete _settings_game.ai_config[c];
    00167         _settings_game.ai_config[c] = NULL;
    00168       }
    00169       if (_settings_newgame.ai_config[c] != NULL) {
    00170         delete _settings_newgame.ai_config[c];
    00171         _settings_newgame.ai_config[c] = NULL;
    00172       }
    00173     }
    00174   }
    00175 }
    00176
    00177 /* static */ void AI::ResetConfig()
    00178 {
    00179   /* Check for both newgame as current game if we can reload the AIInfo insde
    00180    *  the AIConfig. If not, remove the AI from the list (which will assign
    00181    *  a random new AI on reload). */
    00182   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    00183     if (_settings_game.ai_config[c] != NULL && _settings_game.ai_config[c]->HasScript()) {
    00184       if (!_settings_game.ai_config[c]->ResetInfo(true)) {
    00185         DEBUG(script, 0, "After a reload, the AI by the name '%s' was no longer found, and removed from the list.", _settings_game.ai_config[c]->GetName());
    00186         _settings_game.ai_config[c]->Change(NULL);
    00187         if (Company::IsValidAiID(c)) {
    00188           /* The code belonging to an already running AI was deleted. We can only do
    00189            * one thing here to keep everything sane and that is kill the AI. After
    00190            * killing the offending AI we start a random other one in it's place, just
    00191            * like what would happen if the AI was missing during loading. */
    00192           AI::Stop(c);
    00193           AI::StartNew(c, false);
    00194         }
    00195       } else if (Company::IsValidAiID(c)) {
    00196         /* Update the reference in the Company struct. */
    00197         Company::Get(c)->ai_info = _settings_game.ai_config[c]->GetInfo();
    00198       }
    00199     }
    00200     if (_settings_newgame.ai_config[c] != NULL && _settings_newgame.ai_config[c]->HasScript()) {
    00201       if (!_settings_newgame.ai_config[c]->ResetInfo(false)) {
    00202         DEBUG(script, 0, "After a reload, the AI by the name '%s' was no longer found, and removed from the list.", _settings_newgame.ai_config[c]->GetName());
    00203         _settings_newgame.ai_config[c]->Change(NULL);
    00204       }
    00205     }
    00206   }
    00207 }
    00208
    00209 /* static */ void AI::NewEvent(CompanyID company, ScriptEvent *event)
    00210 {
    00211   /* AddRef() and Release() need to be called at least once, so do it here */
    00212   event->AddRef();
    00213
    00214   /* Clients should ignore events */
    00215   if (_networking && !_network_server) {
    00216     event->Release();
    00217     return;
    00218   }
    00219
    00220   /* Only AIs can have an event-queue */
    00221   if (!Company::IsValidAiID(company)) {
    00222     event->Release();
    00223     return;
    00224   }
    00225
    00226   /* Queue the event */
    00227   Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00228   Company::Get(_current_company)->ai_instance->InsertEvent(event);
    00229   cur_company.Restore();
    00230
    00231   event->Release();
    00232 }
    00233
    00234 /* static */ void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company)
    00235 {
    00236   /* AddRef() and Release() need to be called at least once, so do it here */
    00237   event->AddRef();
    00238
    00239   /* Clients should ignore events */
    00240   if (_networking && !_network_server) {
    00241     event->Release();
    00242     return;
    00243   }
    00244
    00245   /* Try to send the event to all AIs */
    00246   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    00247     if (c != skip_company) AI::NewEvent(c, event);
    00248   }
    00249
    00250   event->Release();
    00251 }
    00252
    00253 /* static */ void AI::Save(CompanyID company)
    00254 {
    00255   if (!_networking || _network_server) {
    00256     Company *c = Company::GetIfValid(company);
    00257     assert(c != NULL && c->ai_instance != NULL);
    00258
    00259     Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00260     c->ai_instance->Save();
    00261     cur_company.Restore();
    00262   } else {
    00263     AIInstance::SaveEmpty();
    00264   }
    00265 }
    00266
    00267 /* static */ void AI::Load(CompanyID company, int version)
    00268 {
    00269   if (!_networking || _network_server) {
    00270     Company *c = Company::GetIfValid(company);
    00271     assert(c != NULL && c->ai_instance != NULL);
    00272
    00273     Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
    00274     c->ai_instance->Load(version);
    00275     cur_company.Restore();
    00276   } else {
    00277     /* Read, but ignore, the load data */
    00278     AIInstance::LoadEmpty();
    00279   }
    00280 }
    00281
    00282 /* static */ int AI::GetStartNextTime()
    00283 {
    00284   /* Find the first company which doesn't exist yet */
    00285   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    00286     if (!Company::IsValidID(c)) return AIConfig::GetConfig(c)->GetSetting("start_date");
    00287   }
    00288
    00289   /* Currently no AI can be started, check again in a year. */
    00290   return DAYS_IN_YEAR;
    00291 }
    00292
    00293 /* static */ char *AI::GetConsoleList(char *p, const char *last, bool newest_only)
    00294 {
    00295   return AI::scanner_info->GetConsoleList(p, last, newest_only);
    00296 }
    00297
    00298 /* static */ char *AI::GetConsoleLibraryList(char *p, const char *last)
    00299 {
    00300    return AI::scanner_library->GetConsoleList(p, last, true);
    00301 }
    00302
    00303 /* static */ const ScriptInfoList *AI::GetInfoList()
    00304 {
    00305   return AI::scanner_info->GetInfoList();
    00306 }
    00307
    00308 /* static */ const ScriptInfoList *AI::GetUniqueInfoList()
    00309 {
    00310   return AI::scanner_info->GetUniqueInfoList();
    00311 }
    00312
    00313 /* static */ AIInfo *AI::FindInfo(const char *name, int version, bool force_exact_match)
    00314 {
    00315   return AI::scanner_info->FindInfo(name, version, force_exact_match);
    00316 }
    00317
    00318 /* static */ AILibrary *AI::FindLibrary(const char *library, int version)
    00319 {
    00320   return AI::scanner_library->FindLibrary(library, version);
    00321 }
    00322
    00323 /* static */ void AI::Rescan()
    00324 {
    00325   TarScanner::DoScan(TarScanner::AI);
    00326
    00327   AI::scanner_info->RescanDir();
    00328   AI::scanner_library->RescanDir();
    00329   ResetConfig();
    00330
    00331   InvalidateWindowData(WC_AI_LIST, 0, 1);
    00332   SetWindowClassesDirty(WC_AI_DEBUG);
    00333   InvalidateWindowClassesData(WC_AI_SETTINGS);
    00334 }
    00335
    00336 #if defined(ENABLE_NETWORK)
    00337
    00344 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
    00345 {
    00346   return AI::scanner_info->HasScript(ci, md5sum);
    00347 }
    00348
    00349 /* static */ bool AI::HasAILibrary(const ContentInfo *ci, bool md5sum)
    00350 {
    00351   return AI::scanner_library->HasScript(ci, md5sum);
    00352 }
    00353
    00354 #endif /* defined(ENABLE_NETWORK) */