OpenTTD Source: src/script/script_instance.hpp Source File

OpenTTD Source 12.0-beta1

script_instance.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef SCRIPT_INSTANCE_HPP
11 #define SCRIPT_INSTANCE_HPP
12
13 #include <squirrel.h>
14 #include "script_suspend.hpp"
15
16 #include "../command_type.h"
17 #include "../company_type.h"
18 #include "../fileio_type.h"
19
20 static const uint SQUIRREL_MAX_DEPTH = 25;
21
23 class ScriptInstance {
24 public:
25 friend class ScriptObject;
26 friend class ScriptController;
27
31 ScriptInstance(const char *APIName);
32 virtual ~ScriptInstance();
33
40 void Initialize(const char *main_script, const char *instance_name, CompanyID company);
41
47 virtual int GetSetting(const char *name) = 0;
48
55 virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0;
56
61 void Continue();
62
66 void GameLoop();
67
71 void CollectGarbage();
72
76 class ScriptStorage *GetStorage();
77
81 void *GetLogPointer();
82
86 static void DoCommandReturn(ScriptInstance *instance);
87
91 static void DoCommandReturnVehicleID(ScriptInstance *instance);
92
96 static void DoCommandReturnSignID(ScriptInstance *instance);
97
101 static void DoCommandReturnGroupID(ScriptInstance *instance);
102
106 static void DoCommandReturnGoalID(ScriptInstance *instance);
107
111 static void DoCommandReturnStoryPageID(ScriptInstance *instance);
112
116 static void DoCommandReturnStoryPageElementID(ScriptInstance *instance);
117
121 class ScriptController *GetController() { return controller; }
122
126 inline bool IsDead() const { return this->is_dead; }
127
131 void Save();
132
136 static void SaveEmpty();
137
143 void Load(int version);
144
148 static void LoadEmpty();
149
155 void Pause();
156
161 bool IsPaused();
162
168 void Unpause();
169
175 SQInteger GetOpsTillSuspend();
176
186 bool DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd);
187
192 void InsertEvent(class ScriptEvent *event);
193
199 bool IsSleeping() { return this->suspend != 0; }
200
201 size_t GetAllocatedMemory() const;
202
206 inline bool InShutdown() const { return this->in_shutdown; }
207
212 void ReleaseSQObject(HSQOBJECT *obj);
213
214 protected:
215 class Squirrel *engine;
216 const char *versionAPI;
217
221 virtual void RegisterAPI();
222
229 bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
230
234 virtual void Died();
235
239 virtual CommandCallback *GetDoCommandCallback() = 0;
240
244 virtual void LoadDummyScript() = 0;
245
246 private:
247 class ScriptController *controller;
248 class ScriptStorage *storage;
249 SQObject *instance;
250
251 bool is_started;
252 bool is_dead;
253 bool is_save_data_on_stack;
254 int suspend;
255 bool is_paused;
256 bool in_shutdown;
257 Script_SuspendCallbackProc *callback;
258 size_t last_allocated_memory;
259
264 bool CallLoad();
265
276 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
277
282 static bool LoadObjects(HSQUIRRELVM vm);
283 };
284
285 #endif /* SCRIPT_INSTANCE_HPP */
ScriptInstance::LoadDummyScript
virtual void LoadDummyScript()=0
Load the dummy script.
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
ScriptInstance::SaveObject
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
Save one object (int / string / array / table) to the savegame.
Definition: script_instance.cpp:351
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:215
ScriptInstance::last_allocated_memory
size_t last_allocated_memory
Last known allocated memory value (for display for crashed scripts)
Definition: script_instance.hpp:258
ScriptStorage
The storage for each script.
Definition: script_storage.hpp:31
script_suspend.hpp
ScriptInstance::CallLoad
bool CallLoad()
Call the script Load function if it exists and data was loaded from a savegame.
Definition: script_instance.cpp:648
ScriptInstance::is_dead
bool is_dead
True if the script has been stopped.
Definition: script_instance.hpp:252
ScriptInstance::InsertEvent
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
Definition: script_instance.cpp:713
ScriptInstance::GameLoop
void GameLoop()
Run the GameLoop of a script.
Definition: script_instance.cpp:168
ScriptInstance::DoCommandCallback
bool DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by scripts.
Definition: script_instance.cpp:690
ScriptInstance::controller
class ScriptController * controller
The script main class.
Definition: script_instance.hpp:247
ScriptInstance::Save
void Save()
Call the script Save function and save all data in the savegame.
Definition: script_instance.cpp:473
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition: script_instance.hpp:23
ScriptInstance::ScriptInstance
ScriptInstance(const char *APIName)
Create a new script.
Definition: script_instance.cpp:50
ScriptInstance::Unpause
void Unpause()
Resume execution of the script.
Definition: script_instance.cpp:552
ScriptInstance::DoCommandReturnStoryPageElementID
static void DoCommandReturnStoryPageElementID(ScriptInstance *instance)
Return a StoryPageElementID reply for a DoCommand.
Definition: script_instance.cpp:295
ScriptInstance::LoadObjects
static bool LoadObjects(HSQUIRRELVM vm)
Load all objects from a savegame.
Definition: script_instance.cpp:562
ScriptInstance::instance
SQObject * instance
Squirrel-pointer to the script main class.
Definition: script_instance.hpp:249
Script_SuspendCallbackProc
void() Script_SuspendCallbackProc(class ScriptInstance *instance)
The callback function when a script suspends.
Definition: script_suspend.hpp:16
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:474
ScriptInstance::is_save_data_on_stack
bool is_save_data_on_stack
Is the save data still on the squirrel stack?
Definition: script_instance.hpp:253
ScriptInstance::is_paused
bool is_paused
Is the script paused? (a paused script will not be executed until unpaused)
Definition: script_instance.hpp:255
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ScriptInstance::GetDoCommandCallback
virtual CommandCallback * GetDoCommandCallback()=0
Get the callback handling DoCommands in case of networking.
ScriptInstance::GetOpsTillSuspend
SQInteger GetOpsTillSuspend()
Get the number of operations the script can execute before being suspended.
Definition: script_instance.cpp:685
ScriptInstance::Pause
void Pause()
Suspends the script for the current tick and then pause the execution of script.
Definition: script_instance.cpp:543
ScriptInstance::DoCommandReturnGoalID
static void DoCommandReturnGoalID(ScriptInstance *instance)
Return a GoalID reply for a DoCommand.
Definition: script_instance.cpp:285
ScriptInstance::ReleaseSQObject
void ReleaseSQObject(HSQOBJECT *obj)
Decrease the ref count of a squirrel object.
Definition: script_instance.cpp:726
ScriptInstance::storage
class ScriptStorage * storage
Some global information for each running script.
Definition: script_instance.hpp:248
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:161
Squirrel
Definition: squirrel.hpp:23
ScriptInstance::DoCommandReturnSignID
static void DoCommandReturnSignID(ScriptInstance *instance)
Return a SignID reply for a DoCommand.
Definition: script_instance.cpp:275
ScriptInstance::in_shutdown
bool in_shutdown
Is this instance currently being destructed?
Definition: script_instance.hpp:256
CommandCost
Common return value for all commands.
Definition: command_type.h:23
ScriptInstance::InShutdown
bool InShutdown() const
Indicate whether this instance is currently being destroyed.
Definition: script_instance.hpp:206
ScriptInstance::DoCommandReturn
static void DoCommandReturn(ScriptInstance *instance)
Return a true/false reply for a DoCommand.
Definition: script_instance.cpp:265
ScriptInstance::SaveEmpty
static void SaveEmpty()
Don't save any data in the savegame.
Definition: script_instance.cpp:467
ScriptInstance::IsDead
bool IsDead() const
Return the "this script died" value.
Definition: script_instance.hpp:126
ScriptInstance::is_started
bool is_started
Is the scripts constructor executed?
Definition: script_instance.hpp:251
ScriptInstance::DoCommandReturnStoryPageID
static void DoCommandReturnStoryPageID(ScriptInstance *instance)
Return a StoryPageID reply for a DoCommand.
Definition: script_instance.cpp:290
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:109
ScriptInstance::Initialize
void Initialize(const char *main_script, const char *instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition: script_instance.cpp:69
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:620
ScriptInstance::DoCommandReturnGroupID
static void DoCommandReturnGroupID(ScriptInstance *instance)
Return a GroupID reply for a DoCommand.
Definition: script_instance.cpp:280
ScriptInstance::IsSleeping
bool IsSleeping()
Check if the instance is sleeping, which either happened because the script executed a DoCommand,...
Definition: script_instance.hpp:199
ScriptInstance::callback
Script_SuspendCallbackProc * callback
Callback that should be called in the next tick the script runs.
Definition: script_instance.hpp:257
ScriptInstance::GetSetting
virtual int GetSetting(const char *name)=0
Get the value of a setting of the current instance.
ScriptInstance::DoCommandReturnVehicleID
static void DoCommandReturnVehicleID(ScriptInstance *instance)
Return a VehicleID reply for a DoCommand.
Definition: script_instance.cpp:270
ScriptInstance::GetStorage
class ScriptStorage * GetStorage()
Get the storage of this script.
Definition: script_instance.cpp:300
ScriptInstance::GetController
class ScriptController * GetController()
Get the controller attached to the instance.
Definition: script_instance.hpp:121
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:153
ScriptInstance::suspend
int suspend
The amount of ticks to suspend this script before it's allowed to continue.
Definition: script_instance.hpp:254
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition: script_instance.cpp:147
ScriptInstance::IsPaused
bool IsPaused()
Checks if the script is paused.
Definition: script_instance.cpp:557
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:115
ScriptInstance::CollectGarbage
void CollectGarbage()
Let the VM collect any garbage.
Definition: script_instance.cpp:257
ScriptInstance::Load
void Load(int version)
Load data from a savegame and store it on the stack.
Definition: script_instance.cpp:629
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
ScriptInstance::versionAPI
const char * versionAPI
Current API used by this script.
Definition: script_instance.hpp:216
ScriptInstance::FindLibrary
virtual class ScriptInfo * FindLibrary(const char *library, int version)=0
Find a library.
ScriptInstance::GetLogPointer
void * GetLogPointer()
Get the log pointer of this script.
Definition: script_instance.cpp:305
SQUIRREL_MAX_DEPTH
static const uint SQUIRREL_MAX_DEPTH
The maximum recursive depth for items stored in the savegame.
Definition: script_instance.hpp:20