OpenTTD Source 20260311-master-g511d3794ce
tilearea_type.h
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#ifndef TILEAREA_TYPE_H
11#define TILEAREA_TYPE_H
12
13#include "map_func.h"
14
16
20 uint16_t w;
21 uint16_t h;
22
29 OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint16_t w = 0, uint16_t h = 0) : tile(tile), w(w), h(h)
30 {
31 }
32
34
35 void Add(TileIndex to_add);
36
40 void Clear()
41 {
42 this->tile = INVALID_TILE;
43 this->w = 0;
44 this->h = 0;
45 }
46
47 bool Intersects(const OrthogonalTileArea &ta) const;
48
49 bool Contains(TileIndex tile) const;
50
51 OrthogonalTileArea &Expand(int rad);
52
53 void ClampToMap();
54
60 {
61 return TileAddXY(this->tile, this->w / 2, this->h / 2);
62 }
63
65
67};
68
71
73 int16_t a;
74 int16_t b;
75
82 DiagonalTileArea(TileIndex tile = INVALID_TILE, int16_t a = 0, int16_t b = 0) : tile(tile), a(a), b(b)
83 {
84 }
85
87
91 void Clear()
92 {
93 this->tile = INVALID_TILE;
94 this->a = 0;
95 this->b = 0;
96 }
97
98 bool Contains(TileIndex tile) const;
99};
100
103
106protected:
108
116
117public:
119 virtual ~TileIterator() = default;
120
125 inline operator TileIndex () const
126 {
127 return this->tile;
128 }
129
134 inline TileIndex operator *() const
135 {
136 return this->tile;
137 }
138
143 virtual TileIterator& operator ++() = 0;
144
149 virtual std::unique_ptr<TileIterator> Clone() const = 0;
150
156 bool operator ==(const TileIterator &rhs) const
157 {
158 return this->tile == rhs.tile;
159 }
160
166 bool operator ==(const TileIndex &rhs) const
167 {
168 return this->tile == rhs;
169 }
170
171 static std::unique_ptr<TileIterator> Create(TileIndex corner1, TileIndex corner2, bool diagonal);
172};
173
176private:
177 int w;
178 int x;
179 int y;
180
181public:
186 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
187 {
188 }
189
196 {
197 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
198 }
199
204 inline TileIterator& operator ++() override
205 {
206 assert(this->tile != INVALID_TILE);
207
208 if (--this->x > 0) {
209 this->tile++;
210 } else if (--this->y > 0) {
211 this->x = this->w;
212 this->tile += TileDiffXY(1 - this->w, 1);
213 } else {
214 this->tile = INVALID_TILE;
215 }
216 return *this;
217 }
218
219 std::unique_ptr<TileIterator> Clone() const override
220 {
221 return std::make_unique<OrthogonalTileIterator>(*this);
222 }
223};
224
227private:
228 uint base_x;
229 uint base_y;
230 int a_cur;
231 int b_cur;
232 int a_max;
233 int b_max;
234
235public:
236
242 TileIterator(ta.tile), base_x(TileX(ta.tile)), base_y(TileY(ta.tile)), a_cur(0), b_cur(0), a_max(ta.a), b_max(ta.b)
243 {
244 }
245
252 {
253 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
254 }
255
256 TileIterator& operator ++() override;
257
258 std::unique_ptr<TileIterator> Clone() const override
259 {
260 return std::make_unique<DiagonalTileIterator>(*this);
261 }
262};
263
268public:
269 using value_type = TileIndex;
270 using difference_type = std::ptrdiff_t;
271 using iterator_category = std::forward_iterator_tag;
272 using pointer = void;
273 using reference = void;
274
275 SpiralTileIterator(TileIndex center, uint diameter);
276 SpiralTileIterator(TileIndex start_north, uint radius, uint w, uint h);
277
278 bool operator==(const SpiralTileIterator &rhs) const { return this->x == rhs.x && this->y == rhs.y; }
279 bool operator==(const std::default_sentinel_t &) const { return this->IsEnd(); }
280
281 TileIndex operator*() const { return TileXY(this->x, this->y); }
282
283 SpiralTileIterator &operator++()
284 {
285 this->Increment();
286 this->SkipOutsideMap();
287 return *this;
288 }
289
290 SpiralTileIterator operator++(int)
291 {
292 SpiralTileIterator result = *this;
293 ++*this;
294 return result;
295 }
296
297private:
298 /* set by constructor, const afterwards */
299 uint max_radius;
300 std::array<uint, DIAGDIR_END> extent;
301
302 /* mutable iterator state */
303 uint cur_radius;
304 DiagDirection dir;
305 uint position;
306 uint x, y;
307
308 void SkipOutsideMap();
309 void InitPosition();
310 void Increment();
311
316 bool IsEnd() const
317 {
318 return this->cur_radius == this->max_radius && this->dir != INVALID_DIAGDIR;
319 }
320};
321
326public:
348 SpiralTileSequence(TileIndex center, uint diameter) : start(center, diameter) {}
349
374 SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h) : start(start_north, radius, w, h) {}
375
376 SpiralTileIterator begin() const { return start; }
377 std::default_sentinel_t end() const { return std::default_sentinel_t(); }
378
379private:
380 SpiralTileIterator start;
381};
382
383#endif /* TILEAREA_TYPE_H */
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea.cpp:235
DiagonalTileIterator(const DiagonalTileArea &ta)
Construct the iterator.
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.
DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
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.
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
uint base_y
The base tile y coordinate from where the iterating happens.
Iterator to iterate over a tile area (rectangle) of the map.
OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
OrthogonalTileIterator(const OrthogonalTileArea &ta)
Construct the iterator.
int y
The current 'y' position in the rectangle.
int w
The width of the iterated area.
int x
The current 'x' position in the rectangle.
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
Helper class for SpiralTileSequence.
SpiralTileIterator(TileIndex center, uint diameter)
Create the iterator.
Definition tilearea.cpp:301
void InitPosition()
Initialise "position" after "dir" was changed.
Definition tilearea.cpp:353
bool IsEnd() const
Test whether the iterator reached the end.
void Increment()
Advance the internal state to the next potential tile.
Definition tilearea.cpp:362
void SkipOutsideMap()
Advance the internal state until it reaches a valid tile or the end.
Definition tilearea.cpp:345
SpiralTileSequence(TileIndex center, uint diameter)
Generate TileIndices for a square area around a center tile.
SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h)
Generate TileIndices for a rectangular area with an optional rectangular hole in the center.
Base class for tile iterators.
TileIndex operator*() const
Get the tile we are currently at.
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
virtual std::unique_ptr< TileIterator > Clone() const =0
Allocate a new iterator that is a copy of this one.
TileIndex tile
The current tile we are at.
bool operator==(const TileIterator &rhs) const
Equality comparison.
virtual ~TileIterator()=default
Ensure the destructor of the sub classes are called as well.
virtual TileIterator & operator++()=0
Move ourselves to the next tile in the rectangle on the map.
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:292
DiagDirection
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Functions related to maps.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:474
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:392
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
Represents a diagonal tile area.
void Clear()
Clears the TileArea by making the tile invalid and setting a and b to 0.
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.
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 GetCenterTile() const
Get the center tile.
void Clear()
Clears the 'tile area', i.e.
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
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
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.