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