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

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
20 struct CYapfRailSegmentKey {
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
66 struct CYapfRailSegment {
67 typedef CYapfRailSegmentKey Key;
68
69 CYapfRailSegmentKey key;
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
113 struct CYapfRailNode : CYapfNodeT<CYapfNodeKeyTrackDir, CYapfRailNode> {
114 typedef CYapfNodeT<CYapfNodeKeyTrackDir, CYapfRailNode> base;
115 typedef CYapfRailSegment CachedData;
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 follower{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 (!follower.Follow(cur, cur_td)) break;
188 cur = follower.new_tile;
189 assert(KillFirstBit(follower.new_td_bits) == TRACKDIR_BIT_NONE);
190 cur_td = FindFirstTrackdir(follower.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
208typedef NodeList<CYapfRailNode, 8, 10> CRailNodeList;
209
210#endif /* YAPF_NODE_RAIL_HPP */
KillFirstBit
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
Definition bitmath_func.hpp:226
EnumBitSet< EndSegmentReason, uint16_t >
NodeList
Hash table based node list multi-container class.
Definition nodelist.hpp:21
nodelist.hpp
List of nodes used for the A-star pathfinder.
SignalType
SignalType
Type of signal, i.e.
Definition signal_type.h:23
SIGTYPE_PBS
@ SIGTYPE_PBS
normal pbs signal
Definition signal_type.h:28
SIGTYPE_BLOCK
@ SIGTYPE_BLOCK
block signal
Definition signal_type.h:24
CYapfNodeKeyTrackDir
Definition yapf_node.hpp:47
CYapfNodeT
Yapf Node base.
Definition yapf_node.hpp:61
CYapfRailNode
Yapf Node for rail YAPF.
Definition yapf_node_rail.hpp:113
CYapfRailSegmentKey
key for cached segment cost for rail YAPF
Definition yapf_node_rail.hpp:20
CYapfRailSegment
cached segment cost for rail YAPF
Definition yapf_node_rail.hpp:66
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 > >
Train
'Train' is either a loco or a wagon.
Definition train.h:91
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
FindFirstTrackdir
Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition track_func.h:211
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
TRACKDIR_BIT_NONE
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
yapf_node.hpp
Node in the pathfinder's graph.
yapf_type.hpp
Types used by YAPF.