Fix #7925: Reset temporary saveload data at the start of loading a savegame instead...
[openttd-github.git] / src / saveload / labelmaps_sl.cpp
blob542b85b9f146c460dc13641ab134b9fc913224f6
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"
11 #include "../station_map.h"
12 #include "../tunnelbridge_map.h"
14 #include "saveload.h"
15 #include "saveload_internal.h"
17 #include "../safeguards.h"
19 static std::vector<RailTypeLabel> _railtype_list;
21 /**
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;
32 } else {
33 if (_railtype_list[i] != 0) return true;
37 /* No rail type conversion is necessary */
38 return false;
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)) {
55 case MP_RAILWAY:
56 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
57 break;
59 case MP_ROAD:
60 if (IsLevelCrossing(t)) {
61 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
63 break;
65 case MP_STATION:
66 if (HasStationRail(t)) {
67 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
69 break;
71 case MP_TUNNELBRIDGE:
72 if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
73 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
75 break;
77 default:
78 break;
83 ResetLabelMaps();
86 void ResetLabelMaps()
88 _railtype_list.clear();
91 /** Container for a label for SaveLoad system */
92 struct LabelObject {
93 uint32 label;
96 static const SaveLoad _label_object_desc[] = {
97 SLE_VAR(LabelObject, label, SLE_UINT32),
98 SLE_END(),
101 static void Save_RAIL()
103 LabelObject lo;
105 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
106 lo.label = GetRailTypeInfo(r)->label;
108 SlSetArrayIndex(r);
109 SlObject(&lo, _label_object_desc);
113 static void Load_RAIL()
115 ResetLabelMaps();
117 LabelObject lo;
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},