OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_spiral.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_spiral.h"
13
14#include "map_func.h"
15
16#include "safeguards.h"
17
20{
21 assert(diameter > 0);
22
23 if (diameter % 2 == 1) {
24 this->extent.fill(1);
25 this->dir = DiagDirection::Invalid; // special case for odd diameters, see Increment()
26 this->position = 0;
27
28 this->x = TileX(center);
29 this->y = TileY(center);
30 } else {
31 this->extent.fill(0);
33 this->InitPosition();
34
35 /* Start with the west corner of the center 2x2 rect */
36 this->x = TileX(center) + 1;
37 this->y = TileY(center);
38 }
39 this->SkipOutsideMap();
40}
41
43SpiralTileIterator::SpiralTileIterator(TileIndex start_north, uint radius, uint w, uint h) :
44 max_radius(radius), extent{w, h, w, h}, cur_radius(0), dir(DiagDirection::Begin),
45 /* first tile is the west corner */
46 x(TileX(start_north) + w + 1), y(TileY(start_north))
47{
48 assert(max_radius > 0);
49 this->InitPosition();
50 this->SkipOutsideMap();
51}
52
57{
58 while (!this->IsEnd() && (this->x >= Map::SizeX() || this->y >= Map::SizeY())) this->Increment();
59}
60
65{
66 this->position = this->extent[this->dir] + this->cur_radius * 2 + 1;
67}
68
74{
75 assert(!this->IsEnd());
76
77 /* Special value for first tile in areas with odd diameter */
78 if (this->dir == DiagDirection::Invalid) {
79 const auto west = TileIndexDiffCByDir(Direction::W);
80 this->x += west.x;
81 this->y += west.y;
83 this->InitPosition();
84 return;
85 }
86
87 /* Step to the next 'neighbour' in the circular line */
88 const auto diff = TileIndexDiffCByDiagDir(this->dir);
89 this->x += diff.x;
90 this->y += diff.y;
91 --this->position;
92 if (this->position > 0) return;
93
94 /* Corner reached, switch direction */
95 ++this->dir;
96
97 if (this->dir == DiagDirection::End) {
98 /* Jump to next circle */
99 const auto west = TileIndexDiffCByDir(Direction::W);
100 this->x += west.x;
101 this->y += west.y;
102 ++this->cur_radius;
104 }
105
106 this->InitPosition();
107}
@ Begin
Used for iteration.
Definition cargotype.h:22
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.
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.
void SkipOutsideMap()
Advance the internal state until it reaches a valid tile or the end.
uint position
The current position along an edge.
uint x
The current tile X-coordinate.
uint cur_radius
The current radius.
uint y
The current tile Y-coordinate.
DiagDirection
Enumeration for diagonal directions.
@ Begin
Used for iterations.
@ Invalid
Flag for an invalid DiagDirection.
@ End
Used for iterations.
Functions related to maps.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
Definition map_func.h:487
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
TileIndexDiffC TileIndexDiffCByDir(Direction dir)
Returns the TileIndexDiffC offset from a Direction.
Definition map_func.h:501
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
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
Type for iterating a sprial tile area.