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

OpenTTD Source 1.11.0-RC1

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 #include "../object_base.h"
12 #include "../object_map.h"
13
14 #include "saveload.h"
15 #include "newgrf_sl.h"
16
17 #include "../safeguards.h"
18
19 static const SaveLoad _object_desc[] = {
20 SLE_VAR(Object, location.tile, SLE_UINT32),
21 SLE_VAR(Object, location.w, SLE_FILE_U8 | SLE_VAR_U16),
22 SLE_VAR(Object, location.h, SLE_FILE_U8 | SLE_VAR_U16),
23 SLE_REF(Object, town, REF_TOWN),
24 SLE_VAR(Object, build_date, SLE_UINT32),
25 SLE_CONDVAR(Object, colour, SLE_UINT8, SLV_148, SL_MAX_VERSION),
26 SLE_CONDVAR(Object, view, SLE_UINT8, SLV_155, SL_MAX_VERSION),
27 SLE_CONDVAR(Object, type, SLE_UINT16, SLV_186, SL_MAX_VERSION),
28
29 SLE_END()
30 };
31
32 static void Save_OBJS()
33 {
34 /* Write the objects */
35 for (Object *o : Object::Iterate()) {
36 SlSetArrayIndex(o->index);
37 SlObject(o, _object_desc);
38 }
39 }
40
41 static void Load_OBJS()
42 {
43 int index;
44 while ((index = SlIterateArray()) != -1) {
45 Object *o = new (index) Object();
46 SlObject(o, _object_desc);
47 }
48 }
49
50 static void Ptrs_OBJS()
51 {
52 for (Object *o : Object::Iterate()) {
53 SlObject(o, _object_desc);
54 if (IsSavegameVersionBefore(SLV_148) && !IsTileType(o->location.tile, MP_OBJECT)) {
55 /* Due to a small bug stale objects could remain. */
56 delete o;
57 }
58 }
59 }
60
61 static void Save_OBID()
62 {
63 Save_NewGRFMapping(_object_mngr);
64 }
65
66 static void Load_OBID()
67 {
68 Load_NewGRFMapping(_object_mngr);
69 }
70
71 extern const ChunkHandler _object_chunk_handlers[] = {
72 { 'OBID', Save_OBID, Load_OBID, nullptr, nullptr, CH_ARRAY },
73 { 'OBJS', Save_OBJS, Load_OBJS, Ptrs_OBJS, nullptr, CH_ARRAY | CH_LAST},
74 };
SLV_186
@ SLV_186
186 25833 Objects storage
Definition: saveload.h:266
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:393
SLV_155
@ SLV_155
155 21453
Definition: saveload.h:229
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:629
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:51
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
Object
An object, such as transmitter, on the map.
Definition: object_base.h:23
Load_NewGRFMapping
void Load_NewGRFMapping(OverrideManagerBase &mapping)
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:42
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
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:328
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
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:378
SLV_148
@ SLV_148
148 20659
Definition: saveload.h:220
_object_mngr
ObjectOverrideManager _object_mngr
The override manager for our objects.
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
newgrf_sl.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
Save_NewGRFMapping
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:30