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