OpenTTD
ai_config.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 
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,
34  NULL,
35  false
36 };
37 
38 AIConfig::AIConfig(const AIConfig *config) : ScriptConfig(config)
39 {
40  /* Override start_date as per AIConfig::AddRandomDeviation().
41  * This is necessary because the ScriptConfig constructor will instead call
42  * ScriptConfig::AddRandomDeviation(). */
43  int start_date = config->GetSetting("start_date");
44  this->SetSetting("start_date", start_date != 0 ? max(1, this->GetSetting("start_date")) : 0);
45 }
46 
48 {
49  AIConfig **config;
50  if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
51  config = &_settings_newgame.ai_config[company];
52  } else {
53  config = &_settings_game.ai_config[company];
54  }
55  if (*config == NULL) *config = new AIConfig();
56  return *config;
57 }
58 
59 class AIInfo *AIConfig::GetInfo() const
60 {
61  return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
62 }
63 
64 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
65 {
66  return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
67 }
68 
69 bool AIConfig::ResetInfo(bool force_exact_match)
70 {
71  this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
72  return this->info != NULL;
73 }
74 
76 {
77  this->config_list->push_back(_start_date_config);
78 }
79 
81 {
82  /* The special casing for start_date is here to ensure that the
83  * start_date setting won't change even if you chose another Script. */
84  int start_date = this->GetSetting("start_date");
85 
87 
88  this->SetSetting("start_date", start_date);
89 }
90 
91 int AIConfig::GetSetting(const char *name) const
92 {
93  if (this->info == NULL) {
94  SettingValueList::const_iterator it = this->settings.find(name);
95  if (it == this->settings.end()) {
96  assert(strcmp("start_date", name) == 0);
97  switch (GetGameSettings().script.settings_profile) {
98  case SP_EASY: return AI::START_NEXT_EASY;
99  case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
100  case SP_HARD: return AI::START_NEXT_HARD;
101  case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
102  default: NOT_REACHED();
103  }
104  }
105 
106  return (*it).second;
107  }
108 
109  return ScriptConfig::GetSetting(name);
110 }
111 
112 void AIConfig::SetSetting(const char *name, int value)
113 {
114  if (this->info == NULL) {
115  if (strcmp("start_date", name) != 0) return;
116  value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
117 
118  SettingValueList::iterator it = this->settings.find(name);
119  if (it != this->settings.end()) {
120  (*it).second = value;
121  } else {
122  this->settings[stredup(name)] = value;
123  }
124 
125  return;
126  }
127 
128  ScriptConfig::SetSetting(name, value);
129 }
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
Medium difficulty.
Definition: settings_type.h:30
No profile, special "custom" highscore.
Definition: settings_type.h:35
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Script settings.
ScriptConfigItem _start_date_config
Configuration for AI start date, every AI has this setting.
Definition: ai_config.cpp:22
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
static class AIInfo * FindInfo(const char *name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
Definition: ai_core.cpp:342
ScriptConfigItemList * config_list
List with all settings defined by this Script.
No flags set.
Get the Script config from the current game mode.
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
Definition: ai_config.cpp:69
virtual void ClearConfigList()
Routine that clears the config list.
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
void ClearConfigList()
Routine that clears the config list.
Definition: ai_config.cpp:80
int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: ai_config.cpp:91
void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: ai_config.cpp:112
All static information from an Script like name, version, etc.
Definition: script_info.hpp:32
ScriptSettingSource
Where to get the config from, either default (depends on current game mode) or force either newgame o...
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:78
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:47
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
ScriptInfo * FindInfo(const char *name, int version, bool force_exact_match)
This function should call back to the Scanner in charge of this Config, to find the ScriptInfo belong...
Definition: ai_config.cpp:64
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:18
Hard difficulty.
Definition: settings_type.h:31
Info about a single Script setting.
int version
Version of the Script.
void PushExtraConfigList()
In case you have mandatory non-Script-definable config entries in your list, add them to this functio...
Definition: ai_config.cpp:75
static GameSettings & GetGameSettings()
Get the settings-object applicable for the current situation: the newgame settings when we&#39;re in the ...
Get the newgame Script config.
Easy difficulty.
Definition: settings_type.h:29
class ScriptInfo * info
ScriptInfo object for related to this Script version.
class AIConfig * ai_config[MAX_COMPANIES]
settings per company
virtual int GetSetting(const char *name) const
Get the value of a setting for this config.
Base functions for all AIs.
const char * name
Name of the Script.
AIConfig stores the configuration settings of every AI.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
AIInfo keeps track of all information of an AI, like Author, Description, ...