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 
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 
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 
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 */