OpenTTD
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 
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 
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 
67 {
68  this->config_list->push_back(_start_date_config);
69 }
70 
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 
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 }
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.
ScriptConfigItem _start_date_config
Configuration for AI start date, every AI has this setting.
Definition: ai_config.cpp:22
static class AIInfo * FindInfo(const char *name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
Definition: ai_core.cpp:337
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:60
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:71
int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: ai_config.cpp:82
void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: ai_config.cpp:103
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:38
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
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:55
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:66
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, ...