00001 /* $Id: ai_instance.hpp 20499 2010-08-15 14:06:43Z alberth $ */ 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 AI_INSTANCE_HPP 00013 #define AI_INSTANCE_HPP 00014 00015 #include <squirrel.h> 00016 00020 typedef void (AISuspendCallbackProc)(class AIInstance *instance); 00021 00025 class AI_VMSuspend { 00026 public: 00027 AI_VMSuspend(int time, AISuspendCallbackProc *callback) : 00028 time(time), 00029 callback(callback) 00030 {} 00031 00036 int GetSuspendTime() { return time; } 00037 00042 AISuspendCallbackProc *GetSuspendCallback() { return callback; } 00043 00044 private: 00045 int time; 00046 AISuspendCallbackProc *callback; 00047 }; 00048 00052 class AI_FatalError { 00053 public: 00054 AI_FatalError(const char *msg) : 00055 msg(msg) 00056 {} 00057 00058 const char *GetErrorMessage() { return msg; } 00059 00060 private: 00061 const char *msg; 00062 }; 00063 00065 class AIInstance { 00066 public: 00067 friend class AIObject; 00068 AIInstance(class AIInfo *info); 00069 ~AIInstance(); 00070 00075 void Continue(); 00076 00080 void GameLoop(); 00081 00085 void CollectGarbage() const; 00086 00090 static class AIStorage *GetStorage(); 00091 00095 static void DoCommandReturn(AIInstance *instance); 00096 00100 static void DoCommandReturnVehicleID(AIInstance *instance); 00101 00105 static void DoCommandReturnSignID(AIInstance *instance); 00106 00110 static void DoCommandReturnGroupID(AIInstance *instance); 00111 00115 class AIController *GetController() { return controller; } 00116 00120 inline bool IsDead() const { return this->is_dead; } 00121 00125 void Save(); 00126 00130 static void SaveEmpty(); 00131 00137 void Load(int version); 00138 00143 bool CallLoad(); 00144 00148 static void LoadEmpty(); 00149 00156 void Suspend(); 00157 private: 00158 class AIController *controller; 00159 class AIStorage *storage; 00160 class Squirrel *engine; 00161 SQObject *instance; 00162 00163 bool is_started; 00164 bool is_dead; 00165 bool is_save_data_on_stack; 00166 int suspend; 00167 AISuspendCallbackProc *callback; 00168 00172 void RegisterAPI(); 00173 00177 bool LoadCompatibilityScripts(const char *api_version); 00178 00182 void Died(); 00183 00194 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test); 00195 00200 static bool LoadObjects(HSQUIRRELVM vm); 00201 }; 00202 00203 #endif /* AI_INSTANCE_HPP */