OpenTTD Source  1.10.0-RC1
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 
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:357
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:653
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:498
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:596
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
Declaration of functions used in more save/load files.
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:392
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:125