OpenTTD Source: src/tilearea_type.h Source File

OpenTTD Source 1.11.0-RC1

tilearea_type.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifndef TILEAREA_TYPE_H
11 #define TILEAREA_TYPE_H
12
13 #include "map_func.h"
14
16 struct OrthogonalTileArea {
17 TileIndex tile;
18 uint16 w;
19 uint16 h;
20
27 OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 h = 0) : tile(tile), w(w), h(h)
28 {
29 }
30
31 OrthogonalTileArea(TileIndex start, TileIndex end);
32
33 void Add(TileIndex to_add);
34
38 void Clear()
39 {
40 this->tile = INVALID_TILE;
41 this->w = 0;
42 this->h = 0;
43 }
44
45 bool Intersects(const OrthogonalTileArea &ta) const;
46
47 bool Contains(TileIndex tile) const;
48
49 OrthogonalTileArea &Expand(int rad);
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 */
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
TileIterator::tile
TileIndex tile
The current tile we are at.
Definition: tilearea_type.h:101
DiagonalTileIterator
Iterator to iterate over a diagonal area of the map.
Definition: tilearea_type.h:188
DiagonalTileArea::DiagonalTileArea
DiagonalTileArea(TileIndex tile=INVALID_TILE, int8 a=0, int8 b=0)
Construct this tile area with some set values.
Definition: tilearea_type.h:76
OrthogonalTileIterator::y
int y
The current 'y' position in the rectangle.
Definition: tilearea_type.h:142
TileIterator::operator++
virtual TileIterator & operator++()=0
Move ourselves to the next tile in the rectangle on the map.
map_func.h
OrthogonalTileIterator::OrthogonalTileIterator
OrthogonalTileIterator(const OrthogonalTileArea &ta)
Construct the iterator.
Definition: tilearea_type.h:149
DiagonalTileIterator::b_cur
int b_cur
The current (rotated) y coordinate of the iteration.
Definition: tilearea_type.h:193
TileIterator::~TileIterator
virtual ~TileIterator()
Some compilers really like this.
Definition: tilearea_type.h:113
OrthogonalTileIterator::Clone
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: tilearea_type.h:181
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
OrthogonalTileArea::Add
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
Definition: tilearea.cpp:43
DiagonalTileIterator::base_x
uint base_x
The base tile x coordinate from where the iterating happens.
Definition: tilearea_type.h:190
DiagonalTileArea
Represents a diagonal tile area.
Definition: tilearea_type.h:64
DiagonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:187
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
TileIterator::Clone
virtual TileIterator * Clone() const =0
Allocate a new iterator that is a copy of this one.
OrthogonalTileArea::Clear
void Clear()
Clears the 'tile area', i.e.
Definition: tilearea_type.h:38
OrthogonalTileArea::Intersects
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
Definition: tilearea.cpp:75
OrthogonalTileIterator
Iterator to iterate over a tile area (rectangle) of the map.
Definition: tilearea_type.h:138
TileIterator
Base class for tile iterators.
Definition: tilearea_type.h:99
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:18
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:16
DiagonalTileIterator::base_y
uint base_y
The base tile y coordinate from where the iterating happens.
Definition: tilearea_type.h:191
DiagonalTileArea::b
int16 b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
Definition: tilearea_type.h:68
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition: tilearea_type.h:213
DiagonalTileIterator::a_max
int a_max
The (rotated) x coordinate of the end of the iteration.
Definition: tilearea_type.h:194
OrthogonalTileArea::GetCenterTile
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:57
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:19
OrthogonalTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: tilearea_type.h:166
TileIterator::TileIterator
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
Definition: tilearea_type.h:107
DiagonalTileArea::a
int16 a
Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
Definition: tilearea_type.h:67
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:17
OrthogonalTileIterator::OrthogonalTileIterator
OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition: tilearea_type.h:158
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
DiagonalTileArea::Clear
void Clear()
Clears the TileArea by making the tile invalid and setting a and b to 0.
Definition: tilearea_type.h:85
TILE_ADDXY
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:258
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:96
DiagonalTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: tilearea.cpp:216
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(const DiagonalTileArea &ta)
Construct the iterator.
Definition: tilearea_type.h:203
DiagonalTileArea::tile
TileIndex tile
Base tile of the area.
Definition: tilearea_type.h:66
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
OrthogonalTileArea::Expand
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition: tilearea.cpp:123
OrthogonalTileArea::OrthogonalTileArea
OrthogonalTileArea(TileIndex tile=INVALID_TILE, uint8 w=0, uint8 h=0)
Construct this tile area with some set values.
Definition: tilearea_type.h:27
OrthogonalTileArea::ClampToMap
void ClampToMap()
Clamp the tile area to map borders.
Definition: tilearea.cpp:142
OrthogonalTileIterator::x
int x
The current 'x' position in the rectangle.
Definition: tilearea_type.h:141
DiagonalTileIterator::Clone
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: tilearea_type.h:220
OrthogonalTileIterator::w
int w
The width of the iterated area.
Definition: tilearea_type.h:140
DiagonalTileIterator::b_max
int b_max
The (rotated) y coordinate of the end of the iteration.
Definition: tilearea_type.h:195
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:104
DiagonalTileIterator::a_cur
int a_cur
The current (rotated) x coordinate of the iteration.
Definition: tilearea_type.h:192