OpenTTD Source  1.11.2
tilearea.cpp
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 #include "stdafx.h"
11 
12 #include "tilearea_type.h"
13 
14 #include "safeguards.h"
15 
22 {
23  assert(start < MapSize());
24  assert(end < MapSize());
25 
26  uint sx = TileX(start);
27  uint sy = TileY(start);
28  uint ex = TileX(end);
29  uint ey = TileY(end);
30 
31  if (sx > ex) Swap(sx, ex);
32  if (sy > ey) Swap(sy, ey);
33 
34  this->tile = TileXY(sx, sy);
35  this->w = ex - sx + 1;
36  this->h = ey - sy + 1;
37 }
38 
44 {
45  if (this->tile == INVALID_TILE) {
46  this->tile = to_add;
47  this->w = 1;
48  this->h = 1;
49  return;
50  }
51 
52  uint sx = TileX(this->tile);
53  uint sy = TileY(this->tile);
54  uint ex = sx + this->w - 1;
55  uint ey = sy + this->h - 1;
56 
57  uint ax = TileX(to_add);
58  uint ay = TileY(to_add);
59 
60  sx = std::min(ax, sx);
61  sy = std::min(ay, sy);
62  ex = std::max(ax, ex);
63  ey = std::max(ay, ey);
64 
65  this->tile = TileXY(sx, sy);
66  this->w = ex - sx + 1;
67  this->h = ey - sy + 1;
68 }
69 
76 {
77  if (ta.w == 0 || this->w == 0) return false;
78 
79  assert(ta.w != 0 && ta.h != 0 && this->w != 0 && this->h != 0);
80 
81  uint left1 = TileX(this->tile);
82  uint top1 = TileY(this->tile);
83  uint right1 = left1 + this->w - 1;
84  uint bottom1 = top1 + this->h - 1;
85 
86  uint left2 = TileX(ta.tile);
87  uint top2 = TileY(ta.tile);
88  uint right2 = left2 + ta.w - 1;
89  uint bottom2 = top2 + ta.h - 1;
90 
91  return !(
92  left2 > right1 ||
93  right2 < left1 ||
94  top2 > bottom1 ||
95  bottom2 < top1
96  );
97 }
98 
105 {
106  if (this->w == 0) return false;
107 
108  assert(this->w != 0 && this->h != 0);
109 
110  uint left = TileX(this->tile);
111  uint top = TileY(this->tile);
112  uint tile_x = TileX(tile);
113  uint tile_y = TileY(tile);
114 
115  return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h);
116 }
117 
124 {
125  int x = TileX(this->tile);
126  int y = TileY(this->tile);
127 
128  int sx = std::max<int>(x - rad, 0);
129  int sy = std::max<int>(y - rad, 0);
130  int ex = std::min<int>(x + this->w + rad, MapSizeX());
131  int ey = std::min<int>(y + this->h + rad, MapSizeY());
132 
133  this->tile = TileXY(sx, sy);
134  this->w = ex - sx;
135  this->h = ey - sy;
136  return *this;
137 }
138 
143 {
144  assert(this->tile < MapSize());
145  this->w = std::min<int>(this->w, MapSizeX() - TileX(this->tile));
146  this->h = std::min<int>(this->h, MapSizeY() - TileY(this->tile));
147 }
148 
155 {
156  assert(start < MapSize());
157  assert(end < MapSize());
158 
159  /* Unfortunately we can't find a new base and make all a and b positive because
160  * the new base might be a "flattened" corner where there actually is no single
161  * tile. If we try anyway the result is either inaccurate ("one off" half of the
162  * time) or the code gets much more complex;
163  *
164  * We also need to increment/decrement a and b here to have one-past-end semantics
165  * for a and b, just the way the orthogonal tile area does it for w and h. */
166 
167  this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start);
168  this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start);
169  if (this->a > 0) {
170  this->a++;
171  } else {
172  this->a--;
173  }
174 
175  if (this->b > 0) {
176  this->b++;
177  } else {
178  this->b--;
179  }
180 }
181 
188 {
189  int a = TileY(tile) + TileX(tile);
190  int b = TileY(tile) - TileX(tile);
191 
192  int start_a = TileY(this->tile) + TileX(this->tile);
193  int start_b = TileY(this->tile) - TileX(this->tile);
194 
195  int end_a = start_a + this->a;
196  int end_b = start_b + this->b;
197 
198  /* Swap if necessary, preserving the "one past end" semantics. */
199  if (start_a > end_a) {
200  int tmp = start_a;
201  start_a = end_a + 1;
202  end_a = tmp + 1;
203  }
204  if (start_b > end_b) {
205  int tmp = start_b;
206  start_b = end_b + 1;
207  end_b = tmp + 1;
208  }
209 
210  return (a >= start_a && a < end_a && b >= start_b && b < end_b);
211 }
212 
217 {
218  assert(this->tile != INVALID_TILE);
219 
220  /* Determine the next tile, while clipping at map borders */
221  bool new_line = false;
222  do {
223  /* Iterate using the rotated coordinates. */
224  if (this->a_max == 1 || this->a_max == -1) {
225  /* Special case: Every second column has zero length, skip them completely */
226  this->a_cur = 0;
227  if (this->b_max > 0) {
228  this->b_cur = std::min(this->b_cur + 2, this->b_max);
229  } else {
230  this->b_cur = std::max(this->b_cur - 2, this->b_max);
231  }
232  } else {
233  /* Every column has at least one tile to process */
234  if (this->a_max > 0) {
235  this->a_cur += 2;
236  new_line = this->a_cur >= this->a_max;
237  } else {
238  this->a_cur -= 2;
239  new_line = this->a_cur <= this->a_max;
240  }
241  if (new_line) {
242  /* offset of initial a_cur: one tile in the same direction as a_max
243  * every second line.
244  */
245  this->a_cur = abs(this->a_cur) % 2 ? 0 : (this->a_max > 0 ? 1 : -1);
246 
247  if (this->b_max > 0) {
248  ++this->b_cur;
249  } else {
250  --this->b_cur;
251  }
252  }
253  }
254 
255  /* And convert the coordinates back once we've gone to the next tile. */
256  uint x = this->base_x + (this->a_cur - this->b_cur) / 2;
257  uint y = this->base_y + (this->b_cur + this->a_cur) / 2;
258  /* Prevent wrapping around the map's borders. */
259  this->tile = x >= MapSizeX() || y >= MapSizeY() ? INVALID_TILE : TileXY(x, y);
260  } while (this->tile > MapSize() && this->b_max != this->b_cur);
261 
262  if (this->b_max == this->b_cur) this->tile = INVALID_TILE;
263  return *this;
264 }
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:101
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
DiagonalTileIterator::b_cur
int b_cur
The current (rotated) y coordinate of the iteration.
Definition: tilearea_type.h:193
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::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
OrthogonalTileArea::Intersects
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
Definition: tilearea.cpp:75
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
TileIterator
Base class for tile iterators.
Definition: tilearea_type.h:99
IsInsideBS
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:188
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
safeguards.h
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
DiagonalTileArea::b
int16 b
Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
Definition: tilearea_type.h:68
stdafx.h
DiagonalTileIterator::a_max
int a_max
The (rotated) x coordinate of the end of the iteration.
Definition: tilearea_type.h:194
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:19
tilearea_type.h
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
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
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
DiagonalTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: tilearea.cpp:216
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: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:27
Swap
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:215
OrthogonalTileArea::ClampToMap
void ClampToMap()
Clamp the tile area to map borders.
Definition: tilearea.cpp:142
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