4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file ai_core.cpp Implementation of AI. */
12 #include "../stdafx.h"
13 #include "../core/backup_type.hpp"
14 #include "../core/bitmath_func.hpp"
15 #include "../company_base.h"
16 #include "../company_func.h"
17 #include "../network/network.h"
18 #include "../window_func.h"
19 #include "ai_scanner.hpp"
20 #include "ai_instance.hpp"
21 #include "ai_config.hpp"
22 #include "ai_info.hpp"
25 #include "../safeguards.h"
27 /* static */ uint
AI::frame_counter
= 0;
28 /* static */ AIScannerInfo
*AI::scanner_info
= nullptr;
29 /* static */ AIScannerLibrary
*AI::scanner_library
= nullptr;
31 /* static */ bool AI::CanStartNew()
33 /* Only allow new AIs on the server and only when that is allowed in multiplayer */
34 return !_networking
|| (_network_server
&& _settings_game
.ai
.ai_in_multiplayer
);
37 /* static */ void AI::StartNew(CompanyID company
, bool rerandomise_ai
)
39 assert(Company::IsValidID(company
));
41 /* Clients shouldn't start AIs */
42 if (_networking
&& !_network_server
) return;
44 AIConfig
*config
= AIConfig::GetConfig(company
, AIConfig::SSS_FORCE_GAME
);
45 AIInfo
*info
= config
->GetInfo();
46 if (info
== nullptr || (rerandomise_ai
&& config
->IsRandom())) {
47 info
= AI::scanner_info
->SelectRandomAI();
48 assert(info
!= nullptr);
49 /* Load default data and store the name in the settings */
50 config
->Change(info
->GetName(), -1, false, true);
52 config
->AnchorUnchangeableSettings();
54 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
55 Company
*c
= Company::Get(company
);
58 assert(c
->ai_instance
== nullptr);
59 c
->ai_instance
= new AIInstance();
60 c
->ai_instance
->Initialize(info
);
62 cur_company
.Restore();
64 InvalidateWindowData(WC_AI_DEBUG
, 0, -1);
68 /* static */ void AI::GameLoop()
70 /* If we are in networking, only servers run this function, and that only if it is allowed */
71 if (_networking
&& (!_network_server
|| !_settings_game
.ai
.ai_in_multiplayer
)) return;
73 /* The speed with which AIs go, is limited by the 'competitor_speed' */
75 assert(_settings_game
.difficulty
.competitor_speed
<= 4);
76 if ((AI::frame_counter
& ((1 << (4 - _settings_game
.difficulty
.competitor_speed
)) - 1)) != 0) return;
78 Backup
<CompanyByte
> cur_company(_current_company
, FILE_LINE
);
80 FOR_ALL_COMPANIES(c
) {
82 cur_company
.Change(c
->index
);
83 c
->ai_instance
->GameLoop();
86 cur_company
.Restore();
88 /* Occasionally collect garbage; every 255 ticks do one company.
89 * Effectively collecting garbage once every two months per AI. */
90 if ((AI::frame_counter
& 255) == 0) {
91 CompanyID cid
= (CompanyID
)GB(AI::frame_counter
, 8, 4);
92 if (Company::IsValidAiID(cid
)) Company::Get(cid
)->ai_instance
->CollectGarbage();
96 /* static */ uint
AI::GetTick()
98 return AI::frame_counter
;
101 /* static */ void AI::Stop(CompanyID company
)
103 if (_networking
&& !_network_server
) return;
105 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
106 Company
*c
= Company::Get(company
);
108 delete c
->ai_instance
;
109 c
->ai_instance
= nullptr;
110 c
->ai_info
= nullptr;
112 cur_company
.Restore();
114 InvalidateWindowData(WC_AI_DEBUG
, 0, -1);
115 DeleteWindowById(WC_AI_SETTINGS
, company
);
118 /* static */ void AI::Pause(CompanyID company
)
120 /* The reason why dedicated servers are forbidden to execute this
121 * command is not because it is unsafe, but because there is no way
122 * for the server owner to unpause the script again. */
123 if (_network_dedicated
) return;
125 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
126 Company::Get(company
)->ai_instance
->Pause();
128 cur_company
.Restore();
131 /* static */ void AI::Unpause(CompanyID company
)
133 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
134 Company::Get(company
)->ai_instance
->Unpause();
136 cur_company
.Restore();
139 /* static */ bool AI::IsPaused(CompanyID company
)
141 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
142 bool paused
= Company::Get(company
)->ai_instance
->IsPaused();
144 cur_company
.Restore();
149 /* static */ void AI::KillAll()
151 /* It might happen there are no companies .. than we have nothing to loop */
152 if (Company::GetPoolSize() == 0) return;
155 FOR_ALL_COMPANIES(c
) {
156 if (c
->is_ai
) AI::Stop(c
->index
);
160 /* static */ void AI::Initialize()
162 if (AI::scanner_info
!= nullptr) AI::Uninitialize(true);
164 AI::frame_counter
= 0;
165 if (AI::scanner_info
== nullptr) {
166 TarScanner::DoScan(TarScanner::AI
);
167 AI::scanner_info
= new AIScannerInfo();
168 AI::scanner_info
->Initialize();
169 AI::scanner_library
= new AIScannerLibrary();
170 AI::scanner_library
->Initialize();
174 /* static */ void AI::Uninitialize(bool keepConfig
)
179 /* Run a rescan, which indexes all AIInfos again, and check if we can
180 * still load all the AIS, while keeping the configs in place */
183 delete AI::scanner_info
;
184 delete AI::scanner_library
;
185 AI::scanner_info
= nullptr;
186 AI::scanner_library
= nullptr;
188 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
189 if (_settings_game
.ai_config
[c
] != nullptr) {
190 delete _settings_game
.ai_config
[c
];
191 _settings_game
.ai_config
[c
] = nullptr;
193 if (_settings_newgame
.ai_config
[c
] != nullptr) {
194 delete _settings_newgame
.ai_config
[c
];
195 _settings_newgame
.ai_config
[c
] = nullptr;
201 /* static */ void AI::ResetConfig()
203 /* Check for both newgame as current game if we can reload the AIInfo inside
204 * the AIConfig. If not, remove the AI from the list (which will assign
205 * a random new AI on reload). */
206 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
207 if (_settings_game
.ai_config
[c
] != nullptr && _settings_game
.ai_config
[c
]->HasScript()) {
208 if (!_settings_game
.ai_config
[c
]->ResetInfo(true)) {
209 DEBUG(script
, 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());
210 _settings_game
.ai_config
[c
]->Change(nullptr);
211 if (Company::IsValidAiID(c
)) {
212 /* The code belonging to an already running AI was deleted. We can only do
213 * one thing here to keep everything sane and that is kill the AI. After
214 * killing the offending AI we start a random other one in it's place, just
215 * like what would happen if the AI was missing during loading. */
217 AI::StartNew(c
, false);
219 } else if (Company::IsValidAiID(c
)) {
220 /* Update the reference in the Company struct. */
221 Company::Get(c
)->ai_info
= _settings_game
.ai_config
[c
]->GetInfo();
224 if (_settings_newgame
.ai_config
[c
] != nullptr && _settings_newgame
.ai_config
[c
]->HasScript()) {
225 if (!_settings_newgame
.ai_config
[c
]->ResetInfo(false)) {
226 DEBUG(script
, 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());
227 _settings_newgame
.ai_config
[c
]->Change(nullptr);
233 /* static */ void AI::NewEvent(CompanyID company
, ScriptEvent
*event
)
235 /* AddRef() and Release() need to be called at least once, so do it here */
238 /* Clients should ignore events */
239 if (_networking
&& !_network_server
) {
244 /* Only AIs can have an event-queue */
245 if (!Company::IsValidAiID(company
)) {
250 /* Queue the event */
251 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
252 Company::Get(_current_company
)->ai_instance
->InsertEvent(event
);
253 cur_company
.Restore();
258 /* static */ void AI::BroadcastNewEvent(ScriptEvent
*event
, CompanyID skip_company
)
260 /* AddRef() and Release() need to be called at least once, so do it here */
263 /* Clients should ignore events */
264 if (_networking
&& !_network_server
) {
269 /* Try to send the event to all AIs */
270 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
271 if (c
!= skip_company
) AI::NewEvent(c
, event
);
277 /* static */ void AI::Save(CompanyID company
)
279 if (!_networking
|| _network_server
) {
280 Company
*c
= Company::GetIfValid(company
);
281 assert(c
!= nullptr && c
->ai_instance
!= nullptr);
283 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
284 c
->ai_instance
->Save();
285 cur_company
.Restore();
287 AIInstance::SaveEmpty();
291 /* static */ void AI::Load(CompanyID company
, int version
)
293 if (!_networking
|| _network_server
) {
294 Company
*c
= Company::GetIfValid(company
);
295 assert(c
!= nullptr && c
->ai_instance
!= nullptr);
297 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
298 c
->ai_instance
->Load(version
);
299 cur_company
.Restore();
301 /* Read, but ignore, the load data */
302 AIInstance::LoadEmpty();
306 /* static */ int AI::GetStartNextTime()
308 /* Find the first company which doesn't exist yet */
309 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
310 if (!Company::IsValidID(c
)) return AIConfig::GetConfig(c
, AIConfig::SSS_FORCE_GAME
)->GetSetting("start_date");
313 /* Currently no AI can be started, check again in a year. */
317 /* static */ char *AI::GetConsoleList(char *p
, const char *last
, bool newest_only
)
319 return AI::scanner_info
->GetConsoleList(p
, last
, newest_only
);
322 /* static */ char *AI::GetConsoleLibraryList(char *p
, const char *last
)
324 return AI::scanner_library
->GetConsoleList(p
, last
, true);
327 /* static */ const ScriptInfoList
*AI::GetInfoList()
329 return AI::scanner_info
->GetInfoList();
332 /* static */ const ScriptInfoList
*AI::GetUniqueInfoList()
334 return AI::scanner_info
->GetUniqueInfoList();
337 /* static */ AIInfo
*AI::FindInfo(const char *name
, int version
, bool force_exact_match
)
339 return AI::scanner_info
->FindInfo(name
, version
, force_exact_match
);
342 /* static */ AILibrary
*AI::FindLibrary(const char *library
, int version
)
344 return AI::scanner_library
->FindLibrary(library
, version
);
347 /* static */ void AI::Rescan()
349 TarScanner::DoScan(TarScanner::AI
);
351 AI::scanner_info
->RescanDir();
352 AI::scanner_library
->RescanDir();
355 InvalidateWindowData(WC_AI_LIST
, 0, 1);
356 SetWindowClassesDirty(WC_AI_DEBUG
);
357 InvalidateWindowClassesData(WC_AI_SETTINGS
);
360 #if defined(ENABLE_NETWORK)
363 * Check whether we have an AI (library) with the exact characteristics as ci.
364 * @param ci the characteristics to search on (shortname and md5sum)
365 * @param md5sum whether to check the MD5 checksum
366 * @return true iff we have an AI (library) matching.
368 /* static */ bool AI::HasAI(const ContentInfo
*ci
, bool md5sum
)
370 return AI::scanner_info
->HasScript(ci
, md5sum
);
373 /* static */ bool AI::HasAILibrary(const ContentInfo
*ci
, bool md5sum
)
375 return AI::scanner_library
->HasScript(ci
, md5sum
);
378 #endif /* defined(ENABLE_NETWORK) */
380 /* static */ AIScannerInfo
*AI::GetScannerInfo()
382 return AI::scanner_info
;
385 /* static */ AIScannerLibrary
*AI::GetScannerLibrary()
387 return AI::scanner_library
;