ai_info.cpp

Go to the documentation of this file.
00001 /* $Id: ai_info.cpp 16121 2009-04-22 09:00:19Z 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 {
00167   /* Free all allocated strings */
00168   for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00169     free((char *)(*it).name);
00170     free((char *)(*it).description);
00171     if (it->labels != NULL) {
00172       for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
00173         free(it2->second);
00174       }
00175       delete it->labels;
00176     }
00177   }
00178   this->config_list.clear();
00179 }
00180 
00181 bool AIInfo::CanLoadFromVersion(int version) const
00182 {
00183   if (version == -1) return true;
00184   return version >= this->min_loadable_version && version <= this->GetVersion();
00185 }
00186 
00187 SQInteger AIInfo::AddSetting(HSQUIRRELVM vm)
00188 {
00189   AIConfigItem config;
00190   memset(&config, 0, sizeof(config));
00191   config.max_value = 1;
00192   config.step_size = 1;
00193   uint items = 0;
00194 
00195   /* Read the table, and find all properties we care about */
00196   sq_pushnull(vm);
00197   while (SQ_SUCCEEDED(sq_next(vm, -2))) {
00198     const SQChar *sqkey;
00199     if (SQ_FAILED(sq_getstring(vm, -2, &sqkey))) return SQ_ERROR;
00200     const char *key = FS2OTTD(sqkey);
00201 
00202     if (strcmp(key, "name") == 0) {
00203       const SQChar *sqvalue;
00204       if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
00205       config.name = strdup(FS2OTTD(sqvalue));
00206       char *s;
00207       /* Don't allow '=' and ',' in configure setting names, as we need those
00208        *  2 chars to nicely store the settings as a string. */
00209       while ((s = (char *)strchr(config.name, '=')) != NULL) *s = '_';
00210       while ((s = (char *)strchr(config.name, ',')) != NULL) *s = '_';
00211       items |= 0x001;
00212     } else if (strcmp(key, "description") == 0) {
00213       const SQChar *sqdescription;
00214       if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
00215       config.description = strdup(FS2OTTD(sqdescription));
00216       items |= 0x002;
00217     } else if (strcmp(key, "min_value") == 0) {
00218       SQInteger res;
00219       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00220       config.min_value = res;
00221       items |= 0x004;
00222     } else if (strcmp(key, "max_value") == 0) {
00223       SQInteger res;
00224       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00225       config.max_value = res;
00226       items |= 0x008;
00227     } else if (strcmp(key, "easy_value") == 0) {
00228       SQInteger res;
00229       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00230       config.easy_value = res;
00231       items |= 0x010;
00232     } else if (strcmp(key, "medium_value") == 0) {
00233       SQInteger res;
00234       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00235       config.medium_value = res;
00236       items |= 0x020;
00237     } else if (strcmp(key, "hard_value") == 0) {
00238       SQInteger res;
00239       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00240       config.hard_value = res;
00241       items |= 0x040;
00242     }  else if (strcmp(key, "random_deviation") == 0) {
00243       SQInteger res;
00244       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00245       config.random_deviation = res;
00246       items |= 0x200;
00247     } else if (strcmp(key, "custom_value") == 0) {
00248       SQInteger res;
00249       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00250       config.custom_value = res;
00251       items |= 0x080;
00252     } else if (strcmp(key, "step_size") == 0) {
00253       SQInteger res;
00254       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00255       config.step_size = res;
00256     } else if (strcmp(key, "flags") == 0) {
00257       SQInteger res;
00258       if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00259       config.flags = (AIConfigFlags)res;
00260       items |= 0x100;
00261     } else {
00262       char error[1024];
00263       snprintf(error, sizeof(error), "unknown setting property '%s'", key);
00264       this->engine->ThrowError(error);
00265       return SQ_ERROR;
00266     }
00267 
00268     sq_pop(vm, 2);
00269   }
00270   sq_pop(vm, 1);
00271 
00272   /* Don't allow both random_deviation and AICONFIG_RANDOM to
00273    * be set for the same config item. */
00274   if ((items & 0x200) != 0 && (config.flags & AICONFIG_RANDOM) != 0) {
00275     char error[1024];
00276     snprintf(error, sizeof(error), "Setting both random_deviation and AICONFIG_RANDOM is not allowed");
00277     this->engine->ThrowError(error);
00278     return SQ_ERROR;
00279   }
00280   /* Reset the bit for random_deviation as it's optional. */
00281   items &= ~0x200;
00282 
00283   /* Make sure all properties are defined */
00284   uint mask = (config.flags & AICONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
00285   if (items != mask) {
00286     char error[1024];
00287     snprintf(error, sizeof(error), "please define all properties of a setting (min/max not allowed for booleans)");
00288     this->engine->ThrowError(error);
00289     return SQ_ERROR;
00290   }
00291 
00292   this->config_list.push_back(config);
00293   return 0;
00294 }
00295 
00296 SQInteger AIInfo::AddLabels(HSQUIRRELVM vm)
00297 {
00298   const SQChar *sq_setting_name;
00299   if (SQ_FAILED(sq_getstring(vm, -2, &sq_setting_name))) return SQ_ERROR;
00300   const char *setting_name = FS2OTTD(sq_setting_name);
00301 
00302   AIConfigItem *config = NULL;
00303   for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00304     if (strcmp((*it).name, setting_name) == 0) config = &(*it);
00305   }
00306 
00307   if (config == NULL) {
00308     char error[1024];
00309     snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
00310     this->engine->ThrowError(error);
00311     return SQ_ERROR;
00312   }
00313   if (config->labels != NULL) return SQ_ERROR;
00314 
00315   config->labels = new LabelMapping;
00316 
00317   /* Read the table and find all labels */
00318   sq_pushnull(vm);
00319   while (SQ_SUCCEEDED(sq_next(vm, -2))) {
00320     const SQChar *sq_key;
00321     const SQChar *sq_label;
00322     if (SQ_FAILED(sq_getstring(vm, -2, &sq_key))) return SQ_ERROR;
00323     if (SQ_FAILED(sq_getstring(vm, -1, &sq_label))) return SQ_ERROR;
00324     /* Because squirrel doesn't support identifiers starting with a digit,
00325      * we skip the first character. */
00326     const char *key_string = FS2OTTD(sq_key);
00327     int key = atoi(key_string + 1);
00328     const char *label = FS2OTTD(sq_label);
00329 
00330     if (config->labels->Find(key) == config->labels->End()) config->labels->Insert(key, strdup(label));
00331 
00332     sq_pop(vm, 2);
00333   }
00334   sq_pop(vm, 1);
00335 
00336   return 0;
00337 }
00338 
00339 const AIConfigItemList *AIInfo::GetConfigList() const
00340 {
00341   return &this->config_list;
00342 }
00343 
00344 const AIConfigItem *AIInfo::GetConfigItem(const char *name) const
00345 {
00346   for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00347     if (strcmp((*it).name, name) == 0) return &(*it);
00348   }
00349   return NULL;
00350 }
00351 
00352 int AIInfo::GetSettingDefaultValue(const char *name) const
00353 {
00354   for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00355     if (strcmp((*it).name, name) != 0) continue;
00356     /* The default value depends on the difficulty level */
00357     switch ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) {
00358       case 0: return (*it).easy_value;
00359       case 1: return (*it).medium_value;
00360       case 2: return (*it).hard_value;
00361       case 3: return (*it).custom_value;
00362       default: NOT_REACHED();
00363     }
00364   }
00365 
00366   /* There is no such setting */
00367   return -1;
00368 }
00369 
00370 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
00371 {
00372   /* Create a new AIFileInfo */
00373   AILibrary *library = new AILibrary();
00374 
00375   SQInteger res = AIFileInfo::Constructor(vm, library, true);
00376   if (res != 0) {
00377     delete library;
00378     return res;
00379   }
00380 
00381   /* Cache the category */
00382   if (!library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category)) {
00383     delete library;
00384     return SQ_ERROR;
00385   }
00386 
00387   /* Register the Library to the base system */
00388   library->base->RegisterLibrary(library);
00389 
00390   return 0;
00391 }
00392 
00393 /* static */ SQInteger AILibrary::Import(HSQUIRRELVM vm)
00394 {
00395   SQConvert::SQAutoFreePointers ptr;
00396   const char *library = GetParam(SQConvert::ForceType<const char *>(), vm, 2, &ptr);
00397   const char *class_name = GetParam(SQConvert::ForceType<const char *>(), vm, 3, &ptr);
00398   int version = GetParam(SQConvert::ForceType<int>(), vm, 4, &ptr);
00399 
00400   if (!AI::ImportLibrary(library, class_name, version, vm)) return -1;
00401   return 1;
00402 }

Generated on Thu Sep 24 19:35:00 2009 for OpenTTD by  doxygen 1.5.6