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