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  */
7 
8 /** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
9 
10 #include "../stdafx.h"
11 
12 #include "saveload.h"
13 #include "compat/labelmaps_sl_compat.h"
14 
15 #include "saveload_internal.h"
16 #include "../station_map.h"
17 #include "../tunnelbridge_map.h"
18 
19 #include "../safeguards.h"
20 
21 static std::vector<RailTypeLabel> _railtype_list;
22 
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.
27  */
NeedRailTypeConversion()28 static bool NeedRailTypeConversion()
29 {
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;
36 		}
37 	}
38 
39 	/* No rail type conversion is necessary */
40 	return false;
41 }
42 
AfterLoadLabelMaps()43 void AfterLoadLabelMaps()
44 {
45 	if (NeedRailTypeConversion()) {
46 		std::vector<RailType> railtype_conversion_map;
47 
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;
51 
52 			railtype_conversion_map.push_back(r);
53 		}
54 
55 		for (TileIndex t = 0; t < MapSize(); t++) {
56 			switch (GetTileType(t)) {
57 				case MP_RAILWAY:
58 					SetRailType(t, railtype_conversion_map[GetRailType(t)]);
59 					break;
60 
61 				case MP_ROAD:
62 					if (IsLevelCrossing(t)) {
63 						SetRailType(t, railtype_conversion_map[GetRailType(t)]);
64 					}
65 					break;
66 
67 				case MP_STATION:
68 					if (HasStationRail(t)) {
69 						SetRailType(t, railtype_conversion_map[GetRailType(t)]);
70 					}
71 					break;
72 
73 				case MP_TUNNELBRIDGE:
74 					if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
75 						SetRailType(t, railtype_conversion_map[GetRailType(t)]);
76 					}
77 					break;
78 
79 				default:
80 					break;
81 			}
82 		}
83 	}
84 
85 	ResetLabelMaps();
86 }
87 
ResetLabelMaps()88 void ResetLabelMaps()
89 {
90 	_railtype_list.clear();
91 }
92 
93 /** Container for a label for SaveLoad system */
94 struct LabelObject {
95 	uint32 label;
96 };
97 
98 static const SaveLoad _label_object_desc[] = {
99 	SLE_VAR(LabelObject, label, SLE_UINT32),
100 };
101 
102 struct RAILChunkHandler : ChunkHandler {
RAILChunkHandlerRAILChunkHandler103 	RAILChunkHandler() : ChunkHandler('RAIL', CH_TABLE) {}
104 
SaveRAILChunkHandler105 	void Save() const override
106 	{
107 		SlTableHeader(_label_object_desc);
108 
109 		LabelObject lo;
110 
111 		for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
112 			lo.label = GetRailTypeInfo(r)->label;
113 
114 			SlSetArrayIndex(r);
115 			SlObject(&lo, _label_object_desc);
116 		}
117 	}
118 
LoadRAILChunkHandler119 	void Load() const override
120 	{
121 		const std::vector<SaveLoad> slt = SlCompatTableHeader(_label_object_desc, _label_object_sl_compat);
122 
123 		ResetLabelMaps();
124 
125 		LabelObject lo;
126 
127 		while (SlIterateArray() != -1) {
128 			SlObject(&lo, slt);
129 			_railtype_list.push_back((RailTypeLabel)lo.label);
130 		}
131 	}
132 };
133 
134 static const RAILChunkHandler RAIL;
135 static const ChunkHandlerRef labelmaps_chunk_handlers[] = {
136 	RAIL,
137 };
138 
139 extern const ChunkHandlerTable _labelmaps_chunk_handlers(labelmaps_chunk_handlers);
140 
141