OpenTTD Source 20261112-master-gbcbec5e63f
map_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
9
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "saveload_error.hpp"
15
16#include "../map_func.h"
18#include "../fios.h"
19
20#include "../safeguards.h"
21
22static uint32_t _map_dim_x;
23static uint32_t _map_dim_y;
24
25static const SaveLoad _map_desc[] = {
28};
29
30struct MAPSChunkHandler : ChunkHandler {
31 MAPSChunkHandler() : ChunkHandler("MAPS", ChunkType::Table) {}
32
33 void Save() const override
34 {
35 SlTableHeader(_map_desc);
36
37 _map_dim_x = Map::SizeX();
38 _map_dim_y = Map::SizeY();
39
40 SlSetArrayIndex(0);
41 SlGlobList(_map_desc);
42 }
43
44 void Load() const override
45 {
46 const std::vector<SaveLoad> slt = SlCompatTableHeader(_map_desc, _map_sl_compat);
47
49 SlGlobList(slt);
51
52 Map::Allocate(_map_dim_x, _map_dim_y);
53 }
54
55 void LoadCheck(size_t) const override
56 {
57 const std::vector<SaveLoad> slt = SlCompatTableHeader(_map_desc, _map_sl_compat);
58
60 SlGlobList(slt);
62
63 _load_check_data.map_size_x = _map_dim_x;
64 _load_check_data.map_size_y = _map_dim_y;
65 }
66};
67
68static constexpr uint MAP_SL_BUF_SIZE = MIN_MAP_SIZE * MIN_MAP_SIZE;
69
70struct MAPTChunkHandler : ChunkHandler {
71 MAPTChunkHandler() : ChunkHandler("MAPT", ChunkType::Riff) {}
72
73 void Load() const override
74 {
75 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
76 uint size = Map::Size();
77
78 for (TileIndex i{}; i != size;) {
80 for (auto b : buf) Tile(i++).type() = b;
81 }
82 }
83
84 void Save() const override
85 {
86 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
87 uint size = Map::Size();
88
89 SlSetLength(size);
90 for (TileIndex i{}; i != size;) {
91 for (auto &b : buf) b = Tile(i++).type();
93 }
94 }
95};
96
97struct MAPHChunkHandler : ChunkHandler {
98 MAPHChunkHandler() : ChunkHandler("MAPH", ChunkType::Riff) {}
99
100 void Load() const override
101 {
102 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
103 uint size = Map::Size();
104
105 for (TileIndex i{}; i != size;) {
107 for (auto b : buf) Tile(i++).height() = b;
108 }
109 }
110
111 void Save() const override
112 {
113 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
114 uint size = Map::Size();
115
116 SlSetLength(size);
117 for (TileIndex i{}; i != size;) {
118 for (auto &b : buf) b = Tile(i++).height();
120 }
121 }
122};
123
124struct MAPOChunkHandler : ChunkHandler {
125 MAPOChunkHandler() : ChunkHandler("MAPO", ChunkType::Riff) {}
126
127 void Load() const override
128 {
129 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
130 uint size = Map::Size();
131
132 for (TileIndex i{}; i != size;) {
134 for (auto b : buf) Tile(i++).m1() = b;
135 }
136 }
137
138 void Save() const override
139 {
140 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
141 uint size = Map::Size();
142
143 SlSetLength(size);
144 for (TileIndex i{}; i != size;) {
145 for (auto &b : buf) b = Tile(i++).m1();
147 }
148 }
149};
150
151struct MAP2ChunkHandler : ChunkHandler {
152 MAP2ChunkHandler() : ChunkHandler("MAP2", ChunkType::Riff) {}
153
154 void Load() const override
155 {
156 std::array<uint16_t, MAP_SL_BUF_SIZE> buf;
157 uint size = Map::Size();
158
159 for (TileIndex i{}; i != size;) {
160 SlCopy(buf.data(), MAP_SL_BUF_SIZE,
161 /* In those versions the m2 was 8 bits */
163 );
164 for (auto b : buf) Tile(i++).m2() = b;
165 }
166 }
167
168 void Save() const override
169 {
170 std::array<uint16_t, MAP_SL_BUF_SIZE> buf;
171 uint size = Map::Size();
172
173 SlSetLength(static_cast<uint32_t>(size) * sizeof(uint16_t));
174 for (TileIndex i{}; i != size;) {
175 for (auto &b : buf) b = Tile(i++).m2();
177 }
178 }
179};
180
181struct M3LOChunkHandler : ChunkHandler {
182 M3LOChunkHandler() : ChunkHandler("M3LO", ChunkType::Riff) {}
183
184 void Load() const override
185 {
186 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
187 uint size = Map::Size();
188
189 for (TileIndex i{}; i != size;) {
191 for (auto b : buf) Tile(i++).m3() = b;
192 }
193 }
194
195 void Save() const override
196 {
197 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
198 uint size = Map::Size();
199
200 SlSetLength(size);
201 for (TileIndex i{}; i != size;) {
202 for (auto &b : buf) b = Tile(i++).m3();
204 }
205 }
206};
207
208struct M3HIChunkHandler : ChunkHandler {
209 M3HIChunkHandler() : ChunkHandler("M3HI", ChunkType::Riff) {}
210
211 void Load() const override
212 {
213 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
214 uint size = Map::Size();
215
216 for (TileIndex i{}; i != size;) {
218 for (auto b : buf) Tile(i++).m4() = b;
219 }
220 }
221
222 void Save() const override
223 {
224 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
225 uint size = Map::Size();
226
227 SlSetLength(size);
228 for (TileIndex i{}; i != size;) {
229 for (auto &b : buf) b = Tile(i++).m4();
231 }
232 }
233};
234
235struct MAP5ChunkHandler : ChunkHandler {
236 MAP5ChunkHandler() : ChunkHandler("MAP5", ChunkType::Riff) {}
237
238 void Load() const override
239 {
240 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
241 uint size = Map::Size();
242
243 for (TileIndex i{}; i != size;) {
245 for (auto b : buf) Tile(i++).m5() = b;
246 }
247 }
248
249 void Save() const override
250 {
251 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
252 uint size = Map::Size();
253
254 SlSetLength(size);
255 for (TileIndex i{}; i != size;) {
256 for (auto &b : buf) b = Tile(i++).m5();
258 }
259 }
260};
261
262struct MAPEChunkHandler : ChunkHandler {
263 MAPEChunkHandler() : ChunkHandler("MAPE", ChunkType::Riff) {}
264
265 void Load() const override
266 {
267 uint size = Map::Size();
268
270 /* Since this loads 4 tiles per read byte, amend the buffer size to suit. */
271 std::array<uint8_t, MAP_SL_BUF_SIZE / 4> buf;
272 for (TileIndex i{}; i != size;) {
274 for (auto b : buf) {
275 Tile(i++).m6() = GB(b, 0, 2);
276 Tile(i++).m6() = GB(b, 2, 2);
277 Tile(i++).m6() = GB(b, 4, 2);
278 Tile(i++).m6() = GB(b, 6, 2);
279 }
280 }
281 } else {
282 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
283 for (TileIndex i{}; i != size;) {
285 for (auto b : buf) Tile(i++).m6() = b;
286 }
287 }
288 }
289
290 void Save() const override
291 {
292 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
293 uint size = Map::Size();
294
295 SlSetLength(size);
296 for (TileIndex i{}; i != size;) {
297 for (auto &b : buf) b = Tile(i++).m6();
299 }
300 }
301};
302
303struct MAP7ChunkHandler : ChunkHandler {
304 MAP7ChunkHandler() : ChunkHandler("MAP7", ChunkType::Riff) {}
305
306 void Load() const override
307 {
308 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
309 uint size = Map::Size();
310
311 for (TileIndex i{}; i != size;) {
313 for (auto b : buf) Tile(i++).m7() = b;
314 }
315 }
316
317 void Save() const override
318 {
319 std::array<uint8_t, MAP_SL_BUF_SIZE> buf;
320 uint size = Map::Size();
321
322 SlSetLength(size);
323 for (TileIndex i{}; i != size;) {
324 for (auto &b : buf) b = Tile(i++).m7();
326 }
327 }
328};
329
330struct MAP8ChunkHandler : ChunkHandler {
331 MAP8ChunkHandler() : ChunkHandler("MAP8", ChunkType::Riff) {}
332
333 void Load() const override
334 {
335 std::array<uint16_t, MAP_SL_BUF_SIZE> buf;
336 uint size = Map::Size();
337
338 for (TileIndex i{}; i != size;) {
340 for (auto b : buf) Tile(i++).m8() = b;
341 }
342 }
343
344 void Save() const override
345 {
346 std::array<uint16_t, MAP_SL_BUF_SIZE> buf;
347 uint size = Map::Size();
348
349 SlSetLength(static_cast<uint32_t>(size) * sizeof(uint16_t));
350 for (TileIndex i{}; i != size;) {
351 for (auto &b : buf) b = Tile(i++).m8();
353 }
354 }
355};
356
357static const MAPSChunkHandler MAPS;
358static const MAPTChunkHandler MAPT;
359static const MAPHChunkHandler MAPH;
360static const MAPOChunkHandler MAPO;
361static const MAP2ChunkHandler MAP2;
362static const M3LOChunkHandler M3LO;
363static const M3HIChunkHandler M3HI;
364static const MAP5ChunkHandler MAP5;
365static const MAPEChunkHandler MAPE;
366static const MAP7ChunkHandler MAP7;
367static const MAP8ChunkHandler MAP8;
368static const ChunkHandlerRef map_chunk_handlers[] = {
369 MAPS,
370 MAPT,
371 MAPH,
372 MAPO,
373 MAP2,
374 M3LO,
375 M3HI,
376 MAP5,
377 MAPE,
378 MAP7,
379 MAP8,
380};
381
382extern const ChunkHandlerTable _map_chunk_handlers(map_chunk_handlers);
Functions related to bit mathematics.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Declarations for savegames operations.
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:42
@ Tile
Destination is a tile.
Definition goal_type.h:53
Functions related to maps.
static constexpr uint MAP_SL_BUF_SIZE
Buffer size for saving/loading the map array. Sized to the smallest map.
Definition map_sl.cpp:68
Loading for map chunks before table headers were added.
const SaveLoadCompat _map_sl_compat[]
Original field order for _map_desc.
static const uint MIN_MAP_SIZE
Minimal map size = 64.
Definition map_type.h:39
A number of safeguards to prevent using unsafe methods.
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:748
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SaveLoadType::Variables to/from a savegame.
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:372
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition saveload.cpp:802
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Functions/types related to saving and loading games.
@ U16
A 16 bit unsigned int.
Definition saveload.h:662
@ U8
A 8 bit unsigned int.
Definition saveload.h:642
#define SLE_GLOBAL_ADDRESS(variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a global...
Definition saveload.h:760
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1225
@ BigMap
Saveload version: 5.0, SVN revision: 1429 Making maps a different size than 256x256.
Definition saveload.h:49
@ MultipleRoadStops
Saveload version: 6.0, SVN revision: 1721 Multi tile road stops, and some map size related fixes.
Definition saveload.h:52
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:337
@ BridgeWormhole
Saveload version: 42, SVN revision: 7573 Bridges become wormholes, so more things can be built under...
Definition saveload.h:99
@ Table
An Array with a header describing the elements.
Definition saveload.h:449
@ Riff
4 bits store the chunk type, 28 bits the number of bytes.
Definition saveload.h:446
Functions/types related to errors from savegames.
Definition of base types and functions in a cross-platform compatible way.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
void Load() const override
Load the chunk.
Definition map_sl.cpp:211
void Save() const override
Save the chunk.
Definition map_sl.cpp:222
void Load() const override
Load the chunk.
Definition map_sl.cpp:184
void Save() const override
Save the chunk.
Definition map_sl.cpp:195
void Save() const override
Save the chunk.
Definition map_sl.cpp:168
void Load() const override
Load the chunk.
Definition map_sl.cpp:154
void Save() const override
Save the chunk.
Definition map_sl.cpp:249
void Load() const override
Load the chunk.
Definition map_sl.cpp:238
void Save() const override
Save the chunk.
Definition map_sl.cpp:317
void Load() const override
Load the chunk.
Definition map_sl.cpp:306
void Save() const override
Save the chunk.
Definition map_sl.cpp:344
void Load() const override
Load the chunk.
Definition map_sl.cpp:333
void Load() const override
Load the chunk.
Definition map_sl.cpp:265
void Save() const override
Save the chunk.
Definition map_sl.cpp:290
void Load() const override
Load the chunk.
Definition map_sl.cpp:100
void Save() const override
Save the chunk.
Definition map_sl.cpp:111
void Save() const override
Save the chunk.
Definition map_sl.cpp:138
void Load() const override
Load the chunk.
Definition map_sl.cpp:127
void Save() const override
Save the chunk.
Definition map_sl.cpp:33
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition map_sl.cpp:55
void Load() const override
Load the chunk.
Definition map_sl.cpp:44
void Load() const override
Load the chunk.
Definition map_sl.cpp:73
void Save() const override
Save the chunk.
Definition map_sl.cpp:84
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:262
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:271
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:36
static uint Size()
Get the size of the map.
Definition map_func.h:280
SaveLoad type struct.
Definition saveload.h:763
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:925
Container of a variable's characteristics about a variable's storage.
Definition saveload.h:674
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:717
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92