OpenTTD Source: src/tilearea_type.h Source File

OpenTTD Source 12.2

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
15 class OrthogonalTileIterator;
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 OrthogonalTileArea &Expand(int rad);
52
53 void ClampToMap();
54
59 TileIndex GetCenterTile() const
60 {
61 return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
62 }
63
64 OrthogonalTileIterator begin() const;
65
66 OrthogonalTileIterator end() const;
67 };
68
70 struct DiagonalTileArea {
71
72 TileIndex tile;
73 int16 a;
74 int16 b;
75
82 DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
83 {
84 }
85
86 DiagonalTileArea(TileIndex start, TileIndex end);
87
91 void Clear()
92 {
93 this->tile = INVALID_TILE;
94 this->a = 0;
95 this->b = 0;
96 }
97
98 bool Contains(TileIndex tile) const;
99 };
100
102 typedef OrthogonalTileArea TileArea;
103
105 class TileIterator {
106 protected:
107 TileIndex tile;
108
113 TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
114 {
115 }
116
117 public:
119 virtual ~TileIterator()
120 {
121 }
122
127 inline operator TileIndex () const
128 {
129 return this->tile;
130 }
131
136 inline TileIndex operator *() const
137 {
138 return this->tile;
139 }
140
144 virtual TileIterator& operator ++() = 0;
145
149 virtual TileIterator *Clone() const = 0;
150 };
151
153 class OrthogonalTileIterator : public TileIterator {
154 private:
155 int w;
156 int x;
157 int y;
158
159 public:
164 OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
165 {
166 }
167
173 OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
174 {
175 *this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
176 }
177
181 inline TileIterator& operator ++()
182 {
183 assert(this->tile != INVALID_TILE);
184
185 if (--this->x > 0) {
186 this->tile++;
187 } else if (--this->y > 0) {
188 this->x = this->w;
189 this->tile += TileDiffXY(1, 1) - this->w;
190 } else {
191 this->tile = INVALID_TILE;
192 }
193 return *this;
194 }
195
196 virtual TileIterator *Clone() const
197 {
198 return new OrthogonalTileIterator(*this);
199 }
200 };
201
203 class DiagonalTileIterator : public TileIterator {
204 private:
205 uint base_x;
206 uint base_y;
207 int a_cur;
208 int b_cur;
209 int a_max;
210 int b_max;
211
212 public:
213
218 DiagonalTileIterator(const DiagonalTileArea &ta) :
219 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)
220 {
221 }
222
228 DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
229 {
230 *this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
231 }
232
233 TileIterator& operator ++();
234
235 virtual TileIterator *Clone() const
236 {
237 return new DiagonalTileIterator(*this);
238 }
239 };
240
241 #endif /* TILEAREA_TYPE_H */
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
TileIterator::tile
TileIndex tile
The current tile we are at.
Definition: tilearea_type.h:107
DiagonalTileIterator
Iterator to iterate over a diagonal area of the map.
Definition: tilearea_type.h:203
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:82
OrthogonalTileIterator::y
int y
The current 'y' position in the rectangle.
Definition: tilearea_type.h:157
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:164
DiagonalTileIterator::b_cur
int b_cur
The current (rotated) y coordinate of the iteration.
Definition: tilearea_type.h:208
TileIterator::~TileIterator
virtual ~TileIterator()
Some compilers really like this.
Definition: tilearea_type.h:119
OrthogonalTileIterator::Clone
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: tilearea_type.h:196
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:205
DiagonalTileArea
Represents a diagonal tile area.
Definition: tilearea_type.h:70
DiagonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:205
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:40
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:153
TileIterator
Base class for tile iterators.
Definition: tilearea_type.h:105
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:20
OrthogonalTileArea::begin
OrthogonalTileIterator begin() const
Returns an iterator to the beginning of the tile area.
Definition: tilearea.cpp:153
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
DiagonalTileIterator::base_y
uint base_y
The base tile y coordinate from where the iterating happens.
Definition: tilearea_type.h:206
OrthogonalTileArea::end
OrthogonalTileIterator end() const
Returns an iterator to the end of the tile area.
Definition: tilearea.cpp:162
DiagonalTileArea::b
int16 b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
Definition: tilearea_type.h:74
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition: tilearea_type.h:228
DiagonalTileIterator::a_max
int a_max
The (rotated) x coordinate of the end of the iteration.
Definition: tilearea_type.h:209
OrthogonalTileArea::GetCenterTile
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:59
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:21
OrthogonalTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: tilearea_type.h:181
TileIterator::TileIterator
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
Definition: tilearea_type.h:113
DiagonalTileArea::a
int16 a
Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
Definition: tilearea_type.h:73
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
OrthogonalTileIterator::OrthogonalTileIterator
OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
Construct the iterator.
Definition: tilearea_type.h:173
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
TileIterator::operator*
TileIndex operator*() const
Get the tile we are currently at.
Definition: tilearea_type.h:136
DiagonalTileArea::Clear
void Clear()
Clears the TileArea by making the tile invalid and setting a and b to 0.
Definition: tilearea_type.h:91
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:102
DiagonalTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: tilearea.cpp:234
DiagonalTileIterator::DiagonalTileIterator
DiagonalTileIterator(const DiagonalTileArea &ta)
Construct the iterator.
Definition: tilearea_type.h:218
DiagonalTileArea::tile
TileIndex tile
Base tile of the area.
Definition: tilearea_type.h:72
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
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:29
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:156
DiagonalTileIterator::Clone
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: tilearea_type.h:235
OrthogonalTileIterator::w
int w
The width of the iterated area.
Definition: tilearea_type.h:155
DiagonalTileIterator::b_max
int b_max
The (rotated) y coordinate of the end of the iteration.
Definition: tilearea_type.h:210
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:207