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