OpenTTD Source: AI Class Reference

OpenTTD Source 15.0-RC2

Static Public Member Functions | Static Private Attributes
AI Class Reference

Main AI class. More...

`#include <ai.hpp>`

## Static Public Member Functions
static bool | CanStartNew ()
| Is it possible to start a new AI company?
static void | StartNew (CompanyID company)
| Start a new AI company.
static void | GameLoop ()
| Called every game-tick to let AIs do something.
static uint | GetTick ()
| Get the current AI tick.
static void | Stop (CompanyID company)
| Stop a company to be controlled by an AI.
static void | Pause (CompanyID company)
| Suspend the AI and then pause execution of the script.
static void | Unpause (CompanyID company)
| Resume execution of the AI.
static bool | IsPaused (CompanyID company)
| Checks if the AI is paused.
static void | KillAll ()
| Kill any and all AIs we manage.
static void | Initialize ()
| Initialize the AI system.
static void | Uninitialize (bool keepConfig)
| Uninitialize the AI system.
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 void | BroadcastNewEvent (ScriptEvent *event, CompanyID skip_company=CompanyID::Invalid())
| Broadcast a new event to all active AIs.
static void | Save (CompanyID company)
| Save data from an AI to a savegame.
static void | GetConsoleList (std::back_insert_iterator< std::string > &output_iterator, bool newest_only)
| Wrapper function for AIScanner::GetAIConsoleList.
static void | GetConsoleLibraryList (std::back_insert_iterator< std::string > &output_iterator)
| Wrapper function for AIScanner::GetAIConsoleLibraryList.
static const ScriptInfoList * | GetInfoList ()
| Wrapper function for AIScanner::GetAIInfoList.
static const ScriptInfoList * | GetUniqueInfoList ()
| Wrapper function for AIScanner::GetUniqueAIInfoList.
static class AIInfo * | FindInfo (const std::string &name, int version, bool force_exact_match)
| Wrapper function for AIScanner::FindInfo.
static class AILibrary * | FindLibrary (const std::string &library, int version)
| Wrapper function for AIScanner::FindLibrary.
static void | Rescan ()
| Rescans all searchpaths for available AIs.
static AIScannerInfo * | GetScannerInfo ()
| Gets the ScriptScanner instance that is used to find AIs.
static AIScannerLibrary * | GetScannerLibrary ()
| Gets the ScriptScanner instance that is used to find AI Libraries.
static bool | HasAI (const ContentInfo &ci, bool md5sum)
| Wrapper function for AIScanner::HasAI.
static bool | HasAILibrary (const ContentInfo &ci, bool md5sum)

## Static Private Attributes
static uint | frame_counter = 0
| Tick counter for the AI code.
static std::unique_ptr< AIScannerInfo > | scanner_info = nullptr
| ScriptScanner instance that is used to find AIs.
static std::unique_ptr< AIScannerLibrary > | scanner_library = nullptr
| ScriptScanner instance that is used to find AI Libraries.

## Detailed Description

Main AI class.

Contains all functions needed to start, stop, save and load AIs.

Definition at line 19 of file ai.hpp.

## Member Function Documentation

## ◆ BroadcastNewEvent()

void AI::BroadcastNewEvent | ( | ScriptEvent * | event,
|  | CompanyID | skip_company = `CompanyID::Invalid()`
| ) |  |
| static

Broadcast a new event to all active AIs.

Definition at line 255 of file ai_core.cpp.

References _network_server, _networking, and NewEvent().

Referenced by AdvertiseIndustryOpening(), Subsidy::AwardTo(), ChangeIndustryProduction(), CmdCompanyCtrl(), CmdFoundTown(), CmdRenameCompany(), CmdRenamePresident(), CompanyCheckBankrupt(), CreateSubsidy(), DoStartupNewCompany(), GenerateCompanyName(), NewVehicleAvailable(), TownActionBuyRights(), and TownActionRoadRebuild().

