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

OpenTTD Source 13.1

object_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/object_sl_compat.h"
14
15 #include "../object_base.h"
16 #include "../object_map.h"
17 #include "newgrf_sl.h"
18
19 #include "../safeguards.h"
20
21 static const SaveLoad _object_desc[] = {
22 SLE_VAR(Object, location.tile, SLE_UINT32),
23 SLE_VAR(Object, location.w, SLE_FILE_U8 | SLE_VAR_U16),
24 SLE_VAR(Object, location.h, SLE_FILE_U8 | SLE_VAR_U16),
25 SLE_REF(Object, town, REF_TOWN),
26 SLE_VAR(Object, build_date, SLE_UINT32),
27 SLE_CONDVAR(Object, colour, SLE_UINT8, SLV_148, SL_MAX_VERSION),
28 SLE_CONDVAR(Object, view, SLE_UINT8, SLV_155, SL_MAX_VERSION),
29 SLE_CONDVAR(Object, type, SLE_UINT16, SLV_186, SL_MAX_VERSION),
30 };
31
32 struct OBJSChunkHandler : ChunkHandler {
33 OBJSChunkHandler() : ChunkHandler('OBJS', CH_TABLE) {}
34
35 void Save() const override
36 {
37 SlTableHeader(_object_desc);
38
39 /* Write the objects */
40 for (Object *o : Object::Iterate()) {
41 SlSetArrayIndex(o->index);
42 SlObject(o, _object_desc);
43 }
44 }
45
46 void Load() const override
47 {
48 const std::vector<SaveLoad> slt = SlCompatTableHeader(_object_desc, _object_sl_compat);
49
50 int index;
51 while ((index = SlIterateArray()) != -1) {
52 Object *o = new (index) Object();
53 SlObject(o, slt);
54 }
55 }
56
57 void FixPointers() const override
58 {
59 for (Object *o : Object::Iterate()) {
60 SlObject(o, _object_desc);
61 if (IsSavegameVersionBefore(SLV_148) && !IsTileType(o->location.tile, MP_OBJECT)) {
62 /* Due to a small bug stale objects could remain. */
63 delete o;
64 }
65 }
66 }
67 };
68
69 struct OBIDChunkHandler : NewGRFMappingChunkHandler {
70 OBIDChunkHandler() : NewGRFMappingChunkHandler('OBID', _object_mngr) {}
71 };
72
73 static const OBIDChunkHandler OBID;
74 static const OBJSChunkHandler OBJS;
75 static const ChunkHandlerRef object_chunk_handlers[] = {
76 OBID,
77 OBJS,
78 };
79
80 extern const ChunkHandlerTable _object_chunk_handlers(object_chunk_handlers);
SLV_186
@ SLV_186
186 25833 Objects storage
Definition: saveload.h:270
OBJSChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: object_sl.cpp:57
OBJSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: object_sl.cpp:35
NewGRFMappingChunkHandler
Definition: newgrf_sl.h:15
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:544
SLV_155
@ SLV_155
155 21453
Definition: saveload.h:233
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:449
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:708
object_sl_compat.h
saveload.h
OBJSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: object_sl.cpp:46
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:412
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:786
_object_sl_compat
const SaveLoadCompat _object_sl_compat[]
Original field order for _object_desc.
Definition: object_sl_compat.h:16
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
Object
An object, such as transmitter, on the map.
Definition: object_base.h:23
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1030
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:348
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:778
Pool::PoolItem<&_object_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
SLV_148
@ SLV_148
148 20659
Definition: saveload.h:224
OBJSChunkHandler
Definition: object_sl.cpp:32
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2030
_object_mngr
ObjectOverrideManager _object_mngr
The override manager for our objects.
OBIDChunkHandler
Definition: object_sl.cpp:69
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
newgrf_sl.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:671