OpenTTD Source: src/ai/ai_core.cpp Source File

OpenTTD Source 15.0-RC2

ai_core.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
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"
23
24#include "../safeguards.h"
25
26/* static */ uint AI::frame_counter = 0;
27/* static */ std::unique_ptr<AIScannerInfo> AI::scanner_info = nullptr;
28/* static */ std::unique_ptr<AIScannerLibrary> AI::scanner_library = nullptr;
29
30/* static */ bool AI::CanStartNew()
31{
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);
34}
35
36/* static */ void AI::StartNew(CompanyID company)
37{
38 assert(Company::IsValidID(company));
39
40 /* Clients shouldn't start AIs */
41 if (_networking && !_network_server) return;
42
43 Backup<CompanyID> cur_company(_current_company, company);
44 Company *c = Company::Get(company);
45
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();
50 }
51
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);
58 }
59 config->AnchorUnchangeableSettings();
60
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);
67
68 cur_company.Restore();
69
70 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
71 return;
72}
73
74/* static */ void AI::GameLoop()
75{
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;
78
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;
83
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();
94 }
95 } else {
96 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + c->index));
97 }
98 }
99 cur_company.Restore();
100}
101
102/* static */ uint AI::GetTick()
103{
104 return AI::frame_counter;
105}
106
107/* static */ void AI::Stop(CompanyID company)
108{
109 if (_networking && !_network_server) return;
110 PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + company));
111
112 Backup<CompanyID> cur_company(_current_company, company);
113 Company *c = Company::Get(company);
114
115 c->ai_instance.reset();
116 c->ai_info = nullptr;
117 c->ai_config.reset();
118
119 cur_company.Restore();
120
121 InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
122}
123
124/* static */ void AI::Pause(CompanyID company)
125{
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;
130
131 Backup<CompanyID> cur_company(_current_company, company);
132 Company::Get(company)->ai_instance->Pause();
133
134 cur_company.Restore();
135}
136
137/* static */ void AI::Unpause(CompanyID company)
138{
139 Backup<CompanyID> cur_company(_current_company, company);
140 Company::Get(company)->ai_instance->Unpause();
141
142 cur_company.Restore();
143}
144
145/* static */ bool AI::IsPaused(CompanyID company)
146{
147 Backup<CompanyID> cur_company(_current_company, company);
148 bool paused = Company::Get(company)->ai_instance->IsPaused();
149
150 cur_company.Restore();
151
152 return paused;
153}
154
155/* static */ void AI::KillAll()
156{
157 /* It might happen there are no companies .. than we have nothing to loop */
158 if (Company::GetPoolSize() == 0) return;
159
160 for (const Company *c : Company::Iterate()) {
161 if (c->is_ai) AI::Stop(c->index);
162 }
163}
164
165/* static */ void AI::Initialize()
166{
167 if (AI::scanner_info != nullptr) AI::Uninitialize(true);
168
169 AI::frame_counter = 0;
170 if (AI::scanner_info == nullptr) {
171 TarScanner::DoScan(TarScanner::Mode::AI);
172 AI::scanner_info = std::make_unique<AIScannerInfo>();
173 AI::scanner_info->Initialize();
174 AI::scanner_library = std::make_unique<AIScannerLibrary>();
175 AI::scanner_library->Initialize();
176 }
177}
178
179/* static */ void AI::Uninitialize(bool keepConfig)
180{
181 AI::KillAll();
182
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 AI::scanner_info.reset();
189 AI::scanner_library.reset();
190
191 for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
192 _settings_game.script_config.ai[c].reset();
193 _settings_newgame.script_config.ai[c].reset();
194 }
195 }
196}
197
198/* static */ void AI::ResetConfig()
199{
200 /* Check for both newgame as current game if we can reload the AIInfo inside
201 * the AIConfig. If not, remove the AI from the list (which will assign
202 * a random new AI on reload). */
203 for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
204 if (_settings_game.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) {
205 if (!_settings_game.script_config.ai[c]->ResetInfo(true)) {
206 Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_game.script_config.ai[c]->GetName());
207 _settings_game.script_config.ai[c]->Change(std::nullopt);
208 }
209 }
210
211 if (_settings_newgame.script_config.ai[c] != nullptr && _settings_newgame.script_config.ai[c]->HasScript()) {
212 if (!_settings_newgame.script_config.ai[c]->ResetInfo(false)) {
213 Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_newgame.script_config.ai[c]->GetName());
214 _settings_newgame.script_config.ai[c]->Change(std::nullopt);
215 }
216 }
217
218 if (Company::IsValidAiID(c) && Company::Get(c)->ai_config != nullptr) {
219 AIConfig *config = Company::Get(c)->ai_config.get();
220 if (!config->ResetInfo(true)) {
221 /* The code belonging to an already running AI was deleted. We can only do
222 * one thing here to keep everything sane and that is kill the AI. After
223 * killing the offending AI we start a random other one in it's place, just
224 * like what would happen if the AI was missing during loading. */
225 AI::Stop(c);
226 AI::StartNew(c);
227 } else {
228 /* Update the reference in the Company struct. */
229 Company::Get(c)->ai_info = config->GetInfo();
230 }
231 }
232 }
233}
234
235/* static */ void AI::NewEvent(CompanyID company, ScriptEvent *event)
236{
237 ScriptObjectRef counter(event);
238
239 /* Clients should ignore events */
240 if (_networking && !_network_server) {
241 return;
242 }
243
244 /* Only AIs can have an event-queue */
245 if (!Company::IsValidAiID(company)) {
246 return;
247 }
248
249 /* Queue the event */
250 Backup<CompanyID> cur_company(_current_company, company);
251 Company::Get(_current_company)->ai_instance->InsertEvent(event);
252 cur_company.Restore();
253}
254
255/* static */ void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company)
256{
257 ScriptObjectRef counter(event);
258
259 /* Clients should ignore events */
260 if (_networking && !_network_server) {
261 return;
262 }
263
264 /* Try to send the event to all AIs */
265 for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
266 if (c != skip_company) AI::NewEvent(c, event);
267 }
268}
269
270/* static */ void AI::Save(CompanyID company)
271{
272 if (!_networking || _network_server) {
273 Company *c = Company::GetIfValid(company);
274 assert(c != nullptr);
275
276 /* When doing emergency saving, an AI can be not fully initialised. */
277 if (c->ai_instance != nullptr) {
278 Backup<CompanyID> cur_company(_current_company, company);
279 c->ai_instance->Save();
280 cur_company.Restore();
281 return;
282 }
283 }
284
285 AIInstance::SaveEmpty();
286}
287
288/* static */ void AI::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only)
289{
290 AI::scanner_info->GetConsoleList(output_iterator, newest_only);
291}
292
293/* static */ void AI::GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator)
294{
295 AI::scanner_library->GetConsoleList(output_iterator, true);
296}
297
298/* static */ const ScriptInfoList *AI::GetInfoList()
299{
300 return AI::scanner_info->GetInfoList();
301}
302
303/* static */ const ScriptInfoList *AI::GetUniqueInfoList()
304{
305 return AI::scanner_info->GetUniqueInfoList();
306}
307
308/* static */ AIInfo *AI::FindInfo(const std::string &name, int version, bool force_exact_match)
309{
310 return AI::scanner_info->FindInfo(name, version, force_exact_match);
311}
312
313/* static */ AILibrary *AI::FindLibrary(const std::string &library, int version)
314{
315 return AI::scanner_library->FindLibrary(library, version);
316}
317
318/* static */ void AI::Rescan()
319{
320 TarScanner::DoScan(TarScanner::Mode::AI);
321
322 AI::scanner_info->RescanDir();
323 AI::scanner_library->RescanDir();
324 ResetConfig();
325
326 InvalidateWindowData(WC_SCRIPT_LIST, 0, 1);
327 SetWindowClassesDirty(WC_SCRIPT_DEBUG);
328 InvalidateWindowClassesData(WC_SCRIPT_SETTINGS);
329}
330
337/* static */ bool AI::HasAI(const ContentInfo &ci, bool md5sum)
338{
339 return AI::scanner_info->HasScript(ci, md5sum);
340}
341
342/* static */ bool AI::HasAILibrary(const ContentInfo &ci, bool md5sum)
343{
344 return AI::scanner_library->HasScript(ci, md5sum);
345}
346
347/* static */ AIScannerInfo *AI::GetScannerInfo()
348{
349 return AI::scanner_info.get();
350}
351
352/* static */ AIScannerLibrary *AI::GetScannerLibrary()
353{
354 return AI::scanner_library.get();
355}
356
ai.hpp
Base functions for all AIs.
ai_config.hpp
AIConfig stores the configuration settings of every AI.
ai_info.hpp
AIInfo keeps track of all information of an AI, like Author, Description, ...
ai_instance.hpp
The AIInstance tracks an AI.
ai_scanner.hpp
declarations of the class for AI scanner
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
AIConfig
Definition ai_config.hpp:16
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition ai_config.cpp:20
AIConfig::ResetInfo
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
Definition ai_config.cpp:47
AIInfo
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
AILibrary
All static information from an AI library like name, version, etc.
Definition ai_info.hpp:60
AIScannerInfo
Definition ai_scanner.hpp:15
AIScannerLibrary
Definition ai_scanner.hpp:53
AI::GetTick
static uint GetTick()
Get the current AI tick.
Definition ai_core.cpp:102
AI::scanner_library
static std::unique_ptr< AIScannerLibrary > scanner_library
ScriptScanner instance that is used to find AI Libraries.
Definition ai.hpp:143
AI::Uninitialize
static void Uninitialize(bool keepConfig)
Uninitialize the AI system.
Definition ai_core.cpp:179
AI::GetScannerLibrary
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
Definition ai_core.cpp:352
AI::Pause
static void Pause(CompanyID company)
Suspend the AI and then pause execution of the script.
Definition ai_core.cpp:124
AI::GetConsoleLibraryList
static void GetConsoleLibraryList(std::back_insert_iterator< std::string > &output_iterator)
Wrapper function for AIScanner::GetAIConsoleLibraryList.
Definition ai_core.cpp:293
AI::Initialize
static void Initialize()
Initialize the AI system.
Definition ai_core.cpp:165
AI::GetConsoleList
static void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only)
Wrapper function for AIScanner::GetAIConsoleList.
Definition ai_core.cpp:288
AI::BroadcastNewEvent
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=CompanyID::Invalid())
Broadcast a new event to all active AIs.
Definition ai_core.cpp:255
AI::scanner_info
static std::unique_ptr< AIScannerInfo > scanner_info
ScriptScanner instance that is used to find AIs.
Definition ai.hpp:142
AI::GameLoop
static void GameLoop()
Called every game-tick to let AIs do something.
Definition ai_core.cpp:74
AI::CanStartNew
static bool CanStartNew()
Is it possible to start a new AI company?
Definition ai_core.cpp:30
AI::StartNew
static void StartNew(CompanyID company)
Start a new AI company.
Definition ai_core.cpp:36
AI::Stop
static void Stop(CompanyID company)
Stop a company to be controlled by an AI.
Definition ai_core.cpp:107
AI::FindLibrary
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition ai_core.cpp:313
AI::ResetConfig
static void ResetConfig()
Reset all AIConfigs, and make them reload their AIInfo.
Definition ai_core.cpp:198
AI::NewEvent
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition ai_core.cpp:235
AI::GetScannerInfo
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
Definition ai_core.cpp:347
AI::GetUniqueInfoList
static const ScriptInfoList * GetUniqueInfoList()
Wrapper function for AIScanner::GetUniqueAIInfoList.
Definition ai_core.cpp:303
AI::HasAI
static bool HasAI(const ContentInfo &ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition ai_core.cpp:337
AI::Rescan
static void Rescan()
Rescans all searchpaths for available AIs.
Definition ai_core.cpp:318
AI::frame_counter
static uint frame_counter
Tick counter for the AI code.
Definition ai.hpp:141
AI::IsPaused
static bool IsPaused(CompanyID company)
Checks if the AI is paused.
Definition ai_core.cpp:145
AI::Save
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition ai_core.cpp:270
AI::Unpause
static void Unpause(CompanyID company)
Resume execution of the AI.
Definition ai_core.cpp:137
AI::KillAll
static void KillAll()
Kill any and all AIs we manage.
Definition ai_core.cpp:155
AI::GetInfoList
static const ScriptInfoList * GetInfoList()
Wrapper function for AIScanner::GetAIInfoList.
Definition ai_core.cpp:298
AI::FindInfo
static class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
Definition ai_core.cpp:308
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition framerate_type.h:91
PerformanceMeasurer::SetInactive
static void SetInactive(PerformanceElement elem)
Mark a performance element as not currently in use.
Definition framerate_gui.cpp:291
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition script_config.hpp:100
ScriptConfig::AnchorUnchangeableSettings
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
Definition script_config.cpp:74
ScriptConfig::Change
void Change(std::optional< std::string_view > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
Definition script_config.cpp:22
ScriptInfo::GetName
const std::string & GetName() const
Get the Name of the script.
Definition script_info.hpp:40
ScriptInstance::SaveEmpty
static void SaveEmpty()
Don't save any data in the savegame.
Definition script_instance.cpp:507
TarScanner::Mode::AI
@ AI
Scan for AIs and its libraries.
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition fileio.cpp:377
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
PerformanceElement
PerformanceElement
Elements of game performance that can be measured.
Definition framerate_type.h:46
PFE_AI0
@ PFE_AI0
AI execution for player slot 1.
Definition framerate_type.h:62
_networking
bool _networking
are we in networking mode?
Definition network.cpp:66
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition network.cpp:69
_network_server
bool _network_server
network-server is active
Definition network.cpp:67
ScriptInfoList
std::map< std::string, class ScriptInfo *, CaseInsensitiveComparator > ScriptInfoList
Type for the list of scripts.
Definition script_scanner.hpp:16
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition settings.cpp:62
AISettings::ai_in_multiplayer
bool ai_in_multiplayer
so we allow AIs in multiplayer
Definition settings_type.h:439
Backup
Class to backup a specific variable and restore it later.
Definition backup_type.hpp:21
Backup::Change
void Change(const U &new_value)
Change the value of the variable.
Definition backup_type.hpp:82
Backup::Restore
void Restore()
Restore the variable.
Definition backup_type.hpp:110
Company
Definition company_base.h:127
Company::IsValidAiID
static bool IsValidAiID(auto index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition company_base.h:153
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:56
DifficultySettings::competitor_speed
uint8_t competitor_speed
the speed at which the AI builds
Definition settings_type.h:132
GameSettings::ai
AISettings ai
what may the AI do?
Definition settings_type.h:636
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition settings_type.h:633
GameSettings::script_config
ScriptConfigSettings script_config
AI and Gamescript configuration.
Definition settings_type.h:638
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Pool::PoolItem<&_company_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition pool_type.hpp:408
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
ScriptConfigSettings::ai
TypedIndexContainer< std::array< std::unique_ptr< class AIConfig >, MAX_COMPANIES >, CompanyID > ai
settings per company
Definition settings_type.h:621
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition window.cpp:3202
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3294
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3311
WC_SCRIPT_SETTINGS
@ WC_SCRIPT_SETTINGS
Script settings; Window numbers:
Definition window_type.h:180
WC_SCRIPT_LIST
@ WC_SCRIPT_LIST
Scripts list; Window numbers:
Definition window_type.h:289
WC_SCRIPT_DEBUG
@ WC_SCRIPT_DEBUG
Script debug window; Window numbers:
Definition window_type.h:685