## ◆ CanStartNew()

bool AI::CanStartNew | ( |  | ) |
| static

Is it possible to start a new AI company?

Returns
  True if a new AI company can be started.

Definition at line 30 of file ai_core.cpp.

References _network_server, _networking, _settings_game, GameSettings::ai, and AISettings::ai_in_multiplayer.

Referenced by OnTick_Companies().

## ◆ FindInfo()

AIInfo * AI::FindInfo | ( | const std::string & | name,
|  | int | version,
|  | bool | force_exact_match
| ) |  |
| static

Wrapper function for AIScanner::FindInfo.

Definition at line 308 of file ai_core.cpp.

References scanner_info.

Referenced by AIConfig::FindInfo(), and AIConfig::ResetInfo().

## ◆ FindLibrary()

AILibrary * AI::FindLibrary | ( | const std::string & | library,
|  | int | version
| ) |  |
| static

Wrapper function for AIScanner::FindLibrary.

Definition at line 313 of file ai_core.cpp.

References scanner_library.

Referenced by AIInstance::FindLibrary().

## ◆ GameLoop()

void AI::GameLoop | ( |  | ) |
| static

Called every game-tick to let AIs do something.

Definition at line 74 of file ai_core.cpp.

References _current_company, _network_server, _networking, _settings_game, GameSettings::ai, AISettings::ai_in_multiplayer, Backup< T >::Change(), DifficultySettings::competitor_speed, GameSettings::difficulty, frame_counter, GB(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Iterate(), PFE_AI0, Backup< T >::Restore(), and PerformanceMeasurer::SetInactive().

Referenced by StateGameLoop().

## ◆ GetConsoleLibraryList()

void AI::GetConsoleLibraryList | ( | std::back_insert_iterator< std::string > & | output_iterator | ) |
| static

Wrapper function for AIScanner::GetAIConsoleLibraryList.

Definition at line 293 of file ai_core.cpp.

References scanner_library.

## ◆ GetConsoleList()

void AI::GetConsoleList | ( | std::back_insert_iterator< std::string > & | output_iterator,
|  | bool | newest_only
| ) |  |
| static

Wrapper function for AIScanner::GetAIConsoleList.

Definition at line 288 of file ai_core.cpp.

References scanner_info.

Referenced by ShowHelp().

## ◆ GetInfoList()

const ScriptInfoList * AI::GetInfoList | ( |  | ) |
| static

Wrapper function for AIScanner::GetAIInfoList.

Definition at line 298 of file ai_core.cpp.

References scanner_info.

Referenced by ScriptListWindow::ScriptListWindow().

## ◆ GetScannerInfo()

AIScannerInfo * AI::GetScannerInfo | ( |  | ) |
| static

Gets the ScriptScanner instance that is used to find AIs.

Definition at line 347 of file ai_core.cpp.

References scanner_info.

Referenced by ContentInfo::GetTextfile().

## ◆ GetScannerLibrary()

AIScannerLibrary * AI::GetScannerLibrary | ( |  | ) |
| static

Gets the ScriptScanner instance that is used to find AI Libraries.

Definition at line 352 of file ai_core.cpp.

References scanner_library.

Referenced by ContentInfo::GetTextfile().

## ◆ GetTick()

uint AI::GetTick | ( |  | ) |
| static

Get the current AI tick.

Definition at line 102 of file ai_core.cpp.

References frame_counter.

## ◆ GetUniqueInfoList()

const ScriptInfoList * AI::GetUniqueInfoList | ( |  | ) |
| static

Wrapper function for AIScanner::GetUniqueAIInfoList.

Definition at line 303 of file ai_core.cpp.

References scanner_info.

Referenced by ScriptListWindow::ScriptListWindow().

## ◆ HasAI()

bool AI::HasAI | ( | const ContentInfo & | ci,
|  | bool | md5sum
| ) |  |
| static

Wrapper function for AIScanner::HasAI.

Check whether we have an AI (library) with the exact characteristics as ci.

Parameters

  ci | the characteristics to search on (shortname and md5sum)
  md5sum | whether to check the MD5 checksum

Returns
  true iff we have an AI (library) matching.

Definition at line 337 of file ai_core.cpp.

References scanner_info.

Referenced by GetHasContentProcforContentType().

## ◆ HasAILibrary()

bool AI::HasAILibrary | ( | const ContentInfo & | ci,
|  | bool | md5sum
| ) |  |
| static

Definition at line 342 of file ai_core.cpp.

## ◆ Initialize()

void AI::Initialize | ( |  | ) |
| static

Initialize the AI system.

Definition at line 165 of file ai_core.cpp.

References TarScanner::AI, TarScanner::DoScan(), frame_counter, scanner_info, scanner_library, and Uninitialize().

Referenced by AfterNewGRFScan::OnNewGRFsScanned(), and ShowHelp().

## ◆ IsPaused()

bool AI::IsPaused | ( | CompanyID | company | ) |
| static

Checks if the AI is paused.

Parameters

  company | The company for which to check if the AI is paused.

Precondition
  Company::IsValidAiID(company)

Returns
  true if the AI is paused, otherwise false.

Definition at line 145 of file ai_core.cpp.

References _current_company, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), and Backup< T >::Restore().

