Update: Translations from eints
[openttd-github.git] / src / tilearea_type.h
blobd0562826548e305d9c68b6b363eae594ed349c51
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 */
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
13 #include "map_func.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
23 /**
24 * Construct this tile area with some set values
25 * @param tile the base tile
26 * @param w the width
27 * @param h the height
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);
37 /**
38 * Clears the 'tile area', i.e. make the tile invalid.
40 void Clear()
42 this->tile = INVALID_TILE;
43 this->w = 0;
44 this->h = 0;
47 bool Intersects(const OrthogonalTileArea &ta) const;
49 bool Contains(TileIndex tile) const;
51 OrthogonalTileArea &Expand(int rad);
53 void ClampToMap();
55 /**
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)
76 /**
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);
88 /**
89 * Clears the TileArea by making the tile invalid and setting a and b to 0.
91 void Clear()
93 this->tile = INVALID_TILE;
94 this->a = 0;
95 this->b = 0;
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. */
105 class TileIterator {
106 protected:
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)
117 public:
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
129 return this->tile;
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
138 return this->tile;
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 {
186 private:
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.
191 public:
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);
217 if (--this->x > 0) {
218 this->tile++;
219 } else if (--this->y > 0) {
220 this->x = this->w;
221 this->tile += TileDiffXY(1 - this->w, 1);
222 } else {
223 this->tile = INVALID_TILE;
225 return *this;
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 {
236 private:
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.
244 public:
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 */