OpenTTD Source  20241121-master-g67a0fccfad
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"
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 
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 
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);
ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE)
The override manager for our objects.
Code handling saving and loading of NewGRF mappings.
Loading of object chunks before table headers were added.
const SaveLoadCompat _object_sl_compat[]
Original field order for _object_desc.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1702
Functions/types related to saving and loading games.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:507
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:1004
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:863
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1239
@ SLV_186
186 25833 Objects storage
Definition: saveload.h:266
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:397
@ SLV_155
155 21453
Definition: saveload.h:229
@ SLV_148
148 20659
Definition: saveload.h:220
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:602
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:987
Handlers and description of chunk.
Definition: saveload.h:461
void FixPointers() const override
Fix the pointers.
Definition: object_sl.cpp:57
void Load() const override
Load the chunk.
Definition: object_sl.cpp:46
void Save() const override
Save the chunk.
Definition: object_sl.cpp:35
An object, such as transmitter, on the map.
Definition: object_base.h:23
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
SaveLoad type struct.
Definition: saveload.h:713
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58