OpenTTD Source 20260421-master-gc2fbc6fdeb
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef YAPF_COMMON_HPP
11#define YAPF_COMMON_HPP
12
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:
27 inline Tpf &Yapf()
28 {
29 return *static_cast<Tpf *>(this);
30 }
31
32public:
38 void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
39 {
40 bool is_choice = (KillFirstBit(trackdirs) != TRACKDIR_BIT_NONE);
41 for (TrackdirBits tdb = trackdirs; tdb != TRACKDIR_BIT_NONE; tdb = KillFirstBit(tdb)) {
43 Node &node = Yapf().CreateNewNode();
44 node.Set(nullptr, tile, td, is_choice);
45 Yapf().AddStartupNode(node);
46 }
47 }
48};
49
51template <class Types>
53public:
54 typedef typename Types::Tpf Tpf;
55 typedef typename Types::NodeList::Item Node;
56 typedef typename Node::Key Key;
57
58protected:
60 inline Tpf &Yapf()
61 {
62 return *static_cast<Tpf *>(this);
63 }
64
65public:
74 void SetOrigin(TileIndex forward_tile, Trackdir forward_td, TileIndex reverse_tile = INVALID_TILE,
75 Trackdir reverse_td = INVALID_TRACKDIR, int reverse_penalty = 0)
76 {
77 if (forward_tile != INVALID_TILE && forward_td != INVALID_TRACKDIR) {
78 Node &node = Yapf().CreateNewNode();
79 node.Set(nullptr, forward_tile, forward_td, false);
80 Yapf().AddStartupNode(node);
81 }
82 if (reverse_tile != INVALID_TILE && reverse_td != INVALID_TRACKDIR) {
83 Node &node = Yapf().CreateNewNode();
84 node.Set(nullptr, reverse_tile, reverse_td, false);
85 node.cost = reverse_penalty;
86 Yapf().AddStartupNode(node);
87 }
88 }
89};
90
97template <class Ttypes>
98class CYapfT
99 : public Ttypes::PfBase
100 , public Ttypes::PfCost
101 , public Ttypes::PfCache
102 , public Ttypes::PfOrigin
103 , public Ttypes::PfDestination
104 , public Ttypes::PfFollow
105{
106};
107
115inline int OctileDistanceCost(TileIndex start_tile, Trackdir start_td, TileIndex destination_tile)
116{
117 static constexpr int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
118 static constexpr int dg_dir_to_y_offs[] = {0, 1, 0, -1};
119
120 const DiagDirection exitdir = TrackdirToExitdir(start_td);
121
122 const int x1 = 2 * TileX(start_tile) + dg_dir_to_x_offs[static_cast<int>(exitdir)];
123 const int y1 = 2 * TileY(start_tile) + dg_dir_to_y_offs[static_cast<int>(exitdir)];
124 const int x2 = 2 * TileX(destination_tile);
125 const int y2 = 2 * TileY(destination_tile);
126 const int dx = abs(x1 - x2);
127 const int dy = abs(y1 - y2);
128 const int dmin = std::min(dx, dy);
129 const int dxy = abs(dx - dy);
130
131 return dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
132}
133
134#endif /* YAPF_COMMON_HPP */
Functions related to bit mathematics.
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()
Access the inherited path finder.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
Types::NodeList::Item Node
this will be our node type
YAPF origin provider base class - used when there are two tile/trackdir origins.
Tpf & Yapf()
Access the inherited path finder.
void SetOrigin(TileIndex forward_tile, Trackdir forward_td, TileIndex reverse_tile=INVALID_TILE, Trackdir reverse_td=INVALID_TRACKDIR, int reverse_penalty=0)
Set origin (tiles, trackdirs, etc.).
Node::Key Key
key to hash tables
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::NodeList::Item Node
this will be our node type
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
DiagDirection
Enumeration for diagonal directions.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
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.
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
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition track_func.h:441
All types related to tracks.
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.