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

OpenTTD Source 13.2.1

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, SL_MAX_VERSION),
36 };
37
38 static void SaveReal_AIPL(int *index_ptr)
39 {
40 CompanyID index = (CompanyID)*index_ptr;
41 AIConfig *config = AIConfig::GetConfig(index);
42
43 if (config->HasScript()) {
44 _ai_saveload_name = config->GetName();
45 _ai_saveload_version = config->GetVersion();
46 } else {
47 /* No AI is configured for this so store an empty string as name. */
48 _ai_saveload_name.clear();
49 _ai_saveload_version = -1;
50 }
51
52 _ai_saveload_is_random = config->IsRandom();
53 _ai_saveload_settings = config->SettingsToString();
54
55 SlObject(nullptr, _ai_company_desc);
56 /* If the AI was active, store its data too */
57 if (Company::IsValidAiID(index)) AI::Save(index);
58 }
59
60 struct AIPLChunkHandler : ChunkHandler {
61 AIPLChunkHandler() : ChunkHandler('AIPL', CH_TABLE) {}
62
63 void Load() const override
64 {
65 const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat);
66
67 /* Free all current data */
68 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
69 AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr);
70 }
71
72 CompanyID index;
73 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
74 if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
75
76 _ai_saveload_is_random = false;
77 _ai_saveload_version = -1;
78 SlObject(nullptr, slt);
79
80 if (_game_mode == GM_MENU || (_networking && !_network_server)) {
81 if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
82 continue;
83 }
84
85 AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
86 if (_ai_saveload_name.empty()) {
87 /* A random AI. */
88 config->Change(nullptr, -1, false, true);
89 } else {
90 config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random);
91 if (!config->HasScript()) {
92 /* No version of the AI available that can load the data. Try to load the
93 * latest version of the AI instead. */
94 config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random);
95 if (!config->HasScript()) {
96 if (_ai_saveload_name.compare("%_dummy") != 0) {
97 Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
98 Debug(script, 0, "A random other AI will be loaded in its place.");
99 } else {
100 Debug(script, 0, "The savegame had no AIs available at the time of saving.");
101 Debug(script, 0, "A random available AI will be loaded now.");
102 }
103 } else {
104 Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
105 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.");
106 }
107 /* Make sure the AI doesn't get the saveload data, as it was not the
108 * writer of the saveload data in the first place */
109 _ai_saveload_version = -1;
110 }
111 }
112
113 config->StringToSettings(_ai_saveload_settings);
114
115 /* Load the AI saved data */
116 if (Company::IsValidAiID(index)) config->SetToLoadData(AIInstance::Load(_ai_saveload_version));
117 }
118 }
119
120 void Save() const override
121 {
122 SlTableHeader(_ai_company_desc);
123
124 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
125 SlSetArrayIndex(i);
126 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
127 }
128 }
129 };
130
131 static const AIPLChunkHandler AIPL;
132 static const ChunkHandlerRef ai_chunk_handlers[] = {
133 AIPL,
134 };
135
136 extern const ChunkHandlerTable _ai_chunk_handlers(ai_chunk_handlers);
AIConfig
Definition: ai_config.hpp:16
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:449
_network_server
bool _network_server
network-server is active
Definition: network.cpp:59
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:804
saveload.h
_ai_company_sl_compat
const SaveLoadCompat _ai_company_sl_compat[]
Original field order for _ai_company_desc.
Definition: ai_sl_compat.h:16
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:412
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
AIPLChunkHandler
Definition: ai_sl.cpp:60
COMPANY_FIRST
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
SLV_108
@ SLV_108
108 15045
Definition: saveload.h:176
AI::Save
static void Save(CompanyID company)
Save data from an AI to a savegame.
Definition: ai_core.cpp:280
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
ScriptInstance::Load
static ScriptData * Load(int version)
Load data from a savegame.
Definition: script_instance.cpp:673
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:175
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:170
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
Company::IsValidAiID
static bool IsValidAiID(size_t index)
Is this company a valid company, controlled by the computer (a NoAI program)?
Definition: company_base.h:137
ScriptConfig::StringToSettings
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition: script_config.cpp:185
SLEG_SSTR
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition: saveload.h:975
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:664
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:2094
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:180
SLV_136
@ SLV_136
136 18764
Definition: saveload.h:210
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:348
AIPLChunkHandler::Save
void Save() const override
Save the chunk.
Definition: ai_sl.cpp:120
AIPLChunkHandler::Load
void Load() const override
Load the chunk.
Definition: ai_sl.cpp:63
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:855
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:365
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:211
ai_sl_compat.h
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2030
ScriptConfig::Change
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Definition: script_config.cpp:19
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1839
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1892
SaveLoad
SaveLoad type struct.
Definition: saveload.h:659
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:110
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:165
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:671