OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- Data Structures
- Class Hierarchy
- Data Fields

Public Types | Static Public Member Functions | Static Private Attributes
AI Class Reference

Main AI class. More...

`#include <ai.hpp>`

## Public Types
enum | StartNext { START_NEXT_EASY = DAYS_IN_YEAR * 2, START_NEXT_MEDIUM = DAYS_IN_YEAR, START_NEXT_HARD = DAYS_IN_YEAR / 2, START_NEXT_MIN = 1, START_NEXT_MAX = 3600, START_NEXT_DEVIATION = 60 }
| The default months AIs start after each other.

## Static Public Member Functions
static bool | CanStartNew ()
| Is it possible to start a new AI company?
static void | StartNew (CompanyID company, bool rerandomise_ai=true)
| 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=MAX_COMPANIES)
| Broadcast a new event to all active AIs.
static void | Save (CompanyID company)
| Save data from an AI to a savegame.
static void | Load (CompanyID company, int version)
| Load data for an AI from a savegame.
static int | GetStartNextTime ()
| Get the number of days before the next AI should start.
static char * | GetConsoleList (char *p, const char *last, bool newest_only=false)
| Wrapper function for AIScanner::GetAIConsoleList.
static char * | GetConsoleLibraryList (char *p, const char *last)
| 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 char *name, int version, bool force_exact_match)
| Wrapper function for AIScanner::FindInfo.
static class AILibrary * | FindLibrary (const char *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 struct 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 class AIScannerInfo * | scanner_info = NULL
| ScriptScanner instance that is used to find AIs.
static class AIScannerLibrary * | scanner_library = NULL
| 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 26 of file ai.hpp.

## Member Function Documentation

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 31 of file ai_core.cpp.

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

Referenced by OnTick_Companies().

bool AI::HasAI | ( | const struct 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 368 of file ai_core.cpp.

References ScriptScanner::HasScript().

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 139 of file ai_core.cpp.

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

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

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 118 of file ai_core.cpp.

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

Referenced by AIDebugWindow::OnInvalidateData().

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 347 of file ai_core.cpp.

References TarScanner::AI, TarScanner::DoScan(), InvalidateWindowClassesData(), InvalidateWindowData(), ScriptScanner::RescanDir(), ResetConfig(), SetWindowClassesDirty(), WC_AI_DEBUG, WC_AI_LIST, and WC_AI_SETTINGS.

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

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 201 of file ai_core.cpp.

References _settings_game, _settings_newgame, GameSettings::ai_config, ScriptConfig::Change(), COMPANY_FIRST, DEBUG, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), AIConfig::GetInfo(), ScriptConfig::GetName(), ScriptConfig::HasScript(), Company::IsValidAiID(), MAX_COMPANIES, AIConfig::ResetInfo(), StartNew(), and Stop().

Referenced by Rescan().

void AI::StartNew | ( | CompanyID | company,
|  | bool | rerandomise_ai = `true`
| ) |  |
| static

Start a new AI company.

Parameters

  company | At which slot the AI company should start.
  rerandomise_ai | Whether to rerandomise the configured AI.

Definition at line 37 of file ai_core.cpp.

References _current_company, _network_server, _networking, ScriptConfig::AnchorUnchangeableSettings(), ScriptConfig::Change(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), AIConfig::GetConfig(), AIConfig::GetInfo(), ScriptInfo::GetName(), AIInstance::Initialize(), InvalidateWindowData(), ScriptConfig::IsRandom(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Backup< T >::Restore(), AIScannerInfo::SelectRandomAI(), ScriptConfig::SSS_FORCE_GAME, and WC_AI_DEBUG.

Referenced by AfterLoadGame(), DoStartupNewCompany(), and ResetConfig().

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 101 of file ai_core.cpp.

References _current_company, _network_server, _networking, DeleteWindowById(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), InvalidateWindowData(), Backup< T >::Restore(), WC_AI_DEBUG, and WC_AI_SETTINGS.

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

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 174 of file ai_core.cpp.

References _settings_game, _settings_newgame, GameSettings::ai_config, COMPANY_FIRST, KillAll(), MAX_COMPANIES, Rescan(), scanner_info, and scanner_library.

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

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 131 of file ai_core.cpp.

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

Referenced by AIDebugWindow::OnClick().

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

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