Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / ai / ai_core.cpp
blob1e8336fd22952102cfe5d716561e8e51e4723895
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, bool deviate)
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, FILE_LINE);
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 if (deviate) config->AddRandomDeviation(company);
60 config->AnchorUnchangeableSettings();
62 c->ai_info = info;
63 assert(c->ai_instance == nullptr);
64 c->ai_instance = new AIInstance();
65 c->ai_instance->Initialize(info);
66 c->ai_instance->LoadOnStack(config->GetToLoadData());
67 config->SetToLoadData(nullptr);
69 cur_company.Restore();
71 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
72 return;
75 /* static */ void AI::GameLoop()
77 /* If we are in networking, only servers run this function, and that only if it is allowed */
78 if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
80 /* The speed with which AIs go, is limited by the 'competitor_speed' */
81 AI::frame_counter++;
82 assert(_settings_game.difficulty.competitor_speed <= 4);
83 if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
85 Backup<CompanyID> cur_company(_current_company, FILE_LINE);
86 for (const Company *c : Company::Iterate()) {
87 if (c->is_ai) {
88 PerformanceMeasurer framerate((PerformanceElement)(PFE_AI0 + c->index));
89 cur_company.Change(c->index);
90 c->ai_instance->GameLoop();
91 /* Occasionally collect garbage; every 255 ticks do one company.
92 * Effectively collecting garbage once every two months per AI. */
93 if ((AI::frame_counter & 255) == 0 && (CompanyID)GB(AI::frame_counter, 8, 4) == c->index) {
94 c->ai_instance->CollectGarbage();
96 } else {
97 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + c->index));
100 cur_company.Restore();
103 /* static */ uint AI::GetTick()
105 return AI::frame_counter;
108 /* static */ void AI::Stop(CompanyID company)
110 if (_networking && !_network_server) return;
111 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + company));
113 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
114 Company *c = Company::Get(company);
116 delete c->ai_instance;
117 c->ai_instance = nullptr;
118 c->ai_info = nullptr;
119 c->ai_config.reset();
121 cur_company.Restore();
123 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
126 /* static */ void AI::Pause(CompanyID company)
128 /* The reason why dedicated servers are forbidden to execute this
129 * command is not because it is unsafe, but because there is no way
130 * for the server owner to unpause the script again. */
131 if (_network_dedicated) return;
133 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
134 Company::Get(company)->ai_instance->Pause();
136 cur_company.Restore();
139 /* static */ void AI::Unpause(CompanyID company)
141 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
142 Company::Get(company)->ai_instance->Unpause();
144 cur_company.Restore();
147 /* static */ bool AI::IsPaused(CompanyID company)
149 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
150 bool paused = Company::Get(company)->ai_instance->IsPaused();
152 cur_company.Restore();
154 return paused;
157 /* static */ void AI::KillAll()
159 /* It might happen there are no companies .. than we have nothing to loop */
160 if (Company::GetPoolSize() == 0) return;
162 for (const Company *c : Company::Iterate()) {
163 if (c->is_ai) AI::Stop(c->index);
167 /* static */ void AI::Initialize()
169 if (AI::scanner_info != nullptr) AI::Uninitialize(true);
171 AI::frame_counter = 0;
172 if (AI::scanner_info == nullptr) {
173 TarScanner::DoScan(TarScanner::AI);
174 AI::scanner_info = new AIScannerInfo();
175 AI::scanner_info->Initialize();
176 AI::scanner_library = new AIScannerLibrary();
177 AI::scanner_library->Initialize();
181 /* static */ void AI::Uninitialize(bool keepConfig)
183 AI::KillAll();
185 if (keepConfig) {
186 /* Run a rescan, which indexes all AIInfos again, and check if we can
187 * still load all the AIS, while keeping the configs in place */
188 Rescan();
189 } else {
190 delete AI::scanner_info;
191 delete AI::scanner_library;
192 AI::scanner_info = nullptr;
193 AI::scanner_library = nullptr;
195 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
196 if (_settings_game.ai_config[c] != nullptr) {
197 delete _settings_game.ai_config[c];
198 _settings_game.ai_config[c] = nullptr;
200 if (_settings_newgame.ai_config[c] != nullptr) {
201 delete _settings_newgame.ai_config[c];
202 _settings_newgame.ai_config[c] = nullptr;
208 /* static */ void AI::ResetConfig()
210 /* Check for both newgame as current game if we can reload the AIInfo inside
211 * the AIConfig. If not, remove the AI from the list (which will assign
212 * a random new AI on reload). */
213 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
214 if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) {
215 if (!_settings_game.ai_config[c]->ResetInfo(true)) {
216 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());
217 _settings_game.ai_config[c]->Change(std::nullopt);
218 if (Company::IsValidAiID(c)) {
219 /* The code belonging to an already running AI was deleted. We can only do
220 * one thing here to keep everything sane and that is kill the AI. After
221 * killing the offending AI we start a random other one in it's place, just
222 * like what would happen if the AI was missing during loading. */
223 AI::Stop(c);
224 AI::StartNew(c, false);
226 } else if (Company::IsValidAiID(c)) {
227 /* Update the reference in the Company struct. */
228 Company::Get(c)->ai_info = _settings_game.ai_config[c]->GetInfo();
231 if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) {
232 if (!_settings_newgame.ai_config[c]->ResetInfo(false)) {
233 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());
234 _settings_newgame.ai_config[c]->Change(std::nullopt);
240 /* static */ void AI::NewEvent(CompanyID company, ScriptEvent *event)
242 /* AddRef() and Release() need to be called at least once, so do it here */
243 event->AddRef();
245 /* Clients should ignore events */
246 if (_networking && !_network_server) {
247 event->Release();
248 return;
251 /* Only AIs can have an event-queue */
252 if (!Company::IsValidAiID(company)) {
253 event->Release();
254 return;
257 /* Queue the event */
258 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
259 Company::Get(_current_company)->ai_instance->InsertEvent(event);
260 cur_company.Restore();
262 event->Release();
265 /* static */ void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company)
267 /* AddRef() and Release() need to be called at least once, so do it here */
268 event->AddRef();
270 /* Clients should ignore events */
271 if (_networking && !_network_server) {
272 event->Release();
273 return;
276 /* Try to send the event to all AIs */
277 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
278 if (c != skip_company) AI::NewEvent(c, event);
281 event->Release();
284 /* static */ void AI::Save(CompanyID company)
286 if (!_networking || _network_server) {
287 Company *c = Company::GetIfValid(company);
288 assert(c != nullptr);
290 /* When doing emergency saving, an AI can be not fully initialised. */
291 if (c->ai_instance != nullptr) {
292 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
293 c->ai_instance->Save();
294 cur_company.Restore();
295 return;
299 AIInstance::SaveEmpty();
302 /* static */ void AI::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only)
304 AI::scanner_info->GetConsoleList(output_iterator, newest_only);
307 /* static */ void AI::GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator)
309 AI::scanner_library->GetConsoleList(output_iterator, true);
312 /* static */ const ScriptInfoList *AI::GetInfoList()
314 return AI::scanner_info->GetInfoList();
317 /* static */ const ScriptInfoList *AI::GetUniqueInfoList()
319 return AI::scanner_info->GetUniqueInfoList();
322 /* static */ AIInfo *AI::FindInfo(const std::string &name, int version, bool force_exact_match)
324 return AI::scanner_info->FindInfo(name, version, force_exact_match);
327 /* static */ AILibrary *AI::FindLibrary(const std::string &library, int version)
329 return AI::scanner_library->FindLibrary(library, version);
332 /* static */ void AI::Rescan()
334 TarScanner::DoScan(TarScanner::AI);
336 AI::scanner_info->RescanDir();
337 AI::scanner_library->RescanDir();
338 ResetConfig();
340 InvalidateWindowData(WC_SCRIPT_LIST, 0, 1);
341 SetWindowClassesDirty(WC_SCRIPT_DEBUG);
342 InvalidateWindowClassesData(WC_SCRIPT_SETTINGS);
346 * Check whether we have an AI (library) with the exact characteristics as ci.
347 * @param ci the characteristics to search on (shortname and md5sum)
348 * @param md5sum whether to check the MD5 checksum
349 * @return true iff we have an AI (library) matching.
351 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
353 return AI::scanner_info->HasScript(ci, md5sum);
356 /* static */ bool AI::HasAILibrary(const ContentInfo *ci, bool md5sum)
358 return AI::scanner_library->HasScript(ci, md5sum);
361 /* static */ AIScannerInfo *AI::GetScannerInfo()
363 return AI::scanner_info;
366 /* static */ AIScannerLibrary *AI::GetScannerLibrary()
368 return AI::scanner_library;