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"
13 #include "compat/labelmaps_sl_compat.h"
15 #include "saveload_internal.h"
16 #include "../station_map.h"
17 #include "../tunnelbridge_map.h"
19 #include "../safeguards.h"
21 static std::vector
<RailTypeLabel
> _railtype_list
;
24 * Test if any saved rail type labels are different to the currently loaded
25 * rail types, which therefore requires conversion.
26 * @return true if (and only if) conversion due to rail type changes is needed.
28 static bool NeedRailTypeConversion()
30 for (uint i
= 0; i
< _railtype_list
.size(); i
++) {
31 if ((RailType
)i
< RAILTYPE_END
) {
32 const RailTypeInfo
*rti
= GetRailTypeInfo((RailType
)i
);
33 if (rti
->label
!= _railtype_list
[i
]) return true;
35 if (_railtype_list
[i
] != 0) return true;
39 /* No rail type conversion is necessary */
43 void AfterLoadLabelMaps()
45 if (NeedRailTypeConversion()) {
46 std::vector
<RailType
> railtype_conversion_map
;
48 for (uint i
= 0; i
< _railtype_list
.size(); i
++) {
49 RailType r
= GetRailTypeByLabel(_railtype_list
[i
]);
50 if (r
== INVALID_RAILTYPE
) r
= RAILTYPE_BEGIN
;
52 railtype_conversion_map
.push_back(r
);
55 for (TileIndex t
= 0; t
< Map::Size(); t
++) {
56 switch (GetTileType(t
)) {
58 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
62 if (IsLevelCrossing(t
)) {
63 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
68 if (HasStationRail(t
)) {
69 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
74 if (GetTunnelBridgeTransportType(t
) == TRANSPORT_RAIL
) {
75 SetRailType(t
, railtype_conversion_map
[GetRailType(t
)]);
90 _railtype_list
.clear();
93 /** Container for a label for SaveLoad system */
98 static const SaveLoad _label_object_desc
[] = {
99 SLE_VAR(LabelObject
, label
, SLE_UINT32
),
102 struct RAILChunkHandler
: ChunkHandler
{
103 RAILChunkHandler() : ChunkHandler('RAIL', CH_TABLE
) {}
105 void Save() const override
107 SlTableHeader(_label_object_desc
);
111 for (RailType r
= RAILTYPE_BEGIN
; r
!= RAILTYPE_END
; r
++) {
112 lo
.label
= GetRailTypeInfo(r
)->label
;
115 SlObject(&lo
, _label_object_desc
);
119 void Load() const override
121 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_label_object_desc
, _label_object_sl_compat
);
127 while (SlIterateArray() != -1) {
129 _railtype_list
.push_back((RailTypeLabel
)lo
.label
);
134 static const RAILChunkHandler RAIL
;
135 static const ChunkHandlerRef labelmaps_chunk_handlers
[] = {
139 extern const ChunkHandlerTable
_labelmaps_chunk_handlers(labelmaps_chunk_handlers
);