# ai_info.cpp

Go to the documentation of this file.

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