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/>.
8 /** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
10 #include "../stdafx.h"
11 #include "../station_map.h"
12 #include "../tunnelbridge_map.h"
15 #include "saveload_internal.h"
17 #include "../safeguards.h"
19 static std::vector
<RailTypeLabel
> _railtype_list
;
22 * Test if any saved rail type labels are different to the currently loaded
23 * rail types, which therefore requires conversion.
24 * @return true if (and only if) conversion due to rail type changes is needed.
26 static bool NeedRailTypeConversion()
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;
33 if (_railtype_list
[i
] != 0) return true;
37 /* No rail type conversion is necessary */
41 void AfterLoadLabelMaps()
43 if (NeedRailTypeConversion()) {
44 std::vector
<RailType
> railtype_conversion_map
;
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
;
50 railtype_conversion_map
.push_back(r
);
53 for (TileIndex t
= 0; t
< MapSize(); t
++) {
54 switch (GetTileType(t
)) {
56 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
60 if (IsLevelCrossing(t
)) {
61 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
66 if (HasStationRail(t
)) {
67 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
72 if (GetTunnelBridgeTransportType(t
) == TRANSPORT_RAIL
) {
73 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
88 _railtype_list
.clear();
91 /** Container for a label for SaveLoad system */
96 static const SaveLoad _label_object_desc
[] = {
97 SLE_VAR(LabelObject
, label
, SLE_UINT32
),
101 static void Save_RAIL()
105 for (RailType r
= RAILTYPE_BEGIN
; r
!= RAILTYPE_END
; r
++) {
106 lo
.label
= GetRailTypeInfo(r
)->label
;
109 SlObject(&lo
, _label_object_desc
);
113 static void Load_RAIL()
119 while (SlIterateArray() != -1) {
120 SlObject(&lo
, _label_object_desc
);
121 _railtype_list
.push_back((RailTypeLabel
)lo
.label
);
125 extern const ChunkHandler _labelmaps_chunk_handlers
[] = {
126 { 'RAIL', Save_RAIL
, Load_RAIL
, nullptr, nullptr, CH_ARRAY
| CH_LAST
},