2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai.hpp Base functions for all AIs. */
|
|
|
|
|
|
|
|
#ifndef AI_HPP
|
|
|
|
#define AI_HPP
|
|
|
|
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "../script/api/script_event_types.hpp"
|
2012-08-20 21:01:40 +00:00
|
|
|
#include "ai_scanner.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/**
|
|
|
|
* Main AI class. Contains all functions needed to start, stop, save and load AIs.
|
|
|
|
*/
|
2009-01-12 17:11:45 +00:00
|
|
|
class AI {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Is it possible to start a new AI company?
|
|
|
|
* @return True if a new AI company can be started.
|
|
|
|
*/
|
|
|
|
static bool CanStartNew();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a new AI company.
|
|
|
|
* @param company At which slot the AI company should start.
|
2010-01-09 14:41:22 +00:00
|
|
|
* @param rerandomise_ai Whether to rerandomise the configured AI.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2010-01-09 14:41:22 +00:00
|
|
|
static void StartNew(CompanyID company, bool rerandomise_ai = true);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called every game-tick to let AIs do something.
|
|
|
|
*/
|
|
|
|
static void GameLoop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current AI tick.
|
|
|
|
*/
|
|
|
|
static uint GetTick();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop a company to be controlled by an AI.
|
|
|
|
* @param company The company from which the AI needs to detach.
|
2009-06-10 22:05:01 +00:00
|
|
|
* @pre Company::IsValidAiID(company)
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
|
|
|
static void Stop(CompanyID company);
|
|
|
|
|
2010-04-02 17:35:20 +00:00
|
|
|
/**
|
2012-09-21 19:58:18 +00:00
|
|
|
* Suspend the AI and then pause execution of the script. The script
|
|
|
|
* will not be resumed from its suspended state until the script has
|
|
|
|
* been unpaused.
|
|
|
|
* @param company The company for which the AI should be paused.
|
2010-04-02 17:35:20 +00:00
|
|
|
* @pre Company::IsValidAiID(company)
|
|
|
|
*/
|
2012-09-21 19:58:18 +00:00
|
|
|
static void Pause(CompanyID company);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume execution of the AI. This function will not actually execute
|
|
|
|
* the script, but set a flag so that the script is executed my the usual
|
|
|
|
* mechanism that executes the script.
|
|
|
|
* @param company The company for which the AI should be unpaused.
|
|
|
|
* @pre Company::IsValidAiID(company)
|
|
|
|
*/
|
|
|
|
static void Unpause(CompanyID company);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the AI is paused.
|
|
|
|
* @param company The company for which to check if the AI is paused.
|
|
|
|
* @pre Company::IsValidAiID(company)
|
|
|
|
* @return true if the AI is paused, otherwise false.
|
|
|
|
*/
|
|
|
|
static bool IsPaused(CompanyID company);
|
2010-04-02 17:35:20 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Kill any and all AIs we manage.
|
|
|
|
*/
|
|
|
|
static void KillAll();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the AI system.
|
|
|
|
*/
|
|
|
|
static void Initialize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninitialize the AI system
|
|
|
|
* @param keepConfig Should we keep AIConfigs, or can we free that memory?
|
|
|
|
*/
|
|
|
|
static void Uninitialize(bool keepConfig);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all AIConfigs, and make them reload their AIInfo.
|
|
|
|
* If the AIInfo could no longer be found, an error is reported to the user.
|
|
|
|
*/
|
|
|
|
static void ResetConfig();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a new event for an AI.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static void NewEvent(CompanyID company, ScriptEvent *event);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcast a new event to all active AIs.
|
|
|
|
*/
|
2011-11-29 23:15:35 +00:00
|
|
|
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company = MAX_COMPANIES);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save data from an AI to a savegame.
|
|
|
|
*/
|
|
|
|
static void Save(CompanyID company);
|
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIConsoleList */
|
2023-05-19 21:22:30 +00:00
|
|
|
static void GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only);
|
2011-01-03 14:52:30 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIConsoleLibraryList */
|
2023-05-19 21:22:30 +00:00
|
|
|
static void GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator);
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIInfoList */
|
2011-11-29 23:21:52 +00:00
|
|
|
static const ScriptInfoList *GetInfoList();
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetUniqueAIInfoList */
|
2011-11-29 23:21:52 +00:00
|
|
|
static const ScriptInfoList *GetUniqueInfoList();
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::FindInfo */
|
2023-05-05 07:53:34 +00:00
|
|
|
static class AIInfo *FindInfo(const std::string &name, int version, bool force_exact_match);
|
2011-11-23 13:39:36 +00:00
|
|
|
/** Wrapper function for AIScanner::FindLibrary */
|
2023-05-05 07:53:34 +00:00
|
|
|
static class AILibrary *FindLibrary(const std::string &library, int version);
|
2010-07-31 22:16:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rescans all searchpaths for available AIs. If a used AI is no longer
|
|
|
|
* found it is removed from the config.
|
|
|
|
*/
|
2009-01-12 17:11:45 +00:00
|
|
|
static void Rescan();
|
2012-08-20 21:01:40 +00:00
|
|
|
|
|
|
|
/** Gets the ScriptScanner instance that is used to find AIs */
|
|
|
|
static AIScannerInfo *GetScannerInfo();
|
|
|
|
/** Gets the ScriptScanner instance that is used to find AI Libraries */
|
|
|
|
static AIScannerLibrary *GetScannerLibrary();
|
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::HasAI */
|
2009-01-17 16:53:32 +00:00
|
|
|
static bool HasAI(const struct ContentInfo *ci, bool md5sum);
|
2011-11-29 23:21:52 +00:00
|
|
|
static bool HasAILibrary(const ContentInfo *ci, bool md5sum);
|
2009-01-12 17:11:45 +00:00
|
|
|
private:
|
2011-11-29 23:21:52 +00:00
|
|
|
static uint frame_counter; ///< Tick counter for the AI code
|
|
|
|
static class AIScannerInfo *scanner_info; ///< ScriptScanner instance that is used to find AIs
|
|
|
|
static class AIScannerLibrary *scanner_library; ///< ScriptScanner instance that is used to find AI Libraries
|
2009-01-12 17:11:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* AI_HPP */
|