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 */