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

OpenTTD Source 15.0-RC2

yapf_node.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_NODE_HPP
11#define YAPF_NODE_HPP
12
13#include "../../track_func.h"
14#include "../../misc/dbg_helpers.h"
15
17 struct CYapfNodeKeyExitDir {
18 TileIndex tile;
19 Trackdir td;
20 DiagDirection exitdir;
21
22 inline void Set(TileIndex tile, Trackdir td)
23 {
24 this->tile = tile;
25 this->td = td;
26 this->exitdir = (this->td == INVALID_TRACKDIR) ? INVALID_DIAGDIR : TrackdirToExitdir(this->td);
27 }
28
29 inline int CalcHash() const
30 {
31 return this->exitdir | (this->tile.base() << 2);
32 }
33
34 inline bool operator==(const CYapfNodeKeyExitDir &other) const
35 {
36 return this->tile == other.tile && this->exitdir == other.exitdir;
37 }
38
39 void Dump(DumpTarget &dmp) const
40 {
41 dmp.WriteTile("tile", this->tile);
42 dmp.WriteEnumT("td", this->td);
43 dmp.WriteEnumT("exitdir", this->exitdir);
44 }
45};
46
47 struct CYapfNodeKeyTrackDir : public CYapfNodeKeyExitDir {
48 inline int CalcHash() const
49 {
50 return this->td | (this->tile.base() << 4);
51 }
52
53 inline bool operator==(const CYapfNodeKeyTrackDir &other) const
54 {
55 return this->tile == other.tile && this->td == other.td;
56 }
57};
58
60template <class Tkey_, class Tnode>
61 struct CYapfNodeT {
62 typedef Tkey_ Key;
63 typedef Tnode Node;
64
65 Tkey_ key;
66 Node *hash_next;
67 Node *parent;
68 int cost;
69 int estimate;
70 bool is_choice;
71
72 inline void Set(Node *parent, TileIndex tile, Trackdir td, bool is_choice)
73 {
74 this->key.Set(tile, td);
75 this->hash_next = nullptr;
76 this->parent = parent;
77 this->cost = 0;
78 this->estimate = 0;
79 this->is_choice = is_choice;
80 }
81
82 inline Node *GetHashNext()
83 {
84 return this->hash_next;
85 }
86
87 inline void SetHashNext(Node *pNext)
88 {
89 this->hash_next = pNext;
90 }
91
92 inline TileIndex GetTile() const
93 {
94 return this->key.tile;
95 }
96
97 inline Trackdir GetTrackdir() const
98 {
99 return this->key.td;
100 }
101
102 inline const Tkey_ &GetKey() const
103 {
104 return this->key;
105 }
106
107 inline int GetCost() const
108 {
109 return this->cost;
110 }
111
112 inline int GetCostEstimate() const
113 {
114 return this->estimate;
115 }
116
117 inline bool GetIsChoice() const
118 {
119 return this->is_choice;
120 }
121
122 inline bool operator<(const Node &other) const
123 {
124 return this->estimate < other.estimate;
125 }
126
127 void Dump(DumpTarget &dmp) const
128 {
129 dmp.WriteStructT("key", &this->key);
130 dmp.WriteStructT("parent", this->parent);
131 dmp.WriteValue("cost", this->cost);
132 dmp.WriteValue("estimate", this->estimate);
133 }
134};
135
136#endif /* YAPF_NODE_HPP */
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition direction_type.h:85
CYapfNodeKeyExitDir
Yapf Node Key that evaluates hash from (and compares) tile & exit dir.
Definition yapf_node.hpp:17
CYapfNodeKeyTrackDir
Definition yapf_node.hpp:47
CYapfNodeT
Yapf Node base.
Definition yapf_node.hpp:61
DumpTarget
Class that represents the dump-into-string target.
Definition dbg_helpers.h:109
DumpTarget::WriteTile
void WriteTile(std::string_view name, TileIndex t)
Write name & TileIndex to the output.
Definition dbg_helpers.cpp:110
DumpTarget::WriteStructT
void WriteStructT(std::string_view name, const S *s)
Dump nested object (or only its name if this instance is already known).
Definition dbg_helpers.h:166
DumpTarget::WriteEnumT
void WriteEnumT(std::string_view name, E e)
Dump given enum value (as a number and as named value)
Definition dbg_helpers.h:157
DumpTarget::WriteValue
void WriteValue(std::string_view name, const auto &value)
Write 'name = value' with indent and new-line.
Definition dbg_helpers.h:148
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
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