OpenTTD Source 20250905-master-g122023be8d
yapf_common.hpp
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#ifndef YAPF_COMMON_HPP
11#define YAPF_COMMON_HPP
12
13#include "../../core/bitmath_func.hpp"
14#include "../../tile_type.h"
15#include "../../track_type.h"
16
18template <class Types>
20public:
21 typedef typename Types::Tpf Tpf;
22 typedef typename Types::NodeList::Item Node;
23 typedef typename Node::Key Key;
24
25protected:
28
30 inline Tpf &Yapf()
31 {
32 return *static_cast<Tpf *>(this);
33 }
34
35public:
37 void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
38 {
39 this->origin_tile = tile;
40 this->origin_trackdirs = trackdirs;
41 }
42
45 {
46 bool is_choice = (KillFirstBit(this->origin_trackdirs) != TRACKDIR_BIT_NONE);
47 for (TrackdirBits tdb = this->origin_trackdirs; tdb != TRACKDIR_BIT_NONE; tdb = KillFirstBit(tdb)) {
49 Node &n1 = Yapf().CreateNewNode();
50 n1.Set(nullptr, this->origin_tile, td, is_choice);
51 Yapf().AddStartupNode(n1);
52 }
53 }
54};
55
57template <class Types>
59public:
60 typedef typename Types::Tpf Tpf;
61 typedef typename Types::NodeList::Item Node;
62 typedef typename Node::Key Key;
63
64protected:
70
72 inline Tpf &Yapf()
73 {
74 return *static_cast<Tpf *>(this);
75 }
76
77public:
80 {
81 this->origin_tile = tile;
82 this->origin_td = td;
83 this->reverse_tile = tiler;
84 this->reverse_td = tdr;
85 this->reverse_penalty = reverse_penalty;
86 }
87
90 {
91 if (this->origin_tile != INVALID_TILE && this->origin_td != INVALID_TRACKDIR) {
92 Node &n1 = Yapf().CreateNewNode();
93 n1.Set(nullptr, this->origin_tile, this->origin_td, false);
94 Yapf().AddStartupNode(n1);
95 }
96 if (this->reverse_tile != INVALID_TILE && this->reverse_td != INVALID_TRACKDIR) {
97 Node &n2 = Yapf().CreateNewNode();
98 n2.Set(nullptr, this->reverse_tile, this->reverse_td, false);
99 n2.cost = this->reverse_penalty;
100 Yapf().AddStartupNode(n2);
101 }
102 }
103};
104
111template <class Ttypes>
113 : public Ttypes::PfBase
114 , public Ttypes::PfCost
115 , public Ttypes::PfCache
116 , public Ttypes::PfOrigin
117 , public Ttypes::PfDestination
118 , public Ttypes::PfFollow
119{
120};
121
129inline int OctileDistanceCost(TileIndex start_tile, Trackdir start_td, TileIndex destination_tile)
130{
131 static constexpr int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
132 static constexpr int dg_dir_to_y_offs[] = {0, 1, 0, -1};
133
134 const DiagDirection exitdir = TrackdirToExitdir(start_td);
135
136 const int x1 = 2 * TileX(start_tile) + dg_dir_to_x_offs[static_cast<int>(exitdir)];
137 const int y1 = 2 * TileY(start_tile) + dg_dir_to_y_offs[static_cast<int>(exitdir)];
138 const int x2 = 2 * TileX(destination_tile);
139 const int y2 = 2 * TileY(destination_tile);
140 const int dx = abs(x1 - x2);
141 const int dy = abs(y1 - y2);
142 const int dmin = std::min(dx, dy);
143 const int dxy = abs(dx - dy);
144
145 return dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
146}
147
148#endif /* YAPF_COMMON_HPP */
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
Node::Key Key
key to hash tables
Tpf & Yapf()
to access inherited path finder
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
TrackdirBits origin_trackdirs
origin trackdir mask
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
TileIndex origin_tile
origin tile
Types::NodeList::Item Node
this will be our node type
YAPF origin provider base class - used when there are two tile/trackdir origins.
Trackdir reverse_td
second (reverse) origin trackdir
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
Tpf & Yapf()
to access inherited path finder
void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler=INVALID_TILE, Trackdir tdr=INVALID_TRACKDIR, int reverse_penalty=0)
set origin (tiles, trackdirs, etc.)
int reverse_penalty
penalty to be added for using the reverse origin
Node::Key Key
key to hash tables
TileIndex reverse_tile
second (reverse) origin tile
Trackdir origin_td
first origin trackdir
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::NodeList::Item Node
this will be our node type
TileIndex origin_tile
first origin tile
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
DiagDirection
Enumeration for diagonal directions.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:424
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:414
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition track_func.h:439
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
int OctileDistanceCost(TileIndex start_tile, Trackdir start_td, TileIndex destination_tile)
Calculates the octile distance cost between a starting tile / trackdir and a destination tile.