OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_diagonal.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11
12#include "tilearea_diagonal.h"
13
14#include "safeguards.h"
15
22{
23 assert(start < Map::Size());
24 assert(end < Map::Size());
25
26 /* Unfortunately we can't find a new base and make all a and b positive because
27 * the new base might be a "flattened" corner where there actually is no single
28 * tile. If we try anyway the result is either inaccurate ("one off" half of the
29 * time) or the code gets much more complex;
30 *
31 * We also need to increment/decrement a and b here to have one-past-end semantics
32 * for a and b, just the way the orthogonal tile area does it for w and h. */
33
34 this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start);
35 this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start);
36 if (this->a > 0) {
37 this->a++;
38 } else {
39 this->a--;
40 }
41
42 if (this->b > 0) {
43 this->b++;
44 } else {
45 this->b--;
46 }
47}
48
55{
56 int a = TileY(tile) + TileX(tile);
57 int b = TileY(tile) - TileX(tile);
58
59 int start_a = TileY(this->tile) + TileX(this->tile);
60 int start_b = TileY(this->tile) - TileX(this->tile);
61
62 int end_a = start_a + this->a;
63 int end_b = start_b + this->b;
64
65 /* Swap if necessary, preserving the "one past end" semantics. */
66 if (start_a > end_a) {
67 int tmp = start_a;
68 start_a = end_a + 1;
69 end_a = tmp + 1;
70 }
71 if (start_b > end_b) {
72 int tmp = start_b;
73 start_b = end_b + 1;
74 end_b = tmp + 1;
75 }
76
77 return a >= start_a && a < end_a && b >= start_b && b < end_b;
78}
79
85{
86 assert(this->tile != INVALID_TILE);
87
88 /* Determine the next tile, while clipping at map borders */
89 bool new_line = false;
90 do {
91 /* Iterate using the rotated coordinates. */
92 if (this->a_max == 1 || this->a_max == -1) {
93 /* Special case: Every second column has zero length, skip them completely */
94 this->a_cur = 0;
95 if (this->b_max > 0) {
96 this->b_cur = std::min(this->b_cur + 2, this->b_max);
97 } else {
98 this->b_cur = std::max(this->b_cur - 2, this->b_max);
99 }
100 } else {
101 /* Every column has at least one tile to process */
102 if (this->a_max > 0) {
103 this->a_cur += 2;
104 new_line = this->a_cur >= this->a_max;
105 } else {
106 this->a_cur -= 2;
107 new_line = this->a_cur <= this->a_max;
108 }
109 if (new_line) {
110 /* offset of initial a_cur: one tile in the same direction as a_max
111 * every second line.
112 */
113 this->a_cur = abs(this->a_cur) % 2 ? 0 : (this->a_max > 0 ? 1 : -1);
114
115 if (this->b_max > 0) {
116 ++this->b_cur;
117 } else {
118 --this->b_cur;
119 }
120 }
121 }
122
123 /* And convert the coordinates back once we've gone to the next tile. */
124 uint x = this->base_x + (this->a_cur - this->b_cur) / 2;
125 uint y = this->base_y + (this->b_cur + this->a_cur) / 2;
126 /* Prevent wrapping around the map's borders. */
127 this->tile = x >= Map::SizeX() || y >= Map::SizeY() ? INVALID_TILE : TileXY(x, y);
128 } while (this->tile > Map::Size() && this->b_max != this->b_cur);
129
130 if (this->b_max == this->b_cur) this->tile = INVALID_TILE;
131 return *this;
132}
Iterator to iterate over a diagonal area of the map.
uint base_y
The base tile y coordinate from where the iterating happens.
iterator & operator++()
Increment the iterator and set it to the next position.
int b_max
The (rotated) y coordinate of the end of the iteration.
int a_max
The (rotated) x coordinate of the end of the iteration.
uint base_x
The base tile x coordinate from where the iterating happens.
int b_cur
The current (rotated) y coordinate of the iteration.
TileIndex tile
The current tile.
int a_cur
The current (rotated) x coordinate of the iteration.
int16_t b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards).
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
int16_t a
Extent in diagonal "x" direction (may be negative to signify the area stretches to the left).
TileIndex tile
Base tile of the area.
std::default_sentinel_t end() const
Returns an iterator to the end of the tile area.
DiagonalTileArea(TileIndex tile=INVALID_TILE, int16_t a=0, int16_t b=0)
Construct this tile area with some set values.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:262
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:271
static uint Size()
Get the size of the map.
Definition map_func.h:280
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Type for storing and iterating a diagonal tile area.