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.hpp Base functions for all AIs. */
15 #include "../script/api/script_event_types.hpp"
16 #include "../core/string_compare_type.hpp"
17 #include "ai_scanner.hpp"
20 /** A list that maps AI names to their AIInfo object. */
21 typedef std::map
<const char *, class ScriptInfo
*, StringCompare
> ScriptInfoList
;
24 * Main AI class. Contains all functions needed to start, stop, save and load AIs.
29 * The default months AIs start after each other.
32 START_NEXT_EASY
= DAYS_IN_YEAR
* 2,
33 START_NEXT_MEDIUM
= DAYS_IN_YEAR
,
34 START_NEXT_HARD
= DAYS_IN_YEAR
/ 2,
36 START_NEXT_MAX
= 3600,
37 START_NEXT_DEVIATION
= 60,
41 * Is it possible to start a new AI company?
42 * @return True if a new AI company can be started.
44 static bool CanStartNew();
47 * Start a new AI company.
48 * @param company At which slot the AI company should start.
49 * @param rerandomise_ai Whether to rerandomise the configured AI.
51 static void StartNew(CompanyID company
, bool rerandomise_ai
= true);
54 * Called every game-tick to let AIs do something.
56 static void GameLoop();
59 * Get the current AI tick.
61 static uint
GetTick();
64 * Stop a company to be controlled by an AI.
65 * @param company The company from which the AI needs to detach.
66 * @pre Company::IsValidAiID(company)
68 static void Stop(CompanyID company
);
71 * Suspend the AI and then pause execution of the script. The script
72 * will not be resumed from its suspended state until the script has
74 * @param company The company for which the AI should be paused.
75 * @pre Company::IsValidAiID(company)
77 static void Pause(CompanyID company
);
80 * Resume execution of the AI. This function will not actually execute
81 * the script, but set a flag so that the script is executed my the usual
82 * mechanism that executes the script.
83 * @param company The company for which the AI should be unpaused.
84 * @pre Company::IsValidAiID(company)
86 static void Unpause(CompanyID company
);
89 * Checks if the AI is paused.
90 * @param company The company for which to check if the AI is paused.
91 * @pre Company::IsValidAiID(company)
92 * @return true if the AI is paused, otherwise false.
94 static bool IsPaused(CompanyID company
);
97 * Kill any and all AIs we manage.
99 static void KillAll();
102 * Initialize the AI system.
104 static void Initialize();
107 * Uninitialize the AI system
108 * @param keepConfig Should we keep AIConfigs, or can we free that memory?
110 static void Uninitialize(bool keepConfig
);
113 * Reset all AIConfigs, and make them reload their AIInfo.
114 * If the AIInfo could no longer be found, an error is reported to the user.
116 static void ResetConfig();
119 * Queue a new event for an AI.
121 static void NewEvent(CompanyID company
, ScriptEvent
*event
);
124 * Broadcast a new event to all active AIs.
126 static void BroadcastNewEvent(ScriptEvent
*event
, CompanyID skip_company
= MAX_COMPANIES
);
129 * Save data from an AI to a savegame.
131 static void Save(CompanyID company
);
134 * Load data for an AI from a savegame.
136 static void Load(CompanyID company
, int version
);
139 * Get the number of days before the next AI should start.
141 static int GetStartNextTime();
143 /** Wrapper function for AIScanner::GetAIConsoleList */
144 static char *GetConsoleList(char *p
, const char *last
, bool newest_only
= false);
145 /** Wrapper function for AIScanner::GetAIConsoleLibraryList */
146 static char *GetConsoleLibraryList(char *p
, const char *last
);
147 /** Wrapper function for AIScanner::GetAIInfoList */
148 static const ScriptInfoList
*GetInfoList();
149 /** Wrapper function for AIScanner::GetUniqueAIInfoList */
150 static const ScriptInfoList
*GetUniqueInfoList();
151 /** Wrapper function for AIScanner::FindInfo */
152 static class AIInfo
*FindInfo(const char *name
, int version
, bool force_exact_match
);
153 /** Wrapper function for AIScanner::FindLibrary */
154 static class AILibrary
*FindLibrary(const char *library
, int version
);
157 * Rescans all searchpaths for available AIs. If a used AI is no longer
158 * found it is removed from the config.
160 static void Rescan();
162 /** Gets the ScriptScanner instance that is used to find AIs */
163 static AIScannerInfo
*GetScannerInfo();
164 /** Gets the ScriptScanner instance that is used to find AI Libraries */
165 static AIScannerLibrary
*GetScannerLibrary();
167 #if defined(ENABLE_NETWORK)
168 /** Wrapper function for AIScanner::HasAI */
169 static bool HasAI(const struct ContentInfo
*ci
, bool md5sum
);
170 static bool HasAILibrary(const ContentInfo
*ci
, bool md5sum
);
173 static uint frame_counter
; ///< Tick counter for the AI code
174 static class AIScannerInfo
*scanner_info
; ///< ScriptScanner instance that is used to find AIs
175 static class AIScannerLibrary
*scanner_library
; ///< ScriptScanner instance that is used to find AI Libraries