# ai_info.cpp

Go to the documentation of this file.

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