OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

map_sl.cpp
Go to the documentation of this file.
1 /* $Id: map_sl.cpp 26878 2014-09-21 11:23: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 "../map_func.h"
14 #include "../core/bitmath_func.hpp"
15 #include "../fios.h"
16
17 #include "saveload.h"
18
19 #include "../safeguards.h"
20
21 static uint32 _map_dim_x;
22 static uint32 _map_dim_y;
23
24 static const SaveLoadGlobVarList _map_dimensions[] = {
25 SLEG_CONDVAR(_map_dim_x, SLE_UINT32, 6, SL_MAX_VERSION),
26 SLEG_CONDVAR(_map_dim_y, SLE_UINT32, 6, SL_MAX_VERSION),
27 SLEG_END()
28 };
29
30 static void Save_MAPS()
31 {
32 _map_dim_x = MapSizeX();
33 _map_dim_y = MapSizeY();
34 SlGlobList(_map_dimensions);
35 }
36
37 static void Load_MAPS()
38 {
39 SlGlobList(_map_dimensions);
40 AllocateMap(_map_dim_x, _map_dim_y);
41 }
42
43 static void Check_MAPS()
44 {
45 SlGlobList(_map_dimensions);
46 _load_check_data.map_size_x = _map_dim_x;
47 _load_check_data.map_size_y = _map_dim_y;
48 }
49
50 static const uint MAP_SL_BUF_SIZE = 4096;
51
52 static void Load_MAPT()
53 {
54 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
55 TileIndex size = MapSize();
56
57 for (TileIndex i = 0; i != size;) {
58 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
59 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].type = buf[j];
60 }
61 }
62
63 static void Save_MAPT()
64 {
65 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
66 TileIndex size = MapSize();
67
68 SlSetLength(size);
69 for (TileIndex i = 0; i != size;) {
70 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].type;
71 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
72 }
73 }
74
75 static void Load_MAPH()
76 {
77 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
78 TileIndex size = MapSize();
79
80 for (TileIndex i = 0; i != size;) {
81 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
82 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].height = buf[j];
83 }
84 }
85
86 static void Save_MAPH()
87 {
88 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
89 TileIndex size = MapSize();
90
91 SlSetLength(size);
92 for (TileIndex i = 0; i != size;) {
93 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].height;
94 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
95 }
96 }
97
98 static void Load_MAP1()
99 {
100 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
101 TileIndex size = MapSize();
102
103 for (TileIndex i = 0; i != size;) {
104 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
105 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m1 = buf[j];
106 }
107 }
108
109 static void Save_MAP1()
110 {
111 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
112 TileIndex size = MapSize();
113
114 SlSetLength(size);
115 for (TileIndex i = 0; i != size;) {
116 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m1;
117 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
118 }
119 }
120
121 static void Load_MAP2()
122 {
123 SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
124 TileIndex size = MapSize();
125
126 for (TileIndex i = 0; i != size;) {
127 SlArray(buf, MAP_SL_BUF_SIZE,
128 /* In those versions the m2 was 8 bits */
129 IsSavegameVersionBefore(5) ? SLE_FILE_U8 | SLE_VAR_U16 : SLE_UINT16
130 );
131 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m2 = buf[j];
132 }
133 }
134
135 static void Save_MAP2()
136 {
137 SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
138 TileIndex size = MapSize();
139
140 SlSetLength(size * sizeof(uint16));
141 for (TileIndex i = 0; i != size;) {
142 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m2;
143 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16);
144 }
145 }
146
147 static void Load_MAP3()
148 {
149 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
150 TileIndex size = MapSize();
151
152 for (TileIndex i = 0; i != size;) {
153 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
154 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m3 = buf[j];
155 }
156 }
157
158 static void Save_MAP3()
159 {
160 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
161 TileIndex size = MapSize();
162
163 SlSetLength(size);
164 for (TileIndex i = 0; i != size;) {
165 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m3;
166 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
167 }
168 }
169
170 static void Load_MAP4()
171 {
172 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
173 TileIndex size = MapSize();
174
175 for (TileIndex i = 0; i != size;) {
176 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
177 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m4 = buf[j];
178 }
179 }
180
181 static void Save_MAP4()
182 {
183 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
184 TileIndex size = MapSize();
185
186 SlSetLength(size);
187 for (TileIndex i = 0; i != size;) {
188 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m4;
189 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
190 }
191 }
192
193 static void Load_MAP5()
194 {
195 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
196 TileIndex size = MapSize();
197
198 for (TileIndex i = 0; i != size;) {
199 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
200 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m5 = buf[j];
201 }
202 }
203
204 static void Save_MAP5()
205 {
206 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
207 TileIndex size = MapSize();
208
209 SlSetLength(size);
210 for (TileIndex i = 0; i != size;) {
211 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m5;
212 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
213 }
214 }
215
216 static void Load_MAP6()
217 {
218 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
219 TileIndex size = MapSize();
220
221 if (IsSavegameVersionBefore(42)) {
222 for (TileIndex i = 0; i != size;) {
223 /* 1024, otherwise we overflow on 64x64 maps! */
224 SlArray(buf, 1024, SLE_UINT8);
225 for (uint j = 0; j != 1024; j++) {
226 _me[i++].m6 = GB(buf[j], 0, 2);
227 _me[i++].m6 = GB(buf[j], 2, 2);
228 _me[i++].m6 = GB(buf[j], 4, 2);
229 _me[i++].m6 = GB(buf[j], 6, 2);
230 }
231 }
232 } else {
233 for (TileIndex i = 0; i != size;) {
234 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
235 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m6 = buf[j];
236 }
237 }
238 }
239
240 static void Save_MAP6()
241 {
242 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
243 TileIndex size = MapSize();
244
245 SlSetLength(size);
246 for (TileIndex i = 0; i != size;) {
247 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m6;
248 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
249 }
250 }
251
252 static void Load_MAP7()
253 {
254 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
255 TileIndex size = MapSize();
256
257 for (TileIndex i = 0; i != size;) {
258 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
259 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m7 = buf[j];
260 }
261 }
262
263 static void Save_MAP7()
264 {
265 SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
266 TileIndex size = MapSize();
267
268 SlSetLength(size);
269 for (TileIndex i = 0; i != size;) {
270 for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m7;
271 SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
272 }
273 }
274
275 extern const ChunkHandler _map_chunk_handlers[] = {
276 { 'MAPS', Save_MAPS, Load_MAPS, NULL, Check_MAPS, CH_RIFF },
277 { 'MAPT', Save_MAPT, Load_MAPT, NULL, NULL, CH_RIFF },
278 { 'MAPH', Save_MAPH, Load_MAPH, NULL, NULL, CH_RIFF },
279 { 'MAPO', Save_MAP1, Load_MAP1, NULL, NULL, CH_RIFF },
280 { 'MAP2', Save_MAP2, Load_MAP2, NULL, NULL, CH_RIFF },
281 { 'M3LO', Save_MAP3, Load_MAP3, NULL, NULL, CH_RIFF },
282 { 'M3HI', Save_MAP4, Load_MAP4, NULL, NULL, CH_RIFF },
283 { 'MAP5', Save_MAP5, Load_MAP5, NULL, NULL, CH_RIFF },
284 { 'MAPE', Save_MAP6, Load_MAP6, NULL, NULL, CH_RIFF },
285 { 'MAP7', Save_MAP7, Load_MAP7, NULL, NULL, CH_RIFF | CH_LAST },
286 };