OpenTTD Source: src/tests/tilearea.cpp Source File

OpenTTD Source 15.3

tilearea.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
10#include "../stdafx.h"
11
12#include "../3rdparty/catch2/catch.hpp"
13
14#include "../tilearea_type.h"
15#include "../map_func.h"
16
17#include "../safeguards.h"
18
19 struct TileCoord {
20 uint x, y;
21};
22
23static void TestSpiralTileSequence(TileCoord center, uint diameter, std::span<TileCoord> expected)
24{
25 auto tile = TileXY(center.x, center.y);
26
27 std::vector<TileIndex> result;
28 for (auto ti : SpiralTileSequence(tile, diameter)) {
29 result.push_back(ti);
30 }
31 REQUIRE(result.size() == expected.size());
32 for (size_t i = 0; i < result.size(); ++i) {
33 CHECK(TileX(result[i]) == expected[i].x);
34 CHECK(TileY(result[i]) == expected[i].y);
35 }
36}
37
38static void TestSpiralTileSequence(TileCoord start_north, uint radius, uint w, uint h, std::span<TileCoord> expected)
39{
40 auto tile = TileXY(start_north.x, start_north.y);
41
42 std::vector<TileIndex> result;
43 for (auto ti : SpiralTileSequence(tile, radius, w, h)) {
44 result.push_back(ti);
45 }
46 REQUIRE(result.size() == expected.size());
47 for (size_t i = 0; i < result.size(); ++i) {
48 CHECK(TileX(result[i]) == expected[i].x);
49 CHECK(TileY(result[i]) == expected[i].y);
50 }
51}
52
53TEST_CASE("SpiralTileSequence - minimum")
54{
55 Map::Allocate(64, 64);
56
57 TileCoord expected[] = {{63, 63}};
58 TestSpiralTileSequence({63, 63}, 1, expected);
59 TestSpiralTileSequence({63, 63}, 2, expected);
60 TestSpiralTileSequence({63, 63}, 1, 0, 0, expected);
61 TestSpiralTileSequence({63, 63}, 1, 2, 2, expected);
62}
63
64TEST_CASE("SpiralTileSequence - odd")
65{
66 Map::Allocate(64, 64);
67
68 TileCoord expected[] = {
69 {1, 1},
70 {2, 0}, {1, 0}, {0, 0}, {0, 1}, {0, 2}, {1, 2}, {2, 2}, {2, 1},
71 {0, 3}, {1, 3}, {2, 3}, {3, 3}, {3, 2}, {3, 1}, {3, 0},
72 };
73 TestSpiralTileSequence({1, 1}, 5, expected);
74}
75
76TEST_CASE("SpiralTileSequence - even")
77{
78 Map::Allocate(64, 64);
79
80 TileCoord expected[] = {
81 {2, 1}, {1, 1}, {1, 2}, {2, 2},
82 {3, 0}, {2, 0}, {1, 0}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 3}, {2, 3}, {3, 3}, {3, 2}, {3, 1},
83 {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0},
84 };
85 TestSpiralTileSequence({1, 1}, 6, expected);
86 TestSpiralTileSequence({1, 1}, 3, 0, 0, expected);
87}
88
89TEST_CASE("SpiralTileSequence - zero hole")
90{
91 Map::Allocate(64, 64);
92
93 TileCoord expected[] = {
94 {5, 2}, {4, 2}, {3, 2}, {2, 2}, {2, 3}, {3, 3}, {4, 3}, {5, 3},
95 {6, 1}, {5, 1}, {4, 1}, {3, 1}, {2, 1}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {2, 4}, {3, 4}, {4, 4}, {5, 4}, {6, 4}, {6, 3}, {6, 2},
96 };
97 TestSpiralTileSequence({2, 2}, 2, 2, 0, expected);
98}
99
100TEST_CASE("SpiralTileSequence - normal hole")
101{
102 Map::Allocate(64, 64);
103
104 TileCoord expected[] = {
105 {4, 2}, {3, 2}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 5}, {4, 5}, {4, 4}, {4, 3},
106 };
107 TestSpiralTileSequence({2, 2}, 1, 1, 2, expected);
108}
SpiralTileSequence
Generate TileIndices around a center tile or tile area, with increasing distance.
Definition tilearea_type.h:316
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
Map::Allocate
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:37
TileCoord
Definition tilearea.cpp:19