ai_info.cpp

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

Generated on Sun Jan 9 16:01:50 2011 for OpenTTD by  doxygen 1.6.1