Fix 56510b5d7b: CargoArray does need to be initialised... (#13168)
[openttd-github.git] / src / ai / ai_core.cpp
blob358ab4f834128a1f4e9b4acb3b474e07c3d33a77
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file ai_core.cpp Implementation of AI. */
10 #include "../stdafx.h"
11 #include "../core/backup_type.hpp"
12 #include "../core/bitmath_func.hpp"
13 #include "../company_base.h"
14 #include "../company_func.h"
15 #include "../network/network.h"
16 #include "../window_func.h"
17 #include "../framerate_type.h"
18 #include "ai_scanner.hpp"
19 #include "ai_instance.hpp"
20 #include "ai_config.hpp"
21 #include "ai_info.hpp"
22 #include "ai.hpp"
24 #include "../safeguards.h"
26 /* static */ uint AI::frame_counter = 0;
27 /* static */ AIScannerInfo *AI::scanner_info = nullptr;
28 /* static */ AIScannerLibrary *AI::scanner_library = nullptr;
30 /* static */ bool AI::CanStartNew()
32 /* Only allow new AIs on the server and only when that is allowed in multiplayer */
33 return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
36 /* static */ void AI::StartNew(CompanyID company)
38 assert(Company::IsValidID(company));
40 /* Clients shouldn't start AIs */
41 if (_networking && !_network_server) return;
43 Backup<CompanyID> cur_company(_current_company, company);
44 Company *c = Company::Get(company);
46 AIConfig *config = c->ai_config.get();
47 if (config == nullptr) {
48 c->ai_config = std::make_unique<AIConfig>(AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME));
49 config = c->ai_config.get();
52 AIInfo *info = config->GetInfo();
53 if (info == nullptr) {
54 info = AI::scanner_info->SelectRandomAI();
55 assert(info != nullptr);
56 /* Load default data and store the name in the settings */
57 config->Change(info->GetName(), -1, false);
59 config->AnchorUnchangeableSettings();
61 c->ai_info = info;
62 assert(c->ai_instance == nullptr);
63 c->ai_instance = std::make_unique<AIInstance>();
64 c->ai_instance->Initialize(info);
65 c->ai_instance->LoadOnStack(config->GetToLoadData());
66 config->SetToLoadData(nullptr);
68 cur_company.Restore();
70 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
71 return;
74 /* static */ void AI::GameLoop()
76 /* If we are in networking, only servers run this function, and that only if it is allowed */
77 if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
79 /* The speed with which AIs go, is limited by the 'competitor_speed' */
80 AI::frame_counter++;
81 assert(_settings_game.difficulty.competitor_speed <= 4);
82 if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
84 Backup<CompanyID> cur_company(_current_company);
85 for (const Company *c : Company::Iterate()) {
86 if (c->is_ai) {
87 PerformanceMeasurer framerate((PerformanceElement)(PFE_AI0 + c->index));
88 cur_company.Change(c->index);
89 c->ai_instance->GameLoop();
90 /* Occasionally collect garbage; every 255 ticks do one company.
91 * Effectively collecting garbage once every two months per AI. */
92 if ((AI::frame_counter & 255) == 0 && (CompanyID)GB(AI::frame_counter, 8, 4) == c->index) {
93 c->ai_instance->CollectGarbage();
95 } else {
96 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + c->index));
99 cur_company.Restore();
102 /* static */ uint AI::GetTick()
104 return AI::frame_counter;
107 /* static */ void AI::Stop(CompanyID company)
109 if (_networking && !_network_server) return;
110 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + company));
112 Backup<CompanyID> cur_company(_current_company, company);
113 Company *c = Company::Get(company);
115 c->ai_instance.reset();
116 c->ai_info = nullptr;
117 c->ai_config.reset();
119 cur_company.Restore();
121 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
124 /* static */ void AI::Pause(CompanyID company)
126 /* The reason why dedicated servers are forbidden to execute this
127 * command is not because it is unsafe, but because there is no way
128 * for the server owner to unpause the script again. */
129 if (_network_dedicated) return;
131 Backup<CompanyID> cur_company(_current_company, company);
132 Company::Get(company)->ai_instance->Pause();
134 cur_company.Restore();
137 /* static */ void AI::Unpause(CompanyID company)
139 Backup<CompanyID> cur_company(_current_company, company);
140 Company::Get(company)->ai_instance->Unpause();
142 cur_company.Restore();
145 /* static */ bool AI::IsPaused(CompanyID company)
147 Backup<CompanyID> cur_company(_current_company, company);
148 bool paused = Company::Get(company)->ai_instance->IsPaused();
150 cur_company.Restore();
152 return paused;
155 /* static */ void AI::KillAll()
157 /* It might happen there are no companies .. than we have nothing to loop */
158 if (Company::GetPoolSize() == 0) return;
160 for (const Company *c : Company::Iterate()) {
161 if (c->is_ai) AI::Stop(c->index);
165 /* static */ void AI::Initialize()
167 if (AI::scanner_info != nullptr) AI::Uninitialize(true);
169 AI::frame_counter = 0;
170 if (AI::scanner_info == nullptr) {
171 TarScanner::DoScan(TarScanner::AI);
172 AI::scanner_info = new AIScannerInfo();
173 AI::scanner_info->Initialize();
174 AI::scanner_library = new AIScannerLibrary();
175 AI::scanner_library->Initialize();
179 /* static */ void AI::Uninitialize(bool keepConfig)
181 AI::KillAll();
183 if (keepConfig) {
184 /* Run a rescan, which indexes all AIInfos again, and check if we can
185 * still load all the AIS, while keeping the configs in place */
186 Rescan();
187 } else {
188 delete AI::scanner_info;
189 delete AI::scanner_library;
190 AI::scanner_info = nullptr;
191 AI::scanner_library = nullptr;
193 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
194 if (_settings_game.ai_config[c] != nullptr) {
195 delete _settings_game.ai_config[c];
196 _settings_game.ai_config[c] = nullptr;
198 if (_settings_newgame.ai_config[c] != nullptr) {
199 delete _settings_newgame.ai_config[c];
200 _settings_newgame.ai_config[c] = nullptr;
206 /* static */ void AI::ResetConfig()
208 /* Check for both newgame as current game if we can reload the AIInfo inside
209 * the AIConfig. If not, remove the AI from the list (which will assign
210 * a random new AI on reload). */
211 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
212 if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) {
213 if (!_settings_game.ai_config[c]->ResetInfo(true)) {
214 Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_game.ai_config[c]->GetName());
215 _settings_game.ai_config[c]->Change(std::nullopt);
219 if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) {
220 if (!_settings_newgame.ai_config[c]->ResetInfo(false)) {
221 Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_newgame.ai_config[c]->GetName());
222 _settings_newgame.ai_config[c]->Change(std::nullopt);
226 if (Company::IsValidAiID(c) && Company::Get(c)->ai_config != nullptr) {
227 AIConfig *config = Company::Get(c)->ai_config.get();
228 if (!config->ResetInfo(true)) {
229 /* The code belonging to an already running AI was deleted. We can only do
230 * one thing here to keep everything sane and that is kill the AI. After
231 * killing the offending AI we start a random other one in it's place, just
232 * like what would happen if the AI was missing during loading. */
233 AI::Stop(c);
234 AI::StartNew(c);
235 } else {
236 /* Update the reference in the Company struct. */
237 Company::Get(c)->ai_info = config->GetInfo();
243 /* static */ void AI::NewEvent(CompanyID company, ScriptEvent *event)
245 ScriptObjectRef counter(event);
247 /* Clients should ignore events */
248 if (_networking && !_network_server) {
249 return;
252 /* Only AIs can have an event-queue */
253 if (!Company::IsValidAiID(company)) {
254 return;
257 /* Queue the event */
258 Backup<CompanyID> cur_company(_current_company, company);
259 Company::Get(_current_company)->ai_instance->InsertEvent(event);
260 cur_company.Restore();
263 /* static */ void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company)
265 ScriptObjectRef counter(event);
267 /* Clients should ignore events */
268 if (_networking && !_network_server) {
269 return;
272 /* Try to send the event to all AIs */
273 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
274 if (c != skip_company) AI::NewEvent(c, event);
278 /* static */ void AI::Save(CompanyID company)
280 if (!_networking || _network_server) {
281 Company *c = Company::GetIfValid(company);
282 assert(c != nullptr);
284 /* When doing emergency saving, an AI can be not fully initialised. */
285 if (c->ai_instance != nullptr) {
286 Backup<CompanyID> cur_company(_current_company, company);
287 c->ai_instance->Save();
288 cur_company.Restore();
289 return;
293 AIInstance::SaveEmpty();
296 /* static */ void AI::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only)
298 AI::scanner_info->GetConsoleList(output_iterator, newest_only);
301 /* static */ void AI::GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator)
303 AI::scanner_library->GetConsoleList(output_iterator, true);
306 /* static */ const ScriptInfoList *AI::GetInfoList()
308 return AI::scanner_info->GetInfoList();
311 /* static */ const ScriptInfoList *AI::GetUniqueInfoList()
313 return AI::scanner_info->GetUniqueInfoList();
316 /* static */ AIInfo *AI::FindInfo(const std::string &name, int version, bool force_exact_match)
318 return AI::scanner_info->FindInfo(name, version, force_exact_match);
321 /* static */ AILibrary *AI::FindLibrary(const std::string &library, int version)
323 return AI::scanner_library->FindLibrary(library, version);
326 /* static */ void AI::Rescan()
328 TarScanner::DoScan(TarScanner::AI);
330 AI::scanner_info->RescanDir();
331 AI::scanner_library->RescanDir();
332 ResetConfig();
334 InvalidateWindowData(WC_SCRIPT_LIST, 0, 1);
335 SetWindowClassesDirty(WC_SCRIPT_DEBUG);
336 InvalidateWindowClassesData(WC_SCRIPT_SETTINGS);
340 * Check whether we have an AI (library) with the exact characteristics as ci.
341 * @param ci the characteristics to search on (shortname and md5sum)
342 * @param md5sum whether to check the MD5 checksum
343 * @return true iff we have an AI (library) matching.
345 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
347 return AI::scanner_info->HasScript(ci, md5sum);
350 /* static */ bool AI::HasAILibrary(const ContentInfo *ci, bool md5sum)
352 return AI::scanner_library->HasScript(ci, md5sum);
355 /* static */ AIScannerInfo *AI::GetScannerInfo()
357 return AI::scanner_info;
360 /* static */ AIScannerLibrary *AI::GetScannerLibrary()
362 return AI::scanner_library;