# AIController Class Reference

The Controller, the class each AI should extend. More...

`#include <ai_controller.hpp>`

## Public Member Functions
| AIController ()
| Initializer of the AIController.
| ~AIController ()
| Destructor of the AIController.
void | Start ()
| This function is called to start your AI.
## Static Public Member Functions
static const char * | GetClassName ()
static uint | GetTick ()
| Find at which tick your AI currently is.
static int | GetSetting (const char *name)
| Get the value of one of your settings you set via info.nut.
static uint | GetVersion ()
| Get the OpenTTD version of this executable.
static void | SetCommandDelay (int ticks)
| Change the minimum amount of time the AI should be put in suspend mode when you execute a command.
static void | Sleep (int ticks)
| Sleep for X ticks.
static void | Print (bool error_msg, const char *message)
| When Squirrel triggers a print, this function is called.
## Private Types
typedef std::map< const char *, const char *, StringCompare > | LoadedLibraryList
## Private Member Functions
void | RegisterClasses ()
| Register all classes that are known inside the NoAI API.
bool | LoadedLibrary (const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len)
| Check if a library is already loaded.
void | AddLoadedLibrary (const char *library_name, const char *fake_class_name)
| Add a library as loaded.
## Private Attributes
uint | ticks
LoadedLibraryList | loaded_library
int | loaded_library_count
## Friends
class | AIScanner
class | AIInstance

## Detailed Description

The Controller, the class each AI should extend.

It creates the AI, makes sure the logic kicks in correctly, and that GetTick() has a valid value.

Definition at line 15 of file ai_controller.hpp.

## Member Function Documentation

void AIController::Start | ( |  | ) |

This function is called to start your AI.

Your AI starts here. If you return from this function, your AI dies, so make sure that doesn't happen.

Note:
  Cannot be called from within your AI.

uint AIController::GetTick | ( |  | ) | `[static]`

Find at which tick your AI currently is.

Returns:
  returns the current tick.

Definition at line 60 of file ai_controller.cpp.

int AIController::GetSetting | ( | const char * | name | ) | `[static]`

Get the value of one of your settings you set via info.nut.

Parameters:

  | name | The name of the setting.

Returns:
  the value for the setting, or -1 if the setting is not known.

Definition at line 65 of file ai_controller.cpp.

uint AIController::GetVersion | ( |  | ) | `[static]`

Get the OpenTTD version of this executable.

The version is formatted with the bits having the following meaning: 28-31 major version 24-27 minor version 20-23 build 19 1 if it is a release, 0 if it is not. 0-18 revision number; 0 when the revision is unknown.

Returns:
  The version in newgrf format.

Definition at line 70 of file ai_controller.cpp.

void AIController::SetCommandDelay | ( | int | ticks | ) | `[static]`

Change the minimum amount of time the AI should be put in suspend mode when you execute a command.

Normally in SP this is 1, and in MP it is what ever delay the server has been programmed to delay commands (normally between 1 and 5). To give a more 'real' effect to your AI, you can control that number here.

Parameters:

  | ticks | The minimum amount of ticks to wait.

Precondition:
  Ticks should be positive. Too big values will influence performance of the AI.

Note:
  If the number is lower then the MP setting, the MP setting wins.

Definition at line 18 of file ai_controller.cpp.

References AIObject::SetDoCommandDelay().

void AIController::Sleep | ( | int | ticks | ) | `[static]`

Sleep for X ticks.

The code continues after this line when the X AI ticks are passed. Mind that an AI tick is different from in-game ticks and differ per AI speed.

Parameters:

  | ticks | the ticks to wait

Precondition:
  ticks > 0.

Postcondition:
  the value of GetTick() will be changed exactly 'ticks' in value after calling this.

Definition at line 24 of file ai_controller.cpp.

References AILog::Error(), AIObject::GetAllowDoCommand(), and AILog::Warning().

void AIController::Print | ( | bool | error_msg,
|  | const char * | message |
| ) |  |  | `[static]`

When Squirrel triggers a print, this function is called.

Squirrel calls this when 'print' is used, or when the script made an error.

Parameters:

  | error_msg | If true, it is a Squirrel error message.
  | message | The message Squirrel logged.

Note:
  Use AILog.Info/Warning/Error instead of 'print'.

Definition at line 39 of file ai_controller.cpp.

References AILog::Log(), AILog::LOG_SQ_ERROR, and AILog::LOG_SQ_INFO.

bool AIController::LoadedLibrary | ( | const char * | library_name,
|  | int * | next_number,
|  | char * | fake_class_name,
|  | int | fake_class_name_len |
| ) |  |  | `[private]`

Check if a library is already loaded.

If found, fake_class_name is filled with the fake class name as given via AddLoadedLibrary. If not found, next_number is set to the next number available for the fake namespace.

Parameters:

  | library_name | The library to check if already loaded.
  | next_number | The next available number for a library if not already loaded.
  | fake_class_name | The name the library has if already loaded.
  | fake_class_name_len | The maximum length of fake_class_name.

Returns:
  True if the library is already loaded.

Definition at line 75 of file ai_controller.cpp.

References ttd_strlcpy().

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

- src/ai/api/ai_controller.hpp
- src/ai/api/ai_controller.cpp