Fix crash when setting separation mode for vehicles with no orders list.
[openttd-joker.git] / src / tilearea_type.h
blobba3a08f1ad84890de8cb1f937d6797c9a9ceb0da
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 "stdafx.h"
16 #include INCLUDE_FOR_PREFETCH_NTA
17 #include "map_func.h"
19 /** Represents the covered area of e.g. a rail station */
20 struct OrthogonalTileArea {
21 TileIndex tile; ///< The base tile of the area
22 uint16 w; ///< The width of the area
23 uint16 h; ///< The height of the area
25 /**
26 * Construct this tile area with some set values
27 * @param tile the base tile
28 * @param w the width
29 * @param h the height
31 OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 h = 0) : tile(tile), w(w), h(h)
35 OrthogonalTileArea(TileIndex start, TileIndex end);
37 void Add(TileIndex to_add);
39 /**
40 * Clears the 'tile area', i.e. make the tile invalid.
42 void Clear()
44 this->tile = INVALID_TILE;
45 this->w = 0;
46 this->h = 0;
49 bool Intersects(const OrthogonalTileArea &ta) const;
51 bool Contains(TileIndex tile) const;
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 TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
65 /** Represents a diagonal tile area. */
66 struct DiagonalTileArea {
68 TileIndex tile; ///< Base tile of the area
69 int16 a; ///< Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
70 int16 b; ///< Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
72 /**
73 * Construct this tile area with some set values.
74 * @param tile The base tile.
75 * @param a The "x" extent.
76 * @param b The "y" estent.
78 DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
82 DiagonalTileArea(TileIndex start, TileIndex end);
84 /**
85 * Clears the TileArea by making the tile invalid and setting a and b to 0.
87 void Clear()
89 this->tile = INVALID_TILE;
90 this->a = 0;
91 this->b = 0;
94 bool Contains(TileIndex tile) const;
97 /** Shorthand for the much more common orthogonal tile area. */
98 typedef OrthogonalTileArea TileArea;
100 /** Base class for tile iterators. */
101 class TileIterator {
102 protected:
103 TileIndex tile; ///< The current tile we are at.
106 * Initialise the iterator starting at this tile.
107 * @param tile The tile we start iterating from.
109 TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
113 public:
114 /** Some compilers really like this. */
115 virtual ~TileIterator()
120 * Get the tile we are currently at.
121 * @return The tile we are at, or INVALID_TILE when we're done.
123 inline operator TileIndex () const
125 return this->tile;
129 * Move ourselves to the next tile in the rectangle on the map.
131 virtual TileIterator& operator ++() = 0;
134 * Allocate a new iterator that is a copy of this one.
136 virtual TileIterator *Clone() const = 0;
139 /** Iterator to iterate over a tile area (rectangle) of the map. */
140 class OrthogonalTileIterator : public TileIterator {
141 private:
142 int w; ///< The width of the iterated area.
143 int x; ///< The current 'x' position in the rectangle.
144 int y; ///< The current 'y' position in the rectangle.
146 public:
148 * Construct the iterator.
149 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
151 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
156 * Construct the iterator.
157 * @param corner1 Tile from where to begin iterating.
158 * @param corner2 Tile where to end the iterating.
160 OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
162 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
166 * Move ourselves to the next tile in the rectangle on the map.
168 inline TileIterator& operator ++()
170 assert(this->tile != INVALID_TILE);
172 if (--this->x > 0) {
173 this->tile++;
174 } else if (--this->y > 0) {
175 this->x = this->w;
176 this->tile += TileDiffXY(1, 1) - this->w;
177 } else {
178 this->tile = INVALID_TILE;
180 return *this;
183 virtual TileIterator *Clone() const
185 return new OrthogonalTileIterator(*this);
189 /** Iterator to iterate over a tile area (rectangle) of the map.
190 * It prefetches tiles once per row.
192 class OrthogonalPrefetchTileIterator {
193 private:
194 TileIndex tile; ///< The current tile we are at.
195 int w; ///< The width of the iterated area.
196 int x; ///< The current 'x' position in the rectangle.
197 int y; ///< The current 'y' position in the rectangle.
199 public:
201 * Construct the iterator.
202 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
204 OrthogonalPrefetchTileIterator(const TileArea &ta) : tile(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
206 PREFETCH_NTA(&_m[ta.tile]);
209 /** Some compilers really like this. */
210 virtual ~OrthogonalPrefetchTileIterator()
215 * Get the tile we are currently at.
216 * @return The tile we are at, or INVALID_TILE when we're done.
218 inline operator TileIndex () const
220 return this->tile;
224 * Move ourselves to the next tile in the rectangle on the map.
226 inline OrthogonalPrefetchTileIterator& operator ++()
228 assert(this->tile != INVALID_TILE);
230 if (--this->x > 0) {
231 this->tile++;
232 } else if (--this->y > 0) {
233 this->x = this->w;
234 this->tile += TileDiffXY(1, 1) - this->w;
235 PREFETCH_NTA(&_m[tile]);
236 } else {
237 this->tile = INVALID_TILE;
239 return *this;
242 virtual OrthogonalPrefetchTileIterator *Clone() const
244 return new OrthogonalPrefetchTileIterator(*this);
248 /** Iterator to iterate over a diagonal area of the map. */
249 class DiagonalTileIterator : public TileIterator {
250 private:
251 uint base_x; ///< The base tile x coordinate from where the iterating happens.
252 uint base_y; ///< The base tile y coordinate from where the iterating happens.
253 int a_cur; ///< The current (rotated) x coordinate of the iteration.
254 int b_cur; ///< The current (rotated) y coordinate of the iteration.
255 int a_max; ///< The (rotated) x coordinate of the end of the iteration.
256 int b_max; ///< The (rotated) y coordinate of the end of the iteration.
258 public:
261 * Construct the iterator.
262 * @param ta Area, i.e. begin point and (diagonal) width/height of to-be-iterated area.
264 DiagonalTileIterator(const DiagonalTileArea &ta) :
265 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)
270 * Construct the iterator.
271 * @param corner1 Tile from where to begin iterating.
272 * @param corner2 Tile where to end the iterating.
274 DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
276 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
279 TileIterator& operator ++();
281 virtual TileIterator *Clone() const
283 return new DiagonalTileIterator(*this);
288 * A loop which iterates over the tiles of a TileArea.
289 * @param var The name of the variable which contains the current tile.
290 * This variable will be allocated in this \c for of this loop.
291 * @param ta The tile area to search over.
293 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
294 #define TILE_AREA_LOOP_WITH_PREFETCH(var, ta) for (OrthogonalPrefetchTileIterator var(ta); var != INVALID_TILE; ++var)
296 #endif /* TILEAREA_TYPE_H */