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 "ai_scanner.hpp"
00021 #include "ai_instance.hpp"
00022 #include "ai_config.hpp"
00023 #include "api/ai_error.hpp"
00024
00025 uint AI::frame_counter = 0;
00026 AIScanner *AI::ai_scanner = NULL;
00027
00028 bool AI::CanStartNew()
00029 {
00030
00031 return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
00032 }
00033
00034 void AI::StartNew(CompanyID company, bool rerandomise_ai)
00035 {
00036 assert(Company::IsValidID(company));
00037
00038
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
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 void AI::GameLoop()
00064 {
00065
00066 if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
00067
00068
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
00084
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 uint AI::GetTick()
00092 {
00093 return AI::frame_counter;
00094 }
00095
00096 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 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 void AI::KillAll()
00123 {
00124
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 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 void AI::Uninitialize(bool keepConfig)
00142 {
00143 AI::KillAll();
00144
00145 if (keepConfig) {
00146
00147
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 void AI::ResetConfig()
00167 {
00168
00169
00170
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()) {
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 }
00177 }
00178 if (_settings_newgame.ai_config[c] != NULL && _settings_newgame.ai_config[c]->HasAI()) {
00179 if (!_settings_newgame.ai_config[c]->ResetInfo()) {
00180 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());
00181 _settings_newgame.ai_config[c]->ChangeAI(NULL);
00182 }
00183 }
00184 }
00185 }
00186
00187 void AI::NewEvent(CompanyID company, AIEvent *event)
00188 {
00189
00190 event->AddRef();
00191
00192
00193 if (_networking && !_network_server) {
00194 event->Release();
00195 return;
00196 }
00197
00198
00199 if (!Company::IsValidAiID(company)) {
00200 event->Release();
00201 return;
00202 }
00203
00204
00205 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00206 AIEventController::InsertEvent(event);
00207 cur_company.Restore();
00208
00209 event->Release();
00210 }
00211
00212 void AI::BroadcastNewEvent(AIEvent *event, CompanyID skip_company)
00213 {
00214
00215 event->AddRef();
00216
00217
00218 if (_networking && !_network_server) {
00219 event->Release();
00220 return;
00221 }
00222
00223
00224 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00225 if (c != skip_company) AI::NewEvent(c, event);
00226 }
00227
00228 event->Release();
00229 }
00230
00238 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00239 {
00240 AIObject::SetLastCommandRes(result.Succeeded());
00241
00242 if (result.Failed()) {
00243 AIObject::SetLastError(AIError::StringToError(result.GetErrorMessage()));
00244 } else {
00245 AIObject::IncreaseDoCommandCosts(result.GetCost());
00246 AIObject::SetLastCost(result.GetCost());
00247 }
00248
00249 Company::Get(_current_company)->ai_instance->Continue();
00250 }
00251
00252 void AI::Save(CompanyID company)
00253 {
00254 if (!_networking || _network_server) {
00255 Company *c = Company::GetIfValid(company);
00256 assert(c != NULL && c->ai_instance != NULL);
00257
00258 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00259 c->ai_instance->Save();
00260 cur_company.Restore();
00261 } else {
00262 AIInstance::SaveEmpty();
00263 }
00264 }
00265
00266 void AI::Load(CompanyID company, int version)
00267 {
00268 if (!_networking || _network_server) {
00269 Company *c = Company::GetIfValid(company);
00270 assert(c != NULL && c->ai_instance != NULL);
00271
00272 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
00273 c->ai_instance->Load(version);
00274 cur_company.Restore();
00275 } else {
00276
00277 AIInstance::LoadEmpty();
00278 }
00279 }
00280
00281 int AI::GetStartNextTime()
00282 {
00283
00284 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00285 if (!Company::IsValidID(c)) return AIConfig::GetConfig(c)->GetSetting("start_date");
00286 }
00287
00288
00289 return DAYS_IN_YEAR;
00290 }
00291
00292 char *AI::GetConsoleList(char *p, const char *last)
00293 {
00294 return AI::ai_scanner->GetAIConsoleList(p, last);
00295 }
00296
00297 char *AI::GetConsoleLibraryList(char *p, const char *last)
00298 {
00299 return AI::ai_scanner->GetAIConsoleLibraryList(p, last);
00300 }
00301
00302 const AIInfoList *AI::GetInfoList()
00303 {
00304 return AI::ai_scanner->GetAIInfoList();
00305 }
00306
00307 const AIInfoList *AI::GetUniqueInfoList()
00308 {
00309 return AI::ai_scanner->GetUniqueAIInfoList();
00310 }
00311
00312 AIInfo *AI::FindInfo(const char *name, int version, bool force_exact_match)
00313 {
00314 return AI::ai_scanner->FindInfo(name, version, force_exact_match);
00315 }
00316
00317 bool AI::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm)
00318 {
00319 return AI::ai_scanner->ImportLibrary(library, class_name, version, vm, Company::Get(_current_company)->ai_instance->GetController());
00320 }
00321
00322 void AI::Rescan()
00323 {
00324 AI::ai_scanner->RescanAIDir();
00325 ResetConfig();
00326 }