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"
24#include "../safeguards.h"
46 AIConfig *config = c->ai_config.get();
47 if (config ==
nullptr) {
49 config = c->ai_config.get();
52 AIInfo *info = config->GetInfo();
53 if (info ==
nullptr) {
55 assert(info !=
nullptr);
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);
88 cur_company.
Change(c->index);
89 c->ai_instance->GameLoop();
93 c->ai_instance->CollectGarbage();
115 c->ai_instance.reset();
116 c->ai_info =
nullptr;
117 c->ai_config.reset();
148 bool paused =
Company::Get(company)->ai_instance->IsPaused();
191 for (
CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
203 for (
CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
237 ScriptObjectRef counter(event);
257 ScriptObjectRef counter(event);
265 for (
CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
274 assert(c !=
nullptr);
277 if (c->ai_instance !=
nullptr) {
279 c->ai_instance->Save();
342 bool AI::HasAILibrary(
const ContentInfo &ci,
bool md5sum)
Base functions for all AIs.
AIConfig stores the configuration settings of every AI.
AIInfo keeps track of all information of an AI, like Author, Description, ...
The AIInstance tracks an AI.
declarations of the class for AI scanner
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
All static information from an AI like name, version, etc.
All static information from an AI library like name, version, etc.
static uint GetTick()
Get the current AI tick.
static std::unique_ptr< AIScannerLibrary > scanner_library
ScriptScanner instance that is used to find AI Libraries.
static void Uninitialize(bool keepConfig)
Uninitialize the AI system.
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
static void Pause(CompanyID company)
Suspend the AI and then pause execution of the script.
static void GetConsoleLibraryList(std::back_insert_iterator< std::string > &output_iterator)
Wrapper function for AIScanner::GetAIConsoleLibraryList.
static void Initialize()
Initialize the AI system.
static void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only)
Wrapper function for AIScanner::GetAIConsoleList.
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=CompanyID::Invalid())
Broadcast a new event to all active AIs.
static std::unique_ptr< AIScannerInfo > scanner_info
ScriptScanner instance that is used to find AIs.
static void GameLoop()
Called every game-tick to let AIs do something.
static bool CanStartNew()
Is it possible to start a new AI company?
static void StartNew(CompanyID company)
Start a new AI company.
static void Stop(CompanyID company)
Stop a company to be controlled by an AI.
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
static void ResetConfig()
Reset all AIConfigs, and make them reload their AIInfo.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
static const ScriptInfoList * GetUniqueInfoList()
Wrapper function for AIScanner::GetUniqueAIInfoList.
static bool HasAI(const ContentInfo &ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
static void Rescan()
Rescans all searchpaths for available AIs.
static uint frame_counter
Tick counter for the AI code.
static bool IsPaused(CompanyID company)
Checks if the AI is paused.
static void Save(CompanyID company)
Save data from an AI to a savegame.
static void Unpause(CompanyID company)
Resume execution of the AI.
static void KillAll()
Kill any and all AIs we manage.
static const ScriptInfoList * GetInfoList()
Wrapper function for AIScanner::GetAIInfoList.
static class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
@ SSS_FORCE_GAME
Get the Script config from the current game.
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
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.
const std::string & GetName() const
Get the Name of the script.
static void SaveEmpty()
Don't save any data in the savegame.
@ AI
Scan for AIs and its libraries.
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
CompanyID _current_company
Company currently doing an action.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
PerformanceElement
Elements of game performance that can be measured.
@ PFE_AI0
AI execution for player slot 1.
bool _networking
are we in networking mode?
bool _network_dedicated
are we a dedicated server?
bool _network_server
network-server is active
std::map< std::string, class ScriptInfo *, CaseInsensitiveComparator > ScriptInfoList
Type for the list of scripts.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
bool ai_in_multiplayer
so we allow AIs in multiplayer
Class to backup a specific variable and restore it later.
void Change(const U &new_value)
Change the value of the variable.
void Restore()
Restore the variable.
static bool IsValidAiID(auto index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Container for all important information about a piece of content.
uint8_t competitor_speed
the speed at which the AI builds
AISettings ai
what may the AI do?
DifficultySettings difficulty
settings related to the difficulty
ScriptConfigSettings script_config
AI and Gamescript configuration.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static size_t GetPoolSize()
Returns first unused index.
static Titem * Get(auto index)
Returns Titem with given index.
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Titem * GetIfValid(auto index)
Returns Titem with given index.
ReferenceThroughBaseContainer< std::array< std::unique_ptr< class AIConfig >, MAX_COMPANIES > > ai
settings per company
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
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-...
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...
@ WC_SCRIPT_SETTINGS
Script settings; Window numbers:
@ WC_SCRIPT_LIST
Scripts list; Window numbers:
@ WC_SCRIPT_DEBUG
Script debug window; Window numbers: