(svn r28004) -Update from Eints:
[openttd.git] / src / saveload / labelmaps_sl.cpp
blob3b898a3b87ef60b38af91284394c415b9e76835d
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
12 #include "../stdafx.h"
13 #include "../station_map.h"
14 #include "../tunnelbridge_map.h"
16 #include "saveload.h"
18 #include "../safeguards.h"
20 static SmallVector<RailTypeLabel, RAILTYPE_END> _railtype_list;
22 /**
23 * Test if any saved rail type labels are different to the currently loaded
24 * rail types, which therefore requires conversion.
25 * @return true if (and only if) conversion due to rail type changes is needed.
27 static bool NeedRailTypeConversion()
29 for (uint i = 0; i < _railtype_list.Length(); i++) {
30 if ((RailType)i < RAILTYPE_END) {
31 const RailtypeInfo *rti = GetRailTypeInfo((RailType)i);
32 if (rti->label != _railtype_list[i]) return true;
33 } else {
34 if (_railtype_list[i] != 0) return true;
38 /* No rail type conversion is necessary */
39 return false;
42 void AfterLoadLabelMaps()
44 if (NeedRailTypeConversion()) {
45 SmallVector<RailType, RAILTYPE_END> railtype_conversion_map;
47 for (uint i = 0; i < _railtype_list.Length(); i++) {
48 RailType r = GetRailTypeByLabel(_railtype_list[i]);
49 if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
51 *railtype_conversion_map.Append() = r;
54 for (TileIndex t = 0; t < MapSize(); t++) {
55 switch (GetTileType(t)) {
56 case MP_RAILWAY:
57 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
58 break;
60 case MP_ROAD:
61 if (IsLevelCrossing(t)) {
62 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
64 break;
66 case MP_STATION:
67 if (HasStationRail(t)) {
68 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
70 break;
72 case MP_TUNNELBRIDGE:
73 if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
74 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
76 break;
78 default:
79 break;
84 _railtype_list.Clear();
87 /** Container for a label for SaveLoad system */
88 struct LabelObject {
89 uint32 label;
92 static const SaveLoad _label_object_desc[] = {
93 SLE_VAR(LabelObject, label, SLE_UINT32),
94 SLE_END(),
97 static void Save_RAIL()
99 LabelObject lo;
101 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
102 lo.label = GetRailTypeInfo(r)->label;
104 SlSetArrayIndex(r);
105 SlObject(&lo, _label_object_desc);
109 static void Load_RAIL()
111 _railtype_list.Clear();
113 LabelObject lo;
115 while (SlIterateArray() != -1) {
116 SlObject(&lo, _label_object_desc);
117 *_railtype_list.Append() = (RailTypeLabel)lo.label;
121 extern const ChunkHandler _labelmaps_chunk_handlers[] = {
122 { 'RAIL', Save_RAIL, Load_RAIL, NULL, NULL, CH_ARRAY | CH_LAST},