OpenTTD Source  12.0-beta2
ScriptInstance Class Referenceabstract

Runtime information about a script like a pointer to the squirrel vm and the current state. More...

#include <script_instance.hpp>

Inheritance diagram for ScriptInstance:
AIInstance GameInstance

Public Member Functions

 ScriptInstance (const char *APIName)
 Create a new script.
 
void Initialize (const char *main_script, const char *instance_name, CompanyID company)
 Initialize the script and prepare it for its first run. More...
 
virtual int GetSetting (const char *name)=0
 Get the value of a setting of the current instance. More...
 
virtual class ScriptInfoFindLibrary (const char *library, int version)=0
 Find a library. More...
 
void Continue ()
 A script in multiplayer waits for the server to handle its DoCommand. More...
 
void GameLoop ()
 Run the GameLoop of a script.
 
void CollectGarbage ()
 Let the VM collect any garbage.
 
class ScriptStorageGetStorage ()
 Get the storage of this script.
 
void * GetLogPointer ()
 Get the log pointer of this script.
 
class ScriptController * GetController ()
 Get the controller attached to the instance.
 
bool IsDead () const
 Return the "this script died" value.
 
void Save ()
 Call the script Save function and save all data in the savegame.
 
void Load (int version)
 Load data from a savegame and store it on the stack. More...
 
void Pause ()
 Suspends the script for the current tick and then pause the execution of script. More...
 
bool IsPaused ()
 Checks if the script is paused. More...
 
void Unpause ()
 Resume execution of the script. More...
 
SQInteger GetOpsTillSuspend ()
 Get the number of operations the script can execute before being suspended. More...
 
