# ai_info.cpp

Go to the documentation of this file.

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