OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
10 #include "../stdafx.h"
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();
291 assert(c !=
nullptr);
294 if (c->ai_instance !=
nullptr) {
296 c->ai_instance->Save();
359 bool AI::HasAILibrary(
const ContentInfo *ci,
bool md5sum)
static void Uninitialize(bool keepConfig)
Uninitialize the AI system.
void Change(const U &new_value)
Change the value of the variable.
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-...
@ WC_SCRIPT_DEBUG
Script debug window; Window numbers:
std::map< std::string, class ScriptInfo *, CaseInsensitiveComparator > ScriptInfoList
Type for the list of scripts.
class AIConfig * ai_config[MAX_COMPANIES]
settings per company
static Titem * Get(size_t index)
Returns Titem with given index.
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Class to backup a specific variable and restore it later.
class AILibrary * FindLibrary(const std::string &library, int version)
Find a library in the pool.
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
bool _network_server
network-server is active
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Owner
Enum for all companies/owners.
DifficultySettings difficulty
settings related to the difficulty
uint8_t competitor_speed
the speed at which the AI builds
static bool CanStartNew()
Is it possible to start a new AI company?
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.
void RescanDir()
Rescan the script dir.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
PerformanceElement
Elements of game performance that can be measured.
const ScriptInfoList * GetUniqueInfoList()
Get the list of the latest version of all registered scripts.
static void StartNew(CompanyID company)
Start a new AI company.
int version
Version of the script.
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.
static void Unpause(CompanyID company)
Resume execution of the AI.
static size_t GetPoolSize()
Returns first unused index.
static void GetConsoleLibraryList(std::back_insert_iterator< std::string > &output_iterator)
Wrapper function for AIScanner::GetAIConsoleLibraryList.
@ COMPANY_FIRST
First company, same as owner.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
static class AIScannerLibrary * scanner_library
ScriptScanner instance that is used to find AI Libraries.
static void Save(CompanyID company)
Save data from an AI to a savegame.
Container for all important information about a piece of content.
static void SaveEmpty()
Don't save any data in the savegame.
All static information from an AI library like name, version, etc.
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
static void Pause(CompanyID company)
Suspend the AI and then pause execution of the script.
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
static void Initialize()
Initialize the AI system.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static class AIScannerInfo * scanner_info
ScriptScanner instance that is used to find AIs.
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
const std::string & GetName() const
Get the Name of the script.
static const ScriptInfoList * GetInfoList()
Wrapper function for AIScanner::GetAIInfoList.
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
bool _networking
are we in networking mode?
static uint GetTick()
Get the current AI tick.
bool _network_dedicated
are we a dedicated server?
static void GameLoop()
Called every game-tick to let AIs do something.
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
static bool IsPaused(CompanyID company)
Checks if the AI is paused.
static uint frame_counter
Tick counter for the AI code.
AISettings ai
what may the AI do?
CompanyID _current_company
Company currently doing an action.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
const ScriptInfoList * GetInfoList()
Get the list of all registered scripts.
static void KillAll()
Kill any and all AIs we manage.
void Restore()
Restore the variable.
@ AI
Scan for AIs and its libraries.
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...
std::string name
Full name of the script.
All static information from an AI like name, version, etc.
static const ScriptInfoList * GetUniqueInfoList()
Wrapper function for AIScanner::GetUniqueAIInfoList.
static void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only)
Wrapper function for AIScanner::GetAIConsoleList.
static class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
@ MAX_COMPANIES
Maximum number of companies.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
@ PFE_AI0
AI execution for player slot 1.
static void Stop(CompanyID company)
Stop a company to be controlled by an AI.
@ WC_SCRIPT_SETTINGS
Script settings; Window numbers:
@ WC_SCRIPT_LIST
Scripts list; Window numbers:
static void Rescan()
Rescans all searchpaths for available AIs.
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
@ SSS_FORCE_GAME
Get the Script config from the current game.
static void ResetConfig()
Reset all AIConfigs, and make them reload their AIInfo.
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
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.
bool ai_in_multiplayer
so we allow AIs in multiplayer
class AIInfo * SelectRandomAI() const
Select a random AI.
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
const std::string & GetName() const
Get the name of the Script.