OpenTTD

labelmaps_sl.cpp
Go to the documentation of this file.
1 /* $Id: labelmaps_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../station_map.h"
14 #include "../tunnelbridge_map.h"
15
16 #include "saveload.h"
17
18 #include "../safeguards.h"
19
20 static SmallVector<RailTypeLabel, RAILTYPE_END> _railtype_list;
21
27 static bool NeedRailTypeConversion()
28 {
29 for (uint i = 0; i < _railtype_list.Length(); i++) {
30 if ((RailType)i < RAILTYPE_END) {
31 const RailtypeInfo *rti = GetRailTypeInfo((RailType)i);
32 if (rti->label != _railtype_list[i]) return true;
33 } else {
34 if (_railtype_list[i] != 0) return true;
35 }
36 }
37
38 /* No rail type conversion is necessary */
39 return false;
40 }
41
42 void AfterLoadLabelMaps()
43 {
44 if (NeedRailTypeConversion()) {
45 SmallVector<RailType, RAILTYPE_END> railtype_conversion_map;
46
47 for (uint i = 0; i < _railtype_list.Length(); i++) {
48 RailType r = GetRailTypeByLabel(_railtype_list[i]);
49 if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
50
51 *railtype_conversion_map.Append() = r;
52 }
53
54 for (TileIndex t = 0; t < MapSize(); t++) {
55 switch (GetTileType(t)) {
56 case MP_RAILWAY:
57 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
58 break;
59
60 case MP_ROAD:
61 if (IsLevelCrossing(t)) {
62 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
63 }
64 break;
65
66 case MP_STATION:
67 if (HasStationRail(t)) {
68 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
69 }
70 break;
71
72 case MP_TUNNELBRIDGE:
73 if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
74 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
75 }
76 break;
77
78 default:
79 break;
80 }
81 }
82 }
83
84 _railtype_list.Clear();
85 }
86
88 struct LabelObject {
89 uint32 label;
90 };
91
92 static const SaveLoad _label_object_desc[] = {
93 SLE_VAR(LabelObject, label, SLE_UINT32),
94 SLE_END(),
95 };
96
97 static void Save_RAIL()
98 {
99 LabelObject lo;
100
101 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
102 lo.label = GetRailTypeInfo(r)->label;
103
104 SlSetArrayIndex(r);
105 SlObject(&lo, _label_object_desc);
106 }
107 }
108
109 static void Load_RAIL()
110 {
111 _railtype_list.Clear();
112
113 LabelObject lo;
114
115 while (SlIterateArray() != -1) {
116 SlObject(&lo, _label_object_desc);
117 *_railtype_list.Append() = (RailTypeLabel)lo.label;
118 }
119 }
120
121 extern const ChunkHandler _labelmaps_chunk_handlers[] = {
122 { 'RAIL', Save_RAIL, Load_RAIL, NULL, NULL, CH_ARRAY | CH_LAST},
123 };
124
RAILTYPE_END
Used for iterations.
Definition: rail_type.h:35
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:89
GetRailTypeInfo
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:296
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:41
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:45
SmallVector::Clear
void Clear()
Remove all items from the list.
Definition: smallvec_type.hpp:98
SmallVector
Simple vector template class.
Definition: smallvec_type.hpp:29
MP_RAILWAY
A railway.
Definition: tile_type.h:44
IsLevelCrossing
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:68
RailtypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:116
SmallVector::Append
T * Append(uint to_add=1)
Append an item and return it.
Definition: smallvec_type.hpp:134
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:136
saveload.h
Functions/types related to saving and loading games.
SmallVector::Length
uint Length() const
Get the number of items in the list.
Definition: smallvec_type.hpp:262
RailtypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:225
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:29
LabelObject
Container for a label for SaveLoad system.
Definition: labelmaps_sl.cpp:88
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
MP_STATION
A tile of a station.
Definition: tile_type.h:48
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
RAILTYPE_BEGIN
Used for iterations.
Definition: rail_type.h:30
INVALID_RAILTYPE
Flag for invalid railtype.
Definition: rail_type.h:36
GetRailTypeByLabel
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition: rail.cpp:285
NeedRailTypeConversion
static bool NeedRailTypeConversion()
Test if any saved rail type labels are different to the currently loaded rail types, which therefore requires conversion.
Definition: labelmaps_sl.cpp:27
GetRailType
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
CH_LAST
Last chunk in this array.
Definition: saveload.h:104
SetRailType
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:126