Debian Codegraph @

CorsixTH/Src/th_map.cpp

raw
1 /*
2 Copyright (c) 2009 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "th_map.h"
24
25 #include "config.h"
26
27 #ifdef WITH_TRACY
28 #include <tracy/Tracy.hpp>
29 #endif
30
31 #include <algorithm>
32 #include <cstdio>
33 #include <cstring>
34 #include <fstream>
35 #include <new>
36 #include <stdexcept>
37 #include <string>
38
39 #include "lua.hpp"
40 #include "persist_lua.h"
41 #include "run_length_encoder.h"
42 #include "th.h"
43 #include "th_gfx.h"
44 #include "th_gfx_sdl.h"
45 #include "th_lua.h"
46 #include "th_map_overlays.h"
47
48 constexpr int max_player_count = 4;
49 constexpr int default_width = 128;
50 constexpr int default_height = 128;
51
52 map_tile_flags& map_tile_flags::operator=(uint32_t raw) {
53 using flags = map_tile_flags::key;
54
55 passable = (raw & static_cast<uint32_t>(flags::passable_mask)) != 0;
56 can_travel_n = (raw & static_cast<uint32_t>(flags::can_travel_n_mask)) != 0;
57 can_travel_e = (raw & static_cast<uint32_t>(flags::can_travel_e_mask)) != 0;
58 can_travel_s = (raw & static_cast<uint32_t>(flags::can_travel_s_mask)) != 0;
59 can_travel_w = (raw & static_cast<uint32_t>(flags::can_travel_w_mask)) != 0;
60 hospital = (raw & static_cast<uint32_t>(flags::hospital_mask)) != 0;
61 buildable = (raw & static_cast<uint32_t>(flags::buildable_mask)) != 0;
62 passable_if_not_for_blueprint =
63 (raw &
64 static_cast<uint32_t>(flags::passable_if_not_for_blueprint_mask)) != 0;
65 room = (raw & static_cast<uint32_t>(flags::room_mask)) != 0;
66 shadow_half = (raw & static_cast<uint32_t>(flags::shadow_half_mask)) != 0;
67 shadow_full = (raw & static_cast<uint32_t>(flags::shadow_full_mask)) != 0;
68 shadow_wall = (raw & static_cast<uint32_t>(flags::shadow_wall_mask)) != 0;
69 door_north = (raw & static_cast<uint32_t>(flags::door_north_mask)) != 0;
70 door_west = (raw & static_cast<uint32_t>(flags::door_west_mask)) != 0;
71 do_not_idle = (raw & static_cast<uint32_t>(flags::do_not_idle_mask)) != 0;
72 tall_north = (raw & static_cast<uint32_t>(flags::tall_north_mask)) != 0;
73 tall_west = (raw & static_cast<uint32_t>(flags::tall_west_mask)) != 0;
74 buildable_n = (raw & static_cast<uint32_t>(flags::buildable_n_mask)) != 0;
75 buildable_e = (raw & static_cast<uint32_t>(flags::buildable_e_mask)) != 0;
76 buildable_s = (raw & static_cast<uint32_t>(flags::buildable_s_mask)) != 0;
77 buildable_w = (raw & static_cast<uint32_t>(flags::buildable_w_mask)) != 0;
78
79 return *this;
80 }
81
82 bool& map_tile_flags::operator[](map_tile_flags::key key) {
83 using flags = map_tile_flags::key;
84
85 switch (key) {
86 case flags::passable_mask:
87 return passable;
88 case flags::can_travel_n_mask:
89 return can_travel_n;
90 case flags::can_travel_e_mask:
91 return can_travel_e;
92 case flags::can_travel_s_mask:
93 return can_travel_s;
94 case flags::can_travel_w_mask:
95 return can_travel_w;
96 case flags::hospital_mask:
97 return hospital;
98 case flags::buildable_mask:
99 return buildable;
100 case flags::passable_if_not_for_blueprint_mask:
101 return passable_if_not_for_blueprint;
102 case flags::room_mask:
103 return room;
104 case flags::shadow_half_mask:
105 return shadow_half;
106 case flags::shadow_full_mask:
107 return shadow_full;
108 case flags::shadow_wall_mask:
109 return shadow_wall;
110 case flags::door_north_mask:
111 return door_north;
112 case flags::door_west_mask:
113 return door_west;
114 case flags::do_not_idle_mask:
115 return do_not_idle;
116 case flags::tall_north_mask:
117 return tall_north;
118 case flags::tall_west_mask:
119 return tall_west;
120 case flags::buildable_n_mask:
121 return buildable_n;
122 case flags::buildable_e_mask:
123 return buildable_e;
124 case flags::buildable_s_mask:
125 return buildable_s;
126 case flags::buildable_w_mask:
127 return buildable_w;
128 default:
129 throw std::out_of_range("map tile flag is invalid");
130 }
131 }
132
133 const bool& map_tile_flags::operator[](map_tile_flags::key key) const {
134 using flags = map_tile_flags::key;
135
136 switch (key) {
137 case flags::passable_mask:
138 return passable;
139 case flags::can_travel_n_mask:
140 return can_travel_n;
141 case flags::can_travel_e_mask:
142 return can_travel_e;
143 case flags::can_travel_s_mask:
144 return can_travel_s;
145 case flags::can_travel_w_mask:
146 return can_travel_w;
147 case flags::hospital_mask:
148 return hospital;
149 case flags::buildable_mask:
150 return buildable;
151 case flags::passable_if_not_for_blueprint_mask:
152 return passable_if_not_for_blueprint;
153 case flags::room_mask:
154 return room;
155 case flags::shadow_half_mask:
156 return shadow_half;
157 case flags::shadow_full_mask:
158 return shadow_full;
159 case flags::shadow_wall_mask:
160 return shadow_wall;
161 case flags::door_north_mask:
162 return door_north;
163 case flags::door_west_mask:
164 return door_west;
165 case flags::do_not_idle_mask:
166 return do_not_idle;
167 case flags::tall_north_mask:
168 return tall_north;
169 case flags::tall_west_mask:
170 return tall_west;
171 case flags::buildable_n_mask:
172 return buildable_n;
173 case flags::buildable_e_mask:
174 return buildable_e;
175 case flags::buildable_s_mask:
176 return buildable_s;
177 case flags::buildable_w_mask:
178 return buildable_w;
179 default:
180 throw std::out_of_range("map tile flag is invalid");
181 }
182 }
183
184 map_tile_flags::operator uint32_t() const {
185 using flags = map_tile_flags::key;
186
187 uint32_t raw = 0;
188 if (passable) {
189 raw |= static_cast<uint32_t>(flags::passable_mask);
190 }
191 if (can_travel_n) {
192 raw |= static_cast<uint32_t>(flags::can_travel_n_mask);
193 }
194 if (can_travel_e) {
195 raw |= static_cast<uint32_t>(flags::can_travel_e_mask);
196 }
197 if (can_travel_s) {
198 raw |= static_cast<uint32_t>(flags::can_travel_s_mask);
199 }
200 if (can_travel_w) {
201 raw |= static_cast<uint32_t>(flags::can_travel_w_mask);
202 }
203 if (hospital) {
204 raw |= static_cast<uint32_t>(flags::hospital_mask);
205 }
206 if (buildable) {
207 raw |= static_cast<uint32_t>(flags::buildable_mask);
208 }
209 if (passable_if_not_for_blueprint) {
210 raw |= static_cast<uint32_t>(flags::passable_if_not_for_blueprint_mask);
211 }
212 if (room) {
213 raw |= static_cast<uint32_t>(flags::room_mask);
214 }
215 if (shadow_half) {
216 raw |= static_cast<uint32_t>(flags::shadow_half_mask);
217 }
218 if (shadow_full) {
219 raw |= static_cast<uint32_t>(flags::shadow_full_mask);
220 }
221 if (shadow_wall) {
222 raw |= static_cast<uint32_t>(flags::shadow_wall_mask);
223 }
224 if (door_north) {
225 raw |= static_cast<uint32_t>(flags::door_north_mask);
226 }
227 if (door_west) {
228 raw |= static_cast<uint32_t>(flags::door_west_mask);
229 }
230 if (do_not_idle) {
231 raw |= static_cast<uint32_t>(flags::do_not_idle_mask);
232 }
233 if (tall_north) {
234 raw |= static_cast<uint32_t>(flags::tall_north_mask);
235 }
236 if (tall_west) {
237 raw |= static_cast<uint32_t>(flags::tall_west_mask);
238 }
239 if (buildable_n) {
240 raw |= static_cast<uint32_t>(flags::buildable_n_mask);
241 }
242 if (buildable_e) {
243 raw |= static_cast<uint32_t>(flags::buildable_e_mask);
244 }
245 if (buildable_s) {
246 raw |= static_cast<uint32_t>(flags::buildable_s_mask);
247 }
248 if (buildable_w) {
249 raw |= static_cast<uint32_t>(flags::buildable_w_mask);
250 }
251
252 return raw;
253 }
254
255 level_map::~level_map() {
256 set_overlay(nullptr, false);
257 delete[] cells;
258 delete[] original_cells;
259 delete[] plot_owner;
260 delete[] parcel_tile_counts;
261 delete[] parcel_adjacency_matrix;
262 delete[] purchasable_matrix;
263 }
264
265 void level_map::set_overlay(map_overlay* pOverlay, bool bTakeOwnership) {
266 if (overlay && owns_overlay) {
267 delete overlay;
268 }
269 overlay = pOverlay;
270 owns_overlay = bTakeOwnership;
271 }
272
273 bool level_map::set_size(int iWidth, int iHeight) {
274 if (iWidth <= 0 || iHeight <= 0) {
275 return false;
276 }
277
278 delete[] cells;
279 delete[] original_cells;
280 delete[] parcel_adjacency_matrix;
281 delete[] purchasable_matrix;
282 width = iWidth;
283 height = iHeight;
284 cells = nullptr;
285 cells = new (std::nothrow) map_tile[iWidth * iHeight];
286 original_cells = nullptr;
287 original_cells = new (std::nothrow) map_tile[iWidth * iHeight];
288 parcel_adjacency_matrix = nullptr;
289 purchasable_matrix = nullptr;
290
291 if (cells == nullptr || original_cells == nullptr) {
292 delete[] cells;
293 delete[] original_cells;
294 original_cells = nullptr;
295 cells = nullptr;
296 width = 0;
297 height = 0;
298 return false;
299 }
300
301 return true;
302 }
303
304 namespace {
305
306 // NB: http://connection-endpoint.de/th-format-specification/
307 // gives a (slightly) incorrect array, which is why it differs from this one.
308 constexpr uint8_t gs_iTHMapBlockLUT[256] = {
309 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
310 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
311 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
312 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
313 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
314 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
315 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x52, 0x53, 0x54, 0x55,
316 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61,
317 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D,
318 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
319 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x84, 0x85, 0x88, 0x89,
320 0x8C, 0x8D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8E, 0x8F, 0x00, 0x00,
321 0x00, 0x00, 0x8E, 0x8F, 0xD5, 0xD6, 0x9C, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0,
322 0xD1, 0xD2, 0xD3, 0xD4, 0xB3, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5,
323 0xB6, 0xB7, 0xB8, 0xB9, 0xB3, 0xB3, 0xB4, 0xB4, 0xBA, 0xBB, 0xBC, 0xBD,
324 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
325 0xCA, 0xCB, 0x00, 0x82, 0x83, 0x86, 0x87, 0x8A, 0x8B, 0x92, 0x93, 0x94,
326 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x00, 0x9D, 0x9E, 0x9F, 0xA0,
327 0xA1, 0xA2, 0xA3, 0xA4, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE,
328 0xDF, 0xE0, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330 0x00, 0x00, 0x00, 0x00};
331
332 } // namespace
333
334 void level_map::read_tile_index(const uint8_t* pData, int& iX, int& iY) const {
335 int iIndex = static_cast<int>(pData[1]);
336 iIndex = iIndex * 0x100 + static_cast<int>(pData[0]);
337 iX = iIndex % width;
338 iY = iIndex / width;
339 }
340
341 void level_map::write_tile_index(uint8_t* pData, int iX, int iY) const {
342 uint16_t iIndex = static_cast<uint16_t>(iY * width + iX);
343 pData[0] = static_cast<uint8_t>(iIndex & 0xFF);
344 pData[1] = static_cast<uint8_t>(iIndex >> 8);
345 }
346
347 bool level_map::load_blank() {
348 if (!set_size(default_width, default_height)) {
349 return false;
350 }
351
352 player_count = 1;
353 initial_camera_x[0] = initial_camera_y[0] = 63;
354 heliport_x[0] = heliport_y[0] = 0;
355 parcel_count = 1;
356 delete[] plot_owner;
357 delete[] parcel_tile_counts;
358 plot_owner = nullptr;
359 parcel_tile_counts = nullptr;
360 map_tile* pNode = cells;
361 map_tile* pOriginalNode = original_cells;
362 for (int iY = 0; iY < height; ++iY) {
363 for (int iX = 0; iX < width; ++iX, ++pNode, ++pOriginalNode) {
364 pNode->tile_layers[tile_layer::ground] =
365 static_cast<uint16_t>(2 + (iX % 2));
366 }
367 }
368 plot_owner = new int[1];
369 plot_owner[0] = 0;
370 parcel_tile_counts = new int[1];
371 parcel_tile_counts[0] = height * width;
372
373 return true;
374 }
375
376 namespace {
377
378 inline bool is_divider_wall(const uint8_t byte) { return (byte >> 1) == 70; }
379
380 } // namespace
381
382 bool level_map::load_from_th_file(const uint8_t* pData, size_t iDataLength,
383 map_load_object_callback_fn fnObjectCallback,
384 void* pCallbackToken) {
385 const size_t camera_offset = 163876;
386 const size_t heliport_offset = 163884;
387 const size_t parcel_offset = 131106;
388
389 if (iDataLength < 163948 || !set_size(default_width, default_height)) {
390 return false;
391 }
392
393 player_count = pData[0];
394 if (player_count < 1) player_count = 1;
395 if (player_count > max_player_count) player_count = max_player_count;
396
397 for (int i = 0; i < player_count; ++i) {
398 read_tile_index(pData + camera_offset + (i * 2), initial_camera_x[i],
399 initial_camera_y[i]);
400 read_tile_index(pData + heliport_offset + (i * 2), heliport_x[i],
401 heliport_y[i]);
402 }
403 parcel_count = 0;
404 delete[] plot_owner;
405 delete[] parcel_tile_counts;
406 plot_owner = nullptr;
407 parcel_tile_counts = nullptr;
408
409 map_tile* pNode = cells;
410 map_tile* pOriginalNode = original_cells;
411 const uint8_t* pParcel = pData + parcel_offset;
412 pData += 34;
413
414 pNode->objects.clear();
415 for (int iY = 0; iY < height; ++iY) {
416 for (int iX = 0; iX < width; ++iX) {
417 std::copy_n(pData, map_tile::raw_length, pNode->raw);
418 uint8_t iBaseTile = gs_iTHMapBlockLUT[pData[2]];
419 pNode->flags.can_travel_n = true;
420 pNode->flags.can_travel_e = true;
421 pNode->flags.can_travel_s = true;
422 pNode->flags.can_travel_w = true;
423 if (iX == 0) {
424 pNode->flags.can_travel_w = false;
425 } else if (iX == width - 1) {
426 pNode->flags.can_travel_e = false;
427 }
428
429 if (iY == 0) {
430 pNode->flags.can_travel_n = false;
431 } else if (iY == height - 1) {
432 pNode->flags.can_travel_s = false;
433 }
434
435 pNode->tile_layers[tile_layer::ground] = iBaseTile;
436 if (pData[3] == 0 || is_divider_wall(pData[3])) {
437 // Tiles 71, 72 and 73 (pond foliage) are used as floor tiles,
438 // but are too tall to be floor tiles, so move them to a wall,
439 // and replace the floor with something similar (pond base).
440 if (71 <= iBaseTile && iBaseTile <= 73) {
441 pNode->tile_layers[tile_layer::north_wall] = iBaseTile;
442 pNode->tile_layers[tile_layer::ground] = 69;
443 } else {
444 pNode->tile_layers[tile_layer::north_wall] = 0;
445 }
446 } else {
447 pNode->tile_layers[tile_layer::north_wall] =
448 gs_iTHMapBlockLUT[pData[3]];
449 pNode->flags.can_travel_n = false;
450 if (iY != 0) {
451 pNode[-this->width].flags.can_travel_s = false;
452 }
453 }
454 if (pData[4] == 0 || is_divider_wall(pData[4])) {
455 pNode->tile_layers[tile_layer::west_wall] = 0;
456 } else {
457 pNode->tile_layers[tile_layer::west_wall] = gs_iTHMapBlockLUT[pData[4]];
458 pNode->flags.can_travel_w = false;
459 if (iX != 0) {
460 pNode[-1].flags.can_travel_e = false;
461 }
462 }
463
464 pNode->iRoomId = 0;
465 pNode->iParcelId = bytes_to_uint16_le(pParcel);
466 if (pNode->iParcelId >= parcel_count) {
467 parcel_count = pNode->iParcelId + 1;
468 }
469
470 if (!(pData[5] & 1)) {
471 pNode->flags.passable = true;
472 if (!(pData[7] & 16)) {
473 pNode->flags.hospital = true;
474 if (!(pData[5] & 2)) {
475 pNode->flags.buildable = true;
476 }
477 if (!(pData[5] & 4) || pData[1] == 0) {
478 pNode->flags.buildable_n = true;
479 }
480 if (!(pData[5] & 8) || pData[1] == 0) {
481 pNode->flags.buildable_e = true;
482 }
483 if (!(pData[5] & 16) || pData[1] == 0) {
484 pNode->flags.buildable_s = true;
485 }
486 if (!(pData[5] & 32) || pData[1] == 0) {
487 pNode->flags.buildable_w = true;
488 }
489 }
490 }
491
492 *pOriginalNode = *pNode;
493 if (is_divider_wall(pData[3])) {
494 pOriginalNode->tile_layers[tile_layer::north_wall] =
495 gs_iTHMapBlockLUT[pData[3]];
496 }
497 if (is_divider_wall(pData[4])) {
498 pOriginalNode->tile_layers[tile_layer::west_wall] =
499 gs_iTHMapBlockLUT[pData[4]];
500 }
501
502 if (pData[1] != 0 && fnObjectCallback != nullptr) {
503 fnObjectCallback(pCallbackToken, iX, iY,
504 static_cast<object_type>(pData[1]), pData[0]);
505 }
506
507 ++pNode;
508 ++pOriginalNode;
509 pData += 8;
510 pParcel += 2;
511 }
512 }
513
514 plot_owner = new int[parcel_count];
515 plot_owner[0] = 0;
516 for (int i = 1; i < parcel_count; ++i) {
517 plot_owner[i] = 1;
518 }
519
520 update_shadows();
521
522 parcel_tile_counts = new int[parcel_count];
523 parcel_tile_counts[0] = 0;
524 for (int i = 1; i < parcel_count; ++i) {
525 parcel_tile_counts[i] = count_parcel_tiles(i);
526 }
527
528 return true;
529 }
530
531 void level_map::save(const std::string& filename) {
532 uint8_t aBuffer[256] = {0};
533 int iBufferNext = 0;
534 std::ofstream os(filename, std::ios_base::trunc | std::ios_base::binary);
535
536 // Header
537 aBuffer[0] = static_cast<uint8_t>(player_count);
538 // TODO: Determine correct contents for the next 33 bytes
539 os.write(reinterpret_cast<char*>(aBuffer), 34);
540
541 uint8_t aReverseBlockLUT[256] = {0};
542 for (int i = 0; i < 256; ++i) {
543 aReverseBlockLUT[gs_iTHMapBlockLUT[i]] = static_cast<uint8_t>(i);
544 }
545 aReverseBlockLUT[0] = 0;
546
547 for (map_tile *pNode = cells, *pLimitNode = pNode + width * height;
548 pNode != pLimitNode; ++pNode) {
549 // TODO: Nicer system for saving object data
550 aBuffer[iBufferNext++] = pNode->flags.tall_west ? 1 : 0;
551 aBuffer[iBufferNext++] =
552 static_cast<uint8_t>(pNode->objects.empty() ? object_type::no_object
553 : pNode->objects.front());
554
555 // Blocks
556 aBuffer[iBufferNext++] =
557 aReverseBlockLUT[pNode->tile_layers[tile_layer::ground] & 0xFF];
558 aBuffer[iBufferNext++] =
559 aReverseBlockLUT[pNode->tile_layers[tile_layer::north_wall] & 0xFF];
560 aBuffer[iBufferNext++] =
561 aReverseBlockLUT[pNode->tile_layers[tile_layer::west_wall] & 0xFF];
562
563 // Flags (TODO: Set a few more flag bits?)
564 uint8_t iFlags = 63;
565 if (pNode->flags.passable) {
566 iFlags ^= 1;
567 }
568 if (pNode->flags.buildable) {
569 iFlags ^= 2;
570 }
571 if (pNode->flags.buildable_n) {
572 iFlags ^= 4;
573 }
574 if (pNode->flags.buildable_e) {
575 iFlags ^= 8;
576 }
577 if (pNode->flags.buildable_s) {
578 iFlags ^= 16;
579 }
580 if (pNode->flags.buildable_w) {
581 iFlags ^= 32;
582 }
583
584 aBuffer[iBufferNext++] = iFlags;
585
586 aBuffer[iBufferNext++] = 0;
587 iFlags = 16;
588 if (pNode->flags.hospital) {
589 iFlags ^= 16;
590 }
591 aBuffer[iBufferNext++] = iFlags;
592
593 if (iBufferNext == sizeof(aBuffer)) {
594 os.write(reinterpret_cast<char*>(aBuffer), sizeof(aBuffer));
595 iBufferNext = 0;
596 }
597 }
598 for (map_tile *pNode = cells, *pLimitNode = pNode + width * height;
599 pNode != pLimitNode; ++pNode) {
600 aBuffer[iBufferNext++] = static_cast<uint8_t>(pNode->iParcelId & 0xFF);
601 aBuffer[iBufferNext++] = static_cast<uint8_t>(pNode->iParcelId >> 8);
602 if (iBufferNext == sizeof(aBuffer)) {
603 os.write(reinterpret_cast<char*>(aBuffer), sizeof(aBuffer));
604 iBufferNext = 0;
605 }
606 }
607
608 // TODO: What are these two bytes?
609 aBuffer[iBufferNext++] = 3;
610 aBuffer[iBufferNext++] = 0;
611 os.write(reinterpret_cast<char*>(aBuffer), iBufferNext);
612 iBufferNext = 0;
613
614 std::memset(aBuffer, 0, 56);
615 for (int i = 0; i < player_count; ++i) {
616 write_tile_index(aBuffer + iBufferNext, initial_camera_x[i],
617 initial_camera_y[i]);
618 write_tile_index(aBuffer + iBufferNext + 8, heliport_x[i], heliport_y[i]);
619 iBufferNext += 2;
620 }
621 os.write(reinterpret_cast<char*>(aBuffer), 16);
622 std::memset(aBuffer, 0, 16);
623 // TODO: What are these 56 bytes?
624 os.write(reinterpret_cast<char*>(aBuffer), 56);
625 os.close();
626 }
627
628 namespace {
629
630 //! Add or remove divider wall for the given tile
631 /*!
632 If the given 'pNode' has an indoor border to another parcel in the 'delta'
633 direction:
634 * A divider wall is added in the layer specified by 'block' if the owners
635 of the two parcels are not the same, or
636 * A divider wall is removed if the owners are the same and 'iParcelId' is
637 involved. \return True if a border was removed, false otherwise
638 */
639 bool addRemoveDividerWalls(level_map* pMap, map_tile* pNode,
640 const map_tile* pOriginalNode, int iXY, int delta,
641 tile_layer layer, int iParcelId) {
642 if (iXY > 0 && pOriginalNode->flags.hospital &&
643 pOriginalNode[-delta].flags.hospital &&
644 pNode->iParcelId != pNode[-delta].iParcelId) {
645 int iOwner = pMap->get_parcel_owner(pNode->iParcelId);
646 int iOtherOwner = pMap->get_parcel_owner(pNode[-delta].iParcelId);
647 if (iOwner != iOtherOwner) {
648 pNode->tile_layers[layer] = layer + (iOwner ? 143 : 141);
649 } else if (pNode->iParcelId == iParcelId ||
650 pNode[-delta].iParcelId == iParcelId) {
651 pNode->tile_layers[layer] = 0;
652 return true;
653 }
654 }
655 return false;
656 }
657
658 } // namespace
659
660 std::vector<std::pair<int, int>> level_map::set_parcel_owner(int iParcelId,
661 int iOwner) {
662 std::vector<std::pair<int, int>> vSplitTiles;
663 if (iParcelId <= 0 || parcel_count <= iParcelId || iOwner < 0) {
664 return vSplitTiles;
665 }
666 plot_owner[iParcelId] = iOwner;
667
668 map_tile* pNode = cells;
669 const map_tile* pOriginalNode = original_cells;
670
671 for (int iY = 0; iY < this->height; ++iY) {
672 for (int iX = 0; iX < this->width; ++iX, ++pNode, ++pOriginalNode) {
673 if (pNode->iParcelId == iParcelId) {
674 if (iOwner != 0) {
675 pNode->tile_layers[tile_layer::ground] =
676 pOriginalNode->tile_layers[tile_layer::ground];
677 pNode->tile_layers[tile_layer::north_wall] =
678 pOriginalNode->tile_layers[tile_layer::north_wall];
679 pNode->tile_layers[tile_layer::west_wall] =
680 pOriginalNode->tile_layers[tile_layer::west_wall];
681 pNode->flags = pOriginalNode->flags;
682 } else {
683 // Nicely mown grass pattern
684 pNode->tile_layers[tile_layer::ground] =
685 static_cast<uint16_t>(((iX & 1) << 1) + 1);
686
687 pNode->tile_layers[tile_layer::north_wall] = 0;
688 pNode->tile_layers[tile_layer::west_wall] = 0;
689 pNode->flags = {};
690
691 // Random decoration
692 if (((iX | iY) & 0x7) == 0) {
693 int iWhich = (iX ^ iY) % 9;
694 pNode->tile_layers[tile_layer::north_wall] =
695 static_cast<uint16_t>(192 + iWhich);
696 }
697 }
698 }
699 if (addRemoveDividerWalls(this, pNode, pOriginalNode, iX, 1,
700 tile_layer::west_wall, iParcelId)) {
701 vSplitTiles.emplace_back(iX, iY);
702 }
703 if (addRemoveDividerWalls(this, pNode, pOriginalNode, iY, this->width,
704 tile_layer::north_wall, iParcelId)) {
705 vSplitTiles.emplace_back(iX, iY);
706 }
707 }
708 }
709
710 update_pathfinding();
711 update_shadows();
712 update_purchase_matrix();
713 return vSplitTiles;
714 }
715
716 namespace {
717
718 void test_adj(bool* parcel_adjacency_matrix, int parcel_count,
719 const map_tile* original_node, const map_tile* tested_node) {
720 if (original_node->iParcelId != tested_node->iParcelId &&
721 original_node->flags.passable && tested_node->flags.passable) {
722 // These cells are part of two different parcels, and they are passable, so
723 // we consider them adjacent. Update the adjacency matrix accordingly.
724 // NB: the matrix is always symmetrical such that xy = yx.
725 parcel_adjacency_matrix[original_node->iParcelId * parcel_count +
726 tested_node->iParcelId] = true;
727 parcel_adjacency_matrix[original_node->iParcelId +
728 tested_node->iParcelId * parcel_count] = true;
729 }
730 }
731
732 } // namespace
733
734 void level_map::make_adjacency_matrix() {
735 if (parcel_adjacency_matrix != nullptr) {
736 return;
737 }
738
739 // The adjacency matrix tracks which parcels are adjacent to each other.
740 // Initialize the adjacency matrix to the identity matrix - i.e. every
741 // parcel is adjacent to itself.
742 parcel_adjacency_matrix = new bool[parcel_count * parcel_count];
743 for (int i = 0; i < parcel_count; ++i) {
744 for (int j = 0; j < parcel_count; ++j) {
745 parcel_adjacency_matrix[i * parcel_count + j] = (i == j);
746 }
747 }
748
749 // Update the adjacency matrix using the parcel IDs of each cell.
750 for (int iY = 0; iY < this->height; ++iY) {
751 for (int iX = 0; iX < this->width; ++iX) {
752 const map_tile* thisCell = get_original_tile(iX, iY);
753 // Test the cell immediately to the left, and above it.
754 if (iX > 0) {
755 const map_tile* testedCell = get_original_tile(iX - 1, iY);
756 test_adj(parcel_adjacency_matrix, parcel_count, thisCell, testedCell);
757 }
758 if (iY > 0) {
759 const map_tile* testedCell = get_original_tile(iX, iY - 1);
760 test_adj(parcel_adjacency_matrix, parcel_count, thisCell, testedCell);
761 }
762 }
763 }
764 }
765
766 void level_map::make_purchase_matrix() {
767 if (purchasable_matrix != nullptr) {
768 return; // Already made
769 }
770
771 purchasable_matrix = new bool[max_player_count * parcel_count];
772 update_purchase_matrix();
773 }
774
775 void level_map::update_purchase_matrix() {
776 if (purchasable_matrix == nullptr) {
777 return; // Nothing to update
778 }
779
780 for (int iPlayer = 1; iPlayer <= max_player_count; ++iPlayer) {
781 for (int iParcel = 0; iParcel < parcel_count; ++iParcel) {
782 bool bPurchasable = false;
783 if (iParcel != 0 && plot_owner[iParcel] == 0) {
784 for (int iParcel2 = 0; iParcel2 < parcel_count; ++iParcel2) {
785 if ((plot_owner[iParcel2] == iPlayer) || (iParcel2 == 0)) {
786 if (are_parcels_adjacent(iParcel, iParcel2)) {
787 bPurchasable = true;
788 break;
789 }
790 }
791 }
792 }
793 purchasable_matrix[iParcel * max_player_count + iPlayer - 1] =
794 bPurchasable;
795 }
796 }
797 }
798
799 bool level_map::are_parcels_adjacent(int iParcel1, int iParcel2) {
800 if (0 <= iParcel1 && iParcel1 < parcel_count && 0 <= iParcel2 &&
801 iParcel2 < parcel_count) {
802 make_adjacency_matrix();
803 return parcel_adjacency_matrix[iParcel1 * parcel_count + iParcel2];
804 }
805 return false;
806 }
807
808 bool level_map::is_parcel_purchasable(int iParcelId, int iPlayer) {
809 if (0 <= iParcelId && iParcelId < parcel_count && 1 <= iPlayer &&
810 iPlayer <= max_player_count) {
811 make_purchase_matrix();
812 return purchasable_matrix[iParcelId * max_player_count + iPlayer - 1];
813 }
814 return false;
815 }
816
817 void level_map::set_player_count(int count) {
818 if (count < 1 || count > max_player_count) {
819 throw std::out_of_range("Player count must be between 1 and 4");
820 }
821
822 player_count = count;
823 }
824
825 bool level_map::get_player_camera_tile(int iPlayer, int* pX, int* pY) const {
826 if (iPlayer < 0 || iPlayer >= get_player_count()) {
827 if (pX) {
828 *pX = 0;
829 }
830 if (pY) {
831 *pY = 0;
832 }
833 return false;
834 }
835 if (pX) {
836 *pX = initial_camera_x[iPlayer];
837 }
838 if (pY) {
839 *pY = initial_camera_y[iPlayer];
840 }
841 return true;
842 }
843
844 bool level_map::get_player_heliport_tile(int iPlayer, int* pX, int* pY) const {
845 if (iPlayer < 0 || iPlayer >= get_player_count()) {
846 if (pX) {
847 *pX = 0;
848 }
849 if (pY) {
850 *pY = 0;
851 }
852 return false;
853 }
854 if (pX) {
855 *pX = heliport_x[iPlayer];
856 }
857 if (pY) {
858 *pY = heliport_y[iPlayer];
859 }
860 return true;
861 }
862
863 void level_map::set_player_camera_tile(int iPlayer, int iX, int iY) {
864 if (0 <= iPlayer && iPlayer < get_player_count()) {
865 initial_camera_x[iPlayer] = iX;
866 initial_camera_y[iPlayer] = iY;
867 }
868 }
869
870 void level_map::set_player_heliport_tile(int iPlayer, int iX, int iY) {
871 if (0 <= iPlayer && iPlayer < get_player_count()) {
872 heliport_x[iPlayer] = iX;
873 heliport_y[iPlayer] = iY;
874 }
875 }
876
877 int level_map::get_parcel_tile_count(int iParcelId) const {
878 if (iParcelId < 1 || iParcelId >= parcel_count) {
879 return 0;
880 }
881 return parcel_tile_counts[iParcelId];
882 }
883
884 int level_map::count_parcel_tiles(int iParcelId) const {
885 int iTiles = 0;
886 for (int iY = 0; iY < height; ++iY) {
887 for (int iX = 0; iX < width; ++iX) {
888 const map_tile* pNode = get_tile_unchecked(iX, iY);
889 if (pNode->iParcelId == iParcelId) {
890 iTiles++;
891 }
892 }
893 }
894 return iTiles;
895 }
896
897 map_tile* level_map::get_tile(int iX, int iY) {
898 if (0 <= iX && iX < width && 0 <= iY && iY < height) {
899 return get_tile_unchecked(iX, iY);
900 } else {
901 return nullptr;
902 }
903 }
904
905 const map_tile* level_map::get_tile(int iX, int iY) const {
906 if (0 <= iX && iX < width && 0 <= iY && iY < height) {
907 return get_tile_unchecked(iX, iY);
908 } else {
909 return nullptr;
910 }
911 }
912
913 const map_tile* level_map::get_original_tile(int iX, int iY) const {
914 if (0 <= iX && iX < width && 0 <= iY && iY < height) {
915 return get_original_tile_unchecked(iX, iY);
916 } else {
917 return nullptr;
918 }
919 }
920
921 map_tile* level_map::get_tile_unchecked(int iX, int iY) {
922 return cells + iY * width + iX;
923 }
924
925 const map_tile* level_map::get_tile_unchecked(int iX, int iY) const {
926 return cells + iY * width + iX;
927 }
928
929 const map_tile* level_map::get_original_tile_unchecked(int iX, int iY) const {
930 return original_cells + iY * width + iX;
931 }
932
933 void level_map::set_block_sheet(sprite_sheet* pSheet) { wall_blocks = pSheet; }
934
935 void level_map::set_all_wall_draw_flags(uint8_t iFlags) {
936 uint16_t draw_flags = static_cast<uint16_t>(iFlags << 8);
937 map_tile* pNode = cells;
938 for (int i = 0; i < width * height; ++i, ++pNode) {
939 pNode->tile_layers[tile_layer::north_wall] = static_cast<uint16_t>(
940 (pNode->tile_layers[tile_layer::north_wall] & 0xFF) | draw_flags);
941 pNode->tile_layers[tile_layer::west_wall] = static_cast<uint16_t>(
942 (pNode->tile_layers[tile_layer::west_wall] & 0xFF) | draw_flags);
943 }
944 }
945
946 bool level_map::layer_exists(uint16_t layer, int& height) const {
947 if (layer == 0) {
948 return false;
949 }
950 if (wall_blocks->get_sprite_size(layer & 0xFF, nullptr, &height)) {
951 return height > 0;
952 }
953 return false;
954 }
955
956 void level_map::draw_floor(render_target* pCanvas, int iScreenX, int iScreenY,
957 int iWidth, int iHeight, int iCanvasX,
958 int iCanvasY) const {
959 for (map_tile_iterator itrNode1(this, iScreenX, iScreenY, iWidth, iHeight);
960 itrNode1; ++itrNode1) {
961 int tile_x = itrNode1.tile_x_position_on_screen() + iCanvasX - 32;
962 int tile_y = itrNode1.tile_y_position_on_screen() + iCanvasY;
963
964 // First, draw the floor tile as it should be below everything else.
965 int height = 32;
966 uint16_t layer = itrNode1->tile_layers[tile_layer::ground];
967 wall_blocks->get_sprite_size(layer & 0xFF, nullptr, &height);
968 wall_blocks->draw_sprite(pCanvas, layer & 0xFF, tile_x,
969 tile_y - height + 32, (layer >> 8) | thdf_nearest);
970
971 // Draw floor shadows immediately after floor tiles ensuring that all
972 // shadow pixels are drawn onto freshly drawn opaque floor tile pixels.
973 if (itrNode1->flags.shadow_full) {
974 wall_blocks->draw_sprite(pCanvas, 74, tile_x, tile_y,
975 thdf_alpha_75 | thdf_nearest);
976 } else if (itrNode1->flags.shadow_half) {
977 wall_blocks->draw_sprite(pCanvas, 75, tile_x, tile_y,
978 thdf_alpha_75 | thdf_nearest);
979 }
980 }
981 }
982
983 void level_map::draw_north_wall(const map_tile* tile, int tile_x, int tile_y,
984 render_target* pCanvas) const {
985 int height =
986 draw_layer(tile, tile_x, tile_y, tile_layer::north_wall, pCanvas);
987
988 // Draw shadow if needed.
989 if (height > 0 && tile->flags.shadow_wall) {
990 clip_rect rcNewClip;
991 rcNewClip.x = static_cast<clip_rect::x_y_type>(tile_x - 32);
992 rcNewClip.y = static_cast<clip_rect::x_y_type>(tile_y - height + 32 + 4);
993 rcNewClip.w = static_cast<clip_rect::w_h_type>(64);
994 rcNewClip.h = static_cast<clip_rect::w_h_type>(86 - 4);
995 render_target::scoped_clip clip(pCanvas, &rcNewClip);
996 wall_blocks->draw_sprite(pCanvas, 156, tile_x - 32, tile_y - 56,
997 thdf_alpha_75 | thdf_nearest);
998 }
999 }
1000
1001 //! Draw a layer.
1002 /*
1003 @param tile Map tile with the layer data to draw.
1004 @param tile_x X position of the tile at the screen.
1005 @param tile_y Y position of the tile at the screen.
1006 @param layer The layer to draw.
1007 @param pCanvas The canvas to paint.
1008 @return The height of the layer sprite in pixels.
1009 */
1010 int level_map::draw_layer(const map_tile* tile, int tile_x, int tile_y,
1011 tile_layer layer, render_target* pCanvas) const {
1012 int height = 0;
1013 uint16_t layer_data = tile->tile_layers[layer];
1014 if (layer_exists(layer_data, height)) {
1015 wall_blocks->draw_sprite(pCanvas, layer_data & 0xFF, tile_x - 32,
1016 tile_y - height + 32,
1017 (layer_data >> 8) | thdf_nearest);
1018 }
1019 return height;
1020 }
1021
1022 void level_map::draw(render_target* pCanvas, int iScreenX, int iScreenY,
1023 int iWidth, int iHeight, int iCanvasX,
1024 int iCanvasY) const {
1025 /*
1026 The map is drawn in two passes, with each pass done one scanline at a
1027 time (a scanline is a list of tiles with the same screen Y coordinate).
1028 The first pass draws the floor tiles and floor shadows, as the entire
1029 floor needs to be painted below anything else (for example, see the
1030 walking north through a door animation or death animation, which needs
1031 to paint over the floor of the scanline below the animation). On the
1032 second pass, walls and entities are drawn, with the order controlled
1033 such that entities appear in the right order relative to the walls
1034 around them. For each scanline, the following is done:
1035
1036 1st pass:
1037 1) For each tile, left to right, the floor tile (layer 0)
1038 2) The floor shadow of that tile.
1039 2nd pass:
1040 1) For each tile, right to left, the north wall, then the early entities
1041 2) For each tile, left to right, the west wall, then the late entities
1042 */
1043
1044 if (wall_blocks == nullptr || cells == nullptr) {
1045 return;
1046 }
1047
1048 // Clip to the canvas.
1049 clip_rect rcClip;
1050 rcClip.x = static_cast<clip_rect::x_y_type>(iCanvasX);
1051 rcClip.y = static_cast<clip_rect::x_y_type>(iCanvasY);
1052 rcClip.w = static_cast<clip_rect::w_h_type>(iWidth);
1053 rcClip.h = static_cast<clip_rect::w_h_type>(iHeight);
1054 render_target::scoped_clip clip(pCanvas, &rcClip);
1055
1056 // For proper hit-testing, the 'level_map::hit_test' function below should
1057 // 'paint' in the same way as here, except reversed.
1058
1059 draw_floor(pCanvas, iScreenX, iScreenY, iWidth, iHeight, iCanvasX, iCanvasY);
1060
1061 bool bFirst = true;
1062 map_scanline_iterator formerIterator;
1063 for (map_tile_iterator itrNode1(this, iScreenX, iScreenY, iWidth, iHeight);
1064 itrNode1; ++itrNode1) {
1065 if (!itrNode1.is_last_on_scanline()) {
1066 continue;
1067 }
1068
1069 for (map_scanline_iterator itrNode(
1070 itrNode1, map_scanline_iterator_direction::backward, iCanvasX,
1071 iCanvasY);
1072 itrNode; ++itrNode) {
1073 draw_north_wall(itrNode.get_tile(), itrNode.x(), itrNode.y(), pCanvas);
1074
1075 // Draw early entities.
1076 drawable* pItem = static_cast<drawable*>(itrNode->oEarlyEntities.next);
1077 while (pItem) {
1078 pItem->draw_fn(pCanvas, {itrNode.x(), itrNode.y()});
1079 pItem = static_cast<drawable*>(pItem->next);
1080 }
1081 }
1082
1083 map_scanline_iterator itrNode(
1084 itrNode1, map_scanline_iterator_direction::forward, iCanvasX, iCanvasY);
1085 if (!bFirst) {
1086 // since the scanline count from one THMapScanlineIterator to
1087 // another can differ synchronization between the current iterator
1088 // and the former one is needed
1089 if (itrNode.x() < -64) {
1090 ++itrNode;
1091 }
1092 while (formerIterator.x() < itrNode.x()) {
1093 ++formerIterator;
1094 }
1095 }
1096 bool bPreviousTileNeedsRedraw = false;
1097 for (; itrNode; ++itrNode) {
1098 bool bNeedsRedraw = false;
1099
1100 // Draw the west wall and ui layers.
1101 draw_layer(itrNode.get_tile(), itrNode.x(), itrNode.y(),
1102 tile_layer::west_wall, pCanvas);
1103 draw_layer(itrNode.get_tile(), itrNode.x(), itrNode.y(), tile_layer::ui,
1104 pCanvas);
1105
1106 int height;
1107 uint16_t layer = itrNode->tile_layers[tile_layer::north_wall];
1108 if (layer_exists(layer, height)) {
1109 bNeedsRedraw = true;
1110 }
1111 if (itrNode->oEarlyEntities.next) {
1112 bNeedsRedraw = true;
1113 }
1114
1115 bool bRedrawAnimations = false;
1116
1117 drawable* pItem = static_cast<drawable*>(itrNode->entities.next);
1118 while (pItem) {
1119 pItem->draw_fn(pCanvas, {itrNode.x(), itrNode.y()});
1120 if (pItem->is_multiple_frame_animation_fn()) {
1121 bRedrawAnimations = true;
1122 }
1123 if (pItem->get_drawing_layer() == 1) {
1124 bNeedsRedraw = true;
1125 }
1126 pItem = static_cast<drawable*>(pItem->next);
1127 }
1128
1129 // if the current tile contained a multiple frame animation (e.g. a
1130 // doctor walking) check to see if in the tile to its left and above
1131 // it there are items that need to be redrawn (i.e. in the tile to
1132 // its left side objects to the south of the tile and in the tile
1133 // above it side objects to the east of the tile).
1134 if (bRedrawAnimations && !bFirst) {
1135 bool bTileNeedsRedraw = bPreviousTileNeedsRedraw;
1136
1137 // check if an object in the adjacent tile to the left of the
1138 // current tile needs to be redrawn and if necessary draw it
1139 pItem = static_cast<drawable*>(
1140 formerIterator.get_previous_tile()->entities.next);
1141 while (pItem) {
1142 if (pItem->get_drawing_layer() == 9) {
1143 pItem->draw_fn(pCanvas,
1144 {formerIterator.x() - 64, formerIterator.y()});
1145 bTileNeedsRedraw = true;
1146 }
1147 pItem = static_cast<drawable*>(pItem->next);
1148 }
1149
1150 // check if an object in the adjacent tile above the current
1151 // tile needs to be redrawn and if necessary draw it
1152 pItem = formerIterator
1153 ? static_cast<drawable*>(formerIterator->entities.next)
1154 : nullptr;
1155 while (pItem) {
1156 if (pItem->get_drawing_layer() == 8) {
1157 pItem->draw_fn(pCanvas, {formerIterator.x(), formerIterator.y()});
1158 }
1159 pItem = static_cast<drawable*>(pItem->next);
1160 }
1161
1162 // If an object was redrawn in the tile to the left of the
1163 // current tile, or if the tile below it had an object in the
1164 // north side or a wall to the north, then redraw that tile.
1165 if (bTileNeedsRedraw) {
1166 const map_tile* prev_tile = itrNode.get_previous_tile();
1167 int prev_tile_x = itrNode.x() - 64;
1168 int prev_tile_y = itrNode.y();
1169
1170 // Redraw the north wall of the previous tile.
1171 draw_north_wall(prev_tile, prev_tile_x, prev_tile_y, pCanvas);
1172
1173 // Redraw early entities of previous tile.
1174 pItem = static_cast<drawable*>(prev_tile->oEarlyEntities.next);
1175 for (; pItem; pItem = static_cast<drawable*>(pItem->next)) {
1176 pItem->draw_fn(pCanvas, {prev_tile_x, prev_tile_y});
1177 }
1178
1179 // Redraw entities of previous tile.
1180 pItem = static_cast<drawable*>(prev_tile->entities.next);
1181 for (; pItem; pItem = static_cast<drawable*>(pItem->next)) {
1182 pItem->draw_fn(pCanvas, {prev_tile_x, prev_tile_y});
1183 }
1184 }
1185 }
1186 bPreviousTileNeedsRedraw = bNeedsRedraw;
1187 if (!bFirst) {
1188 ++formerIterator;
1189 }
1190 }
1191
1192 formerIterator = itrNode;
1193 bFirst = false;
1194 }
1195
1196 // Draw map overlay if active.
1197 if (overlay) {
1198 for (map_tile_iterator itrNode(this, iScreenX, iScreenY, iWidth, iHeight);
1199 itrNode; ++itrNode) {
1200 overlay->draw_cell(pCanvas,
1201 itrNode.tile_x_position_on_screen() + iCanvasX - 32,
1202 itrNode.tile_y_position_on_screen() + iCanvasY, this,
1203 itrNode.tile_x(), itrNode.tile_y());
1204 }
1205 }
1206 }
1207
1208 drawable* level_map::hit_test(int iTestX, int iTestY) const {
1209 // This function needs to hitTest each drawable object, in the reverse
1210 // order to that in which they would be drawn.
1211
1212 if (wall_blocks == nullptr || cells == nullptr) {
1213 return nullptr;
1214 }
1215
1216 for (map_tile_iterator itrNode2(this, iTestX, iTestY, 1, 1,
1217 map_scanline_iterator_direction::backward);
1218 itrNode2; ++itrNode2) {
1219 if (!itrNode2.is_last_on_scanline()) {
1220 continue;
1221 }
1222
1223 for (map_scanline_iterator itrNode(
1224 itrNode2, map_scanline_iterator_direction::backward);
1225 itrNode; ++itrNode) {
1226 if (itrNode->entities.next != nullptr) {
1227 drawable* pResult = hit_test_drawables(itrNode->entities.next,
1228 itrNode.x(), itrNode.y(), 0, 0);
1229 if (pResult) {
1230 return pResult;
1231 }
1232 }
1233 }
1234 for (map_scanline_iterator itrNode(
1235 itrNode2, map_scanline_iterator_direction::forward);
1236 itrNode; ++itrNode) {
1237 if (itrNode->oEarlyEntities.next != nullptr) {
1238 drawable* pResult = hit_test_drawables(itrNode->oEarlyEntities.next,
1239 itrNode.x(), itrNode.y(), 0, 0);
1240 if (pResult) {
1241 return pResult;
1242 }
1243 }
1244 }
1245 }
1246
1247 return nullptr;
1248 }
1249
1250 drawable* level_map::hit_test_drawables(link_list* pListStart, int iXs, int iYs,
1251 int iTestX, int iTestY) const {
1252 link_list* pListEnd = pListStart;
1253 while (pListEnd->next) {
1254 pListEnd = pListEnd->next;
1255 }
1256 drawable* pList = static_cast<drawable*>(pListEnd);
1257
1258 while (true) {
1259 if (pList->hit_test_fn({iXs, iYs}, {iTestX, iTestY})) return pList;
1260
1261 if (pList == pListStart) {
1262 return nullptr;
1263 } else {
1264 pList = static_cast<drawable*>(pList->prev);
1265 }
1266 }
1267 }
1268
1269 int level_map::get_tile_owner(const map_tile* pNode) const {
1270 uint16_t parcelId = pNode->iParcelId;
1271 return get_parcel_owner(parcelId);
1272 }
1273
1274 int level_map::get_parcel_owner(int iParcel) const {
1275 if (0 <= iParcel && iParcel < parcel_count) {
1276 return plot_owner[iParcel];
1277 } else {
1278 return 0;
1279 }
1280 }
1281
1282 uint16_t level_map::get_tile_temperature(const map_tile* pNode) const {
1283 return pNode->aiTemperature[current_temperature_index];
1284 }
1285
1286 void level_map::set_temperature_display(temperature_theme eTempDisplay) {
1287 current_temperature_theme = eTempDisplay;
1288 }
1289
1290 uint32_t level_map::thermal_neighbour(uint32_t& iNeighbourSum, bool canTravel,
1291 std::ptrdiff_t relative_idx,
1292 map_tile* pNode, int prevTemp) const {
1293 int iNeighbourCount = 0;
1294
1295 map_tile* pNeighbour = pNode + relative_idx;
1296
1297 // Ensure the neighbour is within the map bounds
1298 map_tile* pLimitNode = cells + width * height;
1299 if (pNeighbour < cells || pNeighbour >= pLimitNode) {
1300 return 0;
1301 }
1302
1303 if (canTravel) {
1304 iNeighbourCount += 4;
1305 iNeighbourSum += pNeighbour->aiTemperature[prevTemp] * 4;
1306 } else {
1307 bool bObjectPresent = false;
1308 int iHospital1 = pNeighbour->flags.hospital;
1309 int iHospital2 = pNode->flags.hospital;
1310 if (iHospital1 == iHospital2) {
1311 if (pNeighbour->flags.room == pNode->flags.room) {
1312 bObjectPresent = true;
1313 }
1314 }
1315 if (bObjectPresent) {
1316 iNeighbourCount += 4;
1317 iNeighbourSum += pNeighbour->aiTemperature[prevTemp] * 4;
1318 } else {
1319 iNeighbourCount += 1;
1320 iNeighbourSum += pNeighbour->aiTemperature[prevTemp];
1321 }
1322 }
1323
1324 return iNeighbourCount;
1325 }
1326
1327 namespace {
1328
1329 //! Merge temperature at a tile with the external merge temperature.
1330 /*!
1331 \param node Tile node to update.
1332 \param temp_idx Index of the temperature to update at the tile.
1333 \param other_temp External temperature to merge.
1334 \param ratio Weight of the old node temperature, \c N-1 parts of
1335 the node temperature and \c 1 part of the external temperature.
1336 */
1337 void merge_temperatures(map_tile& node, size_t temp_idx, uint32_t other_temp,
1338 double ratio) {
1339 const uint32_t node_temp = node.aiTemperature[temp_idx];
1340 node.aiTemperature[temp_idx] =
1341 static_cast<uint16_t>(((node_temp * (ratio - 1)) + other_temp) / ratio);
1342 }
1343
1344 } // namespace
1345
1346 void level_map::update_temperatures(uint16_t iAirTemperature,
1347 uint16_t iRadiatorTemperature) {
1348 if (iRadiatorTemperature < iAirTemperature) {
1349 iRadiatorTemperature = iAirTemperature;
1350 }
1351 const int iPrevTemp = current_temperature_index;
1352 current_temperature_index ^= 1;
1353 const int iNewTemp = current_temperature_index;
1354
1355 map_tile* pLimitNode = cells + width * height;
1356 for (map_tile* pNode = cells; pNode != pLimitNode; ++pNode) {
1357 // Get average temperature of neighbour cells
1358 uint32_t iNeighbourSum = 0;
1359 uint32_t iNeighbourCount = 0;
1360
1361 iNeighbourCount += thermal_neighbour(
1362 iNeighbourSum, pNode->flags.can_travel_n, -width, pNode, iPrevTemp);
1363 iNeighbourCount += thermal_neighbour(
1364 iNeighbourSum, pNode->flags.can_travel_s, width, pNode, iPrevTemp);
1365 iNeighbourCount += thermal_neighbour(
1366 iNeighbourSum, pNode->flags.can_travel_e, 1, pNode, iPrevTemp);
1367 iNeighbourCount += thermal_neighbour(
1368 iNeighbourSum, pNode->flags.can_travel_w, -1, pNode, iPrevTemp);
1369
1370 uint32_t iMergeTemp = 0;
1371 double mergeRatio = 100;
1372 if (pNode->flags.hospital) {
1373 bool hasRadiator = false;
1374 for (auto thob : pNode->objects) {
1375 if (thob == object_type::radiator) {
1376 hasRadiator = true;
1377 break;
1378 }
1379 }
1380 if (hasRadiator) {
1381 iMergeTemp = iRadiatorTemperature;
1382 mergeRatio = 2; // Merge 50% against radiator temperature.
1383 } else {
1384 iMergeTemp = 0;
1385 mergeRatio = 1000; // Generally dissipate 0.1% of temperature.
1386 }
1387 } else {
1388 iMergeTemp = iAirTemperature;
1389 mergeRatio = 100; // Merge 1% against air temperature.
1390 }
1391
1392 // Diffuse 25% with neighbours
1393 pNode->aiTemperature[iNewTemp] = pNode->aiTemperature[iPrevTemp];
1394 if (iNeighbourCount != 0) {
1395 merge_temperatures(*pNode, iNewTemp, iNeighbourSum / iNeighbourCount, 4);
1396 }
1397
1398 merge_temperatures(*pNode, iNewTemp, iMergeTemp, mergeRatio);
1399 }
1400 }
1401
1402 void level_map::update_pathfinding() {
1403 map_tile* pNode = cells;
1404 for (int iY = 0; iY < this->height; ++iY) {
1405 for (int iX = 0; iX < this->width; ++iX, ++pNode) {
1406 pNode->flags.can_travel_n = true;
1407 pNode->flags.can_travel_e = true;
1408 pNode->flags.can_travel_s = true;
1409 pNode->flags.can_travel_w = true;
1410
1411 if (iX == 0) {
1412 pNode->flags.can_travel_w = false;
1413 } else if (iX == 127) {
1414 pNode->flags.can_travel_e = false;
1415 }
1416
1417 if (iY == 0) {
1418 pNode->flags.can_travel_n = false;
1419 } else if (iY == 127) {
1420 pNode->flags.can_travel_s = false;
1421 }
1422
1423 if (pNode->tile_layers[tile_layer::north_wall] & 0xFF) {
1424 pNode->flags.can_travel_n = false;
1425 if (iY != 0) {
1426 pNode[-this->width].flags.can_travel_s = false;
1427 }
1428 }
1429 if (pNode->tile_layers[tile_layer::west_wall] & 0xFF) {
1430 pNode->flags.can_travel_w = false;
1431 if (iX != 0) {
1432 pNode[-1].flags.can_travel_e = false;
1433 }
1434 }
1435 }
1436 }
1437 }
1438
1439 namespace {
1440
1441 //! For shadow casting, a tile is considered to have a wall on a direction
1442 //! if it has a door in that direction, or the block is from the hardcoded
1443 //! range of wall-like blocks.
1444 bool is_wall(map_tile* tile, tile_layer layer, bool flag) {
1445 return flag || (82 <= (tile->tile_layers[layer] & 0xFF) &&
1446 (tile->tile_layers[layer] & 0xFF) <= 164);
1447 }
1448
1449 } // namespace
1450
1451 void level_map::update_shadows() {
1452 map_tile* pNode = cells;
1453 for (int iY = 0; iY < this->height; ++iY) {
1454 for (int iX = 0; iX < this->width; ++iX, ++pNode) {
1455 pNode->flags.shadow_full = false;
1456 pNode->flags.shadow_half = false;
1457 pNode->flags.shadow_wall = false;
1458 if (is_wall(pNode, tile_layer::west_wall, pNode->flags.tall_west)) {
1459 pNode->flags.shadow_half = true;
1460 if (is_wall(pNode, tile_layer::north_wall, pNode->flags.tall_north)) {
1461 pNode->flags.shadow_wall = true;
1462 } else if (iY != 0) {
1463 map_tile* pNeighbour = pNode - this->width;
1464 pNeighbour->flags.shadow_full = true;
1465 if (iX != 0 && !is_wall(pNeighbour, tile_layer::west_wall,
1466 pNode->flags.tall_west)) {
1467 // Wrap the shadow around a corner (no need to continue
1468 // all the way along the wall, as the shadow would be
1469 // occluded by the wall. If Debug->Transparent Walls is
1470 // toggled on, then this optimisation becomes very
1471 // visible, but it's a debug option, so it doesn't
1472 // matter).
1473 pNeighbour[-1].flags.shadow_full = true;
1474 }
1475 }
1476 }
1477 }
1478 }
1479 }
1480
1481 void level_map::persist(lua_persist_writer* pWriter) const {
1482 lua_State* L = pWriter->get_stack();
1483
1484 uint32_t iVersion = 5;
1485 pWriter->write_uint(iVersion);
1486 pWriter->write_uint(player_count);
1487 for (int i = 0; i < player_count; ++i) {
1488 pWriter->write_uint(initial_camera_x[i]);
1489 pWriter->write_uint(initial_camera_y[i]);
1490 pWriter->write_uint(heliport_x[i]);
1491 pWriter->write_uint(heliport_y[i]);
1492 }
1493 pWriter->write_uint(parcel_count);
1494 for (int i = 0; i < parcel_count; ++i) {
1495 pWriter->write_uint(plot_owner[i]);
1496 }
1497 for (int i = 0; i < parcel_count; ++i) {
1498 pWriter->write_uint(parcel_tile_counts[i]);
1499 }
1500 pWriter->write_uint(width);
1501 pWriter->write_uint(height);
1502 pWriter->write_uint(current_temperature_index);
1503 integer_run_length_encoder oEncoder(6);
1504 for (map_tile *pNode = cells, *pLimitNode = cells + width * height;
1505 pNode != pLimitNode; ++pNode) {
1506 oEncoder.write(pNode->tile_layers[tile_layer::ground]);
1507 oEncoder.write(pNode->tile_layers[tile_layer::north_wall]);
1508 oEncoder.write(pNode->tile_layers[tile_layer::west_wall]);
1509 oEncoder.write(pNode->tile_layers[tile_layer::ui]);
1510 oEncoder.write(pNode->iParcelId);
1511 oEncoder.write(pNode->iRoomId);
1512 // Flags include THOB values, and other things which do not work
1513 // well with run-length encoding.
1514 pWriter->write_uint(static_cast<uint32_t>(pNode->flags));
1515 pWriter->write_uint(pNode->aiTemperature[0]);
1516 pWriter->write_uint(pNode->aiTemperature[1]);
1517 pWriter->write_byte_stream(pNode->raw, map_tile::raw_length);
1518
1519 lua_rawgeti(L, luaT_upvalueindex(1), 2);
1520 lua_pushlightuserdata(L, pNode->entities.next);
1521 lua_rawget(L, -2);
1522 pWriter->write_stack_object(-1);
1523 lua_pop(L, 1);
1524 lua_pushlightuserdata(L, pNode->oEarlyEntities.next);
1525 lua_rawget(L, -2);
1526 pWriter->write_stack_object(-1);
1527 lua_pop(L, 2);
1528 }
1529 oEncoder.finish();
1530 oEncoder.pump_output(pWriter);
1531
1532 oEncoder = integer_run_length_encoder(5);
1533 for (map_tile *pNode = original_cells,
1534 *pLimitNode = original_cells + width * height;
1535 pNode != pLimitNode; ++pNode) {
1536 oEncoder.write(pNode->tile_layers[tile_layer::ground]);
1537 oEncoder.write(pNode->tile_layers[tile_layer::north_wall]);
1538 oEncoder.write(pNode->tile_layers[tile_layer::west_wall]);
1539 oEncoder.write(pNode->iParcelId);
1540 oEncoder.write(static_cast<uint32_t>(pNode->flags));
1541 }
1542 oEncoder.finish();
1543 oEncoder.pump_output(pWriter);
1544 }
1545
1546 namespace {
1547 //! Assign the tile position in the map to the just loaded animations.
1548 void restore_map_position(animation_base* anim, int tile_x_pos,
1549 int tile_y_pos) {
1550 while (anim != nullptr) {
1551 anim->set_tile({tile_x_pos, tile_y_pos});
1552 anim = dynamic_cast<animation_base*>(anim->next);
1553 }
1554 }
1555
1556 } // namespace
1557
1558 void level_map::depersist(lua_persist_reader* pReader) {
1559 lua_State* L = pReader->get_stack();
1560 int iWidth, iHeight;
1561
1562 uint32_t iVersion;
1563 if (!pReader->read_uint(iVersion)) return;
1564 if (iVersion < 2 || iVersion == 128) {
1565 luaL_error(L,
1566 "TODO: Write code to load map data from earlier "
1567 "savegame versions (if really necessary).");
1568 } else if (iVersion > 5) {
1569 luaL_error(L, "Cannot load savegame from a newer version.");
1570 }
1571 if (!pReader->read_uint(player_count)) return;
1572 for (int i = 0; i < player_count; ++i) {
1573 if (!pReader->read_uint(initial_camera_x[i])) return;
1574 if (!pReader->read_uint(initial_camera_y[i])) return;
1575 if (!pReader->read_uint(heliport_x[i])) return;
1576 if (!pReader->read_uint(heliport_y[i])) return;
1577 }
1578 if (!pReader->read_uint(parcel_count)) {
1579 return;
1580 }
1581 delete[] plot_owner;
1582 plot_owner = new int[parcel_count];
1583 for (int i = 0; i < parcel_count; ++i) {
1584 if (!pReader->read_uint(plot_owner[i])) {
1585 return;
1586 }
1587 }
1588 delete[] parcel_tile_counts;
1589 parcel_tile_counts = new int[parcel_count];
1590 parcel_tile_counts[0] = 0;
1591
1592 if (iVersion >= 3) {
1593 for (int i = 0; i < parcel_count; ++i) {
1594 if (!pReader->read_uint(parcel_tile_counts[i])) {
1595 return;
1596 }
1597 }
1598 }
1599
1600 if (!pReader->read_uint(iWidth) || !pReader->read_uint(iHeight)) {
1601 return;
1602 }
1603 if (!set_size(iWidth, iHeight)) {
1604 pReader->set_error("Unable to set size while depersisting map");
1605 return;
1606 }
1607 if (iVersion >= 4) {
1608 if (!pReader->read_uint(current_temperature_index)) {
1609 return;
1610 }
1611 }
1612
1613 int tile_x_pos = -1;
1614 int tile_y_pos = 0;
1615 for (map_tile *pNode = cells, *pLimitNode = cells + width * height;
1616 pNode != pLimitNode; ++pNode) {
1617 // Update tile 2D position.
1618 tile_x_pos++;
1619 if (tile_x_pos == width) {
1620 tile_x_pos = 0;
1621 tile_y_pos++;
1622 }
1623
1624 uint32_t f;
1625 if (!pReader->read_uint(f)) return;
1626
1627 pNode->flags = f;
1628 if (iVersion >= 4) {
1629 if (!pReader->read_uint(pNode->aiTemperature[0]) ||
1630 !pReader->read_uint(pNode->aiTemperature[1])) {
1631 return;
1632 }
1633 }
1634 if (iVersion >= 5) {
1635 if (!pReader->read_byte_stream(pNode->raw, map_tile::raw_length)) return;
1636 }
1637
1638 if (!pReader->read_stack_object()) return;
1639
1640 animation_base* anim = luaT_toanimationbase(L, -1);
1641 restore_map_position(anim, tile_x_pos, tile_y_pos);
1642
1643 pNode->entities.next = anim;
1644 if (pNode->entities.next) {
1645 if (pNode->entities.next->prev != nullptr) {
1646 std::fprintf(stderr, "Warning: THMap linked-lists are corrupted.\n");
1647 }
1648 pNode->entities.next->prev = &pNode->entities;
1649 }
1650
1651 lua_pop(L, 1);
1652 if (!pReader->read_stack_object()) return;
1653
1654 animation_base* early_anim = luaT_toanimationbase(L, -1);
1655 restore_map_position(early_anim, tile_x_pos, tile_y_pos);
1656
1657 pNode->oEarlyEntities.next = early_anim;
1658 if (pNode->oEarlyEntities.next) {
1659 if (pNode->oEarlyEntities.next->prev != nullptr) {
1660 std::fprintf(stderr, "Warning: THMap linked-lists are corrupted.\n");
1661 }
1662 pNode->oEarlyEntities.next->prev = &pNode->oEarlyEntities;
1663 }
1664
1665 lua_pop(L, 1);
1666 }
1667
1668 integer_run_length_decoder oDecoder(6, pReader);
1669 for (map_tile *pNode = cells, *pLimitNode = cells + width * height;
1670 pNode != pLimitNode; ++pNode) {
1671 pNode->tile_layers[tile_layer::ground] =
1672 static_cast<uint16_t>(oDecoder.read());
1673 pNode->tile_layers[tile_layer::north_wall] =
1674 static_cast<uint16_t>(oDecoder.read());
1675 pNode->tile_layers[tile_layer::west_wall] =
1676 static_cast<uint16_t>(oDecoder.read());
1677 pNode->tile_layers[tile_layer::ui] = static_cast<uint16_t>(oDecoder.read());
1678 pNode->iParcelId = static_cast<uint16_t>(oDecoder.read());
1679 pNode->iRoomId = static_cast<uint16_t>(oDecoder.read());
1680 }
1681
1682 oDecoder = integer_run_length_decoder(5, pReader);
1683 for (map_tile *pNode = original_cells,
1684 *pLimitNode = original_cells + width * height;
1685 pNode != pLimitNode; ++pNode) {
1686 pNode->tile_layers[tile_layer::ground] =
1687 static_cast<uint16_t>(oDecoder.read());
1688 pNode->tile_layers[tile_layer::north_wall] =
1689 static_cast<uint16_t>(oDecoder.read());
1690 pNode->tile_layers[tile_layer::west_wall] =
1691 static_cast<uint16_t>(oDecoder.read());
1692 pNode->iParcelId = static_cast<uint16_t>(oDecoder.read());
1693 pNode->flags = oDecoder.read();
1694 }
1695
1696 if (iVersion < 3) {
1697 for (int i = 1; i < parcel_count; ++i) {
1698 parcel_tile_counts[i] = get_parcel_tile_count(i);
1699 }
1700 }
1701 }
1702
1703 map_tile_iterator::map_tile_iterator(
1704 const level_map* pMap, int iScreenX, int iScreenY, int iWidth, int iHeight,
1705 map_scanline_iterator_direction eScanlineDirection)
1706 : container(pMap),
1707 screen_offset_x(iScreenX),
1708 screen_offset_y(iScreenY),
1709 screen_width(iWidth),
1710 screen_height(iHeight),
1711 direction(eScanlineDirection) {
1712 if (direction == map_scanline_iterator_direction::forward) {
1713 base_x = 0;
1714 base_y = (iScreenY - 32) / 16;
1715 if (base_y < 0) {
1716 base_y = 0;
1717 } else if (base_y >= container->get_height()) {
1718 base_x = base_y - container->get_height() + 1;
1719 base_y = container->get_height() - 1;
1720 if (base_x >= container->get_width()) base_x = container->get_width() - 1;
1721 }
1722 } else {
1723 base_x = container->get_width() - 1;
1724 base_y = container->get_height() - 1;
1725 }
1726 world_x = base_x;
1727 world_y = base_y;
1728 advance_until_visible();
1729 }
1730
1731 map_tile_iterator& map_tile_iterator::operator++() {
1732 --world_y;
1733 ++world_x;
1734 advance_until_visible();
1735 return *this;
1736 }
1737
1738 void map_tile_iterator::advance_until_visible() {
1739 ZoneScoped;
1740 tile = nullptr;
1741
1742 while (true) {
1743 x_relative_to_screen = world_x;
1744 y_relative_to_screen = world_y;
1745 container->world_to_screen(x_relative_to_screen, y_relative_to_screen);
1746 x_relative_to_screen -= screen_offset_x;
1747 y_relative_to_screen -= screen_offset_y;
1748 if (direction == map_scanline_iterator_direction::forward
1749 ? y_relative_to_screen >= screen_height + margin_bottom
1750 : y_relative_to_screen < -margin_top) {
1751 return;
1752 }
1753 if (direction == map_scanline_iterator_direction::forward
1754 ? (y_relative_to_screen > -margin_top)
1755 : (y_relative_to_screen < screen_height + margin_bottom)) {
1756 while (world_y >= 0 && world_x < container->get_width()) {
1757 if (x_relative_to_screen < -margin_left) {
1758 // Nothing to do
1759 } else if (x_relative_to_screen < screen_width + margin_right) {
1760 ++scanline_count;
1761 tile = container->get_tile_unchecked(world_x, world_y);
1762 return;
1763 } else {
1764 break;
1765 }
1766 --world_y;
1767 ++world_x;
1768 x_relative_to_screen += 64;
1769 }
1770 }
1771 scanline_count = 0;
1772 if (direction == map_scanline_iterator_direction::forward) {
1773 if (base_y == container->get_height() - 1) {
1774 if (++base_x == container->get_width()) {
1775 break;
1776 }
1777 } else {
1778 ++base_y;
1779 }
1780 } else {
1781 if (base_x == 0) {
1782 if (base_y == 0) {
1783 break;
1784 } else {
1785 --base_y;
1786 }
1787 } else {
1788 --base_x;
1789 }
1790 }
1791 world_x = base_x;
1792 world_y = base_y;
1793 }
1794 }
1795
1796 bool map_tile_iterator::is_last_on_scanline() const {
1797 return world_y <= 0 || world_x + 1 >= container->get_width() ||
1798 x_relative_to_screen + 64 >= screen_width + margin_right;
1799 }
1800
1801 map_scanline_iterator::map_scanline_iterator()
1802 : tile(nullptr),
1803 first_tile(nullptr),
1804 end_tile(nullptr),
1805 tile_step(0),
1806 x_step(0),
1807 x_relative_to_screen(0),
1808 y_relative_to_screen(0) {}
1809
1810 map_scanline_iterator::map_scanline_iterator(
1811 const map_tile_iterator& itrNodes,
1812 map_scanline_iterator_direction eDirection, int iXOffset, int iYOffset)
1813 : tile_step((static_cast<int>(eDirection) - 1) *
1814 (1 - itrNodes.container->get_width())),
1815 x_step((static_cast<int>(eDirection) - 1) * 64) {
1816 if (eDirection == map_scanline_iterator_direction::backward) {
1817 tile = itrNodes.tile;
1818 x_relative_to_screen = itrNodes.tile_x_position_on_screen();
1819 } else {
1820 tile = itrNodes.tile - tile_step * (itrNodes.scanline_count - 1);
1821 x_relative_to_screen = itrNodes.tile_x_position_on_screen() -
1822 x_step * (itrNodes.scanline_count - 1);
1823 }
1824
1825 x_relative_to_screen += iXOffset;
1826 y_relative_to_screen = itrNodes.tile_y_position_on_screen() + iYOffset;
1827
1828 end_tile = tile + tile_step * itrNodes.scanline_count;
1829 first_tile = tile;
1830 }
1831
1832 map_scanline_iterator& map_scanline_iterator::operator++() {
1833 tile += tile_step;
1834 x_relative_to_screen += x_step;
1835 steps_taken++;
1836 return *this;
1837 }
1838
1839 // copies the members of the given THMapScanlineIterator and resets the tile
1840 // member to the first element.
1841 map_scanline_iterator map_scanline_iterator::operator=(
1842 const map_scanline_iterator& iterator) {
1843 tile = iterator.first_tile;
1844 end_tile = iterator.end_tile;
1845 x_relative_to_screen =
1846 iterator.x_relative_to_screen - iterator.steps_taken * iterator.x_step;
1847 y_relative_to_screen = iterator.y_relative_to_screen;
1848 x_step = iterator.x_step;
1849 tile_step = iterator.tile_step;
1850 return *this;
1851 }