bool DoCommandCallback (const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
 DoCommand callback function for all commands executed by scripts. More...
 
void InsertEvent (class ScriptEvent *event)
 Insert an event for this script. More...
 
bool IsSleeping ()
 Check if the instance is sleeping, which either happened because the script executed a DoCommand, executed this.Sleep() or it has been paused.
 
size_t GetAllocatedMemory () const
 
bool InShutdown () const
 Indicate whether this instance is currently being destroyed.
 
void ReleaseSQObject (HSQOBJECT *obj)
 Decrease the ref count of a squirrel object. More...
 

Static Public Member Functions

static void DoCommandReturn (ScriptInstance *instance)
 Return a true/false reply for a DoCommand.
 
static void DoCommandReturnVehicleID (ScriptInstance *instance)
 Return a VehicleID reply for a DoCommand.
 
static void DoCommandReturnSignID (ScriptInstance *instance)
 Return a SignID reply for a DoCommand.
 
static void DoCommandReturnGroupID (ScriptInstance *instance)
 Return a GroupID reply for a DoCommand.
 
static void DoCommandReturnGoalID (ScriptInstance *instance)
 Return a GoalID reply for a DoCommand.
 
static void DoCommandReturnStoryPageID (ScriptInstance *instance)
 Return a StoryPageID reply for a DoCommand.
 
static void DoCommandReturnStoryPageElementID (ScriptInstance *instance)
 Return a StoryPageElementID reply for a DoCommand.
 
static void SaveEmpty ()
 Don't save any data in the savegame.
 
static void LoadEmpty ()
 Load and discard data from a savegame.
 

Protected Member Functions

virtual void RegisterAPI ()
 Register all API functions to the VM.
 
bool LoadCompatibilityScripts (const char *api_version, Subdirectory dir)
 Load squirrel scripts to emulate an older API. More...
 
virtual void Died ()
 Tell the script it died.
 
virtual CommandCallbackGetDoCommandCallback ()=0
 Get the callback handling DoCommands in case of networking.
 
virtual void LoadDummyScript ()=0
 Load the dummy script.
 

Protected Attributes

class Squirrelengine
 A wrapper around the squirrel vm.
 
const char * versionAPI
 Current API used by this script.
 

Private Member Functions

bool CallLoad ()
 Call the script Load function if it exists and data was loaded from a savegame.
 

Static Private Member Functions

static bool SaveObject (HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
 Save one object (int / string / array / table) to the savegame. More...
 
static bool LoadObjects (HSQUIRRELVM vm)
 Load all objects from a savegame. More...
 

Private Attributes

class ScriptController * controller
 The script main class.
 
class ScriptStoragestorage
 Some global information for each running script.
 
SQObject * instance
 Squirrel-pointer to the script main class.
 
bool is_started
 Is the scripts constructor executed?
 
bool is_dead
 True if the script has been stopped.
 
bool is_save_data_on_stack
 Is the save data still on the squirrel stack?
 
int suspend
 The amount of ticks to suspend this script before it's allowed to continue.
 
bool is_paused
 Is the script paused? (a paused script will not be executed until unpaused)
 
bool in_shutdown
 Is this instance currently being destructed?
 
Script_SuspendCallbackProccallback
 Callback that should be called in the next tick the script runs.
 
size_t last_allocated_memory
 Last known allocated memory value (for display for crashed scripts)
 

Friends

class ScriptObject
 
class ScriptController
 

Detailed Description

Runtime information about a script like a pointer to the squirrel vm and the current state.

Definition at line 23 of file script_instance.hpp.

Member Function Documentation

◆ Continue()

void ScriptInstance::Continue ( )

A script in multiplayer waits for the server to handle its DoCommand.

It keeps waiting for this until this function is called.

Definition at line 147 of file script_instance.cpp.

References suspend.

Referenced by CcAI(), and CcGame().

◆ DoCommandCallback()

bool ScriptInstance::DoCommandCallback ( const CommandCost result,
TileIndex  tile,
uint32  p1,
uint32  p2,
uint32  cmd 
)

DoCommand callback function for all commands executed by scripts.

Parameters
resultThe result of the command.
tileThe tile on which the command was executed.
p1p1 as given to DoCommandPInternal.
p2p2 as given to DoCommandPInternal.
cmdcmd as given to DoCommandPInternal.
Returns
true if we handled result.

Definition at line 690 of file script_instance.cpp.

References CMD_END, Debug, CommandCost::Failed(), CommandCost::GetCost(), CommandCost::GetErrorMessage(), INVALID_TILE, and CommandCost::Succeeded().

Referenced by CcAI().

◆ FindLibrary()

virtual class ScriptInfo* ScriptInstance::FindLibrary ( const char *  library,
int  version 
)
pure virtual

Find a library.

Parameters
libraryThe library name to find.
versionThe version the library should have.
Returns
The library if found, nullptr otherwise.

Implemented in AIInstance, and GameInstance.

◆ GetOpsTillSuspend()

SQInteger ScriptInstance::GetOpsTillSuspend ( )

Get the number of operations the script can execute before being suspended.

This function is safe to call from within a function called by the script.

Returns
The number of operations to execute.

Definition at line 685 of file script_instance.cpp.

References engine, and Squirrel::GetOpsTillSuspend().

◆ GetSetting()

virtual int ScriptInstance::GetSetting ( const char *  name)
pure virtual

Get the value of a setting of the current instance.

Parameters
nameThe name of the setting.
Returns
the value for the setting, or -1 if the setting is not known.

Implemented in AIInstance, and GameInstance.

◆ Initialize()

void ScriptInstance::Initialize ( const char *  main_script,
const char *  instance_name,
CompanyID  company 
)

Initialize the script and prepare it for its first run.

Parameters
main_scriptThe full path of the script to load.
instance_nameThe name of the instance out of the script to load.
companyWhich company this script is serving.

Definition at line 69 of file script_instance.cpp.

References controller, Squirrel::CreateClassInstance(), Died(), engine, Script_FatalError::GetErrorMessage(), instance, is_dead, Squirrel::IsSuspended(), LoadDummyScript(), Squirrel::LoadScript(), ScriptScanner::main_script, RegisterAPI(), Squirrel::ResumeError(), Squirrel::SetGlobalPointer(), and Squirrel::ThrowError().

Referenced by AIInstance::Initialize(), and GameInstance::Initialize().

◆ InsertEvent()

void ScriptInstance::InsertEvent ( class ScriptEvent *  event)

Insert an event for this script.

Parameters
eventThe event to insert.

Definition at line 713 of file script_instance.cpp.

◆ IsPaused()

bool ScriptInstance::IsPaused ( )

Checks if the script is paused.

Returns
true if the script is paused, otherwise false

Definition at line 557 of file script_instance.cpp.

References is_paused.

Referenced by Game::IsPaused(), and AIDebugWindow::OnPaint().

◆ Load()

void ScriptInstance::Load ( int  version)

Load data from a savegame and store it on the stack.

Parameters
versionThe version of the script when saving, or -1 if this was not the original script saving the game.

Definition at line 629 of file script_instance.cpp.

References _script_sl_byte, engine, Squirrel::GetVM(), is_save_data_on_stack, LoadEmpty(), LoadObjects(), and SlObject().

◆ LoadCompatibilityScripts()

bool ScriptInstance::LoadCompatibilityScripts ( const char *  api_version,
Subdirectory  dir 
)
protected

Load squirrel scripts to emulate an older API.

Parameters
api_versionAPI version to load scripts for
dirSubdirectory to find the scripts in
Returns
true iff script loading should proceed

Definition at line 115 of file script_instance.cpp.

References lastof, and seprintf().

Referenced by AIInstance::RegisterAPI(), and GameInstance::RegisterAPI().

◆ LoadObjects()

bool ScriptInstance::LoadObjects ( HSQUIRRELVM  vm)
staticprivate

Load all objects from a savegame.

Returns
True if the loading was successful.

Definition at line 562 of file script_instance.cpp.

References _script_byte, _script_sl_byte, IsSavegameVersionBefore(), SlCopy(), SlObject(), SLV_SCRIPT_INT64, and SQSL_INT.

Referenced by Load(), and LoadEmpty().

◆ Pause()

void ScriptInstance::Pause ( )

Suspends the script for the current tick and then pause the execution of script.

The script will not be resumed from its suspended state until the script has been unpaused.

Definition at line 543 of file script_instance.cpp.

References _settings_game, Squirrel::DecreaseOps(), engine, Squirrel::GetVM(), is_paused, GameSettings::script, and ScriptSettings::script_max_opcode_till_suspend.

Referenced by Game::Pause().

◆ ReleaseSQObject()

void ScriptInstance::ReleaseSQObject ( HSQOBJECT *  obj)

Decrease the ref count of a squirrel object.

Parameters
objThe object to release.

Definition at line 726 of file script_instance.cpp.

References engine, in_shutdown, and Squirrel::ReleaseObject().

◆ SaveObject()

bool ScriptInstance::SaveObject ( HSQUIRRELVM  vm,
SQInteger  index,
int  max_depth,
bool  test 
)
staticprivate

Save one object (int / string / array / table) to the savegame.

Parameters
vmThe virtual machine to get all the data from.
indexThe index on the squirrel stack of the element to save.
max_depthThe maximum depth recursive arrays / tables will be stored with before an error is returned.
testIf true, don't really store the data but only check if it is valid.
Returns
True if the saving was successful.

Definition at line 351 of file script_instance.cpp.

References _script_byte, _script_sl_byte, SlObject(), and SQSL_INT.

Referenced by Save().

◆ Unpause()

void ScriptInstance::Unpause ( )

Resume execution of the script.

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.

Definition at line 552 of file script_instance.cpp.

References is_paused.

Referenced by Game::Unpause().


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