00001 /* $Id: ai_instance.hpp 18809 2010-01-15 16:41:15Z rubidium $ */ 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 00032 int GetSuspendTime() { return time; } 00033 AISuspendCallbackProc *GetSuspendCallback() { return callback; } 00034 00035 private: 00036 int time; 00037 AISuspendCallbackProc *callback; 00038 }; 00039 00043 class AI_FatalError { 00044 public: 00045 AI_FatalError(const char *msg) : 00046 msg(msg) 00047 {} 00048 00049 const char *GetErrorMessage() { return msg; } 00050 00051 private: 00052 const char *msg; 00053 }; 00054 00055 class AIInstance { 00056 public: 00057 friend class AIObject; 00058 AIInstance(class AIInfo *info); 00059 ~AIInstance(); 00060 00065 void Continue(); 00066 00070 void GameLoop(); 00071 00075 void CollectGarbage() const; 00076 00080 static class AIStorage *GetStorage(); 00081 00085 static void DoCommandReturn(AIInstance *instance); 00086 00090 static void DoCommandReturnVehicleID(AIInstance *instance); 00091 00095 static void DoCommandReturnSignID(AIInstance *instance); 00096 00100 static void DoCommandReturnGroupID(AIInstance *instance); 00101 00105 class AIController *GetController() { return controller; } 00106 00110 inline bool IsDead() const { return this->is_dead; } 00111 00115 void Save(); 00116 00120 static void SaveEmpty(); 00121 00127 void Load(int version); 00128 00133 bool CallLoad(); 00134 00138 static void LoadEmpty(); 00139 00140 private: 00141 class AIController *controller; 00142 class AIStorage *storage; 00143 class Squirrel *engine; 00144 SQObject *instance; 00145 00146 bool is_started; 00147 bool is_dead; 00148 bool is_save_data_on_stack; 00149 int suspend; 00150 AISuspendCallbackProc *callback; 00151 00155 void RegisterAPI(); 00156 00160 bool LoadCompatibilityScripts(const char *api_version); 00161 00165 void Died(); 00166 00177 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test); 00178 00183 static bool LoadObjects(HSQUIRRELVM vm); 00184 }; 00185 00186 #endif /* AI_INSTANCE_HPP */