OpenTTD Source 20250813-master-g5b5bdd346d
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 <http://www.gnu.org/licenses/>.
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
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
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>
61struct 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
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Yapf Node Key that evaluates hash from (and compares) tile & exit dir.
Definition yapf_node.hpp:17
Yapf Node base.
Definition yapf_node.hpp:61
Class that represents the dump-into-string target.
void WriteTile(std::string_view name, TileIndex t)
Write name & TileIndex to the output.
void WriteStructT(std::string_view name, const S *s)
Dump nested object (or only its name if this instance is already known).
void WriteEnumT(std::string_view name, E e)
Dump given enum value (as a number and as named value)
void WriteValue(std::string_view name, const auto &value)
Write 'name = value' with indent and new-line.
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