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

OpenTTD Source 15.0-beta1

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>
19 class CYapfOriginTileT
20{
21public:
22 typedef typename Types::Tpf Tpf;
23 typedef typename Types::NodeList::Item Node;
24 typedef typename Node::Key Key;
25
26protected:
27 TileIndex origin_tile;
28 TrackdirBits origin_trackdirs;
29
31 inline Tpf &Yapf()
32 {
33 return *static_cast<Tpf *>(this);
34 }
35
36public:
38 void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
39 {
40 this->origin_tile = tile;
41 this->origin_trackdirs = trackdirs;
42 }
43
45 void PfSetStartupNodes()
46 {
47 bool is_choice = (KillFirstBit(this->origin_trackdirs) != TRACKDIR_BIT_NONE);
48 for (TrackdirBits tdb = this->origin_trackdirs; tdb != TRACKDIR_BIT_NONE; tdb = KillFirstBit(tdb)) {
49 Trackdir td = (Trackdir)FindFirstBit(tdb);
50 Node &n1 = Yapf().CreateNewNode();
51 n1.Set(nullptr, this->origin_tile, td, is_choice);
52 Yapf().AddStartupNode(n1);
53 }
54 }
55};
56
58template <class Types>
59 class CYapfOriginTileTwoWayT
60{
61public:
62 typedef typename Types::Tpf Tpf;
63 typedef typename Types::NodeList::Item Node;
64 typedef typename Node::Key Key;
65
66protected:
67 TileIndex origin_tile;
68 Trackdir origin_td;
69 TileIndex reverse_tile;
70 Trackdir reverse_td;
71 int reverse_penalty;
72
74 inline Tpf &Yapf()
75 {
76 return *static_cast<Tpf *>(this);
77 }
78
79public:
81 void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler = INVALID_TILE, Trackdir tdr = INVALID_TRACKDIR, int reverse_penalty = 0)
82 {
83 this->origin_tile = tile;
84 this->origin_td = td;
85 this->reverse_tile = tiler;
86 this->reverse_td = tdr;
87 this->reverse_penalty = reverse_penalty;
88 }
89
91 void PfSetStartupNodes()
92 {
93 if (this->origin_tile != INVALID_TILE && this->origin_td != INVALID_TRACKDIR) {
94 Node &n1 = Yapf().CreateNewNode();
95 n1.Set(nullptr, this->origin_tile, this->origin_td, false);
96 Yapf().AddStartupNode(n1);
97 }
98 if (this->reverse_tile != INVALID_TILE && this->reverse_td != INVALID_TRACKDIR) {
99 Node &n2 = Yapf().CreateNewNode();
100 n2.Set(nullptr, this->reverse_tile, this->reverse_td, false);
101 n2.cost = this->reverse_penalty;
102 Yapf().AddStartupNode(n2);
103 }
104 }
105};
106
113template <class Ttypes>
114 class CYapfT
115 : public Ttypes::PfBase
116 , public Ttypes::PfCost
117 , public Ttypes::PfCache
118 , public Ttypes::PfOrigin
119 , public Ttypes::PfDestination
120 , public Ttypes::PfFollow
121{
122};
123
124
125
126#endif /* YAPF_COMMON_HPP */
FindFirstBit
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Definition bitmath_func.hpp:213
KillFirstBit
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
Definition bitmath_func.hpp:250
CYapfOriginTileT
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
Definition yapf_common.hpp:20
CYapfOriginTileT::Key
Node::Key Key
key to hash tables
Definition yapf_common.hpp:24
CYapfOriginTileT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_common.hpp:31
CYapfOriginTileT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_common.hpp:22
CYapfOriginTileT::origin_trackdirs
TrackdirBits origin_trackdirs
origin trackdir mask
Definition yapf_common.hpp:28
CYapfOriginTileT::SetOrigin
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
Definition yapf_common.hpp:38
CYapfOriginTileT::PfSetStartupNodes
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
Definition yapf_common.hpp:45
CYapfOriginTileT::origin_tile
TileIndex origin_tile
origin tile
Definition yapf_common.hpp:27
CYapfOriginTileT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_common.hpp:23
CYapfOriginTileTwoWayT
YAPF origin provider base class - used when there are two tile/trackdir origins.
Definition yapf_common.hpp:60
CYapfOriginTileTwoWayT::reverse_td
Trackdir reverse_td
second (reverse) origin trackdir
Definition yapf_common.hpp:70
CYapfOriginTileTwoWayT::PfSetStartupNodes
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
Definition yapf_common.hpp:91
CYapfOriginTileTwoWayT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_common.hpp:74
CYapfOriginTileTwoWayT::SetOrigin
void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler=INVALID_TILE, Trackdir tdr=INVALID_TRACKDIR, int reverse_penalty=0)
set origin (tiles, trackdirs, etc.)
Definition yapf_common.hpp:81
CYapfOriginTileTwoWayT::reverse_penalty
int reverse_penalty
penalty to be added for using the reverse origin
Definition yapf_common.hpp:71
CYapfOriginTileTwoWayT::Key
Node::Key Key
key to hash tables
Definition yapf_common.hpp:64
CYapfOriginTileTwoWayT::reverse_tile
TileIndex reverse_tile
second (reverse) origin tile
Definition yapf_common.hpp:69
CYapfOriginTileTwoWayT::origin_td
Trackdir origin_td
first origin trackdir
Definition yapf_common.hpp:68
CYapfOriginTileTwoWayT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_common.hpp:62
CYapfOriginTileTwoWayT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_common.hpp:63
CYapfOriginTileTwoWayT::origin_tile
TileIndex origin_tile
first origin tile
Definition yapf_common.hpp:67
CYapfT
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
Definition yapf_common.hpp:121
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
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:67
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:86
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:98
TRACKDIR_BIT_NONE
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:99