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();
245 ScriptObjectRef counter(event);
265 ScriptObjectRef counter(event);
282 assert(c !=
nullptr);
285 if (c->ai_instance !=
nullptr) {
287 c->ai_instance->Save();
350 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.
class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Check if we have an AI by name and version available in our list.
class AIInfo * SelectRandomAI() const
Select a random AI.
class AILibrary * FindLibrary(const std::string &library, int version)
Find a library in the pool.
static uint GetTick()
Get the current AI tick.
static class AIScannerInfo * scanner_info
ScriptScanner instance that is used to find AIs.
static void Uninitialize(bool keepConfig)
Uninitialize the AI system.
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
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 class AIScannerLibrary * scanner_library
ScriptScanner instance that is used to find AI Libraries.
static void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only)
Wrapper function for AIScanner::GetAIConsoleList.
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 BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
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 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.
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void Change(std::optional< const std::string > 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.
const std::string & GetName() const
Get the Name of the script.
static void SaveEmpty()
Don't save any data in the savegame.
const ScriptInfoList * GetInfoList()
Get the list of all registered scripts.
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
const ScriptInfoList * GetUniqueInfoList()
Get the list of the latest version of all registered scripts.
void RescanDir()
Rescan the script dir.
void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only) const
Get the list of registered scripts to print on the console.
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
@ AI
Scan for AIs and its libraries.
CompanyID _current_company
Company currently doing an action.
Owner
Enum for all companies/owners.
@ COMPANY_FIRST
First company, same as owner.
@ MAX_COMPANIES
Maximum number of companies.
#define Debug(category, level, format_string,...)
Ouptut 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(size_t 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
class AIConfig * ai_config[MAX_COMPANIES]
settings per company
AISettings ai
what may the AI do?
DifficultySettings difficulty
settings related to the difficulty
static size_t GetPoolSize()
Returns first unused index.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static Titem * Get(size_t index)
Returns Titem with given index.
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: