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/>.
10 /** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
12 #include "../stdafx.h"
13 #include "../station_map.h"
14 #include "../tunnelbridge_map.h"
18 #include "../safeguards.h"
20 static SmallVector
<RailTypeLabel
, RAILTYPE_END
> _railtype_list
;
23 * Test if any saved rail type labels are different to the currently loaded
24 * rail types, which therefore requires conversion.
25 * @return true if (and only if) conversion due to rail type changes is needed.
27 static bool NeedRailTypeConversion()
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;
34 if (_railtype_list
[i
] != 0) return true;
38 /* No rail type conversion is necessary */
42 void AfterLoadLabelMaps()
44 if (NeedRailTypeConversion()) {
45 SmallVector
<RailType
, RAILTYPE_END
> railtype_conversion_map
;
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
;
51 *railtype_conversion_map
.Append() = r
;
54 for (TileIndex t
= 0; t
< MapSize(); t
++) {
55 switch (GetTileType(t
)) {
57 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
61 if (IsLevelCrossing(t
)) {
62 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
67 if (HasStationRail(t
)) {
68 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
73 if (GetTunnelBridgeTransportType(t
) == TRANSPORT_RAIL
) {
74 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
84 _railtype_list
.Clear();
87 /** Container for a label for SaveLoad system */
92 static const SaveLoad _label_object_desc
[] = {
93 SLE_VAR(LabelObject
, label
, SLE_UINT32
),
97 static void Save_RAIL()
101 for (RailType r
= RAILTYPE_BEGIN
; r
!= RAILTYPE_END
; r
++) {
102 lo
.label
= GetRailTypeInfo(r
)->label
;
105 SlObject(&lo
, _label_object_desc
);
109 static void Load_RAIL()
111 _railtype_list
.Clear();
115 while (SlIterateArray() != -1) {
116 SlObject(&lo
, _label_object_desc
);
117 *_railtype_list
.Append() = (RailTypeLabel
)lo
.label
;
121 extern const ChunkHandler _labelmaps_chunk_handlers
[] = {
122 { 'RAIL', Save_RAIL
, Load_RAIL
, NULL
, NULL
, CH_ARRAY
| CH_LAST
},