00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013
00014 #include "../script/squirrel_class.hpp"
00015 #include "ai_info.hpp"
00016 #include "ai_scanner.hpp"
00017 #include "../debug.h"
00018 #include "../rev.h"
00019
00024 static bool CheckAPIVersion(const char *api_version)
00025 {
00026 return strcmp(api_version, "0.7") == 0 || strcmp(api_version, "1.0") == 0 || strcmp(api_version, "1.1") == 0 || strcmp(api_version, "1.2") == 0;
00027 }
00028
00029 #if defined(WIN32)
00030 #undef GetClassName
00031 #endif
00032 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
00033
00034 void AIInfo::RegisterAPI(Squirrel *engine)
00035 {
00036
00037 DefSQClass<AIInfo, ST_AI> SQAIInfo("AIInfo");
00038 SQAIInfo.PreRegister(engine);
00039 SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
00040 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
00041 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
00042 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
00043 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
00044 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
00045 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
00046 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
00047
00048
00049 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
00050 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "AICONFIG_RANDOM");
00051 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
00052 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
00053
00054 SQAIInfo.PostRegister(engine);
00055 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
00056 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
00057 }
00058
00059 SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
00060 {
00061
00062 SQUserPointer instance = NULL;
00063 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of AIInfo to RegisterAI"));
00064 AIInfo *info = (AIInfo *)instance;
00065
00066 SQInteger res = ScriptInfo::Constructor(vm, info);
00067 if (res != 0) return res;
00068
00069 ScriptConfigItem config = _start_date_config;
00070 config.name = strdup(config.name);
00071 config.description = strdup(config.description);
00072 info->config_list.push_front(config);
00073
00074 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
00075 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
00076 } else {
00077 info->min_loadable_version = info->GetVersion();
00078 }
00079
00080 if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
00081 if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
00082 } else {
00083 info->use_as_random = true;
00084 }
00085
00086 if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) {
00087 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
00088 if (!CheckAPIVersion(info->api_version)) {
00089 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
00090 return SQ_ERROR;
00091 }
00092 } else {
00093 info->api_version = strdup("0.7");
00094 }
00095
00096
00097 sq_setinstanceup(vm, 2, NULL);
00098
00099 info->GetScanner()->RegisterScript(info);
00100 return 0;
00101 }
00102
00103 SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
00104 {
00105
00106 SQUserPointer instance;
00107 sq_getinstanceup(vm, 2, &instance, 0);
00108 AIInfo *info = (AIInfo *)instance;
00109 info->api_version = NULL;
00110
00111 SQInteger res = ScriptInfo::Constructor(vm, info);
00112 if (res != 0) return res;
00113
00114 char buf[8];
00115 seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
00116 info->api_version = strdup(buf);
00117
00118
00119 sq_setinstanceup(vm, 2, NULL);
00120
00121 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info);
00122 return 0;
00123 }
00124
00125 AIInfo::AIInfo() :
00126 min_loadable_version(0),
00127 use_as_random(false),
00128 api_version(NULL)
00129 {
00130 }
00131
00132 AIInfo::~AIInfo()
00133 {
00134 free(this->api_version);
00135 }
00136
00137 bool AIInfo::CanLoadFromVersion(int version) const
00138 {
00139 if (version == -1) return true;
00140 return version >= this->min_loadable_version && version <= this->GetVersion();
00141 }
00142
00143
00144 AILibrary::~AILibrary()
00145 {
00146 free(this->category);
00147 }
00148
00149 void AILibrary::RegisterAPI(Squirrel *engine)
00150 {
00151
00152 engine->AddClassBegin("AILibrary");
00153 engine->AddClassEnd();
00154 engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
00155 }
00156
00157 SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
00158 {
00159
00160 AILibrary *library = new AILibrary();
00161
00162 SQInteger res = ScriptInfo::Constructor(vm, library);
00163 if (res != 0) {
00164 delete library;
00165 return res;
00166 }
00167
00168
00169 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
00170 delete library;
00171 return SQ_ERROR;
00172 }
00173
00174
00175 library->GetScanner()->RegisterScript(library);
00176
00177 return 0;
00178 }