ai_info.cpp

Go to the documentation of this file.
00001 /* $Id: ai_info.cpp 23386 2011-12-01 12:04:10Z truebrain $ */
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 #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 /* WIN32 */
00035 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
00036 
00037 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
00038 {
00039   /* Create the AIInfo class, and add the RegisterAI function */
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   /* Pre 1.2 had an AI prefix */
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 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
00063 {
00064   /* Get the AIInfo */
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   /* When there is an UseAsRandomAI function, call it. */
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   /* Try to get the API version the AI is written for. */
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   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
00100   sq_setinstanceup(vm, 2, NULL);
00101   /* Register the AI to the base system */
00102   info->GetScanner()->RegisterScript(info);
00103   return 0;
00104 }
00105 
00106 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
00107 {
00108   /* Get the AIInfo */
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   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
00122   sq_setinstanceup(vm, 2, NULL);
00123   /* Register the AI to the base system */
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 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
00153 {
00154   /* Create the AILibrary class, and add the RegisterLibrary function */
00155   engine->AddClassBegin("AILibrary");
00156   engine->AddClassEnd();
00157   engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
00158 }
00159 
00160 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
00161 {
00162   /* Create a new library */
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   /* Cache the category */
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   /* Register the Library to the base system */
00178   library->GetScanner()->RegisterScript(library);
00179 
00180   return 0;
00181 }