OpenTTD
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 
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() const;
72 
76  class ScriptStorage *GetStorage();
77 
81  void *GetLogPointer();
82 
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 
203 protected:
204  class Squirrel *engine;
205  const char *versionAPI;
206 
210  virtual void RegisterAPI();
211 
218  bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
219 
223  virtual void Died();
224 
228  virtual CommandCallback *GetDoCommandCallback() = 0;
229 
233  virtual void LoadDummyScript() = 0;
234 
235 private:
236  class ScriptController *controller;
238  SQObject *instance;
239 
240  bool is_started;
241  bool is_dead;
243  int suspend;
244  bool is_paused;
247 
252  bool CallLoad();
253 
264  static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
265 
270  static bool LoadObjects(HSQUIRRELVM vm);
271 };
272 
273 #endif /* SCRIPT_INSTANCE_HPP */
static void DoCommandReturnStoryPageID(ScriptInstance *instance)
Return a StoryPageID reply for a DoCommand.
static const uint SQUIRREL_MAX_DEPTH
The maximum recursive depth for items stored in the savegame.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
bool is_dead
True if the script has been stopped.
size_t last_allocated_memory
Last known allocated memory value (for display for crashed scripts)
bool is_started
Is the scripts constructor executed?
int version
Version of the script.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
class ScriptStorage * GetStorage()
Get the storage of this script.
bool is_save_data_on_stack
Is the save data still on the squirrel stack?
static void DoCommandReturnVehicleID(ScriptInstance *instance)
Return a VehicleID reply for a DoCommand.
bool is_paused
Is the script paused? (a paused script will not be executed until unpaused)
void Continue()
A script in multiplayer waits for the server to handle his DoCommand.
virtual CommandCallback * GetDoCommandCallback()=0
Get the callback handling DoCommands in case of networking.
Common return value for all commands.
Definition: command_type.h:23
virtual class ScriptInfo * FindLibrary(const char *library, int version)=0
Find a library.
static void DoCommandReturnStoryPageElementID(ScriptInstance *instance)
Return a StoryPageElementID reply for a DoCommand.
int suspend
The amount of ticks to suspend this script before it&#39;s allowed to continue.
bool IsSleeping()
Check if the instance is sleeping, which either happened because the script executed a DoCommand...
void * GetLogPointer()
Get the log pointer of this script.
void Save()
Call the script Save function and save all data in the savegame.
void Unpause()
Resume execution of the script.
SQInteger GetOpsTillSuspend()
Get the number of operations the script can execute before being suspended.
class ScriptController * GetController()
Get the controller attached to the instance.
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:470
static void DoCommandReturnGroupID(ScriptInstance *instance)
Return a GroupID reply for a DoCommand.
ScriptInstance(const char *APIName)
Create a new script.
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
class ScriptController * controller
The script main class.
bool DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by scripts.
Runtime information about a script like a pointer to the squirrel vm and the current state...
virtual int GetSetting(const char *name)=0
Get the value of a setting of the current instance.
bool IsPaused()
Checks if the script is paused.
static void DoCommandReturnSignID(ScriptInstance *instance)
Return a SignID reply for a DoCommand.
class ScriptStorage * storage
Some global information for each running script.
bool IsDead() const
Return the "this script died" value.
void Pause()
Suspends the script for the current tick and then pause the execution of script.
Script_SuspendCallbackProc * callback
Callback that should be called in the next tick the script runs.
virtual void LoadDummyScript()=0
Load the dummy script.
bool CallLoad()
Call the script Load function if it exists and data was loaded from a savegame.
static void SaveEmpty()
Don&#39;t save any data in the savegame.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
static void DoCommandReturnGoalID(ScriptInstance *instance)
Return a GoalID reply for a DoCommand.
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
Save one object (int / string / array / table) to the savegame.
void() Script_SuspendCallbackProc(class ScriptInstance *instance)
The callback function when a script suspends.
virtual void RegisterAPI()
Register all API functions to the VM.
SQObject * instance
Squirrel-pointer to the script main class.
static bool LoadObjects(HSQUIRRELVM vm)
Load all objects from a savegame.
void Initialize(const char *main_script, const char *instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
static void DoCommandReturn(ScriptInstance *instance)
Return a true/false reply for a DoCommand.
virtual void Died()
Tell the script it died.
The Script_Suspend tracks the suspension of a script.
static void LoadEmpty()
Load and discard data from a savegame.
void Load(int version)
Load data from a savegame and store it on the stack.
const char * versionAPI
Current API used by this script.
The storage for each script.
void CollectGarbage() const
Let the VM collect any garbage.
void GameLoop()
Run the GameLoop of a script.
class Squirrel * engine
A wrapper around the squirrel vm.