OpenTTD Source: src/tilearea_type.h Source File

OpenTTD Source 15.3

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
10#ifndef TILEAREA_TYPE_H
11#define TILEAREA_TYPE_H
12
13#include "map_func.h"
14
15class OrthogonalTileIterator;
16
18 struct OrthogonalTileArea {
19 TileIndex tile;
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
33 OrthogonalTileArea(TileIndex start, TileIndex end);
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
59 TileIndex GetCenterTile() const
60 {
61 return TileAddXY(this->tile, this->w / 2, this->h / 2);
62 }
63
64 OrthogonalTileIterator begin() const;
65
66 OrthogonalTileIterator end() const;
67};
68
70 struct DiagonalTileArea {
71
72 TileIndex tile;
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
86 DiagonalTileArea(TileIndex start, TileIndex end);
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
102 typedef OrthogonalTileArea TileArea;
103
105 class TileIterator {
106protected:
107 TileIndex tile;
108
113 TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
114 {
115 }
116
117public:
118 virtual ~TileIterator() = default;
119
124 inline operator TileIndex () const
125 {
126 return this->tile;
127 }
128
133 inline TileIndex operator *() const
134 {
135 return this->tile;
136 }
137
141 virtual TileIterator& operator ++() = 0;
142
146 virtual std::unique_ptr<TileIterator> Clone() const = 0;
147
151 bool operator ==(const TileIterator &rhs) const
152 {
153 return this->tile == rhs.tile;
154 }
155
159 bool operator ==(const TileIndex &rhs) const
160 {
161 return this->tile == rhs;
162 }
163
164 static std::unique_ptr<TileIterator> Create(TileIndex corner1, TileIndex corner2, bool diagonal);
165};
166
168 class OrthogonalTileIterator : public TileIterator {
169private:
170 int w;
171 int x;
172 int y;
173
174public:
179 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
180 {
181 }
182
188 OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
189 {
190 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
191 }
192
196 inline TileIterator& operator ++() override
197 {
198 assert(this->tile != INVALID_TILE);
199
200 if (--this->x > 0) {
201 this->tile++;
202 } else if (--this->y > 0) {
203 this->x = this->w;
204 this->tile += TileDiffXY(1 - this->w, 1);
205 } else {
206 this->tile = INVALID_TILE;
207 }
208 return *this;
209 }
210
211 std::unique_ptr<TileIterator> Clone() const override
212 {
213 return std::make_unique<OrthogonalTileIterator>(*this);
214 }
215};
216
218 class DiagonalTileIterator : public TileIterator {
219private:
220 uint base_x;
221 uint base_y;
222 int a_cur;
223 int b_cur;
224 int a_max;
225 int b_max;
226
227public:
228
233 DiagonalTileIterator(const DiagonalTileArea &ta) :
234 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)
235 {
236 }
237
243 DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
244 {
245 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
246 }
247
248 TileIterator& operator ++() override;
249
250 std::unique_ptr<TileIterator> Clone() const override
251 {
252 return std::make_unique<DiagonalTileIterator>(*this);
253 }
254};
255
259 class SpiralTileIterator {
260public:
261 using value_type = TileIndex;
262 using difference_type = std::ptrdiff_t;
263 using iterator_category = std::forward_iterator_tag;
264 using pointer = void;
265 using reference = void;
266
267 SpiralTileIterator(TileIndex center, uint diameter);
268 SpiralTileIterator(TileIndex start_north, uint radius, uint w, uint h);
269
270 bool operator==(const SpiralTileIterator &rhs) const { return this->x == rhs.x && this->y == rhs.y; }
271 bool operator==(const std::default_sentinel_t &) const { return this->IsEnd(); }
272
273 TileIndex operator*() const { return TileXY(this->x, this->y); }
274
275 SpiralTileIterator &operator++()
276 {
277 this->Increment();
278 this->SkipOutsideMap();
279 return *this;
280 }
281
282 SpiralTileIterator operator++(int)
283 {
284 SpiralTileIterator result = *this;
285 ++*this;
286 return result;
287 }
288
289private:
290 /* set by constructor, const afterwards */
291 uint max_radius;
292 std::array<uint, DIAGDIR_END> extent;
293
294 /* mutable iterator state */
295 uint cur_radius;
296 DiagDirection dir;
297 uint position;
298 uint x, y;
299
300 void SkipOutsideMap();
301 void InitPosition();
302 void Increment();
303
307 bool IsEnd() const
308 {
309 return this->cur_radius == this->max_radius && this->dir != INVALID_DIAGDIR;
310 }
311};
312
316 class SpiralTileSequence {
317public:
339 SpiralTileSequence(TileIndex center, uint diameter) : start(center, diameter) {}
340
365 SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h) : start(start_north, radius, w, h) {}
366
367 SpiralTileIterator begin() const { return start; }
368 std::default_sentinel_t end() const { return std::default_sentinel_t(); }
369
370private:
371 SpiralTileIterator start;
372};
373
374#endif /* TILEAREA_TYPE_H */
DiagonalTileIterator
Iterator to iterate over a diagonal area of the map.
Definition tilearea_type.h:218
DiagonalTileIterator::operator++
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea.cpp:234
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(const DiagonalTileArea &ta)
Construct the iterator.
Definition tilearea_type.h:233
DiagonalTileIterator::base_x
uint base_x
The base tile x coordinate from where the iterating happens.
Definition tilearea_type.h:220
DiagonalTileIterator::a_max
int a_max
The (rotated) x coordinate of the end of the iteration.
Definition tilearea_type.h:224
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition tilearea_type.h:243
DiagonalTileIterator::b_max
int b_max
The (rotated) y coordinate of the end of the iteration.
Definition tilearea_type.h:225
DiagonalTileIterator::b_cur
int b_cur
The current (rotated) y coordinate of the iteration.
Definition tilearea_type.h:223
DiagonalTileIterator::a_cur
int a_cur
The current (rotated) x coordinate of the iteration.
Definition tilearea_type.h:222
DiagonalTileIterator::Clone
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
Definition tilearea_type.h:250
DiagonalTileIterator::base_y
uint base_y
The base tile y coordinate from where the iterating happens.
Definition tilearea_type.h:221
OrthogonalTileIterator
Iterator to iterate over a tile area (rectangle) of the map.
Definition tilearea_type.h:168
OrthogonalTileIterator::OrthogonalTileIterator
OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition tilearea_type.h:188
OrthogonalTileIterator::OrthogonalTileIterator
OrthogonalTileIterator(const OrthogonalTileArea &ta)
Construct the iterator.
Definition tilearea_type.h:179
OrthogonalTileIterator::y
int y
The current 'y' position in the rectangle.
Definition tilearea_type.h:172
OrthogonalTileIterator::w
int w
The width of the iterated area.
Definition tilearea_type.h:170
OrthogonalTileIterator::x
int x
The current 'x' position in the rectangle.
Definition tilearea_type.h:171
OrthogonalTileIterator::operator++
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea_type.h:196
OrthogonalTileIterator::Clone
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
Definition tilearea_type.h:211
SpiralTileIterator
Helper class for SpiralTileSequence.
Definition tilearea_type.h:259
SpiralTileIterator::InitPosition
void InitPosition()
Initialise "position" after "dir" was changed.
Definition tilearea.cpp:356
SpiralTileIterator::IsEnd
bool IsEnd() const
Test whether the iterator reached the end.
Definition tilearea_type.h:307
SpiralTileIterator::Increment
void Increment()
Advance the internal state to the next potential tile.
Definition tilearea.cpp:365
SpiralTileIterator::SkipOutsideMap
void SkipOutsideMap()
Advance the internal state until it reaches a valid tile or the end.
Definition tilearea.cpp:348
SpiralTileSequence
Generate TileIndices around a center tile or tile area, with increasing distance.
Definition tilearea_type.h:316
SpiralTileSequence::SpiralTileSequence
SpiralTileSequence(TileIndex center, uint diameter)
Generate TileIndices for a square area around a center tile.
Definition tilearea_type.h:339
SpiralTileSequence::SpiralTileSequence
SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h)
Generate TileIndices for a rectangular area with an optional rectangular hole in the center.
Definition tilearea_type.h:365
TileIterator
Base class for tile iterators.
Definition tilearea_type.h:105
TileIterator::operator*
TileIndex operator*() const
Get the tile we are currently at.
Definition tilearea_type.h:133
TileIterator::TileIterator
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
Definition tilearea_type.h:113
TileIterator::Clone
virtual std::unique_ptr< TileIterator > Clone() const =0
Allocate a new iterator that is a copy of this one.
TileIterator::tile
TileIndex tile
The current tile we are at.
Definition tilearea_type.h:107
TileIterator::operator==
bool operator==(const TileIterator &rhs) const
Equality comparison.
Definition tilearea_type.h:151
TileIterator::operator++
virtual TileIterator & operator++()=0
Move ourselves to the next tile in the rectangle on the map.
TileIterator::Create
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
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition direction_type.h:85
map_func.h
Functions related to maps.
TileAddXY
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:483
TileDiffXY
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:401
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
DiagonalTileArea
Represents a diagonal tile area.
Definition tilearea_type.h:70
DiagonalTileArea::Clear
void Clear()
Clears the TileArea by making the tile invalid and setting a and b to 0.
Definition tilearea_type.h:91
DiagonalTileArea::b
int16_t b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
Definition tilearea_type.h:74
DiagonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition tilearea.cpp:205
DiagonalTileArea::a
int16_t a
Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
Definition tilearea_type.h:73
DiagonalTileArea::tile
TileIndex tile
Base tile of the area.
Definition tilearea_type.h:72
DiagonalTileArea::DiagonalTileArea
DiagonalTileArea(TileIndex tile=INVALID_TILE, int16_t a=0, int16_t b=0)
Construct this tile area with some set values.
Definition tilearea_type.h:82
OrthogonalTileArea
Represents the covered area of e.g.
Definition tilearea_type.h:18
OrthogonalTileArea::end
OrthogonalTileIterator end() const
Returns an iterator to the end of the tile area.
Definition tilearea.cpp:162
OrthogonalTileArea::ClampToMap
void ClampToMap()
Clamp the tile area to map borders.
Definition tilearea.cpp:142
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition tilearea.cpp:104
OrthogonalTileArea::w
uint16_t w
The width of the area.
Definition tilearea_type.h:20
OrthogonalTileArea::Add
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition tilearea.cpp:43
OrthogonalTileArea::Intersects
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
Definition tilearea.cpp:75
OrthogonalTileArea::GetCenterTile
TileIndex GetCenterTile() const
Get the center tile.
Definition tilearea_type.h:59
OrthogonalTileArea::Clear
void Clear()
Clears the 'tile area', i.e.
Definition tilearea_type.h:40
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition tilearea_type.h:19
OrthogonalTileArea::h
uint16_t h
The height of the area.
Definition tilearea_type.h:21
OrthogonalTileArea::begin
OrthogonalTileIterator begin() const
Returns an iterator to the beginning of the tile area.
Definition tilearea.cpp:153
OrthogonalTileArea::OrthogonalTileArea
OrthogonalTileArea(TileIndex tile=INVALID_TILE, uint16_t w=0, uint16_t h=0)
Construct this tile area with some set values.
Definition tilearea_type.h:29
OrthogonalTileArea::Expand
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 > >
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
TileIndex
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:87
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition tilearea_type.h:102