OpenTTD
tilearea_type.h
Go to the documentation of this file.
1 /* $Id$ */
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  OrthogonalTileArea &Expand(int rad);
52 
53  void ClampToMap();
54 
60  {
61  return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
62  }
63 };
64 
67 
69  int16 a;
70  int16 b;
71 
78  DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
79  {
80  }
81 
83 
87  void Clear()
88  {
89  this->tile = INVALID_TILE;
90  this->a = 0;
91  this->b = 0;
92  }
93 
94  bool Contains(TileIndex tile) const;
95 };
96 
99 
102 protected:
104 
109  TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
110  {
111  }
112 
113 public:
115  virtual ~TileIterator()
116  {
117  }
118 
123  inline operator TileIndex () const
124  {
125  return this->tile;
126  }
127 
131  virtual TileIterator& operator ++() = 0;
132 
136  virtual TileIterator *Clone() const = 0;
137 };
138 
141 private:
142  int w;
143  int x;
144  int y;
145 
146 public:
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)
152  {
153  }
154 
161  {
162  *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
163  }
164 
168  inline TileIterator& operator ++()
169  {
170  assert(this->tile != INVALID_TILE);
171 
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;
179  }
180  return *this;
181  }
182 
183  virtual TileIterator *Clone() const
184  {
185  return new OrthogonalTileIterator(*this);
186  }
187 };
188 
191 private:
192  uint base_x;
193  uint base_y;
194  int a_cur;
195  int b_cur;
196  int a_max;
197  int b_max;
198 
199 public:
200 
206  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)
207  {
208  }
209 
216  {
217  *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
218  }
219 
220  TileIterator& operator ++();
221 
222  virtual TileIterator *Clone() const
223  {
224  return new DiagonalTileIterator(*this);
225  }
226 };
227 
234 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
235 
236 #endif /* TILEAREA_TYPE_H */
TileIndex tile
Base tile of the area.
Definition: tilearea_type.h:68
void ClampToMap()
Clamp the tile area to map borders.
Definition: tilearea.cpp:144
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:78
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:87
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition: tilearea.cpp:125
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:69
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:98
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:70
int w
The width of the iterated area.
Represents a diagonal tile area.
Definition: tilearea_type.h:66
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:59
#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.
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.