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

Generated on Sun Jun 5 04:19:52 2011 for OpenTTD by  doxygen 1.6.1