Referenced by ScriptDebugWindow::OnClick(), and ScriptDebugWindow::OnInvalidateData().

## ◆ KillAll()

void AI::KillAll | ( |  | ) |
| static

Kill any and all AIs we manage.

Definition at line 155 of file ai_core.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::GetPoolSize(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Iterate(), and Stop().

Referenced by Uninitialize().

## ◆ NewEvent()

void AI::NewEvent | ( | CompanyID | company,
|  | ScriptEvent * | event
| ) |  |
| static

Queue a new event for an AI.

Definition at line 235 of file ai_core.cpp.

References _current_company, _network_server, _networking, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), Company::IsValidAiID(), and Backup< T >::Restore().

Referenced by AircraftEntersTerminal(), AircraftHandleDestTooFar(), BroadcastNewEvent(), CrashAirplane(), DisasterTick_Ufo(), DisasterTick_Zeppeliner(), FloodVehicle(), HandleBankruptcyTakeover(), Vehicle::HandlePathfindingResult(), ReplaceChain(), ReplaceFreeUnit(), RoadVehArrivesAt(), ShipArrivesAt(), TrainCrashed(), TrainEnterStation(), and VehicleEnterDepot().

## ◆ Pause()

void AI::Pause | ( | CompanyID | company | ) |
| static

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.

Parameters

  company | The company for which the AI should be paused.

Precondition
  Company::IsValidAiID(company)

Definition at line 124 of file ai_core.cpp.

References _current_company, _network_dedicated, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), and Backup< T >::Restore().

Referenced by ScriptDebugWindow::OnInvalidateData().

## ◆ Rescan()

void AI::Rescan | ( |  | ) |
| static

Rescans all searchpaths for available AIs.

If a used AI is no longer found it is removed from the config.

Definition at line 318 of file ai_core.cpp.

References TarScanner::AI, TarScanner::DoScan(), InvalidateWindowClassesData(), InvalidateWindowData(), ResetConfig(), scanner_info, scanner_library, SetWindowClassesDirty(), WC_SCRIPT_DEBUG, WC_SCRIPT_LIST, and WC_SCRIPT_SETTINGS.

Referenced by NetworkContentDownloadStatusWindow::Close(), and Uninitialize().

## ◆ ResetConfig()

void AI::ResetConfig | ( |  | ) |
| static

Reset all AIConfigs, and make them reload their AIInfo.

If the AIInfo could no longer be found, an error is reported to the user.

Definition at line 198 of file ai_core.cpp.

