OpenTTD Source  1.11.0-beta2
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 #include "../station_map.h"
12 #include "../tunnelbridge_map.h"
13 
14 #include "saveload.h"
15 #include "saveload_internal.h"
16 
17 #include "../safeguards.h"
18 
19 static std::vector<RailTypeLabel> _railtype_list;
20 
27 {
28  for (uint i = 0; i < _railtype_list.size(); i++) {
29  if ((RailType)i < RAILTYPE_END) {
30  const RailtypeInfo *rti = GetRailTypeInfo((RailType)i);
31  if (rti->label != _railtype_list[i]) return true;
32  } else {
33  if (_railtype_list[i] != 0) return true;
34  }
35  }
36 
37  /* No rail type conversion is necessary */
38  return false;
39 }
40 
41 void AfterLoadLabelMaps()
42 {
43  if (NeedRailTypeConversion()) {
44  std::vector<RailType> railtype_conversion_map;
45 
46  for (uint i = 0; i < _railtype_list.size(); i++) {
47  RailType r = GetRailTypeByLabel(_railtype_list[i]);
48  if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
49 
50  railtype_conversion_map.push_back(r);
51  }
52 
53  for (TileIndex t = 0; t < MapSize(); t++) {
54  switch (GetTileType(t)) {
55  case MP_RAILWAY:
56  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
57  break;
58 
59  case MP_ROAD:
60  if (IsLevelCrossing(t)) {
61  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
62  }
63  break;
64 
65  case MP_STATION:
66  if (HasStationRail(t)) {
67  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
68  }
69  break;
70 
71  case MP_TUNNELBRIDGE:
73  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
74  }
75  break;
76 
77  default:
78  break;
79  }
80  }
81  }
82 
83  ResetLabelMaps();
84 }
85 
86 void ResetLabelMaps()
87 {
88  _railtype_list.clear();
89 }
90 
92 struct LabelObject {
93  uint32 label;
94 };
95 
96 static const SaveLoad _label_object_desc[] = {
97  SLE_VAR(LabelObject, label, SLE_UINT32),
98  SLE_END(),
99 };
100 
101 static void Save_RAIL()
102 {
103  LabelObject lo;
104 
105  for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
106  lo.label = GetRailTypeInfo(r)->label;
107 
108  SlSetArrayIndex(r);
109  SlObject(&lo, _label_object_desc);
110  }
111 }
112 
113 static void Load_RAIL()
114 {
115  ResetLabelMaps();
116 
117  LabelObject lo;
118 
119  while (SlIterateArray() != -1) {
120  SlObject(&lo, _label_object_desc);
121  _railtype_list.push_back((RailTypeLabel)lo.label);
122  }
123 }
124 
125 extern const ChunkHandler _labelmaps_chunk_handlers[] = {
126  { 'RAIL', Save_RAIL, Load_RAIL, nullptr, nullptr, CH_ARRAY | CH_LAST},
127 };
128 
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:27
RailtypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:124
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:42
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
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:43
IsLevelCrossing
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:84
GetRailTypeInfo
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:304
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
HasStationRail
static bool HasStationRail(TileIndex 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
LabelObject
Container for a label for SaveLoad system.
Definition: labelmaps_sl.cpp:92
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:50
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
NeedRailTypeConversion
static bool NeedRailTypeConversion()
Test if any saved rail type labels are different to the currently loaded rail types,...
Definition: labelmaps_sl.cpp:26
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition: rail_type.h:33
GetRailType
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:46
RailtypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:233
saveload_internal.h
GetRailTypeByLabel
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition: rail.cpp:311
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
GetTunnelBridgeTransportType
static TransportType GetTunnelBridgeTransportType(TileIndex 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
SetRailType
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:125
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
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