OpenTTD Source  20240919-master-gdf0233f4c2
ai_config.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../company_base.h"
12 #include "../settings_type.h"
13 #include "../string_func.h"
14 #include "ai.hpp"
15 #include "ai_config.hpp"
16 #include "ai_info.hpp"
17 
18 #include "../safeguards.h"
19 
21 {
22  assert(company < MAX_COMPANIES);
23 
24  AIConfig **config;
25  if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
26  config = &_settings_newgame.ai_config[company];
27  } else {
28  if (source != SSS_FORCE_GAME) {
29  Company *c = Company::GetIfValid(company);
30  if (c != nullptr && c->ai_config != nullptr) return c->ai_config.get();
31  }
32  config = &_settings_game.ai_config[company];
33  }
34  if (*config == nullptr) *config = new AIConfig();
35  return *config;
36 }
37 
38 class AIInfo *AIConfig::GetInfo() const
39 {
40  return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
41 }
42 
43 ScriptInfo *AIConfig::FindInfo(const std::string &name, int version, bool force_exact_match)
44 {
45  return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
46 }
47 
48 bool AIConfig::ResetInfo(bool force_exact_match)
49 {
50  this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
51  return this->info != nullptr;
52 }
AIConfig
Definition: ai_config.hpp:16
GameSettings::ai_config
class AIConfig * ai_config[MAX_COMPANIES]
settings per company
Definition: settings_type.h:598
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:152
ai.hpp
ai_info.hpp
ScriptConfig::SSS_DEFAULT
@ SSS_DEFAULT
Get the Script config from the current game mode.
Definition: script_config.hpp:94
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition: script_config.cpp:54
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:20
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
ScriptConfig::version
int version
Version of the Script.
Definition: script_config.hpp:174
AIConfig::FindInfo
ScriptInfo * FindInfo(const std::string &name, int version, bool force_exact_match) override
This function should call back to the Scanner in charge of this Config, to find the ScriptInfo belong...
Definition: ai_config.cpp:43
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:175
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:95
ScriptInfo::name
std::string name
Full name of the script.
Definition: script_info.hpp:147
ScriptConfig::name
std::string name
Name of the Script.
Definition: script_config.hpp:173
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
AI::FindInfo
static class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
Definition: ai_core.cpp:325
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
Company
Definition: company_base.h:133
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:96
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:58
AIConfig::ResetInfo
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
Definition: ai_config.cpp:48
ai_config.hpp
ScriptConfig::ScriptSettingSource
ScriptSettingSource
Where to get the config from, either default (depends on current game mode) or force either newgame o...
Definition: script_config.hpp:93