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/>.
8 /** @file tilearea_type.h Type for storing the 'area' of something uses on the map. */
10 #ifndef TILEAREA_TYPE_H
11 #define TILEAREA_TYPE_H
15 class OrthogonalTileIterator
;
17 /** Represents the covered area of e.g. a rail station */
18 struct OrthogonalTileArea
{
19 TileIndex tile
; ///< The base tile of the area
20 uint16_t w
; ///< The width of the area
21 uint16_t h
; ///< The height of the area
24 * Construct this tile area with some set values
25 * @param tile the base tile
29 OrthogonalTileArea(TileIndex tile
= INVALID_TILE
, uint16_t w
= 0, uint16_t h
= 0) : tile(tile
), w(w
), h(h
)
33 OrthogonalTileArea(TileIndex start
, TileIndex end
);
35 void Add(TileIndex to_add
);
38 * Clears the 'tile area', i.e. make the tile invalid.
42 this->tile
= INVALID_TILE
;
47 bool Intersects(const OrthogonalTileArea
&ta
) const;
49 bool Contains(TileIndex tile
) const;
51 OrthogonalTileArea
&Expand(int rad
);
56 * Get the center tile.
57 * @return The tile at the center, or just north of it.
59 TileIndex
GetCenterTile() const
61 return TileAddXY(this->tile
, this->w
/ 2, this->h
/ 2);
64 OrthogonalTileIterator
begin() const;
66 OrthogonalTileIterator
end() const;
69 /** Represents a diagonal tile area. */
70 struct DiagonalTileArea
{
72 TileIndex tile
; ///< Base tile of the area
73 int16_t a
; ///< Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
74 int16_t b
; ///< Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
77 * Construct this tile area with some set values.
78 * @param tile The base tile.
79 * @param a The "x" extent.
80 * @param b The "y" estent.
82 DiagonalTileArea(TileIndex tile
= INVALID_TILE
, int16_t a
= 0, int16_t b
= 0) : tile(tile
), a(a
), b(b
)
86 DiagonalTileArea(TileIndex start
, TileIndex end
);
89 * Clears the TileArea by making the tile invalid and setting a and b to 0.
93 this->tile
= INVALID_TILE
;
98 bool Contains(TileIndex tile
) const;
101 /** Shorthand for the much more common orthogonal tile area. */
102 typedef OrthogonalTileArea TileArea
;
104 /** Base class for tile iterators. */
107 TileIndex tile
; ///< The current tile we are at.
110 * Initialise the iterator starting at this tile.
111 * @param tile The tile we start iterating from.
113 TileIterator(TileIndex tile
= INVALID_TILE
) : tile(tile
)
118 /** Some compilers really like this. */
119 virtual ~TileIterator()
124 * Get the tile we are currently at.
125 * @return The tile we are at, or INVALID_TILE when we're done.
127 inline operator TileIndex () const
133 * Get the tile we are currently at.
134 * @return The tile we are at, or INVALID_TILE when we're done.
136 inline TileIndex
operator *() const
142 * Move ourselves to the next tile in the rectangle on the map.
144 virtual TileIterator
& operator ++() = 0;
147 * Allocate a new iterator that is a copy of this one.
149 virtual std::unique_ptr
<TileIterator
> Clone() const = 0;
152 * Equality comparison.
154 bool operator ==(const TileIterator
&rhs
) const
156 return this->tile
== rhs
.tile
;
159 * Inequality comparison.
161 bool operator !=(const TileIterator
&rhs
) const
163 return this->tile
!= rhs
.tile
;
167 * Equality comparison.
169 bool operator ==(const TileIndex
&rhs
) const
171 return this->tile
== rhs
;
174 * Inequality comparison.
176 bool operator !=(const TileIndex
&rhs
) const
178 return this->tile
!= rhs
;
181 static std::unique_ptr
<TileIterator
> Create(TileIndex corner1
, TileIndex corner2
, bool diagonal
);
184 /** Iterator to iterate over a tile area (rectangle) of the map. */
185 class OrthogonalTileIterator
: public TileIterator
{
187 int w
; ///< The width of the iterated area.
188 int x
; ///< The current 'x' position in the rectangle.
189 int y
; ///< The current 'y' position in the rectangle.
193 * Construct the iterator.
194 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
196 OrthogonalTileIterator(const OrthogonalTileArea
&ta
) : TileIterator(ta
.w
== 0 || ta
.h
== 0 ? INVALID_TILE
: ta
.tile
), w(ta
.w
), x(ta
.w
), y(ta
.h
)
201 * Construct the iterator.
202 * @param corner1 Tile from where to begin iterating.
203 * @param corner2 Tile where to end the iterating.
205 OrthogonalTileIterator(TileIndex corner1
, TileIndex corner2
)
207 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1
, corner2
));
211 * Move ourselves to the next tile in the rectangle on the map.
213 inline TileIterator
& operator ++() override
215 assert(this->tile
!= INVALID_TILE
);
219 } else if (--this->y
> 0) {
221 this->tile
+= TileDiffXY(1 - this->w
, 1);
223 this->tile
= INVALID_TILE
;
228 std::unique_ptr
<TileIterator
> Clone() const override
230 return std::make_unique
<OrthogonalTileIterator
>(*this);
234 /** Iterator to iterate over a diagonal area of the map. */
235 class DiagonalTileIterator
: public TileIterator
{
237 uint base_x
; ///< The base tile x coordinate from where the iterating happens.
238 uint base_y
; ///< The base tile y coordinate from where the iterating happens.
239 int a_cur
; ///< The current (rotated) x coordinate of the iteration.
240 int b_cur
; ///< The current (rotated) y coordinate of the iteration.
241 int a_max
; ///< The (rotated) x coordinate of the end of the iteration.
242 int b_max
; ///< The (rotated) y coordinate of the end of the iteration.
247 * Construct the iterator.
248 * @param ta Area, i.e. begin point and (diagonal) width/height of to-be-iterated area.
250 DiagonalTileIterator(const DiagonalTileArea
&ta
) :
251 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
)
256 * Construct the iterator.
257 * @param corner1 Tile from where to begin iterating.
258 * @param corner2 Tile where to end the iterating.
260 DiagonalTileIterator(TileIndex corner1
, TileIndex corner2
)
262 *this = DiagonalTileIterator(DiagonalTileArea(corner1
, corner2
));
265 TileIterator
& operator ++() override
;
267 std::unique_ptr
<TileIterator
> Clone() const override
269 return std::make_unique
<DiagonalTileIterator
>(*this);
273 #endif /* TILEAREA_TYPE_H */