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