OpenTTD Source 20241224-master-gee860a5c8e
tilearea.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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11
12#include "tilearea_type.h"
13
14#include "safeguards.h"
15
22{
23 assert(start < Map::Size());
24 assert(end < Map::Size());
25
26 uint sx = TileX(start);
27 uint sy = TileY(start);
28 uint ex = TileX(end);
29 uint ey = TileY(end);
30
31 if (sx > ex) Swap(sx, ex);
32 if (sy > ey) Swap(sy, ey);
33
34 this->tile = TileXY(sx, sy);
35 this->w = ex - sx + 1;
36 this->h = ey - sy + 1;
37}
38
44{
45 if (this->tile == INVALID_TILE) {
46 this->tile = to_add;
47 this->w = 1;
48 this->h = 1;
49 return;
50 }
51
52 uint sx = TileX(this->tile);
53 uint sy = TileY(this->tile);
54 uint ex = sx + this->w - 1;
55 uint ey = sy + this->h - 1;
56
57 uint ax = TileX(to_add);
58 uint ay = TileY(to_add);
59
60 sx = std::min(ax, sx);
61 sy = std::min(ay, sy);
62 ex = std::max(ax, ex);
63 ey = std::max(ay, ey);
64
65 this->tile = TileXY(sx, sy);
66 this->w = ex - sx + 1;
67 this->h = ey - sy + 1;
68}
69
76{
77 if (ta.w == 0 || this->w == 0) return false;
78
79 assert(ta.w != 0 && ta.h != 0 && this->w != 0 && this->h != 0);
80
81 uint left1 = TileX(this->tile);
82 uint top1 = TileY(this->tile);
83 uint right1 = left1 + this->w - 1;
84 uint bottom1 = top1 + this->h - 1;
85
86 uint left2 = TileX(ta.tile);
87 uint top2 = TileY(ta.tile);
88 uint right2 = left2 + ta.w - 1;
89 uint bottom2 = top2 + ta.h - 1;
90
91 return !(
92 left2 > right1 ||
93 right2 < left1 ||
94 top2 > bottom1 ||
95 bottom2 < top1
96 );
97}
98
105{
106 if (this->w == 0) return false;
107
108 assert(this->w != 0 && this->h != 0);
109
110 uint left = TileX(this->tile);
111 uint top = TileY(this->tile);
112 uint tile_x = TileX(tile);
113 uint tile_y = TileY(tile);
114
115 return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h);
116}
117
124{
125 int x = TileX(this->tile);
126 int y = TileY(this->tile);
127
128 int sx = std::max<int>(x - rad, 0);
129 int sy = std::max<int>(y - rad, 0);
130 int ex = std::min<int>(x + this->w + rad, Map::SizeX());
131 int ey = std::min<int>(y + this->h + rad, Map::SizeY());
132
133 this->tile = TileXY(sx, sy);
134 this->w = ex - sx;
135 this->h = ey - sy;
136 return *this;
137}
138
143{
144 assert(this->tile < Map::Size());
145 this->w = std::min<int>(this->w, Map::SizeX() - TileX(this->tile));
146 this->h = std::min<int>(this->h, Map::SizeY() - TileY(this->tile));
147}
148
157
166
173{
174 assert(start < Map::Size());
175 assert(end < Map::Size());
176
177 /* Unfortunately we can't find a new base and make all a and b positive because
178 * the new base might be a "flattened" corner where there actually is no single
179 * tile. If we try anyway the result is either inaccurate ("one off" half of the
180 * time) or the code gets much more complex;
181 *
182 * We also need to increment/decrement a and b here to have one-past-end semantics
183 * for a and b, just the way the orthogonal tile area does it for w and h. */
184
185 this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start);
186 this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start);
187 if (this->a > 0) {
188 this->a++;
189 } else {
190 this->a--;
191 }
192
193 if (this->b > 0) {
194 this->b++;
195 } else {
196 this->b--;
197 }
198}
199
206{
207 int a = TileY(tile) + TileX(tile);
208 int b = TileY(tile) - TileX(tile);
209
210 int start_a = TileY(this->tile) + TileX(this->tile);
211 int start_b = TileY(this->tile) - TileX(this->tile);
212
213 int end_a = start_a + this->a;
214 int end_b = start_b + this->b;
215
216 /* Swap if necessary, preserving the "one past end" semantics. */
217 if (start_a > end_a) {
218 int tmp = start_a;
219 start_a = end_a + 1;
220 end_a = tmp + 1;
221 }
222 if (start_b > end_b) {
223 int tmp = start_b;
224 start_b = end_b + 1;
225 end_b = tmp + 1;
226 }
227
228 return (a >= start_a && a < end_a && b >= start_b && b < end_b);
229}
230
235{
236 assert(this->tile != INVALID_TILE);
237
238 /* Determine the next tile, while clipping at map borders */
239 bool new_line = false;
240 do {
241 /* Iterate using the rotated coordinates. */
242 if (this->a_max == 1 || this->a_max == -1) {
243 /* Special case: Every second column has zero length, skip them completely */
244 this->a_cur = 0;
245 if (this->b_max > 0) {
246 this->b_cur = std::min(this->b_cur + 2, this->b_max);
247 } else {
248 this->b_cur = std::max(this->b_cur - 2, this->b_max);
249 }
250 } else {
251 /* Every column has at least one tile to process */
252 if (this->a_max > 0) {
253 this->a_cur += 2;
254 new_line = this->a_cur >= this->a_max;
255 } else {
256 this->a_cur -= 2;
257 new_line = this->a_cur <= this->a_max;
258 }
259 if (new_line) {
260 /* offset of initial a_cur: one tile in the same direction as a_max
261 * every second line.
262 */
263 this->a_cur = abs(this->a_cur) % 2 ? 0 : (this->a_max > 0 ? 1 : -1);
264
265 if (this->b_max > 0) {
266 ++this->b_cur;
267 } else {
268 --this->b_cur;
269 }
270 }
271 }
272
273 /* And convert the coordinates back once we've gone to the next tile. */
274 uint x = this->base_x + (this->a_cur - this->b_cur) / 2;
275 uint y = this->base_y + (this->b_cur + this->a_cur) / 2;
276 /* Prevent wrapping around the map's borders. */
277 this->tile = x >= Map::SizeX() || y >= Map::SizeY() ? INVALID_TILE : TileXY(x, y);
278 } while (this->tile > Map::Size() && this->b_max != this->b_cur);
279
280 if (this->b_max == this->b_cur) this->tile = INVALID_TILE;
281 return *this;
282}
283
291/* static */ std::unique_ptr<TileIterator> TileIterator::Create(TileIndex corner1, TileIndex corner2, bool diagonal)
292{
293 if (diagonal) {
294 return std::make_unique<DiagonalTileIterator>(corner1, corner2);
295 }
296 return std::make_unique<OrthogonalTileIterator>(corner1, corner2);
297}
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea.cpp:234
uint base_x
The base tile x coordinate from where the iterating happens.
int a_max
The (rotated) x coordinate of the end of the iteration.
int b_max
The (rotated) y coordinate of the end of the iteration.
int b_cur
The current (rotated) y coordinate of the iteration.
int a_cur
The current (rotated) x coordinate of the iteration.
uint base_y
The base tile y coordinate from where the iterating happens.
Iterator to iterate over a tile area (rectangle) of the map.
Base class for tile iterators.
TileIndex tile
The current tile we are at.
static std::unique_ptr< TileIterator > Create(TileIndex corner1, TileIndex corner2, bool diagonal)
Create either an OrthogonalTileIterator or DiagonalTileIterator given the diagonal parameter.
Definition tilearea.cpp:291
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:373
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
constexpr void Swap(T &a, T &b)
Type safe swap operation.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
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?
Definition tilearea.cpp:205
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.
DiagonalTileArea(TileIndex tile=INVALID_TILE, int16_t a=0, int16_t b=0)
Construct this tile area with some set values.
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:279
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition map_func.h:270
static debug_inline uint Size()
Get the size of the map.
Definition map_func.h:288
Represents the covered area of e.g.
OrthogonalTileIterator end() const
Returns an iterator to the end of the tile area.
Definition tilearea.cpp:162
void ClampToMap()
Clamp the tile area to map borders.
Definition tilearea.cpp:142
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition tilearea.cpp:104
uint16_t w
The width of the area.
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition tilearea.cpp:43
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
Definition tilearea.cpp:75
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
OrthogonalTileIterator begin() const
Returns an iterator to the beginning of the tile area.
Definition tilearea.cpp:153
OrthogonalTileArea(TileIndex tile=INVALID_TILE, uint16_t w=0, uint16_t h=0)
Construct this tile area with some set values.
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition tilearea.cpp:123
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Type for storing the 'area' of something uses on the map.