OpenTTD Source 20250924-master-gbec4e71d53
yapf_node_rail.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_RAIL_HPP
11#define YAPF_NODE_RAIL_HPP
12
13#include "../../misc/dbg_helpers.h"
14#include "../../train.h"
15#include "nodelist.hpp"
16#include "yapf_node.hpp"
17#include "yapf_type.hpp"
18
21 uint32_t value;
22
23 inline CYapfRailSegmentKey(const CYapfNodeKeyTrackDir &node_key)
24 {
25 this->Set(node_key);
26 }
27
28 inline void Set(const CYapfRailSegmentKey &src)
29 {
30 this->value = src.value;
31 }
32
33 inline void Set(const CYapfNodeKeyTrackDir &node_key)
34 {
35 this->value = (node_key.tile.base() << 4) | node_key.td;
36 }
37
38 inline int32_t CalcHash() const
39 {
40 return this->value;
41 }
42
43 inline TileIndex GetTile() const
44 {
45 return (TileIndex)(this->value >> 4);
46 }
47
48 inline Trackdir GetTrackdir() const
49 {
50 return (Trackdir)(this->value & 0x0F);
51 }
52
53 inline bool operator==(const CYapfRailSegmentKey &other) const
54 {
55 return this->value == other.value;
56 }
57
58 void Dump(DumpTarget &dmp) const
59 {
60 dmp.WriteTile("tile", this->GetTile());
61 dmp.WriteEnumT("td", this->GetTrackdir());
62 }
63};
64
68
70 TileIndex last_tile = INVALID_TILE;
71 Trackdir last_td = INVALID_TRACKDIR;
72 int cost = -1;
73 TileIndex last_signal_tile = INVALID_TILE;
74 Trackdir last_signal_td = INVALID_TRACKDIR;
75 EndSegmentReasons end_segment_reason{};
76 CYapfRailSegment *hash_next = nullptr;
77
78 inline CYapfRailSegment(const CYapfRailSegmentKey &key) : key(key) {}
79
80 inline const Key &GetKey() const
81 {
82 return this->key;
83 }
84
85 inline TileIndex GetTile() const
86 {
87 return this->key.GetTile();
88 }
89
90 inline CYapfRailSegment *GetHashNext()
91 {
92 return this->hash_next;
93 }
94
95 inline void SetHashNext(CYapfRailSegment *next)
96 {
97 this->hash_next = next;
98 }
99
100 void Dump(DumpTarget &dmp) const
101 {
102 dmp.WriteStructT("key", &this->key);
103 dmp.WriteTile("last_tile", this->last_tile);
104 dmp.WriteEnumT("last_td", this->last_td);
105 dmp.WriteValue("cost", this->cost);
106 dmp.WriteTile("last_signal_tile", this->last_signal_tile);
107 dmp.WriteEnumT("last_signal_td", this->last_signal_td);
108 dmp.WriteEnumT("end_segment_reason", this->end_segment_reason);
109 }
110};
111
113struct CYapfRailNode : CYapfNodeT<CYapfNodeKeyTrackDir, CYapfRailNode> {
116
117 CYapfRailSegment *segment;
118 uint16_t num_signals_passed;
119 union {
120 uint32_t inherited_flags;
121 struct {
122 bool target_seen;
123 bool choice_seen;
124 bool last_signal_was_red;
125 } flags_s;
126 } flags_u;
127 SignalType last_red_signal_type;
128 SignalType last_signal_type;
129
130 inline void Set(CYapfRailNode *parent, TileIndex tile, Trackdir td, bool is_choice)
131 {
132 this->base::Set(parent, tile, td, is_choice);
133 this->segment = nullptr;
134 if (parent == nullptr) {
135 this->num_signals_passed = 0;
136 this->flags_u.inherited_flags = 0;
137 this->last_red_signal_type = SIGTYPE_BLOCK;
138 /* We use PBS as initial signal type because if we are in
139 * a PBS section and need to route, i.e. we're at a safe
140 * waiting point of a station, we need to account for the
141 * reservation costs. If we are in a normal block then we
142 * should be alone in there and as such the reservation
143 * costs should be 0 anyway. If there would be another
144 * train in the block, i.e. passing signals at danger
145 * then avoiding that train with help of the reservation
146 * costs is not a bad thing, actually it would probably
147 * be a good thing to do. */
148 this->last_signal_type = SIGTYPE_PBS;
149 } else {
150 this->num_signals_passed = parent->num_signals_passed;
151 this->flags_u.inherited_flags = parent->flags_u.inherited_flags;
152 this->last_red_signal_type = parent->last_red_signal_type;
153 this->last_signal_type = parent->last_signal_type;
154 }
155 this->flags_u.flags_s.choice_seen |= is_choice;
156 }
157
158 inline TileIndex GetLastTile() const
159 {
160 assert(this->segment != nullptr);
161 return this->segment->last_tile;
162 }
163
164 inline Trackdir GetLastTrackdir() const
165 {
166 assert(this->segment != nullptr);
167 return this->segment->last_td;
168 }
169
170 inline void SetLastTileTrackdir(TileIndex tile, Trackdir td)
171 {
172 assert(this->segment != nullptr);
173 this->segment->last_tile = tile;
174 this->segment->last_td = td;
175 }
176
177 template <class Tbase, class Tfunc, class Tpf>
178 bool IterateTiles(const Train *v, Tpf &yapf, Tbase &obj, bool (Tfunc::*func)(TileIndex, Trackdir)) const
179 {
180 typename Tbase::TrackFollower ft(v, yapf.GetCompatibleRailTypes());
181 TileIndex cur = this->base::GetTile();
182 Trackdir cur_td = this->base::GetTrackdir();
183
184 while (cur != this->GetLastTile() || cur_td != this->GetLastTrackdir()) {
185 if (!((obj.*func)(cur, cur_td))) return false;
186
187 if (!ft.Follow(cur, cur_td)) break;
188 cur = ft.new_tile;
189 assert(KillFirstBit(ft.new_td_bits) == TRACKDIR_BIT_NONE);
190 cur_td = FindFirstTrackdir(ft.new_td_bits);
191 }
192
193 return (obj.*func)(cur, cur_td);
194 }
195
196 void Dump(DumpTarget &dmp) const
197 {
198 this->base::Dump(dmp);
199 dmp.WriteStructT("segment", this->segment);
200 dmp.WriteValue("num_signals_passed", this->num_signals_passed);
201 dmp.WriteValue("target_seen", this->flags_u.flags_s.target_seen ? "Yes" : "No");
202 dmp.WriteValue("choice_seen", this->flags_u.flags_s.choice_seen ? "Yes" : "No");
203 dmp.WriteValue("last_signal_was_red", this->flags_u.flags_s.last_signal_was_red ? "Yes" : "No");
204 dmp.WriteEnumT("last_red_signal_type", this->last_red_signal_type);
205 }
206};
207
209
210#endif /* YAPF_NODE_RAIL_HPP */
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
Hash table based node list multi-container class.
Definition nodelist.hpp:21
List of nodes used for the A-star pathfinder.
SignalType
Type of signal, i.e.
Definition signal_type.h:23
@ SIGTYPE_PBS
normal pbs signal
Definition signal_type.h:28
@ SIGTYPE_BLOCK
block signal
Definition signal_type.h:24
Yapf Node base.
Definition yapf_node.hpp:61
Yapf Node for rail YAPF.
key for cached segment cost for rail YAPF
cached segment cost for rail YAPF
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.
'Train' is either a loco or a wagon.
Definition train.h:91
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition track_func.h:211
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
Node in the pathfinder's graph.
Types used by YAPF.