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 #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   class ScriptController *GetController() { return controller; }
00114 
00118   inline bool IsDead() const { return this->is_dead; }
00119 
00123   void Save();
00124 
00128   static void SaveEmpty();
00129 
00135   void Load(int version);
00136 
00140   static void LoadEmpty();
00141 
00147   void Pause();
00148 
00153   bool IsPaused();
00154 
00160   void Unpause();
00161 
00167   SQInteger GetOpsTillSuspend();
00168 
00176   void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2);
00177 
00182   void InsertEvent(class ScriptEvent *event);
00183 
00189   bool IsSleeping() { return this->suspend != 0; }
00190 
00191 protected:
00192   class Squirrel *engine;               
00193   const char *versionAPI;               
00194 
00198   virtual void RegisterAPI();
00199 
00206   bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
00207 
00211   virtual void Died();
00212 
00216   virtual CommandCallback *GetDoCommandCallback() = 0;
00217 
00221   virtual void LoadDummyScript() = 0;
00222 
00223 private:
00224   class ScriptController *controller;   
00225   class ScriptStorage *storage;         
00226   SQObject *instance;                   
00227 
00228   bool is_started;                      
00229   bool is_dead;                         
00230   bool is_save_data_on_stack;           
00231   int suspend;                          
00232   bool is_paused;                       
00233   Script_SuspendCallbackProc *callback; 
00234 
00239   bool CallLoad();
00240 
00251   static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
00252 
00257   static bool LoadObjects(HSQUIRRELVM vm);
00258 };
00259 
00260 #endif /* SCRIPT_INSTANCE_HPP */