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

OpenTTD Source 14.0-RC3

labelmaps_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/labelmaps_sl_compat.h"
14
15 #include "saveload_internal.h"
16 #include "../station_map.h"
17 #include "../tunnelbridge_map.h"
18
19 #include "../safeguards.h"
20
21 static std::vector<RailTypeLabel> _railtype_list;
22
28 static bool NeedRailTypeConversion()
29 {
30 for (uint i = 0; i < _railtype_list.size(); i++) {
31 if ((RailType)i < RAILTYPE_END) {
32 const RailTypeInfo *rti = GetRailTypeInfo((RailType)i);
33 if (rti->label != _railtype_list[i]) return true;
34 } else {
35 if (_railtype_list[i] != 0) return true;
36 }
37 }
38
39 /* No rail type conversion is necessary */
40 return false;
41 }
42
43 void AfterLoadLabelMaps()
44 {
45 if (NeedRailTypeConversion()) {
46 std::vector<RailType> railtype_conversion_map;
47
48 for (uint i = 0; i < _railtype_list.size(); i++) {
49 RailType r = GetRailTypeByLabel(_railtype_list[i]);
50 if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
51
52 railtype_conversion_map.push_back(r);
53 }
54
55 for (TileIndex t = 0; t < Map::Size(); t++) {
56 switch (GetTileType(t)) {
57 case MP_RAILWAY:
58 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
59 break;
60
61 case MP_ROAD:
62 if (IsLevelCrossing(t)) {
63 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
64 }
65 break;
66
67 case MP_STATION:
68 if (HasStationRail(t)) {
69 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
70 }
71 break;
72
73 case MP_TUNNELBRIDGE:
74 if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
75 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
76 }
77 break;
78
79 default:
80 break;
81 }
82 }
83 }
84
85 ResetLabelMaps();
86 }
87
88 void ResetLabelMaps()
89 {
90 _railtype_list.clear();
91 }
92
94 struct LabelObject {
95 uint32_t label;
96 };
97
98 static const SaveLoad _label_object_desc[] = {
99 SLE_VAR(LabelObject, label, SLE_UINT32),
100 };
101
102 struct RAILChunkHandler : ChunkHandler {
103 RAILChunkHandler() : ChunkHandler('RAIL', CH_TABLE) {}
104
105 void Save() const override
106 {
107 SlTableHeader(_label_object_desc);
108
109 LabelObject lo;
110
111 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
112 lo.label = GetRailTypeInfo(r)->label;
113
114 SlSetArrayIndex(r);
115 SlObject(&lo, _label_object_desc);
116 }
117 }
118
119 void Load() const override
120 {
121 const std::vector<SaveLoad> slt = SlCompatTableHeader(_label_object_desc, _label_object_sl_compat);
122
123 ResetLabelMaps();
124
125 LabelObject lo;
126
127 while (SlIterateArray() != -1) {
128 SlObject(&lo, slt);
129 _railtype_list.push_back((RailTypeLabel)lo.label);
130 }
131 }
132 };
133
134 static const RAILChunkHandler RAIL;
135 static const ChunkHandlerRef labelmaps_chunk_handlers[] = {
136 RAIL,
137 };
138
139 extern const ChunkHandlerTable _labelmaps_chunk_handlers(labelmaps_chunk_handlers);
140
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:307
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:491
RailTypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:127
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
saveload.h
IsLevelCrossing
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition: road_map.h:85
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:445
GetRailType
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
RAILChunkHandler
Definition: labelmaps_sl.cpp:102
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:50
_label_object_sl_compat
const SaveLoadCompat _label_object_sl_compat[]
Original field order for _label_object_desc.
Definition: labelmaps_sl_compat.h:16
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:27
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
LabelObject
Container for a label for SaveLoad system.
Definition: labelmaps_sl.cpp:94
RailTypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:236
RAILChunkHandler::Load
void Load() const override
Load the chunk.
Definition: labelmaps_sl.cpp:119
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
NeedRailTypeConversion
static bool NeedRailTypeConversion()
Test if any saved rail type labels are different to the currently loaded rail types,...
Definition: labelmaps_sl.cpp:28
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:952
Map::Size
static debug_inline uint Size()
Get the size of the map.
Definition: map_func.h:288
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition: rail_type.h:33
HasStationRail
bool HasStationRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
Definition: station_map.h:135
SetRailType
void SetRailType(Tile t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:125
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
GetTunnelBridgeTransportType
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Definition: tunnelbridge_map.h:39
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:494
labelmaps_sl_compat.h
saveload_internal.h
RAILChunkHandler::Save
void Save() const override
Save the chunk.
Definition: labelmaps_sl.cpp:105
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1852
GetRailTypeByLabel
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition: rail.cpp:311
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1661
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1714
SaveLoad
SaveLoad type struct.
Definition: saveload.h:697
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:656
INVALID_RAILTYPE
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition: rail_type.h:34
RAILTYPE_BEGIN
@ RAILTYPE_BEGIN
Used for iterations.
Definition: rail_type.h:28