00001
00002
00003
00004
00005
00006
00007
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 "../fileio_func.h"
00021 #include "ai_scanner.hpp"
00022 #include "ai_instance.hpp"
00023 #include "ai_config.hpp"
00024 #include "ai_info.hpp"
00025 #include "ai.hpp"
00026 #include "../script/api/script_error.hpp"
00027
00028 uint AI::frame_counter = 0;
00029 AIScannerInfo *AI::scanner_info = NULL;
00030 AIScannerLibrary *AI::scanner_library = NULL;
00031
00032 bool AI::CanStartNew()
00033 {
00034
00035 return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
00036 }
00037
00038 void AI::StartNew(CompanyID company, bool rerandomise_ai)
00039 {
00040 assert(Company::IsValidID(company));
00041
00042
00043 if (_networking && !_network_server) return;
00044
00045 AIConfig *config = AIConfig::GetConfig(company);
00046 AIInfo *info = config->GetInfo();
00047 if (info == NULL || (rerandomise_ai && config->IsRandom())) {
00048 info = AI::scanner_info->SelectRandomAI();
00049 assert(info != NULL);
00050
00051 config->Change(info->GetName(), -1, false, true);
00052 }
00053
00054 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00055 Company *c = Company::Get(company);
00056
00057 c->ai_info = info;
00058 assert(c->ai_instance == NULL);
00059 c->ai_instance = new AIInstance();
00060 c->ai_instance->Initialize(info);
00061
00062 cur_company.Restore();
00063
00064 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00065 return;
00066 }
00067
00068 void AI::GameLoop()
00069 {
00070
00071 if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
00072
00073
00074 AI::frame_counter++;
00075 assert(_settings_game.difficulty.competitor_speed <= 4);
00076 if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
00077
00078 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00079 const Company *c;
00080 FOR_ALL_COMPANIES(c) {
00081 if (c->is_ai) {
00082 cur_company.Change(c->index);
00083 c->ai_instance->GameLoop();
00084 }
00085 }
00086 cur_company.Restore();
00087
00088
00089
00090 if ((AI::frame_counter & 255) == 0) {
00091 CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
00092 if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
00093 }
00094 }
00095
00096 uint AI::GetTick()
00097 {
00098 return AI::frame_counter;
00099 }
00100
00101 void AI::Stop(CompanyID company)
00102 {
00103 if (_networking && !_network_server) return;
00104
00105 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00106 Company *c = Company::Get(company);
00107
00108 delete c->ai_instance;
00109 c->ai_instance = NULL;
00110
00111 cur_company.Restore();
00112
00113 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00114 DeleteWindowById(WC_AI_SETTINGS, company);
00115 }
00116
00117 void AI::Suspend(CompanyID company)
00118 {
00119 if (_networking && !_network_server) return;
00120
00121 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00122 Company::Get(company)->ai_instance->Suspend();
00123
00124 cur_company.Restore();
00125 }
00126
00127 void AI::KillAll()
00128 {
00129
00130 if (Company::GetPoolSize() == 0) return;
00131
00132 const Company *c;
00133 FOR_ALL_COMPANIES(c) {
00134 if (c->is_ai) AI::Stop(c->index);
00135 }
00136 }
00137
00138 void AI::Initialize()
00139 {
00140 if (AI::scanner_info != NULL) AI::Uninitialize(true);
00141
00142 AI::frame_counter = 0;
00143 if (AI::scanner_info == NULL) {
00144 TarScanner::DoScan(TarScanner::AI);
00145 AI::scanner_info = new AIScannerInfo();
00146 AI::scanner_info->Initialize("AIScanner");
00147 AI::scanner_library = new AIScannerLibrary();
00148 AI::scanner_library->Initialize("AISCanner");
00149 }
00150 }
00151
00152 void AI::Uninitialize(bool keepConfig)
00153 {
00154 AI::KillAll();
00155
00156 if (keepConfig) {
00157
00158
00159 Rescan();
00160 } else {
00161 delete AI::scanner_info;
00162 delete AI::scanner_library;
00163 AI::scanner_info = NULL;
00164 AI::scanner_library = NULL;
00165
00166 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00167 if (_settings_game.ai_config[c] != NULL) {
00168 delete _settings_game.ai_config[c];
00169 _settings_game.ai_config[c] = NULL;
00170 }
00171 if (_settings_newgame.ai_config[c] != NULL) {
00172 delete _settings_newgame.ai_config[c];
00173 _settings_newgame.ai_config[c] = NULL;
00174 }
00175 }
00176 }
00177 }
00178
00179 void AI::ResetConfig()
00180 {
00181
00182
00183
00184 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00185 if (_settings_game.ai_config[c] != NULL && _settings_game.ai_config[c]->HasScript()) {
00186 if (!_settings_game.ai_config[c]->ResetInfo(true)) {
00187 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());
00188 _settings_game.ai_config[c]->Change(NULL);
00189 if (Company::IsValidAiID(c)) {
00190
00191
00192
00193
00194 AI::Stop(c);
00195 AI::StartNew(c, false);
00196 }
00197 } else if (Company::IsValidAiID(c)) {
00198
00199 Company::Get(c)->ai_info = _settings_game.ai_config[c]->GetInfo();
00200 }
00201 }
00202 if (_settings_newgame.ai_config[c] != NULL && _settings_newgame.ai_config[c]->HasScript()) {
00203 if (!_settings_newgame.ai_config[c]->ResetInfo(false)) {
00204 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());
00205 _settings_newgame.ai_config[c]->Change(NULL);
00206 }
00207 }
00208 }
00209 }
00210
00211 void AI::NewEvent(CompanyID company, ScriptEvent *event)
00212 {
00213
00214 event->AddRef();
00215
00216
00217 if (_networking && !_network_server) {
00218 event->Release();
00219 return;
00220 }
00221
00222
00223 if (!Company::IsValidAiID(company)) {
00224 event->Release();
00225 return;
00226 }
00227
00228
00229 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00230 Company::Get(_current_company)->ai_instance->InsertEvent(event);
00231 cur_company.Restore();
00232
00233 event->Release();
00234 }
00235
00236 void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company)
00237 {
00238
00239 event->AddRef();
00240
00241
00242 if (_networking && !_network_server) {
00243 event->Release();
00244 return;
00245 }
00246
00247
00248 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00249 if (c != skip_company) AI::NewEvent(c, event);
00250 }
00251
00252 event->Release();
00253 }
00254
00255 void AI::Save(CompanyID company)
00256 {
00257 if (!_networking || _network_server) {
00258 Company *c = Company::GetIfValid(company);
00259 assert(c != NULL && c->ai_instance != NULL);
00260
00261 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00262 c->ai_instance->Save();
00263 cur_company.Restore();
00264 } else {
00265 AIInstance::SaveEmpty();
00266 }
00267 }
00268
00269 void AI::Load(CompanyID company, int version)
00270 {
00271 if (!_networking || _network_server) {
00272 Company *c = Company::GetIfValid(company);
00273 assert(c != NULL && c->ai_instance != NULL);
00274
00275 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00276 c->ai_instance->Load(version);
00277 cur_company.Restore();
00278 } else {
00279
00280 AIInstance::LoadEmpty();
00281 }
00282 }
00283
00284 int AI::GetStartNextTime()
00285 {
00286
00287 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00288 if (!Company::IsValidID(c)) return AIConfig::GetConfig(c)->GetSetting("start_date");
00289 }
00290
00291
00292 return DAYS_IN_YEAR;
00293 }
00294
00295 char *AI::GetConsoleList(char *p, const char *last, bool newest_only)
00296 {
00297 return AI::scanner_info->GetConsoleList(p, last, newest_only);
00298 }
00299
00300 char *AI::GetConsoleLibraryList(char *p, const char *last)
00301 {
00302 return AI::scanner_library->GetConsoleList(p, last, true);
00303 }
00304
00305 const ScriptInfoList *AI::GetInfoList()
00306 {
00307 return AI::scanner_info->GetInfoList();
00308 }
00309
00310 const ScriptInfoList *AI::GetUniqueInfoList()
00311 {
00312 return AI::scanner_info->GetUniqueInfoList();
00313 }
00314
00315 AIInfo *AI::FindInfo(const char *name, int version, bool force_exact_match)
00316 {
00317 return AI::scanner_info->FindInfo(name, version, force_exact_match);
00318 }
00319
00320 AILibrary *AI::FindLibrary(const char *library, int version)
00321 {
00322 return AI::scanner_library->FindLibrary(library, version);
00323 }
00324
00325 void AI::Rescan()
00326 {
00327 TarScanner::DoScan(TarScanner::AI);
00328
00329 AI::scanner_info->RescanDir();
00330 AI::scanner_library->RescanDir();
00331 ResetConfig();
00332
00333 InvalidateWindowData(WC_AI_LIST, 0, 1);
00334 SetWindowClassesDirty(WC_AI_DEBUG);
00335 SetWindowDirty(WC_AI_SETTINGS, 0);
00336 }
00337
00338 #if defined(ENABLE_NETWORK)
00339
00346 bool AI::HasAI(const ContentInfo *ci, bool md5sum)
00347 {
00348 return AI::scanner_info->HasScript(ci, md5sum);
00349 }
00350
00351 bool AI::HasAILibrary(const ContentInfo *ci, bool md5sum)
00352 {
00353 return AI::scanner_library->HasScript(ci, md5sum);
00354 }
00355
00356 #endif