(svn r28004) -Update from Eints:
[openttd.git] / src / tilearea_type.h
blob45bfb3d4c0184c71117554673312d4a228aa4691
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file tilearea_type.h Type for storing the 'area' of something uses on the map. */
12 #ifndef TILEAREA_TYPE_H
13 #define TILEAREA_TYPE_H
15 #include "map_func.h"
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 w; ///< The width of the area
21 uint16 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, uint8 w = 0, uint8 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 void ClampToMap();
53 /**
54 * Get the center tile.
55 * @return The tile at the center, or just north of it.
57 TileIndex GetCenterTile() const
59 return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
63 /** Represents a diagonal tile area. */
64 struct DiagonalTileArea {
66 TileIndex tile; ///< Base tile of the area
67 int16 a; ///< Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
68 int16 b; ///< Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
70 /**
71 * Construct this tile area with some set values.
72 * @param tile The base tile.
73 * @param a The "x" extent.
74 * @param b The "y" estent.
76 DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
80 DiagonalTileArea(TileIndex start, TileIndex end);
82 /**
83 * Clears the TileArea by making the tile invalid and setting a and b to 0.
85 void Clear()
87 this->tile = INVALID_TILE;
88 this->a = 0;
89 this->b = 0;
92 bool Contains(TileIndex tile) const;
95 /** Shorthand for the much more common orthogonal tile area. */
96 typedef OrthogonalTileArea TileArea;
98 /** Base class for tile iterators. */
99 class TileIterator {
100 protected:
101 TileIndex tile; ///< The current tile we are at.
104 * Initialise the iterator starting at this tile.
105 * @param tile The tile we start iterating from.
107 TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
111 public:
112 /** Some compilers really like this. */
113 virtual ~TileIterator()
118 * Get the tile we are currently at.
119 * @return The tile we are at, or INVALID_TILE when we're done.
121 inline operator TileIndex () const
123 return this->tile;
127 * Move ourselves to the next tile in the rectangle on the map.
129 virtual TileIterator& operator ++() = 0;
132 * Allocate a new iterator that is a copy of this one.
134 virtual TileIterator *Clone() const = 0;
137 /** Iterator to iterate over a tile area (rectangle) of the map. */
138 class OrthogonalTileIterator : public TileIterator {
139 private:
140 int w; ///< The width of the iterated area.
141 int x; ///< The current 'x' position in the rectangle.
142 int y; ///< The current 'y' position in the rectangle.
144 public:
146 * Construct the iterator.
147 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
149 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
154 * Construct the iterator.
155 * @param corner1 Tile from where to begin iterating.
156 * @param corner2 Tile where to end the iterating.
158 OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
160 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
164 * Move ourselves to the next tile in the rectangle on the map.
166 inline TileIterator& operator ++()
168 assert(this->tile != INVALID_TILE);
170 if (--this->x > 0) {
171 this->tile++;
172 } else if (--this->y > 0) {
173 this->x = this->w;
174 this->tile += TileDiffXY(1, 1) - this->w;
175 } else {
176 this->tile = INVALID_TILE;
178 return *this;
181 virtual TileIterator *Clone() const
183 return new OrthogonalTileIterator(*this);
187 /** Iterator to iterate over a diagonal area of the map. */
188 class DiagonalTileIterator : public TileIterator {
189 private:
190 uint base_x; ///< The base tile x coordinate from where the iterating happens.
191 uint base_y; ///< The base tile y coordinate from where the iterating happens.
192 int a_cur; ///< The current (rotated) x coordinate of the iteration.
193 int b_cur; ///< The current (rotated) y coordinate of the iteration.
194 int a_max; ///< The (rotated) x coordinate of the end of the iteration.
195 int b_max; ///< The (rotated) y coordinate of the end of the iteration.
197 public:
200 * Construct the iterator.
201 * @param ta Area, i.e. begin point and (diagonal) width/height of to-be-iterated area.
203 DiagonalTileIterator(const DiagonalTileArea &ta) :
204 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)
209 * Construct the iterator.
210 * @param corner1 Tile from where to begin iterating.
211 * @param corner2 Tile where to end the iterating.
213 DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
215 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
218 TileIterator& operator ++();
220 virtual TileIterator *Clone() const
222 return new DiagonalTileIterator(*this);
227 * A loop which iterates over the tiles of a TileArea.
228 * @param var The name of the variable which contains the current tile.
229 * This variable will be allocated in this \c for of this loop.
230 * @param ta The tile area to search over.
232 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
234 #endif /* TILEAREA_TYPE_H */