OpenTTD Source 20241224-master-gf74b0cf984
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 <http://www.gnu.org/licenses/>.
6 */
7
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:
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 }
158 bool operator !=(const TileIterator &rhs) const
159 {
160 return this->tile != rhs.tile;
161 }
162
166 bool operator ==(const TileIndex &rhs) const
167 {
168 return this->tile == rhs;
169 }
173 bool operator !=(const TileIndex &rhs) const
174 {
175 return this->tile != rhs;
176 }
177
178 static std::unique_ptr<TileIterator> Create(TileIndex corner1, TileIndex corner2, bool diagonal);
179};
180
183private:
184 int w;
185 int x;
186 int y;
187
188public:
193 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
194 {
195 }
196
203 {
204 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
205 }
206
210 inline TileIterator& operator ++() override
211 {
212 assert(this->tile != INVALID_TILE);
213
214 if (--this->x > 0) {
215 this->tile++;
216 } else if (--this->y > 0) {
217 this->x = this->w;
218 this->tile += TileDiffXY(1 - this->w, 1);
219 } else {
220 this->tile = INVALID_TILE;
221 }
222 return *this;
223 }
224
225 std::unique_ptr<TileIterator> Clone() const override
226 {
227 return std::make_unique<OrthogonalTileIterator>(*this);
228 }
229};
230
233private:
234 uint base_x;
235 uint base_y;
236 int a_cur;
237 int b_cur;
238 int a_max;
239 int b_max;
240
241public:
242
248 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)
249 {
250 }
251
258 {
259 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
260 }
261
262 TileIterator& operator ++() override;
263
264 std::unique_ptr<TileIterator> Clone() const override
265 {
266 return std::make_unique<DiagonalTileIterator>(*this);
267 }
268};
269
270#endif /* TILEAREA_TYPE_H */
Iterator to iterate over a diagonal area of the map.
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea.cpp:234
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.
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.
bool operator!=(const TileIterator &rhs) const
Inequality comparison.
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:291
Functions related to maps.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:467
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:389
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
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
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
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
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.