OpenTTD: src/ai/ai_config.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ai_config.cpp
Go to the documentation of this file.
1 /* $Id: ai_config.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
2
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 */
9
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"
18
19 #include "../safeguards.h"
20
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
36 };
37
38 /* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
39 {
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];
45 }
46 if (*config == NULL) *config = new AIConfig();
47 return *config;
48 }
49
50 class AIInfo *AIConfig::GetInfo() const
51 {
52 return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
53 }
54
55 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
56 {
57 return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
58 }
59
60 bool AIConfig::ResetInfo(bool force_exact_match)
61 {
62 this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
63 return this->info != NULL;
64 }
65
66 void AIConfig::PushExtraConfigList()
67 {
68 this->config_list->push_back(_start_date_config);
69 }
70
71 void AIConfig::ClearConfigList()
72 {
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");
76
77 ScriptConfig::ClearConfigList();
78
79 this->SetSetting("start_date", start_date);
80 }
81
82 int AIConfig::GetSetting(const char *name) const
83 {
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();
94 }
95 }
96
97 return (*it).second;
98 }
99
100 return ScriptConfig::GetSetting(name);
101 }
102
103 void AIConfig::SetSetting(const char *name, int value)
104 {
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);
108
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;
114 }
115
116 return;
117 }
118
119 ScriptConfig::SetSetting(name, value);
120 }