OpenTTD
tilearea_type.h
Go to the documentation of this file.
1 /* $Id: tilearea_type.h 26289 2014-02-02 14:53:26Z fonsinchen $ */
2 
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  */
9 
12 #ifndef TILEAREA_TYPE_H
13 #define TILEAREA_TYPE_H
14 
15 #include "map_func.h"
16 
20  uint16 w;
21  uint16 h;
22 
29  OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 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  void ClampToMap();
52 
58  {
59  return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
60  }
61 };
62 
65 
67  int16 a;
68  int16 b;
69 
76  DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
77  {
78  }
79 
81 
85  void Clear()
86  {
87  this->tile = INVALID_TILE;
88  this->a = 0;
89  this->b = 0;
90  }
91 
92  bool Contains(TileIndex tile) const;
93 };
94 
97 
99 class TileIterator {
100 protected:
102 
107  TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
108  {
109  }
110 
111 public:
113  virtual ~TileIterator()
114  {
115  }
116 
121  inline operator TileIndex () const
122  {
123  return this->tile;
124  }
125 
129  virtual TileIterator& operator ++() = 0;
130 
134  virtual TileIterator *Clone() const = 0;
135 };
136 
139 private:
140  int w;
141  int x;
142  int y;
143 
144 public:
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)
150  {
151  }
152 
159  {
160  *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
161  }
162 
166  inline TileIterator& operator ++()
167  {
168  assert(this->tile != INVALID_TILE);
169 
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;
177  }
178  return *this;
179  }
180 
181  virtual TileIterator *Clone() const
182  {
183  return new OrthogonalTileIterator(*this);
184  }
185 };
186 
189 private:
190  uint base_x;
191  uint base_y;
192  int a_cur;
193  int b_cur;
194  int a_max;
195  int b_max;
196 
197 public:
198 
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)
205  {
206  }
207 
214  {
215  *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
216  }
217 
218  TileIterator& operator ++();
219 
220  virtual TileIterator *Clone() const
221  {
222  return new DiagonalTileIterator(*this);
223  }
224 };
225 
232 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
233 
234 #endif /* TILEAREA_TYPE_H */
TileIndex tile
Base tile of the area.
Definition: tilearea_type.h:66
void ClampToMap()
Clamp the tile area to map borders.
Definition: tilearea.cpp:123
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:106
DiagonalTileArea(TileIndex tile=INVALID_TILE, int8 a=0, int8 b=0)
Construct this tile area with some set values.
Definition: tilearea_type.h:76
uint base_x
The base tile x coordinate from where the iterating happens.
int y
The current &#39;y&#39; position in the rectangle.
void Clear()
Clears the TileArea by making the tile invalid and setting a and b to 0.
Definition: tilearea_type.h:85
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
TileIndex tile
The current tile we are at.
int16 a
Extent in diagonal "x" direction (may be negative to signify the area stretches to the left) ...
Definition: tilearea_type.h:67
uint16 w
The width of the area.
Definition: tilearea_type.h:20
void Clear()
Clears the &#39;tile area&#39;, i.e.
Definition: tilearea_type.h:40
Functions related to maps.
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
OrthogonalTileArea(TileIndex tile=INVALID_TILE, uint8 w=0, uint8 h=0)
Construct this tile area with some set values.
Definition: tilearea_type.h:29
DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
virtual ~TileIterator()
Some compilers really like this.
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:96
Iterator to iterate over a diagonal area of the map.
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition: tilearea.cpp:45
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
Definition: tilearea.cpp:77
int16 b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards) ...
Definition: tilearea_type.h:68
int w
The width of the iterated area.
Represents a diagonal tile area.
Definition: tilearea_type.h:64
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:57
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:260
int x
The current &#39;x&#39; position in the rectangle.
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Base class for tile iterators.
Definition: tilearea_type.h:99
int b_max
The (rotated) y coordinate of the end of the iteration.
OrthogonalTileIterator(const OrthogonalTileArea &ta)
Construct the iterator.
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
int b_cur
The current (rotated) y coordinate of the iteration.
DiagonalTileIterator(const DiagonalTileArea &ta)
Construct the iterator.
Iterator to iterate over a tile area (rectangle) of the map.
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:181
uint base_y
The base tile y coordinate from where the iterating happens.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
int a_cur
The current (rotated) x coordinate of the iteration.
uint16 h
The height of the area.
Definition: tilearea_type.h:21
int a_max
The (rotated) x coordinate of the end of the iteration.