script_instance.hpp

Go to the documentation of this file.
00001 /* $Id: script_instance.hpp 25342 2013-06-09 12:19:09Z zuu $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef SCRIPT_INSTANCE_HPP
00013 #define SCRIPT_INSTANCE_HPP
00014 
00015 #include <squirrel.h>
00016 #include "script_suspend.hpp"
00017 
00018 #include "../command_type.h"
00019 #include "../company_type.h"
00020 #include "../fileio_type.h"
00021 
00022 static const uint SQUIRREL_MAX_DEPTH = 25; 
00023 
00025 class ScriptInstance {
00026 public:
00027   friend class ScriptObject;
00028   friend class ScriptController;
00029 
00033   ScriptInstance(const char *APIName);
00034   virtual ~ScriptInstance();
00035 
00042   void Initialize(const char *main_script, const char *instance_name, CompanyID company);
00043 
00049   virtual int GetSetting(const char *name) = 0;
00050 
00057   virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0;
00058 
00063   void Continue();
00064 
00068   void GameLoop();
00069 
00073   void CollectGarbage() const;
00074 
00078   class ScriptStorage *GetStorage();
00079 
00083   void *GetLogPointer();
00084 
00088   static void DoCommandReturn(ScriptInstance *instance);
00089 
00093   static void DoCommandReturnVehicleID(ScriptInstance *instance);
00094 
00098   static void DoCommandReturnSignID(ScriptInstance *instance);
00099 
00103   static void DoCommandReturnGroupID(ScriptInstance *instance);
00104 
00108   static void DoCommandReturnGoalID(ScriptInstance *instance);
00109 
00113   static void DoCommandReturnStoryPageID(ScriptInstance *instance);
00114 
00118   static void DoCommandReturnStoryPageElementID(ScriptInstance *instance);
00119 
00123   class ScriptController *GetController() { return controller; }
00124 
00128   inline bool IsDead() const { return this->is_dead; }
00129 
00133   void Save();
00134 
00138   static void SaveEmpty();
00139 
00145   void Load(int version);
00146 
00150   static void LoadEmpty();
00151 
00157   void Pause();
00158 
00163   bool IsPaused();
00164 
00170   void Unpause();
00171 
00177   SQInteger GetOpsTillSuspend();
00178 
00186   void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2);
00187 
00192   void InsertEvent(class ScriptEvent *event);
00193 
00199   bool IsSleeping() { return this->suspend != 0; }
00200 
00201 protected:
00202   class Squirrel *engine;               
00203   const char *versionAPI;               
00204 
00208   virtual void RegisterAPI();
00209 
00216   bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
00217 
00221   virtual void Died();
00222 
00226   virtual CommandCallback *GetDoCommandCallback() = 0;
00227 
00231   virtual void LoadDummyScript() = 0;
00232 
00233 private:
00234   class ScriptController *controller;   
00235   class ScriptStorage *storage;         
00236   SQObject *instance;                   
00237 
00238   bool is_started;                      
00239   bool is_dead;                         
00240   bool is_save_data_on_stack;           
00241   int suspend;                          
00242   bool is_paused;                       
00243   Script_SuspendCallbackProc *callback; 
00244 
00249   bool CallLoad();
00250 
00261   static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
00262 
00267   static bool LoadObjects(HSQUIRRELVM vm);
00268 };
00269 
00270 #endif /* SCRIPT_INSTANCE_HPP */