# ai_info.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_info.cpp 23969 2012-02-19 19:14:17Z 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 #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 || strcmp(api_version, "1.3") == 0;
    00027 }
    00028
    00029 #if defined(WIN32)
    00030 #undef GetClassName
    00031 #endif /* WIN32 */
    00032 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
    00033
    00034 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
    00035 {
    00036   /* Create the AIInfo class, and add the RegisterAI function */
    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   /* Pre 1.2 had an AI prefix */
    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 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
    00060 {
    00061   /* Get the AIInfo */
    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   /* When there is an UseAsRandomAI function, call it. */
    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   /* Try to get the API version the AI is written for. */
    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   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
    00097   sq_setinstanceup(vm, 2, NULL);
    00098   /* Register the AI to the base system */
    00099   info->GetScanner()->RegisterScript(info);
    00100   return 0;
    00101 }
    00102
    00103 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
    00104 {
    00105   /* Get the AIInfo */
    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   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
    00119   sq_setinstanceup(vm, 2, NULL);
    00120   /* Register the AI to the base system */
    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 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
    00150 {
    00151   /* Create the AILibrary class, and add the RegisterLibrary function */
    00152   engine->AddClassBegin("AILibrary");
    00153   engine->AddClassEnd();
    00154   engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
    00155 }
    00156
    00157 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
    00158 {
    00159   /* Create a new library */
    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   /* Cache the category */
    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   /* Register the Library to the base system */
    00175   library->GetScanner()->RegisterScript(library);
    00176
    00177   return 0;
    00178 }