References _settings_game, _settings_newgame, ScriptConfigSettings::ai, Debug, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), Company::IsValidAiID(), AIConfig::ResetInfo(), GameSettings::script_config, StartNew(), and Stop().

Referenced by Rescan().

## ◆ Save()

void AI::Save | ( | CompanyID | company | ) |
| static

Save data from an AI to a savegame.

Definition at line 270 of file ai_core.cpp.

References _current_company, _network_server, _networking, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::GetIfValid(), Backup< T >::Restore(), and ScriptInstance::SaveEmpty().

## ◆ StartNew()

void AI::StartNew | ( | CompanyID | company | ) |
| static

Start a new AI company.

Parameters

  company | At which slot the AI company should start.

Definition at line 36 of file ai_core.cpp.

References _current_company, _network_server, _networking, ScriptConfig::AnchorUnchangeableSettings(), ScriptConfig::Change(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), AIConfig::GetConfig(), ScriptInfo::GetName(), InvalidateWindowClassesData(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::IsValidID(), Backup< T >::Restore(), scanner_info, ScriptConfig::SSS_FORCE_GAME, and WC_SCRIPT_DEBUG.

Referenced by ScriptListWindow::ChangeScript(), DoStartupNewCompany(), ResetConfig(), and StartScripts().

## ◆ Stop()

void AI::Stop | ( | CompanyID | company | ) |
| static

Stop a company to be controlled by an AI.

Parameters

  company | The company from which the AI needs to detach.

Precondition
  Company::IsValidAiID(company)

Definition at line 107 of file ai_core.cpp.

References _current_company, _network_server, _networking, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), InvalidateWindowClassesData(), PFE_AI0, Backup< T >::Restore(), PerformanceMeasurer::SetInactive(), and WC_SCRIPT_DEBUG.

Referenced by CmdCompanyCtrl(), KillAll(), and ResetConfig().

## ◆ Uninitialize()

void AI::Uninitialize | ( | bool | keepConfig | ) |
| static

Uninitialize the AI system.

Parameters

  keepConfig | Should we keep AIConfigs, or can we free that memory?

Definition at line 179 of file ai_core.cpp.

References _settings_game, _settings_newgame, ScriptConfigSettings::ai, KillAll(), Rescan(), scanner_info, scanner_library, and GameSettings::script_config.

Referenced by Initialize(), AfterNewGRFScan::OnNewGRFsScanned(), ShowHelp(), and ShutdownGame().

## ◆ Unpause()

void AI::Unpause | ( | CompanyID | company | ) |
| static

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.

Parameters

  company | The company for which the AI should be unpaused.

Precondition
  Company::IsValidAiID(company)

Definition at line 137 of file ai_core.cpp.

References _current_company, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::Get(), and Backup< T >::Restore().

Referenced by ScriptDebugWindow::OnClick().

## Field Documentation

## ◆ frame_counter

uint AI::frame_counter = 0
| staticprivate

Tick counter for the AI code.

Definition at line 141 of file ai.hpp.

Referenced by GameLoop(), GetTick(), and Initialize().

## ◆ scanner_info

std::unique_ptr< AIScannerInfo > AI::scanner_info = nullptr
| staticprivate

ScriptScanner instance that is used to find AIs.

Definition at line 142 of file ai.hpp.

Referenced by FindInfo(), GetConsoleList(), GetInfoList(), GetScannerInfo(), GetUniqueInfoList(), HasAI(), Initialize(), Rescan(), StartNew(), and Uninitialize().

## ◆ scanner_library

std::unique_ptr< AIScannerLibrary > AI::scanner_library = nullptr
| staticprivate

ScriptScanner instance that is used to find AI Libraries.

Definition at line 143 of file ai.hpp.

Referenced by FindLibrary(), GetConsoleLibraryList(), GetScannerLibrary(), Initialize(), Rescan(), and Uninitialize().

The documentation for this class was generated from the following files:

- src/ai/ai.hpp
- src/ai/ai_core.cpp