OpenTTD Source  20240919-master-gdf0233f4c2
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);
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:596
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:501
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:857
object_sl_compat.h
saveload.h
OBJSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: object_sl.cpp:46
SLV_155
@ SLV_155
155 21453
Definition: saveload.h:229
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:988
_object_sl_compat
const SaveLoadCompat _object_sl_compat[]
Original field order for _object_desc.
Definition: object_sl_compat.h:16
SLV_148
@ SLV_148
148 20659
Definition: saveload.h:220
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
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:391
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:971
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:388
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
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:1888
_object_mngr
ObjectOverrideManager _object_mngr
The override manager for our objects.
OBIDChunkHandler
Definition: object_sl.cpp:69
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1234
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
SaveLoad
SaveLoad type struct.
Definition: saveload.h:707
newgrf_sl.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
SLV_186
@ SLV_186
186 25833 Objects storage
Definition: saveload.h:266