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 00021 class ScriptInstance { 00022 public: 00023 friend class ScriptObject; 00024 friend class ScriptController; 00025 00029 ScriptInstance(const char *APIName); 00030 virtual ~ScriptInstance(); 00031 00037 void Initialize(const char *main_script, const char *instance_name); 00038 00044 virtual int GetSetting(const char *name) = 0; 00045 00052 virtual class ScriptInfo *FindLibrary(const char *library, int version) = 0; 00053 00058 void Continue(); 00059 00063 void GameLoop(); 00064 00068 void CollectGarbage() const; 00069 00073 class ScriptStorage *GetStorage(); 00074 00078 void *GetLogPointer(); 00079 00083 static void DoCommandReturn(ScriptInstance *instance); 00084 00088 static void DoCommandReturnVehicleID(ScriptInstance *instance); 00089 00093 static void DoCommandReturnSignID(ScriptInstance *instance); 00094 00098 static void DoCommandReturnGroupID(ScriptInstance *instance); 00099 00103 class ScriptController *GetController() { return controller; } 00104 00108 inline bool IsDead() const { return this->is_dead; } 00109 00113 void Save(); 00114 00118 static void SaveEmpty(); 00119 00125 void Load(int version); 00126 00130 static void LoadEmpty(); 00131 00138 void Suspend(); 00139 00145 SQInteger GetOpsTillSuspend(); 00146 00154 void DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2); 00155 00160 void InsertEvent(class ScriptEvent *event); 00161 00162 protected: 00163 class Squirrel *engine; 00164 00168 virtual void RegisterAPI(); 00169 00173 virtual void Died(); 00174 00178 virtual CommandCallback *GetDoCommandCallback() = 0; 00179 00183 virtual void LoadDummyScript() = 0; 00184 00185 private: 00186 class ScriptController *controller; 00187 class ScriptStorage *storage; 00188 SQObject *instance; 00189 00190 bool is_started; 00191 bool is_dead; 00192 bool is_save_data_on_stack; 00193 int suspend; 00194 Script_SuspendCallbackProc *callback; 00195 00200 bool CallLoad(); 00201 00212 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test); 00213 00218 static bool LoadObjects(HSQUIRRELVM vm); 00219 }; 00220 00221 #endif /* SCRIPT_INSTANCE_HPP */