OpenTTD Source  20241121-master-g67a0fccfad
ai_sl.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 "../debug.h"
12 
13 #include "saveload.h"
14 #include "compat/ai_sl_compat.h"
15 
16 #include "../company_base.h"
17 #include "../string_func.h"
18 
19 #include "../ai/ai.hpp"
20 #include "../ai/ai_config.hpp"
21 #include "../network/network.h"
22 #include "../ai/ai_instance.hpp"
23 
24 #include "../safeguards.h"
25 
26 static std::string _ai_saveload_name;
27 static int _ai_saveload_version;
28 static std::string _ai_saveload_settings;
29 static bool _ai_saveload_is_random;
30 
31 static const SaveLoad _ai_company_desc[] = {
32  SLEG_SSTR("name", _ai_saveload_name, SLE_STR),
33  SLEG_SSTR("settings", _ai_saveload_settings, SLE_STR),
34  SLEG_CONDVAR("version", _ai_saveload_version, SLE_UINT32, SLV_108, SL_MAX_VERSION),
35  SLEG_CONDVAR("is_random", _ai_saveload_is_random, SLE_BOOL, SLV_136, SLV_AI_LOCAL_CONFIG),
36 };
37 
38 static const SaveLoad _ai_running_desc[] = {
39  SLEG_CONDSSTR("running_name", _ai_saveload_name, SLE_STR, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION),
40  SLEG_CONDSSTR("running_settings", _ai_saveload_settings, SLE_STR, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION),
41  SLEG_CONDVAR("running_version", _ai_saveload_version, SLE_UINT32, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION),
42 };
43 
44 static void SaveReal_AIPL(int arg)
45 {
46  CompanyID index = static_cast<CompanyID>(arg);
48 
49  if (config->HasScript()) {
50  _ai_saveload_name = config->GetName();
51  _ai_saveload_version = config->GetVersion();
52  } else {
53  /* No AI is configured for this so store an empty string as name. */
54  _ai_saveload_name.clear();
55  _ai_saveload_version = -1;
56  }
57 
58  _ai_saveload_settings = config->SettingsToString();
59 
60  SlObject(nullptr, _ai_company_desc);
61 
62  if (!Company::IsValidAiID(index)) return;
63 
64  /* If the AI was active, store its data too */
65  config = AIConfig::GetConfig(index);
66  _ai_saveload_name = config->GetName();
67  _ai_saveload_version = config->GetVersion();
68  _ai_saveload_settings = config->SettingsToString();
69 
70  SlObject(nullptr, _ai_running_desc);
71  AI::Save(index);
72 
73 }
74 
76  AIPLChunkHandler() : ChunkHandler('AIPL', CH_TABLE) {}
77 
78  void Load() const override
79  {
80  const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat);
81 
82  /* Free all current data */
83  for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
85  }
86 
87  CompanyID index;
88  while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
89  if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
90 
91  _ai_saveload_is_random = false;
92  _ai_saveload_version = -1;
93  SlObject(nullptr, slt);
94 
95  if (_game_mode == GM_MENU || (_networking && !_network_server)) {
96  if (Company::IsValidAiID(index)) {
97  SlObject(nullptr, _ai_running_desc);
99  }
100  continue;
101  }
102 
104  if (_ai_saveload_name.empty() || _ai_saveload_is_random) {
105  /* A random AI. */
106  config->Change(std::nullopt, -1, false);
107  } else {
108  config->Change(_ai_saveload_name, _ai_saveload_version, false);
109  if (!config->HasScript()) {
110  /* No version of the AI available. Try to configure the
111  * latest version of the AI instead. */
112  config->Change(_ai_saveload_name, -1, false);
113  if (!config->HasScript()) {
114  if (_ai_saveload_name.compare("%_dummy") != 0) {
115  Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
116  Debug(script, 0, "Configuration switched to Random AI.");
117  }
118  } else {
119  Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
120  Debug(script, 0, "The latest version of that AI has been configured instead");
121  }
122  }
123  }
124  config->StringToSettings(_ai_saveload_settings);
125 
126  if (!Company::IsValidAiID(index)) continue;
127 
128  /* Load the AI saved data */
129  SlObject(nullptr, _ai_running_desc);
130 
131  Company::Get(index)->ai_config = std::make_unique<AIConfig>();
132  config = Company::Get(index)->ai_config.get();
133  config->Change(_ai_saveload_name, _ai_saveload_version, false);
134  if (!config->HasScript()) {
135  /* No version of the AI available that can load the data. Try to load the
136  * latest version of the AI instead. */
137  config->Change(_ai_saveload_name, -1, false);
138  if (!config->HasScript()) {
139  if (_ai_saveload_name.compare("%_dummy") != 0) {
140  Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
141  Debug(script, 0, "A random other AI will be loaded in its place.");
142  } else {
143  Debug(script, 0, "The savegame had no AIs available at the time of saving.");
144  Debug(script, 0, "A random available AI will be loaded now.");
145  }
146  } else {
147  Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
148  Debug(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
149  }
150  /* Make sure the AI doesn't get the saveload data, as it was not the
151  * writer of the saveload data in the first place */
152  _ai_saveload_version = -1;
153  }
154  config->StringToSettings(_ai_saveload_settings);
155  config->SetToLoadData(AIInstance::Load(_ai_saveload_version));
156  }
157  }
158 
159  void Save() const override
160  {
161  SlTableHeader(_ai_company_desc);
162 
163  for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
164  SlSetArrayIndex(i);
165  SlAutolength(SaveReal_AIPL, i);
166  }
167  }
168 };
169 
170 static const AIPLChunkHandler AIPL;
171 static const ChunkHandlerRef ai_chunk_handlers[] = {
172  AIPL,
173 };
174 
175 extern const ChunkHandlerTable _ai_chunk_handlers(ai_chunk_handlers);
Loading for ai chunks before table headers were added.
const SaveLoadCompat _ai_company_sl_compat[]
Original field order for _ai_company_desc.
Definition: ai_sl_compat.h:16
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:20
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition: ai_core.cpp:278
@ SSS_FORCE_GAME
Get the Script config from the current game.
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void Change(std::optional< const std::string > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
const std::string & GetName() const
Get the name of the Script.
int GetVersion() const
Get the version of the Script.
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
static ScriptData * Load(int version)
Load data from a savegame.
static void LoadEmpty()
Load and discard data from a savegame.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
bool _networking
are we in networking mode?
Definition: network.cpp:65
bool _network_server
network-server is active
Definition: network.cpp:66
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1702
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1957
Functions/types related to saving and loading games.
#define SLEG_CONDSSTR(name, variable, type, from, to)
Storage of a global std::string in some savegame versions.
Definition: saveload.h:1117
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1086
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition: saveload.h:1187
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:507
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
@ SLV_AI_LOCAL_CONFIG
332 PR#12003 Config of running AI is stored inside Company.
Definition: saveload.h:378
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:397
@ SLV_108
108 15045
Definition: saveload.h:172
@ SLV_136
136 18764
Definition: saveload.h:206
void Save() const override
Save the chunk.
Definition: ai_sl.cpp:159
void Load() const override
Load the chunk.
Definition: ai_sl.cpp:78
Handlers and description of chunk.
Definition: saveload.h:461
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition: company_base.h:159
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
SaveLoad type struct.
Definition: saveload.h:713