ai_core.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../core/bitmath_func.hpp"
00014 #include "../company_base.h"
00015 #include "../company_func.h"
00016 #include "../debug.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, true);
00048 }
00049
00050 _current_company = company;
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 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00058 return;
00059 }
00060
00061 void AI::GameLoop()
00062 {
00063
00064 if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
00065
00066
00067 AI::frame_counter++;
00068 assert(_settings_game.difficulty.competitor_speed <= 4);
00069 if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
00070
00071 const Company *c;
00072 FOR_ALL_COMPANIES(c) {
00073 if (c->is_ai) {
00074 _current_company = c->index;
00075 c->ai_instance->GameLoop();
00076 }
00077 }
00078
00079
00080
00081 if ((AI::frame_counter & 255) == 0) {
00082 CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
00083 if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
00084 }
00085
00086 _current_company = OWNER_NONE;
00087 }
00088
00089 uint AI::GetTick()
00090 {
00091 return AI::frame_counter;
00092 }
00093
00094 void AI::Stop(CompanyID company)
00095 {
00096 if (_networking && !_network_server) return;
00097
00098 CompanyID old_company = _current_company;
00099 _current_company = company;
00100 Company *c = Company::Get(company);
00101
00102 delete c->ai_instance;
00103 c->ai_instance = NULL;
00104
00105 _current_company = old_company;
00106
00107 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00108 }
00109
00110 void AI::KillAll()
00111 {
00112
00113 if (Company::GetPoolSize() == 0) return;
00114
00115 const Company *c;
00116 FOR_ALL_COMPANIES(c) {
00117 if (c->is_ai) AI::Stop(c->index);
00118 }
00119 }
00120
00121 void AI::Initialize()
00122 {
00123 if (AI::ai_scanner != NULL) AI::Uninitialize(true);
00124
00125 AI::frame_counter = 0;
00126 if (AI::ai_scanner == NULL) AI::ai_scanner = new AIScanner();
00127 }
00128
00129 void AI::Uninitialize(bool keepConfig)
00130 {
00131 AI::KillAll();
00132
00133 if (keepConfig) {
00134
00135
00136 Rescan();
00137 } else {
00138 delete AI::ai_scanner;
00139 AI::ai_scanner = NULL;
00140
00141 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00142 if (_settings_game.ai_config[c] != NULL) {
00143 delete _settings_game.ai_config[c];
00144 _settings_game.ai_config[c] = NULL;
00145 }
00146 if (_settings_newgame.ai_config[c] != NULL) {
00147 delete _settings_newgame.ai_config[c];
00148 _settings_newgame.ai_config[c] = NULL;
00149 }
00150 }
00151 }
00152 }
00153
00154 void AI::ResetConfig()
00155 {
00156
00157
00158
00159 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00160 if (_settings_game.ai_config[c] != NULL && _settings_game.ai_config[c]->HasAI()) {
00161 if (!_settings_game.ai_config[c]->ResetInfo()) {
00162 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());
00163 _settings_game.ai_config[c]->ChangeAI(NULL);
00164 }
00165 }
00166 if (_settings_newgame.ai_config[c] != NULL && _settings_newgame.ai_config[c]->HasAI()) {
00167 if (!_settings_newgame.ai_config[c]->ResetInfo()) {
00168 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());
00169 _settings_newgame.ai_config[c]->ChangeAI(NULL);
00170 }
00171 }
00172 }
00173 }
00174
00175 void AI::NewEvent(CompanyID company, AIEvent *event)
00176 {
00177
00178 event->AddRef();
00179
00180
00181 if (_networking && !_network_server) {
00182 event->Release();
00183 return;
00184 }
00185
00186
00187 if (!Company::IsValidAiID(company)) {
00188 event->Release();
00189 return;
00190 }
00191
00192
00193 CompanyID old_company = _current_company;
00194 _current_company = company;
00195 AIEventController::InsertEvent(event);
00196 _current_company = old_company;
00197
00198 event->Release();
00199 }
00200
00201 void AI::BroadcastNewEvent(AIEvent *event, CompanyID skip_company)
00202 {
00203
00204 event->AddRef();
00205
00206
00207 if (_networking && !_network_server) {
00208 event->Release();
00209 return;
00210 }
00211
00212
00213 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00214 if (c != skip_company) AI::NewEvent(c, event);
00215 }
00216
00217 event->Release();
00218 }
00219
00220 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00221 {
00222 AIObject::SetLastCommandRes(result.Succeeded());
00223
00224 if (result.Failed()) {
00225 AIObject::SetLastError(AIError::StringToError(result.GetErrorMessage()));
00226 } else {
00227 AIObject::IncreaseDoCommandCosts(result.GetCost());
00228 AIObject::SetLastCost(result.GetCost());
00229 }
00230
00231 Company::Get(_current_company)->ai_instance->Continue();
00232 }
00233
00234 void AI::Save(CompanyID company)
00235 {
00236 if (!_networking || _network_server) {
00237 Company *c = Company::GetIfValid(company);
00238 assert(c != NULL && c->ai_instance != NULL);
00239
00240 CompanyID old_company = _current_company;
00241 _current_company = company;
00242 c->ai_instance->Save();
00243 _current_company = old_company;
00244 } else {
00245 AIInstance::SaveEmpty();
00246 }
00247 }
00248
00249 void AI::Load(CompanyID company, int version)
00250 {
00251 if (!_networking || _network_server) {
00252 Company *c = Company::GetIfValid(company);
00253 assert(c != NULL && c->ai_instance != NULL);
00254
00255 CompanyID old_company = _current_company;
00256 _current_company = company;
00257 c->ai_instance->Load(version);
00258 _current_company = old_company;
00259 } else {
00260
00261 AIInstance::LoadEmpty();
00262 }
00263 }
00264
00265 int AI::GetStartNextTime()
00266 {
00267
00268 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00269 if (!Company::IsValidID(c)) return AIConfig::GetConfig(c)->GetSetting("start_date");
00270 }
00271
00272
00273 return DAYS_IN_YEAR;
00274 }
00275
00276 char *AI::GetConsoleList(char *p, const char *last)
00277 {
00278 return AI::ai_scanner->GetAIConsoleList(p, last);
00279 }
00280
00281 const AIInfoList *AI::GetInfoList()
00282 {
00283 return AI::ai_scanner->GetAIInfoList();
00284 }
00285
00286 const AIInfoList *AI::GetUniqueInfoList()
00287 {
00288 return AI::ai_scanner->GetUniqueAIInfoList();
00289 }
00290
00291 AIInfo *AI::FindInfo(const char *name, int version)
00292 {
00293 return AI::ai_scanner->FindInfo(name, version);
00294 }
00295
00296 bool AI::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm)
00297 {
00298 return AI::ai_scanner->ImportLibrary(library, class_name, version, vm, Company::Get(_current_company)->ai_instance->GetController());
00299 }
00300
00301 void AI::Rescan()
00302 {
00303 AI::ai_scanner->RescanAIDir();
00304 ResetConfig();
00305 }