OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_orthogonal.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef TILEAREA_ORTHOGONAL_H
11#define TILEAREA_ORTHOGONAL_H
12
13#include "map_func.h"
14
17public:
19 uint16_t w;
20 uint16_t h;
21
28 OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint16_t w = 0, uint16_t h = 0) : tile(tile), w(w), h(h) {}
29
31
36 inline bool IsEmpty() const
37 {
38 return this->tile == INVALID_TILE;
39 }
40
41 void Add(TileIndex to_add);
42
47 inline void Add(const OrthogonalTileArea &area)
48 {
49 /* Only the top and bottom corners need to be added. */
50 this->Add(area.tile);
51 this->Add(TileAddXY(area.tile, area.w - 1, area.h - 1));
52 }
53
57 inline void Clear()
58 {
59 this->tile = INVALID_TILE;
60 this->w = 0;
61 this->h = 0;
62 }
63
64 bool Intersects(const OrthogonalTileArea &ta) const;
65 bool Contains(TileIndex tile) const;
66 OrthogonalTileArea &Expand(int rad);
67 void ClampToMap();
68
74 {
75 return TileAddXY(this->tile, this->w / 2, this->h / 2);
76 }
77
79 class iterator {
80 private:
82 int w;
83 int x;
84 int y;
85
86 public:
88 using difference_type = std::ptrdiff_t;
89 using iterator_category = std::forward_iterator_tag;
90 using pointer = void;
91 using reference = void;
92
97 explicit iterator(const OrthogonalTileArea &ta) : tile(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h) {}
98
103 bool operator==(const iterator &) const = default;
104
109 bool operator==(const std::default_sentinel_t &) const
110 {
111 return **this == INVALID_TILE;
112 }
113
119 {
120 return this->tile;
121 }
122
128 {
129 assert(this->tile != INVALID_TILE);
130
131 if (--this->x > 0) {
132 this->tile++;
133 } else if (--this->y > 0) {
134 this->x = this->w;
135 this->tile += TileDiffXY(1 - this->w, 1);
136 } else {
137 this->tile = INVALID_TILE;
138 }
139 return *this;
140 }
141 };
142
147 inline iterator begin() const
148 {
149 return iterator{*this};
150 }
151
156 inline std::default_sentinel_t end() const
157 {
158 return std::default_sentinel_t();
159 }
160};
161
164
165#endif /* TILEAREA_ORTHOGONAL_H */
Iterator to iterate over a tile area (rectangle) of the map.
TileIndex operator*() const
Get the current tile.
void pointer
pointer iterator trait
iterator(const OrthogonalTileArea &ta)
Construct the iterator.
std::ptrdiff_t difference_type
difference_type iterator trait
bool operator==(const iterator &) const =default
Compare with other iterator.
int y
The current 'y' position in the rectangle.
void reference
reference iterator trait
TileIndex tile
The current tile.
std::forward_iterator_tag iterator_category
iterator_category iterator trait
int x
The current 'x' position in the rectangle.
int w
The width of the iterated area.
TileIndex value_type
value_type iterator trait
iterator & operator++()
Increment the iterator and set it to the next position.
bool operator==(const std::default_sentinel_t &) const
Compare with end sentinel.
Represents the covered area of e.g.
void ClampToMap()
Clamp the tile area to map borders.
void Add(const OrthogonalTileArea &area)
Add another tile area to this tile area.
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.
void Clear()
Clears the 'tile area', i.e.
TileIndex tile
The base tile of the area.
TileIndex GetCentreTile() const
Get the centre tile.
bool IsEmpty() const
Test if this tile area is empty.
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.
iterator begin() const
Returns an iterator to the start of the tile area.
Functions related to maps.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:474
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:392
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
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.