script_instance.hpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00021 static const uint SQUIRREL_MAX_DEPTH = 25; 
00022 
00024 class ScriptInstance {
00025 public:
00026   friend class ScriptObject;
00027   friend class ScriptController;
00028 
00032   ScriptInstance(const char *APIName);
00033   virtual ~ScriptInstance();
00034 
00041   void Initialize(const char *main_script, const char *instance_name, CompanyID company);
00042 
00048   virtual int GetSetting(const char *name) = 0;
00049 
00056   virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0;
00057 
00062   void Continue();
00063 
00067   void GameLoop();
00068 
00072   void CollectGarbage() const;
00073 
00077   class ScriptStorage *GetStorage();
00078 
00082   void *GetLogPointer();
00083 
00087   static void DoCommandReturn(ScriptInstance *instance);
00088 
00092   static void DoCommandReturnVehicleID(ScriptInstance *instance);
00093 
00097   static void DoCommandReturnSignID(ScriptInstance *instance);
00098 
00102   static void DoCommandReturnGroupID(ScriptInstance *instance);
00103 
00107   static void DoCommandReturnGoalID(ScriptInstance *instance);
00108 
00112   class ScriptController *GetController() { return controller; }
00113 
00117   inline bool IsDead() const { return this->is_dead; }
00118 
00122   void Save();
00123 
00127   static void SaveEmpty();
00128 
00134   void Load(int version);
00135 
00139   static void LoadEmpty();
00140 
00147   void Suspend();
00148 
00154   SQInteger GetOpsTillSuspend();
00155 
00163   void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2);
00164 
00169   void InsertEvent(class ScriptEvent *event);
00170 
00175   bool IsSleeping() { return this->suspend != 0; }
00176 
00177 protected:
00178   class Squirrel *engine;               
00179 
00183   virtual void RegisterAPI();
00184 
00188   virtual void Died();
00189 
00193   virtual CommandCallback *GetDoCommandCallback() = 0;
00194 
00198   virtual void LoadDummyScript() = 0;
00199 
00200 private:
00201   class ScriptController *controller;   
00202   class ScriptStorage *storage;         
00203   SQObject *instance;                   
00204 
00205   bool is_started;                      
00206   bool is_dead;                         
00207   bool is_save_data_on_stack;           
00208   int suspend;                          
00209   Script_SuspendCallbackProc *callback; 
00210 
00215   bool CallLoad();
00216 
00227   static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
00228 
00233   static bool LoadObjects(HSQUIRRELVM vm);
00234 };
00235 
00236 #endif /* SCRIPT_INSTANCE_HPP */