Fix 908ee729: Inverted condition prevented actually writing data to files. (#12941)
[openttd-github.git] / src / saveload / labelmaps_sl.cpp
blobb9e77ad5787c4d0e2e235dbdf2af7bc507e4aeab
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 */
8 /** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
10 #include "../stdafx.h"
12 #include "saveload.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;
23 /**
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;
34 } else {
35 if (_railtype_list[i] != 0) return true;
39 /* No rail type conversion is necessary */
40 return false;
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)) {
57 case MP_RAILWAY:
58 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
59 break;
61 case MP_ROAD:
62 if (IsLevelCrossing(t)) {
63 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
65 break;
67 case MP_STATION:
68 if (HasStationRail(t)) {
69 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
71 break;
73 case MP_TUNNELBRIDGE:
74 if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
75 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
77 break;
79 default:
80 break;
85 ResetLabelMaps();
88 void ResetLabelMaps()
90 _railtype_list.clear();
93 /** Container for a label for SaveLoad system */
94 struct LabelObject {
95 uint32_t label;
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);
109 LabelObject lo;
111 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
112 lo.label = GetRailTypeInfo(r)->label;
114 SlSetArrayIndex(r);
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);
123 ResetLabelMaps();
125 LabelObject lo;
127 while (SlIterateArray() != -1) {
128 SlObject(&lo, slt);
129 _railtype_list.push_back((RailTypeLabel)lo.label);
134 static const RAILChunkHandler RAIL;
135 static const ChunkHandlerRef labelmaps_chunk_handlers[] = {
136 RAIL,
139 extern const ChunkHandlerTable _labelmaps_chunk_handlers(labelmaps_chunk_handlers);