Use ttd_unique_free_ptr in ScriptConfig
[openttd/fttd.git] / src / ai / ai_config.cpp
blob2345f1d7dda9f27617a72950094e95941c9f766a
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 "ai.hpp"
15 #include "ai_config.hpp"
16 #include "ai_info.hpp"
18 /** Configuration for AI start date, every AI has this setting. */
19 ScriptConfigItem _start_date_config = {
20 "start_date",
21 "", // STR_AI_SETTINGS_START_DELAY
22 AI::START_NEXT_MIN,
23 AI::START_NEXT_MAX,
24 AI::START_NEXT_MEDIUM,
25 AI::START_NEXT_EASY,
26 AI::START_NEXT_MEDIUM,
27 AI::START_NEXT_HARD,
28 AI::START_NEXT_DEVIATION,
29 30,
30 SCRIPTCONFIG_NONE,
31 NULL,
32 false
35 /* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
37 AIConfig **config;
38 if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
39 config = &_settings_newgame.ai_config[company];
40 } else {
41 config = &_settings_game.ai_config[company];
43 if (*config == NULL) *config = new AIConfig();
44 return *config;
47 class AIInfo *AIConfig::GetInfo() const
49 return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
52 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
54 return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
57 bool AIConfig::ResetInfo(bool force_exact_match)
59 this->info = (ScriptInfo *)AI::FindInfo (this->GetName(), force_exact_match ? this->version : -1, force_exact_match);
60 return this->info != NULL;
63 void AIConfig::PushExtraConfigList()
65 this->config_list->push_back(_start_date_config);
68 void AIConfig::ClearConfigList()
70 /* The special casing for start_date is here to ensure that the
71 * start_date setting won't change even if you chose another Script. */
72 int start_date = this->GetSetting("start_date");
74 ScriptConfig::ClearConfigList();
76 this->SetSetting("start_date", start_date);
79 int AIConfig::GetSetting(const char *name) const
81 if (this->info == NULL) {
82 SettingValueList::const_iterator it = this->settings.find(name);
83 if (it == this->settings.end()) {
84 assert(strcmp("start_date", name) == 0);
85 switch (GetGameSettings().script.settings_profile) {
86 case SP_EASY: return AI::START_NEXT_EASY;
87 case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
88 case SP_HARD: return AI::START_NEXT_HARD;
89 case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
90 default: NOT_REACHED();
94 return (*it).second;
97 return ScriptConfig::GetSetting(name);
100 void AIConfig::SetSetting(const char *name, int value)
102 if (this->info == NULL) {
103 if (strcmp("start_date", name) != 0) return;
104 value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
106 SettingValueList::iterator it = this->settings.find(name);
107 if (it != this->settings.end()) {
108 (*it).second = value;
109 } else {
110 this->settings[xstrdup(name)] = value;
113 return;
116 ScriptConfig::SetSetting(name, value);