ai_core.cpp

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