(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / ai / ai_config.cpp
blobf920d310148aecb9b26e84bb206cc900a41d5d18
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file ai_config.cpp Implementation of AIConfig. */
12 #include "../stdafx.h"
13 #include "../settings_type.h"
14 #include "../string_func.h"
15 #include "ai.hpp"
16 #include "ai_config.hpp"
17 #include "ai_info.hpp"
19 #include "../safeguards.h"
21 /** Configuration for AI start date, every AI has this setting. */
22 ScriptConfigItem _start_date_config = {
23 "start_date",
24 "", // STR_AI_SETTINGS_START_DELAY
25 AI::START_NEXT_MIN,
26 AI::START_NEXT_MAX,
27 AI::START_NEXT_MEDIUM,
28 AI::START_NEXT_EASY,
29 AI::START_NEXT_MEDIUM,
30 AI::START_NEXT_HARD,
31 AI::START_NEXT_DEVIATION,
32 30,
33 SCRIPTCONFIG_NONE,
34 NULL,
35 false
38 /* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
40 AIConfig **config;
41 if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
42 config = &_settings_newgame.ai_config[company];
43 } else {
44 config = &_settings_game.ai_config[company];
46 if (*config == NULL) *config = new AIConfig();
47 return *config;
50 class AIInfo *AIConfig::GetInfo() const
52 return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
55 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
57 return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
60 bool AIConfig::ResetInfo(bool force_exact_match)
62 this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
63 return this->info != NULL;
66 void AIConfig::PushExtraConfigList()
68 this->config_list->push_back(_start_date_config);
71 void AIConfig::ClearConfigList()
73 /* The special casing for start_date is here to ensure that the
74 * start_date setting won't change even if you chose another Script. */
75 int start_date = this->GetSetting("start_date");
77 ScriptConfig::ClearConfigList();
79 this->SetSetting("start_date", start_date);
82 int AIConfig::GetSetting(const char *name) const
84 if (this->info == NULL) {
85 SettingValueList::const_iterator it = this->settings.find(name);
86 if (it == this->settings.end()) {
87 assert(strcmp("start_date", name) == 0);
88 switch (GetGameSettings().script.settings_profile) {
89 case SP_EASY: return AI::START_NEXT_EASY;
90 case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
91 case SP_HARD: return AI::START_NEXT_HARD;
92 case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
93 default: NOT_REACHED();
97 return (*it).second;
100 return ScriptConfig::GetSetting(name);
103 void AIConfig::SetSetting(const char *name, int value)
105 if (this->info == NULL) {
106 if (strcmp("start_date", name) != 0) return;
107 value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
109 SettingValueList::iterator it = this->settings.find(name);
110 if (it != this->settings.end()) {
111 (*it).second = value;
112 } else {
113 this->settings[stredup(name)] = value;
116 return;
119 ScriptConfig::SetSetting(name, value);