OpenTTD: src/tilearea_type.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
18 struct OrthogonalTileArea {
19 TileIndex tile;
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
33 OrthogonalTileArea(TileIndex start, TileIndex end);
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
57 TileIndex GetCenterTile() const
58 {
59 return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
60 }
61 };
62
64 struct DiagonalTileArea {
65
66 TileIndex tile;
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
80 DiagonalTileArea(TileIndex start, TileIndex end);
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
96 typedef OrthogonalTileArea TileArea;
97
99 class TileIterator {
100 protected:
101 TileIndex tile;
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
138 class OrthogonalTileIterator : public TileIterator {
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
158 OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
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
188 class DiagonalTileIterator : public TileIterator {
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
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)
205 {
206 }
207
213 DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
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 */