Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends

ScriptController Class Reference

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

#include <script_controller.hpp>

Public Member Functions

 ScriptController (CompanyID company)
 Initializer of the ScriptController.
 ~ScriptController ()
 Destructor of the ScriptController.
void Start ()
 This function is called to start your AI.

Static Public Member Functions

static uint GetTick ()
 Find at which tick your AI currently is.
static int GetOpsTillSuspend ()
 Get the number of operations the AI may still execute this tick.
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.
static HSQOBJECT Import (const char *library, const char *class_name, int version)
 Import a library.

Private Types

typedef std::map< const char
*, const char *, StringCompare
LoadedLibraryList
 The type for loaded libraries.

Private Member Functions

void RegisterClasses ()
 Register all classes that are known inside the NoAI API.

Private Attributes

uint ticks
 The amount of ticks we're sleeping.
LoadedLibraryList loaded_library
 The libraries we loaded.
int loaded_library_count
 The amount of libraries.

Friends

class AIScanner
class ScriptInstance

Detailed Description

The Controller, the class each Script should extend.

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

Definition at line 25 of file script_controller.hpp.


Constructor & Destructor Documentation

ScriptController::ScriptController ( CompanyID  company  ) 

Initializer of the ScriptController.

Parameters:
company The company this Script is normally serving.

Definition at line 49 of file script_controller.cpp.

References ScriptObject::SetCompany().


Member Function Documentation

int ScriptController::GetOpsTillSuspend (  )  [static]

Get the number of operations the AI may still execute this tick.

Returns:
The amount of operations left to execute.
Note:
This number can go negative when certain uninteruptable operations are executed. The amount of operations that you go over the limit will be deducted from the next tick you would be allowed to run.

Definition at line 71 of file script_controller.cpp.

References ScriptObject::GetActiveInstance(), and ScriptInstance::GetOpsTillSuspend().

int ScriptController::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 76 of file script_controller.cpp.

References ScriptObject::GetActiveInstance(), and ScriptInstance::GetSetting().

uint ScriptController::GetTick (  )  [static]

Find at which tick your AI currently is.

Returns:
returns the current tick.

Definition at line 66 of file script_controller.cpp.

References ScriptObject::GetActiveInstance(), ScriptInstance::GetController(), and ticks.

uint ScriptController::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 81 of file script_controller.cpp.

HSQOBJECT ScriptController::Import ( const char *  library,
const char *  class_name,
int  version 
) [static]

Import a library.

Parameters:
library The name of the library to import.
class_name Under which name you want it to be available (or "" if you just want the returning object).
version Which version you want specificly.
Returns:
The loaded library object. If class_name is set, it is also available (under the scope of the import) under that name.
Note:
This command can be called from the global space, and does not need an instance.

Definition at line 86 of file script_controller.cpp.

References ScriptInstance::engine, error(), ScriptInstance::FindLibrary(), ScriptObject::GetActiveInstance(), ScriptInstance::GetController(), ScriptInfo::GetInstanceName(), ScriptInfo::GetMainScript(), Squirrel::GetVM(), loaded_library, loaded_library_count, Squirrel::LoadScript(), StrEmpty(), strtolower(), and ttd_strlcpy().

void ScriptController::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 ScriptLog.Info/Warning/Error instead of 'print'.

Definition at line 44 of file script_controller.cpp.

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

Referenced by PrintFunc().

void ScriptController::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 than the MP setting, the MP setting wins.

Definition at line 24 of file script_controller.cpp.

References ScriptObject::SetDoCommandDelay().

void ScriptController::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 30 of file script_controller.cpp.

References ScriptObject::CanSuspend(), and ScriptLog::Warning().

void ScriptController::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.

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