| 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 |
#ifndef CORSIX_TH_TH_MAP_H_ |
| 24 |
#define CORSIX_TH_TH_MAP_H_ |
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <list> |
| 28 |
#include <string> |
| 29 |
#include <utility> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "th.h" |
| 33 |
|
| 34 |
class lua_persist_reader; |
| 35 |
class lua_persist_writer; |
| 36 |
class map_overlay; |
| 37 |
class render_target; |
| 38 |
class sprite_sheet; |
| 39 |
struct drawable; |
| 40 |
|
| 41 |
/* |
| 42 |
Object type enumeration uses same values as original TH does. |
| 43 |
See game string table section 39 for proof. Section 1 also has |
| 44 |
names in this order. |
| 45 |
*/ |
| 46 |
enum class object_type : uint8_t { |
| 47 |
no_object = 0, |
| 48 |
desk = 1, |
| 49 |
cabinet = 2, |
| 50 |
door = 3, |
| 51 |
bench = 4, |
| 52 |
table = 5, // Not in game |
| 53 |
chair = 6, |
| 54 |
drinks_machine = 7, |
| 55 |
bed = 8, |
| 56 |
inflator = 9, |
| 57 |
pool_table = 10, |
| 58 |
reception_desk = 11, |
| 59 |
b_table = 12, // Not in game? |
| 60 |
cardio = 13, |
| 61 |
scanner = 14, |
| 62 |
scanner_console = 15, |
| 63 |
screen = 16, |
| 64 |
litter_bomb = 17, |
| 65 |
couch = 18, |
| 66 |
sofa = 19, |
| 67 |
crash = 20, // The trolley in general diagnosis |
| 68 |
tv = 21, |
| 69 |
ultrascan = 22, |
| 70 |
dna_fixer = 23, |
| 71 |
cast_remover = 24, |
| 72 |
hair_restorer = 25, |
| 73 |
slicer = 26, |
| 74 |
xray = 27, |
| 75 |
radiation_shield = 28, |
| 76 |
xray_viewer = 29, |
| 77 |
op_table = 30, |
| 78 |
lamp = 31, // Not in game? |
| 79 |
sink = 32, |
| 80 |
op_sink1 = 33, |
| 81 |
op_sink2 = 34, |
| 82 |
surgeon_screen = 35, |
| 83 |
lecture_chair = 36, |
| 84 |
projector = 37, |
| 85 |
// 38 is unused |
| 86 |
pharmacy = 39, |
| 87 |
computer = 40, |
| 88 |
chemical_mixer = 41, |
| 89 |
blood_machine = 42, |
| 90 |
extinguisher = 43, |
| 91 |
radiator = 44, |
| 92 |
plant = 45, |
| 93 |
electro = 46, |
| 94 |
jelly_vat = 47, |
| 95 |
hell = 48, |
| 96 |
// 49 is unused |
| 97 |
bin = 50, |
| 98 |
loo = 51, |
| 99 |
double_door1 = 52, |
| 100 |
double_door2 = 53, |
| 101 |
decon_shower = 54, |
| 102 |
autopsy = 55, |
| 103 |
bookcase = 56, |
| 104 |
video_game = 57, |
| 105 |
entrance_left_door = 58, |
| 106 |
entrance_right_door = 59, |
| 107 |
skeleton = 60, |
| 108 |
comfy_chair = 61, |
| 109 |
litter = 62, |
| 110 |
helicopter = 63, |
| 111 |
rathole = 64, |
| 112 |
// 65 through 255 are unused |
| 113 |
}; |
| 114 |
|
| 115 |
//! Map flags and object type |
| 116 |
//! The point of storing the object type here is to allow pathfinding code |
| 117 |
//! to use object types as pathfinding goals. |
| 118 |
struct map_tile_flags { |
| 119 |
enum class key : uint32_t { |
| 120 |
passable_mask = 1 << 0, |
| 121 |
can_travel_n_mask = 1 << 1, |
| 122 |
can_travel_e_mask = 1 << 2, |
| 123 |
can_travel_s_mask = 1 << 3, |
| 124 |
can_travel_w_mask = 1 << 4, |
| 125 |
hospital_mask = 1 << 5, |
| 126 |
buildable_mask = 1 << 6, |
| 127 |
passable_if_not_for_blueprint_mask = 1 << 7, |
| 128 |
room_mask = 1 << 8, |
| 129 |
shadow_half_mask = 1 << 9, |
| 130 |
shadow_full_mask = 1 << 10, |
| 131 |
shadow_wall_mask = 1 << 11, |
| 132 |
door_north_mask = 1 << 12, |
| 133 |
door_west_mask = 1 << 13, |
| 134 |
do_not_idle_mask = 1 << 14, |
| 135 |
tall_north_mask = 1 << 15, |
| 136 |
tall_west_mask = 1 << 16, |
| 137 |
buildable_n_mask = 1 << 17, |
| 138 |
buildable_e_mask = 1 << 18, |
| 139 |
buildable_s_mask = 1 << 19, |
| 140 |
buildable_w_mask = 1 << 20, |
| 141 |
}; |
| 142 |
|
| 143 |
bool passable; //!< Pathfinding: Can walk on this tile |
| 144 |
bool can_travel_n; //!< Pathfinding: Can walk to the north |
| 145 |
bool can_travel_e; //!< Pathfinding: Can walk to the east |
| 146 |
bool can_travel_s; //!< Pathfinding: Can walk to the south |
| 147 |
bool can_travel_w; //!< Pathfinding: Can walk to the west |
| 148 |
bool hospital; //!< World: Tile is inside a hospital building |
| 149 |
bool buildable; //!< Player: Can build on this tile |
| 150 |
bool passable_if_not_for_blueprint; |
| 151 |
bool room; //!< World: Tile is inside a room |
| 152 |
bool shadow_half; //!< Rendering: Put block 75 over floor |
| 153 |
bool shadow_full; //!< Rendering: Put block 74 over floor |
| 154 |
bool shadow_wall; //!< Rendering: Put block 156 over east wall |
| 155 |
bool door_north; //!< World: Door on north wall of tile |
| 156 |
bool door_west; //!< World: Door on west wall of tile |
| 157 |
bool do_not_idle; //!< World: Humanoids should not idle on tile |
| 158 |
bool tall_north; //!< Shadows: Wall-like object on north wall |
| 159 |
bool tall_west; //!< Shadows: Wall-like object on west wall |
| 160 |
bool buildable_n; //!< Can build on the north side of the tile |
| 161 |
bool buildable_e; //!< Can build on the east side of the tile |
| 162 |
bool buildable_s; //!< Can build on the south side of the tile |
| 163 |
bool buildable_w; //!< Can build on the west side of the tile |
| 164 |
|
| 165 |
//! Convert the given uint32_t representation of the map_tile flags |
| 166 |
//! to a map_tile_flags instance. |
| 167 |
map_tile_flags& operator=(uint32_t raw); |
| 168 |
|
| 169 |
//! Get/set the flag with the given key |
| 170 |
bool& operator[](map_tile_flags::key key); |
| 171 |
|
| 172 |
//! Get the flag with the given key |
| 173 |
const bool& operator[](map_tile_flags::key key) const; |
| 174 |
|
| 175 |
//! Convert map_tile_flags into it's uint32_t representation |
| 176 |
operator uint32_t() const; |
| 177 |
}; |
| 178 |
|
| 179 |
enum class temperature_theme { |
| 180 |
red, //!< Default warmth colouring (red gradients) |
| 181 |
multi_colour, //!< Different colours (blue, green, red) |
| 182 |
yellow_red //!< Gradients of yellow, orange, and red |
| 183 |
}; |
| 184 |
|
| 185 |
enum tile_layer : uint8_t { |
| 186 |
ground = 0, |
| 187 |
north_wall = 1, |
| 188 |
west_wall = 2, |
| 189 |
ui = 3, |
| 190 |
|
| 191 |
num_tile_layers = 4 //!< Number of layers in a tile. |
| 192 |
}; |
| 193 |
|
| 194 |
struct map_tile { |
| 195 |
map_tile() = default; |
| 196 |
~map_tile() = default; |
| 197 |
|
| 198 |
// Linked list for entities rendered at this tile |
| 199 |
link_list entities; |
| 200 |
|
| 201 |
//! Linked list for entities rendered in an early (right-to-left) pass |
| 202 |
link_list oEarlyEntities; |
| 203 |
|
| 204 |
//! Tile layers for rendering. |
| 205 |
//! For each layer, the lower byte is the index in the sprite sheet, and the |
| 206 |
//! upper byte is for the drawing flags. |
| 207 |
//! Layer 0 is for the floor |
| 208 |
//! Layer 1 is for the north wall |
| 209 |
//! Layer 2 is for the west wall |
| 210 |
//! Layer 3 is for the UI |
| 211 |
//! NB: In Lua, layers are numbered 1 - 4 rather than 0 - 3 |
| 212 |
uint16_t tile_layers[tile_layer::num_tile_layers]{}; |
| 213 |
|
| 214 |
//! Parcels (plots) of land have an ID, with each tile in the plot having |
| 215 |
//! that ID. Parcel 0 is the outside. |
| 216 |
uint16_t iParcelId{0}; |
| 217 |
|
| 218 |
//! Rooms have an ID, with room #0 being the corridor (and the outside). |
| 219 |
uint16_t iRoomId{0}; |
| 220 |
|
| 221 |
//! A value between 0 (extreme cold) and 65535 (extreme heat) representing |
| 222 |
//! the temperature of the tile. To allow efficient calculation of a tile's |
| 223 |
//! heat based on the previous tick's heat of the surrounding tiles, the |
| 224 |
//! previous temperature is also stored, with the array indices switching |
| 225 |
//! every tick. |
| 226 |
uint16_t aiTemperature[2]{8192, 8192}; |
| 227 |
|
| 228 |
//! Flags for information and object type |
| 229 |
map_tile_flags flags{}; |
| 230 |
|
| 231 |
//! objects in this tile |
| 232 |
std::list<object_type> objects; |
| 233 |
|
| 234 |
static constexpr int raw_length = 8; |
| 235 |
|
| 236 |
//! raw data from map file for debugging |
| 237 |
std::uint8_t raw[raw_length]{}; |
| 238 |
}; |
| 239 |
|
| 240 |
//! Prototype for object callbacks from THMap::loadFromTHFile |
| 241 |
/*! |
| 242 |
The callback function will receive 5 arguments: |
| 243 |
* The opaque pointer passed to THMap::loadFromTHFile (pCallbackToken). |
| 244 |
* The tile X/Y position of the object. |
| 245 |
* The object type. |
| 246 |
* The object flags present in the map data. The meaning of this |
| 247 |
value is left unspecified. |
| 248 |
*/ |
| 249 |
typedef void (*map_load_object_callback_fn)(void*, int, int, object_type, |
| 250 |
uint8_t); |
| 251 |
|
| 252 |
class level_map { |
| 253 |
public: |
| 254 |
level_map() = default; |
| 255 |
~level_map(); |
| 256 |
|
| 257 |
bool set_size(int iWidth, int iHeight); |
| 258 |
bool load_blank(); |
| 259 |
bool load_from_th_file(const uint8_t* pData, size_t iDataLength, |
| 260 |
map_load_object_callback_fn fnObjectCallback, |
| 261 |
void* pCallbackToken); |
| 262 |
|
| 263 |
void save(const std::string& filename); |
| 264 |
|
| 265 |
//! Set the sprite sheet to be used for drawing the map |
| 266 |
/*! |
| 267 |
The sprites for map floor tiles, wall tiles, and map decorators |
| 268 |
all come from the given sheet. |
| 269 |
*/ |
| 270 |
void set_block_sheet(sprite_sheet* pSheet); |
| 271 |
|
| 272 |
//! Set the draw flags on all wall blocks |
| 273 |
/*! |
| 274 |
This is typically called with THDF_Alpha50 to draw walls transparently, |
| 275 |
or with 0 to draw them opaque again. |
| 276 |
*/ |
| 277 |
void set_all_wall_draw_flags(uint8_t iFlags); |
| 278 |
|
| 279 |
void update_pathfinding(); |
| 280 |
void update_shadows(); |
| 281 |
void set_temperature_display(temperature_theme eTempDisplay); |
| 282 |
inline temperature_theme get_temperature_display() const { |
| 283 |
return current_temperature_theme; |
| 284 |
} |
| 285 |
void update_temperatures(uint16_t iAirTemperature, |
| 286 |
uint16_t iRadiatorTemperature); |
| 287 |
|
| 288 |
//! Get the map width (in tiles) |
| 289 |
inline int get_width() const { return width; } |
| 290 |
|
| 291 |
//! Get the map height (in tiles) |
| 292 |
inline int get_height() const { return height; } |
| 293 |
|
| 294 |
//! Get the number of plots of land in this map |
| 295 |
inline int get_parcel_count() const { return parcel_count - 1; } |
| 296 |
|
| 297 |
inline int get_player_count() const { return player_count; } |
| 298 |
|
| 299 |
void set_player_count(int count); |
| 300 |
|
| 301 |
bool get_player_camera_tile(int iPlayer, int* pX, int* pY) const; |
| 302 |
bool get_player_heliport_tile(int iPlayer, int* pX, int* pY) const; |
| 303 |
void set_player_camera_tile(int iPlayer, int iX, int iY); |
| 304 |
void set_player_heliport_tile(int iPlayer, int iX, int iY); |
| 305 |
|
| 306 |
//! Get the number of tiles inside a given parcel |
| 307 |
int get_parcel_tile_count(int iParcelId) const; |
| 308 |
|
| 309 |
//! Change the owner of a particular parcel |
| 310 |
/*! |
| 311 |
\param iParcelId The parcel of land to change ownership of. Should be |
| 312 |
an integer between 1 and get_parcel_count() inclusive (parcel 0 is |
| 313 |
the outside, and should never have its ownership changed). |
| 314 |
\param iOwner The number of the player who should own the parcel, or |
| 315 |
zero if no player should own the parcel. |
| 316 |
\return vSplitTiles A vector that contains tile coordinates where |
| 317 |
iParcelId is adjacent to another part of the hospital. |
| 318 |
*/ |
| 319 |
std::vector<std::pair<int, int>> set_parcel_owner(int iParcelId, int iOwner); |
| 320 |
|
| 321 |
//! Get the owner of a particular parcel of land |
| 322 |
/*! |
| 323 |
\param iParcelId An integer between 0 and get_parcel_count() inclusive. |
| 324 |
\return 0 if the parcel is unowned, otherwise the number of the owning |
| 325 |
player. |
| 326 |
*/ |
| 327 |
int get_parcel_owner(int iParcelId) const; |
| 328 |
|
| 329 |
//! Query if two parcels are directly connected |
| 330 |
/*! |
| 331 |
\param iParcel1 An integer between 0 and get_parcel_count() inclusive. |
| 332 |
\param iParcel2 An integer between 0 and get_parcel_count() inclusive. |
| 333 |
\return true if there is a path between the two parcels which does not |
| 334 |
go into any other parcels. false otherwise. |
| 335 |
*/ |
| 336 |
bool are_parcels_adjacent(int iParcel1, int iParcel2); |
| 337 |
|
| 338 |
//! Query if a given player is in a position to purchase a given parcel |
| 339 |
/*! |
| 340 |
\param iParcelId The parcel of land to query. Should be an integer |
| 341 |
between 1 and get_parcel_count() inclusive. |
| 342 |
\param iPlayer The number of the player to perform the query on behalf |
| 343 |
of. Should be a strictly positive integer. |
| 344 |
\return true if the parcel has a door to the outside, or is directly |
| 345 |
connected to a parcel already owned by the given player. false |
| 346 |
otherwise. |
| 347 |
*/ |
| 348 |
bool is_parcel_purchasable(int iParcelId, int iPlayer); |
| 349 |
|
| 350 |
//! Draw the map (and any attached animations) |
| 351 |
/*! |
| 352 |
Draws the world pixel rectangle (iScreenX, iScreenY, iWidth, iHeight) |
| 353 |
to the rectangle (iCanvasX, iCanvasY, iWidth, iHeight) on pCanvas. Note |
| 354 |
that world pixel coordinates are also known as absolute screen |
| 355 |
coordinates - they are not world (tile) coordinates, nor (relative) |
| 356 |
screen coordinates. |
| 357 |
*/ |
| 358 |
void draw(render_target* pCanvas, int iScreenX, int iScreenY, int iWidth, |
| 359 |
int iHeight, int iCanvasX, int iCanvasY) const; |
| 360 |
|
| 361 |
//! Perform a hit-test against the animations attached to the map |
| 362 |
/*! |
| 363 |
If there is an animation at world pixel coordinates (iTestX, iTestY), |
| 364 |
then it is returned. Otherwise nullptr is returned. |
| 365 |
To perform a hit-test using world (tile) coordinates, get the tile |
| 366 |
itself and query the top 8 bits of map_tile::flags, or traverse the |
| 367 |
tile's animation lists. |
| 368 |
*/ |
| 369 |
drawable* hit_test(int iTestX, int iTestY) const; |
| 370 |
|
| 371 |
// When using the unchecked versions, the map coordinates MUST be valid. |
| 372 |
// When using the normal versions, nullptr is returned for invalid co-ords. |
| 373 |
map_tile* get_tile(int iX, int iY); |
| 374 |
const map_tile* get_tile(int iX, int iY) const; |
| 375 |
const map_tile* get_original_tile(int iX, int iY) const; |
| 376 |
map_tile* get_tile_unchecked(int iX, int iY); |
| 377 |
const map_tile* get_tile_unchecked(int iX, int iY) const; |
| 378 |
const map_tile* get_original_tile_unchecked(int iX, int iY) const; |
| 379 |
|
| 380 |
uint16_t get_tile_temperature(const map_tile* pNode) const; |
| 381 |
int get_tile_owner(const map_tile* pNode) const; |
| 382 |
|
| 383 |
//! Convert world (tile) coordinates to absolute screen coordinates |
| 384 |
template <typename T> |
| 385 |
static inline void world_to_screen(T& x, T& y) { |
| 386 |
T x_(x); |
| 387 |
x = (T)32 * (x_ - y); |
| 388 |
y = (T)16 * (x_ + y); |
| 389 |
} |
| 390 |
|
| 391 |
//! Convert absolute screen coordinates to world (tile) coordinates |
| 392 |
template <typename T> |
| 393 |
static inline void screen_to_world(T& x, T& y) { |
| 394 |
T x_(x); |
| 395 |
x = y / (T)32 + x_ / (T)64; |
| 396 |
y = y / (T)32 - x_ / (T)64; |
| 397 |
} |
| 398 |
|
| 399 |
void persist(lua_persist_writer* pWriter) const; |
| 400 |
void depersist(lua_persist_reader* pReader); |
| 401 |
|
| 402 |
void set_overlay(map_overlay* pOverlay, bool bTakeOwnership); |
| 403 |
|
| 404 |
private: |
| 405 |
void draw_floor(render_target* pCanvas, int iScreenX, int iScreenY, |
| 406 |
int iWidth, int iHeight, int iCanvasX, int iCanvasY) const; |
| 407 |
void draw_north_wall(const map_tile* tile, int tile_x, int tile_y, |
| 408 |
render_target* pCanvas) const; |
| 409 |
|
| 410 |
int draw_layer(const map_tile* tile, int tile_x, int tile_y, tile_layer layer, |
| 411 |
render_target* pCanvas) const; |
| 412 |
|
| 413 |
//! Check that the sprite indicated by the layer can be found and has height. |
| 414 |
/* |
| 415 |
* @param layer Layer to investigate. |
| 416 |
* @param [out] height Height of the sprite if the method returns \c true. |
| 417 |
* @return Whether the sprite exists and is not empty. |
| 418 |
*/ |
| 419 |
bool layer_exists(uint16_t layer, int& height) const; |
| 420 |
|
| 421 |
drawable* hit_test_drawables(link_list* pListStart, int iXs, int iYs, |
| 422 |
int iTestX, int iTestY) const; |
| 423 |
void read_tile_index(const uint8_t* pData, int& iX, int& iY) const; |
| 424 |
void write_tile_index(uint8_t* pData, int iX, int iY) const; |
| 425 |
|
| 426 |
//! Calculate a weighted impact of a neighbour tile on the temperature of |
| 427 |
//! the current tile. \param iNeighbourSum Incremented by the temperature of |
| 428 |
//! the tile multiplied by the weight of the connection. \param canTravel A |
| 429 |
//! tile flag indicating whether travel between this tile and it's neighbour |
| 430 |
//! is allowed. \param relative_idx The index of the neighbour tile, |
| 431 |
//! relative to this tile into cells. \param pNode A pointer to the current |
| 432 |
//! tile being tested. \param prevTemp The array index into |
| 433 |
//! map_tile::temperature that currently stores the temperature of the tile |
| 434 |
//! (prior to this calculation). \return The weight of the connection, 0 if |
| 435 |
//! there is no neighbour, 1 through walls, and 4 through air. |
| 436 |
uint32_t thermal_neighbour(uint32_t& iNeighbourSum, bool canTravel, |
| 437 |
std::ptrdiff_t relative_idx, map_tile* pNode, |
| 438 |
int prevTemp) const; |
| 439 |
|
| 440 |
//! Create the adjacency matrix if it doesn't already exist |
| 441 |
void make_adjacency_matrix(); |
| 442 |
|
| 443 |
//! Create the purchasability matrix if it doesn't already exist |
| 444 |
void make_purchase_matrix(); |
| 445 |
|
| 446 |
//! If it exists, update the purchasability matrix. |
| 447 |
void update_purchase_matrix(); |
| 448 |
|
| 449 |
int count_parcel_tiles(int iParcelId) const; |
| 450 |
|
| 451 |
map_tile* cells{}; |
| 452 |
map_tile* original_cells{}; // Cells at map load time, before any changes |
| 453 |
sprite_sheet* wall_blocks{}; |
| 454 |
map_overlay* overlay{}; |
| 455 |
bool owns_overlay{}; |
| 456 |
int* plot_owner{}; // 0 for unowned, 1 for player 1, etc. |
| 457 |
int width{}; |
| 458 |
int height{}; |
| 459 |
int player_count{}; |
| 460 |
int initial_camera_x[4]{}; |
| 461 |
int initial_camera_y[4]{}; |
| 462 |
int heliport_x[4]{}; |
| 463 |
int heliport_y[4]{}; |
| 464 |
int parcel_count{}; |
| 465 |
int current_temperature_index{}; |
| 466 |
temperature_theme current_temperature_theme{temperature_theme::red}; |
| 467 |
int* parcel_tile_counts{}; |
| 468 |
|
| 469 |
// 2D symmetric array giving true if there is a path between two parcels |
| 470 |
// which doesn't go into any other parcels. |
| 471 |
bool* parcel_adjacency_matrix{}; |
| 472 |
|
| 473 |
// 4 by N matrix giving true if player can purchase parcel. |
| 474 |
bool* purchasable_matrix{}; |
| 475 |
}; |
| 476 |
|
| 477 |
enum class map_scanline_iterator_direction { |
| 478 |
forward = 2, |
| 479 |
backward = 0, |
| 480 |
}; |
| 481 |
|
| 482 |
//! Utility class for iterating over map tiles within a screen rectangle |
| 483 |
/*! |
| 484 |
To easily iterate over the map tiles which might draw something within a |
| 485 |
certain rectangle of screen space, an instance of this class can be used. |
| 486 |
|
| 487 |
By default, it iterates by scanline, top-to-bottom, and then left-to-right |
| 488 |
within each scanline. Alternatively, by passing ScanlineBackward to the |
| 489 |
constructor, it will iterate bottom-to-top. Within a scanline, to visit |
| 490 |
tiles right-to-left, wait until isLastOnScanline() returns true, then use |
| 491 |
an instance of THMapScanlineIterator. |
| 492 |
*/ |
| 493 |
class map_tile_iterator { |
| 494 |
public: |
| 495 |
map_tile_iterator() = delete; |
| 496 |
|
| 497 |
/*! |
| 498 |
@arg pMap The map whose tiles should be iterated |
| 499 |
@arg iScreenX The X coordinate of the top-left corner of the |
| 500 |
screen-space rectangle to iterate. |
| 501 |
@arg iScreenY The Y coordinate of the top-left corner of the |
| 502 |
screen-space rectangle to iterate. |
| 503 |
@arg iWidth The width of the screen-space rectangle to iterate. |
| 504 |
@arg iHeight The width of the screen-space rectangle to iterate. |
| 505 |
@arg eScanlineDirection The direction in which to iterate scanlines; |
| 506 |
forward for top-to-bottom, backward for bottom-to-top. |
| 507 |
*/ |
| 508 |
map_tile_iterator(const level_map* pMap, int iScreenX, int iScreenY, |
| 509 |
int iWidth, int iHeight, |
| 510 |
map_scanline_iterator_direction eScanlineDirection = |
| 511 |
map_scanline_iterator_direction::forward); |
| 512 |
|
| 513 |
//! Returns false if the iterator has exhausted its tiles |
| 514 |
inline operator bool() const { return tile != nullptr; } |
| 515 |
|
| 516 |
//! Advances the iterator to the next tile |
| 517 |
inline map_tile_iterator& operator++(); |
| 518 |
|
| 519 |
//! Accessor for the current tile |
| 520 |
inline const map_tile* operator->() const { return tile; } |
| 521 |
|
| 522 |
//! Get the X position of the tile relative to the top-left corner of the |
| 523 |
//! screen-space rectangle |
| 524 |
inline int tile_x_position_on_screen() const { return x_relative_to_screen; } |
| 525 |
|
| 526 |
//! Get the Y position of the tile relative to the top-left corner of the |
| 527 |
//! screen-space rectangle |
| 528 |
inline int tile_y_position_on_screen() const { return y_relative_to_screen; } |
| 529 |
|
| 530 |
inline int tile_x() const { return world_x; } |
| 531 |
inline int tile_y() const { return world_y; } |
| 532 |
|
| 533 |
inline const level_map* get_map() { return container; } |
| 534 |
inline const map_tile* get_map_tile() { return tile; } |
| 535 |
inline int get_scanline_count() { return scanline_count; } |
| 536 |
inline int get_tile_step() { |
| 537 |
return (static_cast<int>(direction) - 1) * (1 - container->get_width()); |
| 538 |
} |
| 539 |
|
| 540 |
//! Returns true if the next tile will be on a different scanline |
| 541 |
/*! |
| 542 |
To visit a scanline in right-to-left order, or to revisit a scanline, |
| 543 |
wait until this method returns true, then use a THMapScanlineIterator. |
| 544 |
*/ |
| 545 |
inline bool is_last_on_scanline() const; |
| 546 |
|
| 547 |
private: |
| 548 |
// Maximum extents of the visible parts of a tile (pixel distances relative |
| 549 |
// to the top-most corner of an isometric cell) |
| 550 |
// If set too low, things will disappear when near the screen edge |
| 551 |
// If set too high, rendering will slow down |
| 552 |
static const int margin_top = 150; |
| 553 |
static const int margin_left = 110; |
| 554 |
static const int margin_right = 110; |
| 555 |
static const int margin_bottom = 150; |
| 556 |
|
| 557 |
friend class map_scanline_iterator; |
| 558 |
|
| 559 |
const map_tile* tile{nullptr}; |
| 560 |
const level_map* container; |
| 561 |
|
| 562 |
// TODO: Consider removing these, they are trivial to calculate |
| 563 |
int x_relative_to_screen{}; |
| 564 |
int y_relative_to_screen{}; |
| 565 |
|
| 566 |
const int screen_offset_x; |
| 567 |
const int screen_offset_y; |
| 568 |
const int screen_width; |
| 569 |
const int screen_height; |
| 570 |
int base_x; |
| 571 |
int base_y; |
| 572 |
int world_x; |
| 573 |
int world_y; |
| 574 |
int scanline_count{}; |
| 575 |
map_scanline_iterator_direction direction; |
| 576 |
|
| 577 |
void advance_until_visible(); |
| 578 |
}; |
| 579 |
|
| 580 |
//! Utility class for re-iterating a scanline visited by a map_tile_iterator |
| 581 |
class map_scanline_iterator { |
| 582 |
public: |
| 583 |
map_scanline_iterator(); |
| 584 |
|
| 585 |
/*! |
| 586 |
@arg itrNodes A tile iterator which has reached the end of a scanline |
| 587 |
@arg eDirection The direction in which to iterate the scanline; |
| 588 |
forward for left-to-right, backward for right-to-left. |
| 589 |
@arg iXOffset If given, values returned by x() will be offset by this. |
| 590 |
@arg iYOffset If given, values returned by y() will be offset by this. |
| 591 |
*/ |
| 592 |
map_scanline_iterator(const map_tile_iterator& itrNodes, |
| 593 |
map_scanline_iterator_direction eDirection, |
| 594 |
int iXOffset = 0, int iYOffset = 0); |
| 595 |
|
| 596 |
inline operator bool() const { return tile != end_tile; } |
| 597 |
inline map_scanline_iterator& operator++(); |
| 598 |
|
| 599 |
inline const map_tile* operator->() const { return tile; } |
| 600 |
inline int x() const { return x_relative_to_screen; } |
| 601 |
inline int y() const { return y_relative_to_screen; } |
| 602 |
inline const map_tile* get_next_tile() { return tile + tile_step; } |
| 603 |
inline const map_tile* get_previous_tile() { return tile - tile_step; } |
| 604 |
map_scanline_iterator operator=(const map_scanline_iterator& iterator); |
| 605 |
inline const map_tile* get_tile() { return tile; } |
| 606 |
|
| 607 |
private: |
| 608 |
const map_tile* tile; |
| 609 |
const map_tile* first_tile; |
| 610 |
const map_tile* end_tile; |
| 611 |
int tile_step; |
| 612 |
int x_step; |
| 613 |
int x_relative_to_screen; |
| 614 |
int y_relative_to_screen; |
| 615 |
int steps_taken{0}; |
| 616 |
}; |
| 617 |
|
| 618 |
#endif // CORSIX_TH_TH_MAP_H_ |