1 /* $Id: map_sl.cpp 21284 2010-11-21 12:47:04Z alberth $ */
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/>.
10 /** @file map_sl.cpp Code handling saving and loading of map */
12 #include "../stdafx.h"
13 #include "../map_func.h"
14 #include "../core/bitmath_func.hpp"
19 #include "../safeguards.h"
21 static uint32 _map_dim_x
;
22 static uint32 _map_dim_y
;
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
),
30 static void Save_MAPS()
32 _map_dim_x
= MapSizeX();
33 _map_dim_y
= MapSizeY();
34 SlGlobList(_map_dimensions
);
37 static void Load_MAPS()
39 SlGlobList(_map_dimensions
);
40 AllocateMap(_map_dim_x
, _map_dim_y
);
43 static void Check_MAPS()
45 SlGlobList(_map_dimensions
);
46 _load_check_data
.map_size_x
= _map_dim_x
;
47 _load_check_data
.map_size_y
= _map_dim_y
;
50 static const uint MAP_SL_BUF_SIZE
= 4096;
52 static void Load_MAPT()
54 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
55 TileIndex size
= MapSize();
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
];
63 static void Save_MAPT()
65 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
66 TileIndex size
= MapSize();
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
);
75 static void Load_MAPH()
77 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
78 TileIndex size
= MapSize();
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
];
86 static void Save_MAPH()
88 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
89 TileIndex size
= MapSize();
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
);
98 static void Load_MAP1()
100 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
101 TileIndex size
= MapSize();
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
];
109 static void Save_MAP1()
111 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
112 TileIndex size
= MapSize();
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
);
121 static void Load_MAP2()
123 SmallStackSafeStackAlloc
<uint16
, MAP_SL_BUF_SIZE
> buf
;
124 TileIndex size
= MapSize();
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
131 for (uint j
= 0; j
!= MAP_SL_BUF_SIZE
; j
++) _m
[i
++].m2
= buf
[j
];
135 static void Save_MAP2()
137 SmallStackSafeStackAlloc
<uint16
, MAP_SL_BUF_SIZE
> buf
;
138 TileIndex size
= MapSize();
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
);
147 static void Load_MAP3()
149 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
150 TileIndex size
= MapSize();
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
];
158 static void Save_MAP3()
160 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
161 TileIndex size
= MapSize();
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
);
170 static void Load_MAP4()
172 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
173 TileIndex size
= MapSize();
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
];
181 static void Save_MAP4()
183 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
184 TileIndex size
= MapSize();
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
);
193 static void Load_MAP5()
195 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
196 TileIndex size
= MapSize();
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
];
204 static void Save_MAP5()
206 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
207 TileIndex size
= MapSize();
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
);
216 static void Load_MAP6()
218 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
219 TileIndex size
= MapSize();
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);
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
];
240 static void Save_MAP6()
242 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
243 TileIndex size
= MapSize();
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
);
252 static void Load_MAP7()
254 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
255 TileIndex size
= MapSize();
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
];
263 static void Save_MAP7()
265 SmallStackSafeStackAlloc
<byte
, MAP_SL_BUF_SIZE
> buf
;
266 TileIndex size
= MapSize();
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
);
275 extern const ChunkHandler _map_chunk_handlers
[] = {
276 { 'MAPS', Save_MAPS
, Load_MAPS
, nullptr, Check_MAPS
, CH_RIFF
},
277 { 'MAPT', Save_MAPT
, Load_MAPT
, nullptr, nullptr, CH_RIFF
},
278 { 'MAPH', Save_MAPH
, Load_MAPH
, nullptr, nullptr, CH_RIFF
},
279 { 'MAPO', Save_MAP1
, Load_MAP1
, nullptr, nullptr, CH_RIFF
},
280 { 'MAP2', Save_MAP2
, Load_MAP2
, nullptr, nullptr, CH_RIFF
},
281 { 'M3LO', Save_MAP3
, Load_MAP3
, nullptr, nullptr, CH_RIFF
},
282 { 'M3HI', Save_MAP4
, Load_MAP4
, nullptr, nullptr, CH_RIFF
},
283 { 'MAP5', Save_MAP5
, Load_MAP5
, nullptr, nullptr, CH_RIFF
},
284 { 'MAPE', Save_MAP6
, Load_MAP6
, nullptr, nullptr, CH_RIFF
},
285 { 'MAP7', Save_MAP7
, Load_MAP7
, nullptr, nullptr, CH_RIFF
| CH_LAST
},