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 
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 
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;
236  SQObject *instance;
237 
238  bool is_started;
239  bool is_dead;
241  int suspend;
242  bool is_paused;
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 */
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.
bool is_dead
True if the script has been stopped.
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:110
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:25
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.
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:32
class ScriptController * controller
The script main class.
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:80
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.
void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
DoCommand callback function for all commands executed by scripts.
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.
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
virtual void Died()
Tell the script it died.
The Script_Suspend tracks the suspension of a script.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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.