OpenTTD

script_instance.hpp
Go to the documentation of this file.
1 /* $Id: script_instance.hpp 25342 2013-06-09 12:19:09Z zuu $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef SCRIPT_INSTANCE_HPP
13 #define SCRIPT_INSTANCE_HPP
14
15 #include <squirrel.h>
16 #include "script_suspend.hpp"
17
18 #include "../command_type.h"
19 #include "../company_type.h"
20 #include "../fileio_type.h"
21
22 static const uint SQUIRREL_MAX_DEPTH = 25;
23
25 class ScriptInstance {
26 public:
27 friend class ScriptObject;
28 friend class ScriptController;
29
33 ScriptInstance(const char *APIName);
34 virtual ~ScriptInstance();
35
42 void Initialize(const char *main_script, const char *instance_name, CompanyID company);
43
49 virtual int GetSetting(const char *name) = 0;
50
57 virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0;
58
63 void Continue();
64
68 void GameLoop();
69
73 void CollectGarbage() const;
74
78 class ScriptStorage *GetStorage();
79
83 void *GetLogPointer();
84
88 static void DoCommandReturn(ScriptInstance *instance);
89
93 static void DoCommandReturnVehicleID(ScriptInstance *instance);
94
98 static void DoCommandReturnSignID(ScriptInstance *instance);
99
103 static void DoCommandReturnGroupID(ScriptInstance *instance);
104
108 static void DoCommandReturnGoalID(ScriptInstance *instance);
109
113 static void DoCommandReturnStoryPageID(ScriptInstance *instance);
114
118 static void DoCommandReturnStoryPageElementID(ScriptInstance *instance);
119
123 class ScriptController *GetController() { return controller; }
124
128 inline bool IsDead() const { return this->is_dead; }
129
133 void Save();
134
138 static void SaveEmpty();
139
145 void Load(int version);
146
150 static void LoadEmpty();
151
157 void Pause();
158
163 bool IsPaused();
164
170 void Unpause();
171
177 SQInteger GetOpsTillSuspend();
178
186 void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2);
187
192 void InsertEvent(class ScriptEvent *event);
193
199 bool IsSleeping() { return this->suspend != 0; }
200
201 protected:
202 class Squirrel *engine;
203 const char *versionAPI;
204
208 virtual void RegisterAPI();
209
216 bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
217
221 virtual void Died();
222
226 virtual CommandCallback *GetDoCommandCallback() = 0;
227
231 virtual void LoadDummyScript() = 0;
232
233 private:
234 class ScriptController *controller;
235 class ScriptStorage *storage;
236 SQObject *instance;
237
238 bool is_started;
239 bool is_dead;
240 bool is_save_data_on_stack;
241 int suspend;
242 bool is_paused;
243 Script_SuspendCallbackProc *callback;
244
249 bool CallLoad();
250
261 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
262
267 static bool LoadObjects(HSQUIRRELVM vm);
268 };
269
270 #endif /* SCRIPT_INSTANCE_HPP */
ScriptInstance::DoCommandReturnStoryPageID
static void DoCommandReturnStoryPageID(ScriptInstance *instance)
Return a StoryPageID reply for a DoCommand.
Definition: script_instance.cpp:281
SQUIRREL_MAX_DEPTH
static const uint SQUIRREL_MAX_DEPTH
The maximum recursive depth for items stored in the savegame.
Definition: script_instance.hpp:22
ScriptInstance::is_dead
bool is_dead
True if the script has been stopped.
Definition: script_instance.hpp:239
ScriptInstance::is_started
bool is_started
Is the scripts constructor executed?
Definition: script_instance.hpp:238
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:165
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
ScriptInstance::InsertEvent
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
Definition: script_instance.cpp:695
ScriptInstance::GetStorage
class ScriptStorage * GetStorage()
Get the storage of this script.
Definition: script_instance.cpp:291
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:240
ScriptInstance::DoCommandReturnVehicleID
static void DoCommandReturnVehicleID(ScriptInstance *instance)
Return a VehicleID reply for a DoCommand.
Definition: script_instance.cpp:261
ScriptInstance::is_paused
bool is_paused
Is the script paused? (a paused script will not be executed until unpaused)
Definition: script_instance.hpp:242
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle his DoCommand.
Definition: script_instance.cpp:145
ScriptInstance::GetDoCommandCallback
virtual CommandCallback * GetDoCommandCallback()=0
Get the callback handling DoCommands in case of networking.
CommandCost
Common return value for all commands.
Definition: command_type.h:25
ScriptInstance::FindLibrary
virtual class ScriptInfo * FindLibrary(const char *library, int version)=0
Find a library.
ScriptInstance::DoCommandReturnStoryPageElementID
static void DoCommandReturnStoryPageElementID(ScriptInstance *instance)
Return a StoryPageElementID reply for a DoCommand.
Definition: script_instance.cpp:286
ScriptInstance::suspend
int suspend
The amount of ticks to suspend this script before it&#39;s allowed to continue.
Definition: script_instance.hpp:241
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::GetLogPointer
void * GetLogPointer()
Get the log pointer of this script.
Definition: script_instance.cpp:296
ScriptInstance::Save
void Save()
Call the script Save function and save all data in the savegame.
Definition: script_instance.cpp:465
ScriptInstance::Unpause
void Unpause()
Resume execution of the script.
Definition: script_instance.cpp:544
ScriptInstance::GetOpsTillSuspend
SQInteger GetOpsTillSuspend()
Get the number of operations the script can execute before being suspended.
Definition: script_instance.cpp:676
ScriptInstance::GetController
class ScriptController * GetController()
Get the controller attached to the instance.
Definition: script_instance.hpp:123
ScriptInstance::DoCommandReturnGroupID
static void DoCommandReturnGroupID(ScriptInstance *instance)
Return a GroupID reply for a DoCommand.
Definition: script_instance.cpp:271
ScriptInstance::ScriptInstance
ScriptInstance(const char *APIName)
Create a new script.
Definition: script_instance.cpp:52
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:32
ScriptInstance::controller
class ScriptController * controller
The script main class.
Definition: script_instance.hpp:234
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state...
Definition: script_instance.hpp:25
ScriptInstance::GetSetting
virtual int GetSetting(const char *name)=0
Get the value of a setting of the current instance.
ScriptInstance::IsPaused
bool IsPaused()
Checks if the script is paused.
Definition: script_instance.cpp:549
ScriptInstance::DoCommandReturnSignID
static void DoCommandReturnSignID(ScriptInstance *instance)
Return a SignID reply for a DoCommand.
Definition: script_instance.cpp:266
ScriptInstance::storage
class ScriptStorage * storage
Some global information for each running script.
Definition: script_instance.hpp:235
ScriptInstance::IsDead
bool IsDead() const
Return the "this script died" value.
Definition: script_instance.hpp:128
ScriptInstance::Pause
void Pause()
Suspends the script for the current tick and then pause the execution of script.
Definition: script_instance.cpp:535
ScriptInstance::callback
Script_SuspendCallbackProc * callback
Callback that should be called in the next tick the script runs.
Definition: script_instance.hpp:243
ScriptInstance::LoadDummyScript
virtual void LoadDummyScript()=0
Load the dummy script.
ScriptInstance::CallLoad
bool CallLoad()
Call the script Load function if it exists and data was loaded from a savegame.
Definition: script_instance.cpp:639
ScriptInstance::SaveEmpty
static void SaveEmpty()
Don&#39;t save any data in the savegame.
Definition: script_instance.cpp:459
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
ScriptInstance::DoCommandReturnGoalID
static void DoCommandReturnGoalID(ScriptInstance *instance)
Return a GoalID reply for a DoCommand.
Definition: script_instance.cpp:276
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:112
ScriptInstance::DoCommandCallback
void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
DoCommand callback function for all commands executed by scripts.
Definition: script_instance.cpp:681
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:343
Script_SuspendCallbackProc
void() Script_SuspendCallbackProc(class ScriptInstance *instance)
The callback function when a script suspends.
Definition: script_suspend.hpp:18
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:106
ScriptInstance::instance
SQObject * instance
Squirrel-pointer to the script main class.
Definition: script_instance.hpp:236
ScriptInstance::LoadObjects
static bool LoadObjects(HSQUIRRELVM vm)
Load all objects from a savegame.
Definition: script_instance.cpp:554
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:70
ScriptInstance::DoCommandReturn
static void DoCommandReturn(ScriptInstance *instance)
Return a true/false reply for a DoCommand.
Definition: script_instance.cpp:256
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:467
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:151
Squirrel
Definition: squirrel.hpp:23
script_suspend.hpp
The Script_Suspend tracks the suspension of a script.
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:20
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:611
ScriptInstance::Load
void Load(int version)
Load data from a savegame and store it on the stack.
Definition: script_instance.cpp:620
ScriptInstance::versionAPI
const char * versionAPI
Current API used by this script.
Definition: script_instance.hpp:203
ScriptStorage
The storage for each script.
Definition: script_storage.hpp:33
ScriptInstance::CollectGarbage
void CollectGarbage() const
Let the VM collect any garbage.
Definition: script_instance.cpp:251
ScriptInstance::GameLoop
void GameLoop()
Run the GameLoop of a script.
Definition: script_instance.cpp:162
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:202