script_instance.hpp

Go to the documentation of this file.
00001 /* $Id: script_instance.hpp 23632 2011-12-19 21:05:25Z truebrain $ */
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 
00020 static const uint SQUIRREL_MAX_DEPTH = 25; 
00021 
00023 class ScriptInstance {
00024 public:
00025   friend class ScriptObject;
00026   friend class ScriptController;
00027 
00031   ScriptInstance(const char *APIName);
00032   virtual ~ScriptInstance();
00033 
00040   void Initialize(const char *main_script, const char *instance_name, CompanyID company);
00041 
00047   virtual int GetSetting(const char *name) = 0;
00048 
00055   virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0;
00056 
00061   void Continue();
00062 
00066   void GameLoop();
00067 
00071   void CollectGarbage() const;
00072 
00076   class ScriptStorage *GetStorage();
00077 
00081   void *GetLogPointer();
00082 
00086   static void DoCommandReturn(ScriptInstance *instance);
00087 
00091   static void DoCommandReturnVehicleID(ScriptInstance *instance);
00092 
00096   static void DoCommandReturnSignID(ScriptInstance *instance);
00097 
00101   static void DoCommandReturnGroupID(ScriptInstance *instance);
00102 
00106   static void DoCommandReturnGoalID(ScriptInstance *instance);
00107 
00111   class ScriptController *GetController() { return controller; }
00112 
00116   inline bool IsDead() const { return this->is_dead; }
00117 
00121   void Save();
00122 
00126   static void SaveEmpty();
00127 
00133   void Load(int version);
00134 
00138   static void LoadEmpty();
00139 
00146   void Suspend();
00147 
00153   SQInteger GetOpsTillSuspend();
00154 
00162   void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2);
00163 
00168   void InsertEvent(class ScriptEvent *event);
00169 
00174   bool IsSleeping() { return this->suspend != 0; }
00175 
00176 protected:
00177   class Squirrel *engine;               
00178 
00182   virtual void RegisterAPI();
00183 
00187   virtual void Died();
00188 
00192   virtual CommandCallback *GetDoCommandCallback() = 0;
00193 
00197   virtual void LoadDummyScript() = 0;
00198 
00199 private:
00200   class ScriptController *controller;   
00201   class ScriptStorage *storage;         
00202   SQObject *instance;                   
00203 
00204   bool is_started;                      
00205   bool is_dead;                         
00206   bool is_save_data_on_stack;           
00207   int suspend;                          
00208   Script_SuspendCallbackProc *callback; 
00209 
00214   bool CallLoad();
00215 
00226   static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
00227 
00232   static bool LoadObjects(HSQUIRRELVM vm);
00233 };
00234 
00235 #endif /* SCRIPT_INSTANCE_HPP */