ai_config.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 #include "../settings_type.h"
00014 #include "../core/random_func.hpp"
00015 #include "ai.hpp"
00016 #include "ai_config.hpp"
00017 #include "ai_info.hpp"
00018 
00020 ScriptConfigItem _start_date_config = {
00021   "start_date",
00022   "", // STR_AI_SETTINGS_START_DELAY
00023   AI::START_NEXT_MIN,
00024   AI::START_NEXT_MAX,
00025   AI::START_NEXT_MEDIUM,
00026   AI::START_NEXT_EASY,
00027   AI::START_NEXT_MEDIUM,
00028   AI::START_NEXT_HARD,
00029   AI::START_NEXT_DEVIATION,
00030   30,
00031   SCRIPTCONFIG_NONE,
00032   NULL
00033 };
00034 
00035 /* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
00036 {
00037   AIConfig **config;
00038   if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
00039     config = &_settings_newgame.ai_config[company];
00040   } else {
00041     config = &_settings_game.ai_config[company];
00042   }
00043   if (*config == NULL) *config = new AIConfig();
00044   return *config;
00045 }
00046 
00047 class AIInfo *AIConfig::GetInfo() const
00048 {
00049   return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
00050 }
00051 
00052 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
00053 {
00054   return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
00055 }
00056 
00057 bool AIConfig::ResetInfo(bool force_exact_match)
00058 {
00059   this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
00060   return this->info != NULL;
00061 }
00062 
00063 void AIConfig::PushExtraConfigList()
00064 {
00065   this->config_list->push_back(_start_date_config);
00066 }
00067 
00068 void AIConfig::ClearConfigList()
00069 {
00070   /* The special casing for start_date is here to ensure that the
00071    *  start_date setting won't change even if you chose another Script. */
00072   int start_date = this->GetSetting("start_date");
00073 
00074   ScriptConfig::ClearConfigList();
00075 
00076   this->SetSetting("start_date", start_date);
00077 }
00078 
00079 int AIConfig::GetSetting(const char *name) const
00080 {
00081   if (this->info == NULL) {
00082     SettingValueList::const_iterator it = this->settings.find(name);
00083     if (it == this->settings.end() || GetGameSettings().difficulty.diff_level != 3) {
00084       assert(strcmp("start_date", name) == 0);
00085       switch (GetGameSettings().difficulty.diff_level) {
00086         case 0: return AI::START_NEXT_EASY;
00087         case 1: return AI::START_NEXT_MEDIUM;
00088         case 2: return AI::START_NEXT_HARD;
00089         case 3: return AI::START_NEXT_MEDIUM;
00090         default: NOT_REACHED();
00091       }
00092     }
00093 
00094     return (*it).second;
00095   }
00096 
00097   return ScriptConfig::GetSetting(name);
00098 }
00099 
00100 void AIConfig::SetSetting(const char *name, int value)
00101 {
00102   if (this->info == NULL) {
00103     if (strcmp("start_date", name) != 0) return;
00104     value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
00105 
00106     SettingValueList::iterator it = this->settings.find(name);
00107     if (it != this->settings.end()) {
00108       (*it).second = value;
00109     } else {
00110       this->settings[strdup(name)] = value;
00111     }
00112 
00113     return;
00114   }
00115 
00116   ScriptConfig::SetSetting(name, value);
00117 }