OpenTTD Source 20250924-master-gbec4e71d53
yapf_river_builder.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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../../stdafx.h"
11
12#include "../../water.h"
13#include "../../genworld.h"
14#include "yapf.hpp"
15
16#include "../../safeguards.h"
17
18/* River builder pathfinder node. */
19struct YapfRiverBuilderNode : CYapfNodeT<CYapfNodeKeyTrackDir, YapfRiverBuilderNode> {};
20
21/* River builder pathfinder node list. */
23
24/* We don't need a follower but YAPF requires one. */
25struct DummyFollower {};
26
27/* We don't need a vehicle but YAPF requires one. */
29
31
32/* Types struct required for YAPF components. */
39
40/* River builder pathfinder implementation. */
42 : public CYapfBaseT<RiverBuilderTypes>
43 , public CYapfSegmentCostCacheNoneT<RiverBuilderTypes>
44{
45public:
46 using Node = RiverBuilderTypes::NodeList::Item;
47 using Key = Node::Key;
48
49protected:
52
53 inline YapfRiverBuilder &Yapf()
54 {
55 return *static_cast<YapfRiverBuilder *>(this);
56 }
57
58public:
60 {
61 this->start_tile = start_tile;
62 this->end_tile = end_tile;
63 }
64
65 void PfSetStartupNodes()
66 {
67 Node &node = Yapf().CreateNewNode();
68 node.Set(nullptr, this->start_tile, INVALID_TRACKDIR, false);
69 Yapf().AddStartupNode(node);
70 }
71
72 inline bool PfDetectDestination(Node &n) const
73 {
74 return n.GetTile() == this->end_tile;
75 }
76
77 inline bool PfCalcCost(Node &n, const DummyFollower *)
78 {
79 n.cost = n.parent->cost + 1 + RandomRange(_settings_game.game_creation.river_route_random);
80 return true;
81 }
82
83 inline bool PfCalcEstimate(Node &n)
84 {
85 n.estimate = n.cost + DistanceManhattan(this->end_tile, n.GetTile());
86 assert(n.estimate >= n.parent->estimate);
87 return true;
88 }
89
90 inline void PfFollowNode(Node &old_node)
91 {
92 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; ++d) {
93 const TileIndex t = old_node.GetTile() + TileOffsByDiagDir(d);
94 if (IsValidTile(t) && RiverFlowsDown(old_node.GetTile(), t)) {
95 Node &node = Yapf().CreateNewNode();
96 node.Set(&old_node, t, INVALID_TRACKDIR, true);
97 Yapf().AddNewNode(node, DummyFollower{});
98 }
99 }
100 }
101
102 inline char TransportTypeChar() const
103 {
104 return '~';
105 }
106
107 static void BuildRiver(TileIndex start_tile, TileIndex end_tile, TileIndex spring_tile, bool main_river)
108 {
110 if (pf.FindPath(nullptr) == false) return; // No path found
111
112 /* First, build the river without worrying about its width. */
113 for (Node *node = pf.GetBestNode(); node != nullptr; node = node->parent) {
114 TileIndex tile = node->GetTile();
115 if (!IsWaterTile(tile)) {
117 }
118 }
119
120 /* If the river is a main river, go back along the path to widen it.
121 * Don't make wide rivers if we're using the original landscape generator.
122 */
124 const uint long_river_length = _settings_game.game_creation.min_river_length * 4;
125
126 for (Node *node = pf.GetBestNode(); node != nullptr; node = node->parent) {
127 const TileIndex center_tile = node->GetTile();
128
129 /* Check if we should widen river depending on how far we are away from the source. */
130 uint current_river_length = DistanceManhattan(spring_tile, center_tile);
131 uint diameter = std::min(3u, (current_river_length / (long_river_length / 3u)) + 1u);
132 if (diameter <= 1) continue;
133
134 for (auto tile : SpiralTileSequence(center_tile, diameter)) {
135 RiverMakeWider(tile, center_tile);
136 }
137 }
138 }
139 }
140};
141
149void YapfBuildRiver(TileIndex start_tile, TileIndex end_tile, TileIndex spring_tile, bool main_river)
150{
151 YapfRiverBuilder::BuildRiver(start_tile, end_tile, spring_tile, main_river);
152}
CYapfBaseT - A-star type path finder base class.
Definition yapf_base.hpp:49
void AddNewNode(Node &n, const TrackFollower &tf)
AddNewNode() - called by Tderived::PfFollowNode() for each child node.
Node & CreateNewNode()
Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddS...
void AddStartupNode(Node &n)
Add new node (created by CreateNewNode and filled with data) into open list.
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Hash table based node list multi-container class.
Definition nodelist.hpp:21
Generate TileIndices around a center tile or tile area, with increasing distance.
TileIndex start_tile
Start tile of the river.
TileIndex end_tile
End tile of the river.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_END
Used for iterations.
@ DIAGDIR_BEGIN
Used for iterations.
@ LG_ORIGINAL
The original landscape generator.
Definition genworld.h:21
bool RiverFlowsDown(TileIndex begin, TileIndex end)
Check whether a river at begin could (logically) flow down to end.
void RiverMakeWider(TileIndex tile, TileIndex origin_tile)
Widen a river by expanding into adjacent tiles via circular tile search.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:142
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:569
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Yapf Node base.
Definition yapf_node.hpp:61
uint8_t min_river_length
the minimum river length
uint8_t land_generator
the landscape generator
uint8_t river_route_random
the amount of randomicity for the route finding
GameCreationSettings game_creation
settings used during the creation of a game (map)
Node of the link graph.
Definition linkgraph.h:90
Vehicle data structure.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
void MakeRiverAndModifyDesertZoneAround(TileIndex tile)
Make a river tile and remove desert directly around it.
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192
Base includes/functions for YAPF.
void YapfBuildRiver(TileIndex start_tile, TileIndex end_tile, TileIndex spring_tile, bool main_river)
Builds a river from the start tile to the end tile.