# ai_info.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 <squirrel.h>
    00015 #include "../script/squirrel.hpp"
    00016 #include "../script/squirrel_helper.hpp"
    00017 #include "ai.hpp"
    00018 #include "ai_info.hpp"
    00019 #include "ai_scanner.hpp"
    00020 #include "../settings_type.h"
    00021 #include "../openttd.h"
    00022 #include "../debug.h"
    00023 #include "../rev.h"
    00024
    00025 AIConfigItem _start_date_config = {
    00026   "start_date",
    00027   "The amount of days after the start of the last AI, this AI will start (give or take).",
    00028   AI::START_NEXT_MIN,
    00029   AI::START_NEXT_MAX,
    00030   AI::START_NEXT_MEDIUM,
    00031   AI::START_NEXT_EASY,
    00032   AI::START_NEXT_MEDIUM,
    00033   AI::START_NEXT_HARD,
    00034   AI::START_NEXT_DEVIATION,
    00035   30,
    00036   AICONFIG_NONE,
    00037   NULL
    00038 };
    00039
    00040 AILibrary::~AILibrary()
    00041 {
    00042   free((void *)this->category);
    00043 }
    00044
    00045 /* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info)
    00046 {
    00047   SQInteger res = ScriptFileInfo::Constructor(vm, info);
    00048   if (res != 0) return res;
    00049   info->base = ((AIScanner *)Squirrel::GetGlobalPointer(vm));
    00050
    00051   return 0;
    00052 }
    00053
    00054 static bool CheckAPIVersion(const char *api_version)
    00055 {
    00056   return strcmp(api_version, "0.7") == 0 || strcmp(api_version, "1.0") == 0;
    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 = AIFileInfo::Constructor(vm, info);
    00067   if (res != 0) return res;
    00068
    00069   AIConfigItem config = _start_date_config;
    00070   config.name = strdup(config.name);
    00071   config.description = strdup(config.description);
    00072   info->config_list.push_back(config);
    00073
    00074   /* Check if we have settings */
    00075   if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
    00076     if (!info->GetSettings()) return SQ_ERROR;
    00077   }
    00078   if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
    00079     if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version)) return SQ_ERROR;
    00080   } else {
    00081     info->min_loadable_version = info->GetVersion();
    00082   }
    00083   /* When there is an UseAsRandomAI function, call it. */
    00084   if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
    00085     if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random)) return SQ_ERROR;
    00086   } else {
    00087     info->use_as_random = true;
    00088   }
    00089   /* Try to get the API version the AI is written for. */
    00090   if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) {
    00091     if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version)) return SQ_ERROR;
    00092     if (!CheckAPIVersion(info->api_version)) {
    00093       DEBUG(ai, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
    00094       return SQ_ERROR;
    00095     }
    00096   } else {
    00097     info->api_version = strdup("0.7");
    00098   }
    00099
    00100   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
    00101   sq_setinstanceup(vm, 2, NULL);
    00102   /* Register the AI to the base system */
    00103   info->base->RegisterAI(info);
    00104   return 0;
    00105 }
    00106
    00107 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
    00108 {
    00109   /* Get the AIInfo */
    00110   SQUserPointer instance;
    00111   sq_getinstanceup(vm, 2, &instance, 0);
    00112   AIInfo *info = (AIInfo *)instance;
    00113   info->api_version = NULL;
    00114
    00115   SQInteger res = AIFileInfo::Constructor(vm, info);
    00116   if (res != 0) return res;
    00117
    00118   char buf[8];
    00119   seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
    00120   info->api_version = strdup(buf);
    00121
    00122   /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
    00123   sq_setinstanceup(vm, 2, NULL);
    00124   /* Register the AI to the base system */
    00125   info->base->SetDummyAI(info);
    00126   return 0;
    00127 }
    00128
    00129 bool AIInfo::GetSettings()
    00130 {
    00131   return this->engine->CallMethod(*this->SQ_instance, "GetSettings", NULL, -1);
    00132 }
    00133
    00134 AIInfo::AIInfo() :
    00135   min_loadable_version(0),
    00136   use_as_random(false),
    00137   api_version(NULL)
    00138 {
    00139 }
    00140
    00141 AIInfo::~AIInfo()
    00142 {
    00143   /* Free all allocated strings */
    00144   for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
    00145     free((void*)(*it).name);
    00146     free((void*)(*it).description);
    00147     if (it->labels != NULL) {
    00148       for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
    00149         free(it2->second);
    00150       }
    00151       delete it->labels;
    00152     }
    00153   }
    00154   this->config_list.clear();
    00155   free((void*)this->api_version);
    00156 }
    00157
    00158 bool AIInfo::CanLoadFromVersion(int version) const
    00159 {
    00160   if (version == -1) return true;
    00161   return version >= this->min_loadable_version && version <= this->GetVersion();
    00162 }
    00163
    00164 SQInteger AIInfo::AddSetting(HSQUIRRELVM vm)
    00165 {
    00166   AIConfigItem config;
    00167   memset(&config, 0, sizeof(config));
    00168   config.max_value = 1;
    00169   config.step_size = 1;
    00170   uint items = 0;
    00171
    00172   /* Read the table, and find all properties we care about */
    00173   sq_pushnull(vm);
    00174   while (SQ_SUCCEEDED(sq_next(vm, -2))) {
    00175     const SQChar *sqkey;
    00176     if (SQ_FAILED(sq_getstring(vm, -2, &sqkey))) return SQ_ERROR;
    00177     const char *key = FS2OTTD(sqkey);
    00178
    00179     if (strcmp(key, "name") == 0) {
    00180       const SQChar *sqvalue;
    00181       if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
    00182       char *name = strdup(FS2OTTD(sqvalue));
    00183       char *s;
    00184       /* Don't allow '=' and ',' in configure setting names, as we need those
    00185        *  2 chars to nicely store the settings as a string. */
    00186       while ((s = strchr(name, '=')) != NULL) *s = '_';
    00187       while ((s = strchr(name, ',')) != NULL) *s = '_';
    00188       config.name = name;
    00189       items |= 0x001;
    00190     } else if (strcmp(key, "description") == 0) {
    00191       const SQChar *sqdescription;
    00192       if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
    00193       config.description = strdup(FS2OTTD(sqdescription));
    00194       items |= 0x002;
    00195     } else if (strcmp(key, "min_value") == 0) {
    00196       SQInteger res;
    00197       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00198       config.min_value = res;
    00199       items |= 0x004;
    00200     } else if (strcmp(key, "max_value") == 0) {
    00201       SQInteger res;
    00202       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00203       config.max_value = res;
    00204       items |= 0x008;
    00205     } else if (strcmp(key, "easy_value") == 0) {
    00206       SQInteger res;
    00207       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00208       config.easy_value = res;
    00209       items |= 0x010;
    00210     } else if (strcmp(key, "medium_value") == 0) {
    00211       SQInteger res;
    00212       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00213       config.medium_value = res;
    00214       items |= 0x020;
    00215     } else if (strcmp(key, "hard_value") == 0) {
    00216       SQInteger res;
    00217       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00218       config.hard_value = res;
    00219       items |= 0x040;
    00220     } else if (strcmp(key, "random_deviation") == 0) {
    00221       SQInteger res;
    00222       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00223       config.random_deviation = res;
    00224       items |= 0x200;
    00225     } else if (strcmp(key, "custom_value") == 0) {
    00226       SQInteger res;
    00227       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00228       config.custom_value = res;
    00229       items |= 0x080;
    00230     } else if (strcmp(key, "step_size") == 0) {
    00231       SQInteger res;
    00232       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00233       config.step_size = res;
    00234     } else if (strcmp(key, "flags") == 0) {
    00235       SQInteger res;
    00236       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
    00237       config.flags = (AIConfigFlags)res;
    00238       items |= 0x100;
    00239     } else {
    00240       char error[1024];
    00241       snprintf(error, sizeof(error), "unknown setting property '%s'", key);
    00242       this->engine->ThrowError(error);
    00243       return SQ_ERROR;
    00244     }
    00245
    00246     sq_pop(vm, 2);
    00247   }
    00248   sq_pop(vm, 1);
    00249
    00250   /* Don't allow both random_deviation and AICONFIG_RANDOM to
    00251    * be set for the same config item. */
    00252   if ((items & 0x200) != 0 && (config.flags & AICONFIG_RANDOM) != 0) {
    00253     char error[1024];
    00254     snprintf(error, sizeof(error), "Setting both random_deviation and AICONFIG_RANDOM is not allowed");
    00255     this->engine->ThrowError(error);
    00256     return SQ_ERROR;
    00257   }
    00258   /* Reset the bit for random_deviation as it's optional. */
    00259   items &= ~0x200;
    00260
    00261   /* Make sure all properties are defined */
    00262   uint mask = (config.flags & AICONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
    00263   if (items != mask) {
    00264     char error[1024];
    00265     snprintf(error, sizeof(error), "please define all properties of a setting (min/max not allowed for booleans)");
    00266     this->engine->ThrowError(error);
    00267     return SQ_ERROR;
    00268   }
    00269
    00270   this->config_list.push_back(config);
    00271   return 0;
    00272 }
    00273
    00274 SQInteger AIInfo::AddLabels(HSQUIRRELVM vm)
    00275 {
    00276   const SQChar *sq_setting_name;
    00277   if (SQ_FAILED(sq_getstring(vm, -2, &sq_setting_name))) return SQ_ERROR;
    00278   const char *setting_name = FS2OTTD(sq_setting_name);
    00279
    00280   AIConfigItem *config = NULL;
    00281   for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
    00282     if (strcmp((*it).name, setting_name) == 0) config = &(*it);
    00283   }
    00284
    00285   if (config == NULL) {
    00286     char error[1024];
    00287     snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
    00288     this->engine->ThrowError(error);
    00289     return SQ_ERROR;
    00290   }
    00291   if (config->labels != NULL) return SQ_ERROR;
    00292
    00293   config->labels = new LabelMapping;
    00294
    00295   /* Read the table and find all labels */
    00296   sq_pushnull(vm);
    00297   while (SQ_SUCCEEDED(sq_next(vm, -2))) {
    00298     const SQChar *sq_key;
    00299     const SQChar *sq_label;
    00300     if (SQ_FAILED(sq_getstring(vm, -2, &sq_key))) return SQ_ERROR;
    00301     if (SQ_FAILED(sq_getstring(vm, -1, &sq_label))) return SQ_ERROR;
    00302     /* Because squirrel doesn't support identifiers starting with a digit,
    00303      * we skip the first character. */
    00304     const char *key_string = FS2OTTD(sq_key);
    00305     int key = atoi(key_string + 1);
    00306     const char *label = FS2OTTD(sq_label);
    00307
    00308     if (config->labels->Find(key) == config->labels->End()) config->labels->Insert(key, strdup(label));
    00309
    00310     sq_pop(vm, 2);
    00311   }
    00312   sq_pop(vm, 1);
    00313
    00314   return 0;
    00315 }
    00316
    00317 const AIConfigItemList *AIInfo::GetConfigList() const
    00318 {
    00319   return &this->config_list;
    00320 }
    00321
    00322 const AIConfigItem *AIInfo::GetConfigItem(const char *name) const
    00323 {
    00324   for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
    00325     if (strcmp((*it).name, name) == 0) return &(*it);
    00326   }
    00327   return NULL;
    00328 }
    00329
    00330 int AIInfo::GetSettingDefaultValue(const char *name) const
    00331 {
    00332   for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
    00333     if (strcmp((*it).name, name) != 0) continue;
    00334     /* The default value depends on the difficulty level */
    00335     switch ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) {
    00336       case 0: return (*it).easy_value;
    00337       case 1: return (*it).medium_value;
    00338       case 2: return (*it).hard_value;
    00339       case 3: return (*it).custom_value;
    00340       default: NOT_REACHED();
    00341     }
    00342   }
    00343
    00344   /* There is no such setting */
    00345   return -1;
    00346 }
    00347
    00348 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
    00349 {
    00350   /* Create a new AIFileInfo */
    00351   AILibrary *library = new AILibrary();
    00352
    00353   SQInteger res = AIFileInfo::Constructor(vm, library);
    00354   if (res != 0) {
    00355     delete library;
    00356     return res;
    00357   }
    00358
    00359   /* Cache the category */
    00360   if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category)) {
    00361     delete library;
    00362     return SQ_ERROR;
    00363   }
    00364
    00365   /* Register the Library to the base system */
    00366   library->base->RegisterLibrary(library);
    00367
    00368   return 0;
    00369 }
    00370
    00371 /* static */ SQInteger AILibrary::Import(HSQUIRRELVM vm)
    00372 {
    00373   SQConvert::SQAutoFreePointers ptr;
    00374   const char *library = GetParam(SQConvert::ForceType<const char *>(), vm, 2, &ptr);
    00375   const char *class_name = GetParam(SQConvert::ForceType<const char *>(), vm, 3, &ptr);
    00376   int version = GetParam(SQConvert::ForceType<int>(), vm, 4, &ptr);
    00377
    00378   if (!AI::ImportLibrary(library, class_name, version, vm)) return -1;
    00379   return 1;
    00380 }