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

OpenTTD Source 15.0-beta1

engine_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
12#include "saveload.h"
13#include "compat/engine_sl_compat.h"
14
15#include "saveload_internal.h"
16#include "../engine_base.h"
17#include "../string_func.h"
18
19#include "../safeguards.h"
20
21static const SaveLoad _engine_desc[] = {
22 SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
23 SLE_CONDVAR(Engine, intro_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
24 SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
25 SLE_CONDVAR(Engine, age, SLE_INT32, SLV_31, SL_MAX_VERSION),
26 SLE_VAR(Engine, reliability, SLE_UINT16),
27 SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
28 SLE_VAR(Engine, reliability_start, SLE_UINT16),
29 SLE_VAR(Engine, reliability_max, SLE_UINT16),
30 SLE_VAR(Engine, reliability_final, SLE_UINT16),
31 SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
32 SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
33 SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
34 SLE_VAR(Engine, flags, SLE_UINT8),
35 SLE_CONDVAR(Engine, preview_asked, SLE_UINT16, SLV_179, SL_MAX_VERSION),
36 SLE_CONDVAR(Engine, preview_company, SLE_UINT8, SLV_179, SL_MAX_VERSION),
37 SLE_VAR(Engine, preview_wait, SLE_UINT8),
38 SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
39 SLE_CONDVAR(Engine, company_avail, SLE_UINT16, SLV_104, SL_MAX_VERSION),
40 SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, SLV_193, SL_MAX_VERSION),
41 SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION),
42};
43
44static std::vector<Engine*> _temp_engine;
45
51 static Engine *CallocEngine()
52{
53 uint8_t *zero = CallocT<uint8_t>(sizeof(Engine));
54 Engine *engine = new (zero) Engine();
55 return engine;
56}
57
62 static void FreeEngine(Engine *e)
63{
64 if (e != nullptr) {
65 e->~Engine();
66 free(e);
67 }
68}
69
70 Engine *GetTempDataEngine(EngineID index)
71{
72 if (index < _temp_engine.size()) {
73 return _temp_engine[index];
74 } else if (index == _temp_engine.size()) {
75 _temp_engine.push_back(CallocEngine());
76 return _temp_engine[index];
77 } else {
78 NOT_REACHED();
79 }
80}
81
82 struct ENGNChunkHandler : ChunkHandler {
83 ENGNChunkHandler() : ChunkHandler('ENGN', CH_TABLE) {}
84
85 void Save() const override
86 {
87 SlTableHeader(_engine_desc);
88
89 for (Engine *e : Engine::Iterate()) {
90 SlSetArrayIndex(e->index);
91 SlObject(e, _engine_desc);
92 }
93 }
94
95 void Load() const override
96 {
97 const std::vector<SaveLoad> slt = SlCompatTableHeader(_engine_desc, _engine_sl_compat);
98
99 /* As engine data is loaded before engines are initialized we need to load
100 * this information into a temporary array. This is then copied into the
101 * engine pool after processing NewGRFs by CopyTempEngineData(). */
102 int index;
103 while ((index = SlIterateArray()) != -1) {
104 Engine *e = GetTempDataEngine(index);
105 SlObject(e, slt);
106
107 if (IsSavegameVersionBefore(SLV_179)) {
108 /* preview_company_rank was replaced with preview_company and preview_asked.
109 * Just cancel any previews. */
110 e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
111 e->preview_company = INVALID_COMPANY;
112 e->preview_asked = MAX_UVALUE(CompanyMask);
113 }
114 }
115 }
116};
117
121 void CopyTempEngineData()
122{
123 for (Engine *e : Engine::Iterate()) {
124 if (e->index >= _temp_engine.size()) break;
125
126 const Engine *se = GetTempDataEngine(e->index);
127 e->intro_date = se->intro_date;
128 e->age = se->age;
129 e->reliability = se->reliability;
130 e->reliability_spd_dec = se->reliability_spd_dec;
131 e->reliability_start = se->reliability_start;
132 e->reliability_max = se->reliability_max;
133 e->reliability_final = se->reliability_final;
134 e->duration_phase_1 = se->duration_phase_1;
135 e->duration_phase_2 = se->duration_phase_2;
136 e->duration_phase_3 = se->duration_phase_3;
137 e->flags = se->flags;
138 e->preview_asked = se->preview_asked;
139 e->preview_company = se->preview_company;
140 e->preview_wait = se->preview_wait;
141 e->company_avail = se->company_avail;
142 e->company_hidden = se->company_hidden;
143 e->name = se->name;
144 }
145
146 ResetTempEngineData();
147}
148
149void ResetTempEngineData()
150{
151 /* Get rid of temporary data */
152 for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
153 FreeEngine(*it);
154 }
155 _temp_engine.clear();
156}
157
158 struct ENGSChunkHandler : ChunkHandler {
159 ENGSChunkHandler() : ChunkHandler('ENGS', CH_READONLY) {}
160
161 void Load() const override
162 {
163 /* Load old separate String ID list into a temporary array. This
164 * was always 256 entries. */
165 StringID names[256];
166
167 SlCopy(names, lengthof(names), SLE_STRINGID);
168
169 /* Copy each string into the temporary engine array. */
170 for (EngineID engine = 0; engine < lengthof(names); engine++) {
171 Engine *e = GetTempDataEngine(engine);
172 e->name = CopyFromOldName(names[engine]);
173 }
174 }
175};
176
178 static const SaveLoad _engine_id_mapping_desc[] = {
179 SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
180 SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
181 SLE_VAR(EngineIDMapping, type, SLE_UINT8),
182 SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
183};
184
185 struct EIDSChunkHandler : ChunkHandler {
186 EIDSChunkHandler() : ChunkHandler('EIDS', CH_TABLE) {}
187
188 void Save() const override
189 {
190 SlTableHeader(_engine_id_mapping_desc);
191
192 /* Count total entries needed for combined list. */
193 size_t total = 0;
194 for (const auto &mapping : _engine_mngr.mappings) {
195 total += std::size(mapping);
196 }
197
198 /* Combine per-type mappings into single list for all types. */
199 std::vector<EngineIDMapping> temp;
200 temp.reserve(total);
201 for (const auto &mapping : _engine_mngr.mappings) {
202 temp.insert(std::end(temp), std::begin(mapping), std::end(mapping));
203 }
204
205 /* Sort combined list by EngineID */
206 std::ranges::sort(temp, std::less{}, &EngineIDMapping::engine);
207
208 for (EngineIDMapping &eid : temp) {
209 SlSetArrayIndex(eid.engine);
210 SlObject(&eid, _engine_id_mapping_desc);
211 }
212 }
213
214 void Load() const override
215 {
216 const std::vector<SaveLoad> slt = SlCompatTableHeader(_engine_id_mapping_desc, _engine_id_mapping_sl_compat);
217
218 _engine_mngr.mappings = {};
219
220 int index;
221 while ((index = SlIterateArray()) != -1) {
222 EngineIDMapping eid;
223 SlObject(&eid, slt);
224 _engine_mngr.SetID(eid.type, eid.internal_id, eid.grfid, eid.substitute_id, index);
225 }
226 }
227};
228
229static const EIDSChunkHandler EIDS;
230static const ENGNChunkHandler ENGN;
231static const ENGSChunkHandler ENGS;
232static const ChunkHandlerRef engine_chunk_handlers[] = {
233 EIDS,
234 ENGN,
235 ENGS,
236};
237
238extern const ChunkHandlerTable _engine_chunk_handlers(engine_chunk_handlers);
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition company_type.h:30
CallocEngine
static Engine * CallocEngine()
Allocate an Engine structure, but not using the pools.
Definition engine_sl.cpp:51
_engine_id_mapping_desc
static const SaveLoad _engine_id_mapping_desc[]
Save and load the mapping between the engine id in the pool, and the grf file it came from.
Definition engine_sl.cpp:178
FreeEngine
static void FreeEngine(Engine *e)
Deallocate an Engine constructed by CallocEngine.
Definition engine_sl.cpp:62
CopyTempEngineData
void CopyTempEngineData()
Copy data from temporary engine array into the real engine pool.
Definition engine_sl.cpp:121
engine_sl_compat.h
Loading for engine chunks before table headers were added.
_engine_id_mapping_sl_compat
const SaveLoadCompat _engine_id_mapping_sl_compat[]
Original field order for _engine_id_mapping_desc.
Definition engine_sl_compat.h:41
_engine_sl_compat
const SaveLoadCompat _engine_sl_compat[]
Original field order for _engine_desc.
Definition engine_sl_compat.h:16
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition engine_type.h:21
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:1924
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:659
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition saveload.cpp:1030
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1733
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1786
saveload.h
Functions/types related to saving and loading games.
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition saveload.h:459
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:509
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:512
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:933
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:868
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1263
SLV_84
@ SLV_84
84 11822
Definition saveload.h:143
SLV_193
@ SLV_193
193 26802
Definition saveload.h:275
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:399
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_104
@ SLV_104
104 14735
Definition saveload.h:167
SLV_179
@ SLV_179
179 24810
Definition saveload.h:257
SLV_31
@ SLV_31
31 5999
Definition saveload.h:80
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1002
saveload_internal.h
Declaration of functions used in more save/load files.
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition strings_sl.cpp:61
MAX_UVALUE
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
Definition stdafx.h:343
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:334
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:280
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:463
EIDSChunkHandler
Definition engine_sl.cpp:185
EIDSChunkHandler::Load
void Load() const override
Load the chunk.
Definition engine_sl.cpp:214
EIDSChunkHandler::Save
void Save() const override
Save the chunk.
Definition engine_sl.cpp:188
ENGNChunkHandler
Definition engine_sl.cpp:82
ENGNChunkHandler::Load
void Load() const override
Load the chunk.
Definition engine_sl.cpp:95
ENGNChunkHandler::Save
void Save() const override
Save the chunk.
Definition engine_sl.cpp:85
ENGSChunkHandler
Definition engine_sl.cpp:158
ENGSChunkHandler::Load
void Load() const override
Load the chunk.
Definition engine_sl.cpp:161
EngineIDMapping
Definition engine_base.h:196
EngineIDMapping::type
VehicleType type
The engine type.
Definition engine_base.h:199
EngineIDMapping::grfid
uint32_t grfid
The GRF ID of the file the entity belongs to.
Definition engine_base.h:197
EngineIDMapping::substitute_id
uint8_t substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition engine_base.h:200
EngineIDMapping::internal_id
uint16_t internal_id
The internal ID within the GRF file.
Definition engine_base.h:198
Engine
Definition engine_base.h:37
Engine::reliability_spd_dec
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:48
Engine::reliability_start
uint16_t reliability_start
Initial reliability of the engine.
Definition engine_base.h:49
Engine::intro_date
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:44
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:38
Engine::reliability_max
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:50
Engine::reliability_final
uint16_t reliability_final
Final reliability of the engine.
Definition engine_base.h:51
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition engine_base.h:57
Engine::duration_phase_3
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition engine_base.h:54
Engine::duration_phase_2
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:53
Engine::preview_wait
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:58
Engine::company_hidden
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition engine_base.h:39
Engine::reliability
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:47
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:40
Engine::age
int32_t age
Age of the engine in months.
Definition engine_base.h:45
Engine::name
std::string name
Custom name of engine.
Definition engine_base.h:42
Engine::duration_phase_1
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:52
Engine::flags
uint8_t flags
Flags of the engine.
Definition engine_base.h:55
Pool::PoolItem<&_engine_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:388
SaveLoad
SaveLoad type struct.
Definition saveload.h:717