OpenTTD Source: src/pathfinder/yapf/yapf_common.hpp Source File

OpenTTD Source 15.0-RC2

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
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>
19 class CYapfOriginTileT {
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:
34 void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
35 {
36 bool is_choice = (KillFirstBit(trackdirs) != TRACKDIR_BIT_NONE);
37 for (TrackdirBits tdb = trackdirs; tdb != TRACKDIR_BIT_NONE; tdb = KillFirstBit(tdb)) {
38 Trackdir td = (Trackdir)FindFirstBit(tdb);
39 Node &node = Yapf().CreateNewNode();
40 node.Set(nullptr, tile, td, is_choice);
41 Yapf().AddStartupNode(node);
42 }
43 }
44};
45
47template <class Types>
48 class CYapfOriginTileTwoWayT {
49public:
50 typedef typename Types::Tpf Tpf;
51 typedef typename Types::NodeList::Item Node;
52 typedef typename Node::Key Key;
53
54protected:
56 inline Tpf &Yapf()
57 {
58 return *static_cast<Tpf *>(this);
59 }
60
61public:
63 void SetOrigin(TileIndex forward_tile, Trackdir forward_td, TileIndex reverse_tile = INVALID_TILE,
64 Trackdir reverse_td = INVALID_TRACKDIR, int reverse_penalty = 0)
65 {
66 if (forward_tile != INVALID_TILE && forward_td != INVALID_TRACKDIR) {
67 Node &node = Yapf().CreateNewNode();
68 node.Set(nullptr, forward_tile, forward_td, false);
69 Yapf().AddStartupNode(node);
70 }
71 if (reverse_tile != INVALID_TILE && reverse_td != INVALID_TRACKDIR) {
72 Node &node = Yapf().CreateNewNode();
73 node.Set(nullptr, reverse_tile, reverse_td, false);
74 node.cost = reverse_penalty;
75 Yapf().AddStartupNode(node);
76 }
77 }
78};
79
86template <class Ttypes>
87 class CYapfT
88 : public Ttypes::PfBase
89 , public Ttypes::PfCost
90 , public Ttypes::PfCache
91 , public Ttypes::PfOrigin
92 , public Ttypes::PfDestination
93 , public Ttypes::PfFollow
94{
95};
96
104 inline int OctileDistanceCost(TileIndex start_tile, Trackdir start_td, TileIndex destination_tile)
105{
106 static constexpr int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
107 static constexpr int dg_dir_to_y_offs[] = {0, 1, 0, -1};
108
109 const DiagDirection exitdir = TrackdirToExitdir(start_td);
110
111 const int x1 = 2 * TileX(start_tile) + dg_dir_to_x_offs[static_cast<int>(exitdir)];
112 const int y1 = 2 * TileY(start_tile) + dg_dir_to_y_offs[static_cast<int>(exitdir)];
113 const int x2 = 2 * TileX(destination_tile);
114 const int y2 = 2 * TileY(destination_tile);
115 const int dx = abs(x1 - x2);
116 const int dy = abs(y1 - y2);
117 const int dmin = std::min(dx, dy);
118 const int dxy = abs(dx - dy);
119
120 return dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
121}
122
123#endif /* YAPF_COMMON_HPP */
FindFirstBit
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Definition bitmath_func.hpp:189
KillFirstBit
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
Definition bitmath_func.hpp:226
CYapfOriginTileT
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
Definition yapf_common.hpp:19
CYapfOriginTileT::Key
Node::Key Key
key to hash tables
Definition yapf_common.hpp:23
CYapfOriginTileT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_common.hpp:27
CYapfOriginTileT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_common.hpp:21
CYapfOriginTileT::SetOrigin
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
Definition yapf_common.hpp:34
CYapfOriginTileT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_common.hpp:22
CYapfOriginTileTwoWayT
YAPF origin provider base class - used when there are two tile/trackdir origins.
Definition yapf_common.hpp:48
CYapfOriginTileTwoWayT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_common.hpp:56
CYapfOriginTileTwoWayT::SetOrigin
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.)
Definition yapf_common.hpp:63
CYapfOriginTileTwoWayT::Key
Node::Key Key
key to hash tables
Definition yapf_common.hpp:52
CYapfOriginTileTwoWayT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_common.hpp:50
CYapfOriginTileTwoWayT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_common.hpp:51
CYapfT
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
Definition yapf_common.hpp:94
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
YAPF_TILE_CORNER_LENGTH
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
Definition pathfinder_type.h:19
YAPF_TILE_LENGTH
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Definition pathfinder_type.h:16
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
TrackdirToExitdir
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
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
TRACKDIR_BIT_NONE
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
OctileDistanceCost
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.
Definition yapf_common.hpp:104