OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_orthogonal.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11
12#include "tilearea_orthogonal.h"
13
14#include "safeguards.h"
15
22{
23 assert(start < Map::Size());
24 assert(end < Map::Size());
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) std::swap(sx, ex);
32 if (sy > ey) std::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 !(left2 > right1 || right2 < left1 || top2 > bottom1 || bottom2 < top1);
92}
93
100{
101 if (this->w == 0) return false;
102
103 assert(this->w != 0 && this->h != 0);
104
105 uint left = TileX(this->tile);
106 uint top = TileY(this->tile);
107 uint tile_x = TileX(tile);
108 uint tile_y = TileY(tile);
109
110 return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h);
111}
112
119{
120 int x = TileX(this->tile);
121 int y = TileY(this->tile);
122
123 int sx = std::max<int>(x - rad, 0);
124 int sy = std::max<int>(y - rad, 0);
125 int ex = std::min<int>(x + this->w + rad, Map::SizeX());
126 int ey = std::min<int>(y + this->h + rad, Map::SizeY());
127
128 this->tile = TileXY(sx, sy);
129 this->w = ex - sx;
130 this->h = ey - sy;
131 return *this;
132}
133
138{
139 assert(this->tile < Map::Size());
140 this->w = std::min<int>(this->w, Map::SizeX() - TileX(this->tile));
141 this->h = std::min<int>(this->h, Map::SizeY() - TileY(this->tile));
142}
void ClampToMap()
Clamp the tile area to map borders.
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
uint16_t w
The width of the area.
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
bool Intersects(const OrthogonalTileArea &ta) const
Does this tile area intersect with another?
std::default_sentinel_t end() const
Returns an iterator to the end of the tile area.
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
OrthogonalTileArea(TileIndex tile=INVALID_TILE, uint16_t w=0, uint16_t h=0)
Construct this tile area with some set values.
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
constexpr 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.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:262
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:271
static uint Size()
Get the size of the map.
Definition map_func.h:280
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Type for storing and iterating orthogonal tile areas.