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/>.
8 /** @file ai.hpp Base functions for all AIs. */
13 #include "../script/api/script_event_types.hpp"
14 #include "../core/string_compare_type.hpp"
15 #include "ai_scanner.hpp"
18 /** A list that maps AI names to their AIInfo object. */
19 typedef std::map
<const char *, class ScriptInfo
*, StringCompare
> ScriptInfoList
;
22 * Main AI class. Contains all functions needed to start, stop, save and load AIs.
27 * The default months AIs start after each other.
30 START_NEXT_EASY
= DAYS_IN_YEAR
* 2,
31 START_NEXT_MEDIUM
= DAYS_IN_YEAR
,
32 START_NEXT_HARD
= DAYS_IN_YEAR
/ 2,
34 START_NEXT_MAX
= 3600,
35 START_NEXT_DEVIATION
= 60,
39 * Is it possible to start a new AI company?
40 * @return True if a new AI company can be started.
42 static bool CanStartNew();
45 * Start a new AI company.
46 * @param company At which slot the AI company should start.
47 * @param rerandomise_ai Whether to rerandomise the configured AI.
49 static void StartNew(CompanyID company
, bool rerandomise_ai
= true);
52 * Called every game-tick to let AIs do something.
54 static void GameLoop();
57 * Get the current AI tick.
59 static uint
GetTick();
62 * Stop a company to be controlled by an AI.
63 * @param company The company from which the AI needs to detach.
64 * @pre Company::IsValidAiID(company)
66 static void Stop(CompanyID company
);
69 * Suspend the AI and then pause execution of the script. The script
70 * will not be resumed from its suspended state until the script has
72 * @param company The company for which the AI should be paused.
73 * @pre Company::IsValidAiID(company)
75 static void Pause(CompanyID company
);
78 * Resume execution of the AI. This function will not actually execute
79 * the script, but set a flag so that the script is executed my the usual
80 * mechanism that executes the script.
81 * @param company The company for which the AI should be unpaused.
82 * @pre Company::IsValidAiID(company)
84 static void Unpause(CompanyID company
);
87 * Checks if the AI is paused.
88 * @param company The company for which to check if the AI is paused.
89 * @pre Company::IsValidAiID(company)
90 * @return true if the AI is paused, otherwise false.
92 static bool IsPaused(CompanyID company
);
95 * Kill any and all AIs we manage.
97 static void KillAll();
100 * Initialize the AI system.
102 static void Initialize();
105 * Uninitialize the AI system
106 * @param keepConfig Should we keep AIConfigs, or can we free that memory?
108 static void Uninitialize(bool keepConfig
);
111 * Reset all AIConfigs, and make them reload their AIInfo.
112 * If the AIInfo could no longer be found, an error is reported to the user.
114 static void ResetConfig();
117 * Queue a new event for an AI.
119 static void NewEvent(CompanyID company
, ScriptEvent
*event
);
122 * Broadcast a new event to all active AIs.
124 static void BroadcastNewEvent(ScriptEvent
*event
, CompanyID skip_company
= MAX_COMPANIES
);
127 * Save data from an AI to a savegame.
129 static void Save(CompanyID company
);
132 * Load data for an AI from a savegame.
134 static void Load(CompanyID company
, int version
);
137 * Get the number of days before the next AI should start.
139 static int GetStartNextTime();
141 /** Wrapper function for AIScanner::GetAIConsoleList */
142 static char *GetConsoleList(char *p
, const char *last
, bool newest_only
= false);
143 /** Wrapper function for AIScanner::GetAIConsoleLibraryList */
144 static char *GetConsoleLibraryList(char *p
, const char *last
);
145 /** Wrapper function for AIScanner::GetAIInfoList */
146 static const ScriptInfoList
*GetInfoList();
147 /** Wrapper function for AIScanner::GetUniqueAIInfoList */
148 static const ScriptInfoList
*GetUniqueInfoList();
149 /** Wrapper function for AIScanner::FindInfo */
150 static class AIInfo
*FindInfo(const char *name
, int version
, bool force_exact_match
);
151 /** Wrapper function for AIScanner::FindLibrary */
152 static class AILibrary
*FindLibrary(const char *library
, int version
);
155 * Rescans all searchpaths for available AIs. If a used AI is no longer
156 * found it is removed from the config.
158 static void Rescan();
160 /** Gets the ScriptScanner instance that is used to find AIs */
161 static AIScannerInfo
*GetScannerInfo();
162 /** Gets the ScriptScanner instance that is used to find AI Libraries */
163 static AIScannerLibrary
*GetScannerLibrary();
165 /** Wrapper function for AIScanner::HasAI */
166 static bool HasAI(const struct ContentInfo
*ci
, bool md5sum
);
167 static bool HasAILibrary(const ContentInfo
*ci
, bool md5sum
);
169 static uint frame_counter
; ///< Tick counter for the AI code
170 static class AIScannerInfo
*scanner_info
; ///< ScriptScanner instance that is used to find AIs
171 static class AIScannerLibrary
*scanner_library
; ///< ScriptScanner instance that is used to find AI Libraries