OpenTTD Source: src/saveload/ai_sl.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
26static std::string _ai_saveload_name;
27static int _ai_saveload_version;
28static std::string _ai_saveload_settings;
29static bool _ai_saveload_is_random;
30
31static 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
38static 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
44static void SaveReal_AIPL(int arg)
45{
46 CompanyID index = static_cast<CompanyID>(arg);
47 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
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
75 struct AIPLChunkHandler : ChunkHandler {
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 = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
84 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt);
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);
98 AIInstance::LoadEmpty();
99 }
100 continue;
101 }
102
103 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
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 != "%_dummy") {
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 != "%_dummy") {
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 (CompanyID i = CompanyID::Begin(); i < MAX_COMPANIES; ++i) {
164 SlSetArrayIndex(i);
165 SlAutolength(SaveReal_AIPL, i.base());
166 }
167 }
168};
169
170static const AIPLChunkHandler AIPL;
171static const ChunkHandlerRef ai_chunk_handlers[] = {
172 AIPL,
173};
174
175extern const ChunkHandlerTable _ai_chunk_handlers(ai_chunk_handlers);
ai_sl_compat.h
Loading for ai chunks before table headers were added.
_ai_company_sl_compat
const SaveLoadCompat _ai_company_sl_compat[]
Original field order for _ai_company_desc.
Definition ai_sl_compat.h:16
AIConfig
Definition ai_config.hpp:16
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition ai_config.cpp:20
AI::Save
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition ai_core.cpp:270
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition script_config.hpp:100
ScriptConfig::HasScript
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
Definition script_config.cpp:127
ScriptConfig::Change
void Change(std::optional< std::string_view > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
Definition script_config.cpp:22
ScriptConfig::StringToSettings
void StringToSettings(std::string_view value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition script_config.cpp:142
ScriptConfig::GetName
const std::string & GetName() const
Get the name of the Script.
Definition script_config.cpp:132
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition script_config.cpp:137
ScriptConfig::SettingsToString
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition script_config.cpp:164
ScriptInstance::Load
static ScriptData * Load(int version)
Load data from a savegame.
Definition script_instance.cpp:732
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition script_instance.cpp:723
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
_networking
bool _networking
are we in networking mode?
Definition network.cpp:67
_network_server
bool _network_server
network-server is active
Definition network.cpp:68
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
SlAutolength
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition saveload.cpp:2101
saveload.h
Functions/types related to saving and loading games.
SLEG_CONDSSTR
#define SLEG_CONDSSTR(name, variable, type, from, to)
Storage of a global std::string in some savegame versions.
Definition saveload.h:1155
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
SLEG_SSTR
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition saveload.h:1226
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLV_AI_LOCAL_CONFIG
@ SLV_AI_LOCAL_CONFIG
332 PR#12003 Config of running AI is stored inside Company.
Definition saveload.h:378
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SLV_108
@ SLV_108
108 15045
Definition saveload.h:172
SLV_136
@ SLV_136
136 18764
Definition saveload.h:206
AIPLChunkHandler
Definition ai_sl.cpp:75
AIPLChunkHandler::Save
void Save() const override
Save the chunk.
Definition ai_sl.cpp:159
AIPLChunkHandler::Load
void Load() const override
Load the chunk.
Definition ai_sl.cpp:78
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
Company::IsValidAiID
static bool IsValidAiID(auto index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition company_base.h:153
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
SaveLoad
SaveLoad type struct.
Definition saveload.h:732