OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_spiral.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_SPIRAL_H
11#define TILEAREA_SPIRAL_H
12
13#include "direction_type.h"
14#include "map_func.h"
15#include "tile_type.h"
16
21public:
23 using difference_type = std::ptrdiff_t;
24 using iterator_category = std::forward_iterator_tag;
25 using pointer = void;
26 using reference = void;
27
28 SpiralTileIterator(TileIndex center, uint diameter);
29 SpiralTileIterator(TileIndex start_north, uint radius, uint w, uint h);
30
36 bool operator==(const SpiralTileIterator &rhs) const
37 {
38 return this->x == rhs.x && this->y == rhs.y;
39 }
40
45 bool operator==(const std::default_sentinel_t &) const
46 {
47 return this->IsEnd();
48 }
49
55 {
56 return TileXY(this->x, this->y);
57 }
58
64 {
65 this->Increment();
66 this->SkipOutsideMap();
67 return *this;
68 }
69
70private:
71 /* set by constructor, const afterwards */
72 const uint max_radius;
74
75 /* mutable iterator state */
78 uint position;
79 uint x;
80 uint y;
81
82 void SkipOutsideMap();
83 void InitPosition();
84 void Increment();
85
90 bool IsEnd() const
91 {
92 return this->cur_radius == this->max_radius && this->dir != DiagDirection::Invalid;
93 }
94};
95
100public:
102
124 SpiralTileSequence(TileIndex center, uint diameter) : start(center, diameter) {}
125
150 SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h) : start(start_north, radius, w, h) {}
151
157 {
158 return start;
159 }
160
165 std::default_sentinel_t end() const
166 {
167 return std::default_sentinel_t();
168 }
169
170private:
172};
173
174#endif /* TILEAREA_SPIRAL_H */
Helper class for SpiralTileSequence.
SpiralTileIterator(TileIndex center, uint diameter)
Create the iterator.
void InitPosition()
Initialise "position" after "dir" was changed.
const uint max_radius
The maximum radius of the spiral.
bool IsEnd() const
Test whether the iterator reached the end.
SpiralTileIterator & operator++()
Increment the iterator and set it to the next position.
DiagDirection dir
The current spiral direction.
DiagDirectionIndexArray< uint > extent
Size of hole in the centre of the spiral.
void Increment()
Advance the internal state to the next potential tile.
bool operator==(const SpiralTileIterator &rhs) const
Compare with other iterator.
void SkipOutsideMap()
Advance the internal state until it reaches a valid tile or the end.
uint position
The current position along an edge.
bool operator==(const std::default_sentinel_t &) const
Compare with end sentinel.
std::ptrdiff_t difference_type
difference_type iterator trait
uint x
The current tile X-coordinate.
void pointer
pointer iterator trait
TileIndex value_type
value_type iterator trait
uint cur_radius
The current radius.
std::forward_iterator_tag iterator_category
iterator_category iterator trait
TileIndex operator*() const
Get the current tile.
void reference
reference iterator trait
uint y
The current tile Y-coordinate.
SpiralTileIterator begin() const
Returns an iterator to the start of the tile area.
SpiralTileSequence(TileIndex center, uint diameter)
Generate TileIndices for a square area around a center tile.
SpiralTileSequence(TileIndex start_north, uint radius, uint w, uint h)
Generate TileIndices for a rectangular area with an optional rectangular hole in the center.
std::default_sentinel_t end() const
Returns an iterator to the end of the tile area.
SpiralTileIterator start
The starting iterator for thi spiral sequence.
SpiralTileIterator iterator
The tile iterator type for this tile area type.
Different types to 'show' directions.
EnumIndexArray< T, DiagDirection, DiagDirection::End > DiagDirectionIndexArray
Array with DiagDirection as index.
DiagDirection
Enumeration for diagonal directions.
@ Invalid
Flag for an invalid DiagDirection.
Functions related to maps.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
Types related to tiles.
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