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