OpenTTD
labelmaps_sl.cpp
Go to the documentation of this file.
1 /* $Id: labelmaps_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
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  */
9 
12 #include "../stdafx.h"
13 #include "../station_map.h"
14 #include "../tunnelbridge_map.h"
15 
16 #include "saveload.h"
17 
18 #include "../safeguards.h"
19 
20 static SmallVector<RailTypeLabel, RAILTYPE_END> _railtype_list;
21 
28 {
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;
35  }
36  }
37 
38  /* No rail type conversion is necessary */
39  return false;
40 }
41 
42 void AfterLoadLabelMaps()
43 {
44  if (NeedRailTypeConversion()) {
45  SmallVector<RailType, RAILTYPE_END> railtype_conversion_map;
46 
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;
50 
51  *railtype_conversion_map.Append() = r;
52  }
53 
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;
59 
60  case MP_ROAD:
61  if (IsLevelCrossing(t)) {
62  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
63  }
64  break;
65 
66  case MP_STATION:
67  if (HasStationRail(t)) {
68  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
69  }
70  break;
71 
72  case MP_TUNNELBRIDGE:
74  SetRailType(t, railtype_conversion_map[GetRailType(t)]);
75  }
76  break;
77 
78  default:
79  break;
80  }
81  }
82  }
83 
84  _railtype_list.Clear();
85 }
86 
88 struct LabelObject {
89  uint32 label;
90 };
91 
92 static const SaveLoad _label_object_desc[] = {
93  SLE_VAR(LabelObject, label, SLE_UINT32),
94  SLE_END(),
95 };
96 
97 static void Save_RAIL()
98 {
99  LabelObject lo;
100 
101  for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
102  lo.label = GetRailTypeInfo(r)->label;
103 
104  SlSetArrayIndex(r);
105  SlObject(&lo, _label_object_desc);
106  }
107 }
108 
109 static void Load_RAIL()
110 {
111  _railtype_list.Clear();
112 
113  LabelObject lo;
114 
115  while (SlIterateArray() != -1) {
116  SlObject(&lo, _label_object_desc);
117  *_railtype_list.Append() = (RailTypeLabel)lo.label;
118  }
119 }
120 
121 extern const ChunkHandler _labelmaps_chunk_handlers[] = {
122  { 'RAIL', Save_RAIL, Load_RAIL, NULL, NULL, CH_ARRAY | CH_LAST},
123 };
124