Skip to content

Commit

Permalink
Codechange: Move two way signal EOL to a more logical place
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuhnovic committed Dec 6, 2024
1 parent ca14802 commit 701cb2e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
10 changes: 1 addition & 9 deletions src/pathfinder/yapf/yapf_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class CYapfOriginTileTwoWayT
TileIndex reverse_tile; ///< second (reverse) origin tile
Trackdir reverse_td; ///< second (reverse) origin trackdir
int reverse_penalty; ///< penalty to be added for using the reverse origin
bool treat_first_red_two_way_signal_as_eol; ///< in some cases (leaving station) we need to handle first two-way signal differently

/** to access inherited path finder */
inline Tpf &Yapf()
Expand All @@ -79,14 +78,13 @@ class CYapfOriginTileTwoWayT

public:
/** set origin (tiles, trackdirs, etc.) */
void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler = INVALID_TILE, Trackdir tdr = INVALID_TRACKDIR, int reverse_penalty = 0, bool treat_first_red_two_way_signal_as_eol = true)
void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler = INVALID_TILE, Trackdir tdr = INVALID_TRACKDIR, int reverse_penalty = 0)
{
this->origin_tile = tile;
this->origin_td = td;
this->reverse_tile = tiler;
this->reverse_td = tdr;
this->reverse_penalty = reverse_penalty;
this->treat_first_red_two_way_signal_as_eol = treat_first_red_two_way_signal_as_eol;
}

/** Called when YAPF needs to place origin nodes into open list */
Expand All @@ -104,12 +102,6 @@ class CYapfOriginTileTwoWayT
Yapf().AddStartupNode(n2);
}
}

/** return true if first two-way signal should be treated as dead end */
inline bool TreatFirstRedTwoWaySignalAsEOL()
{
return Yapf().PfGetSettings().rail_firstred_twoway_eol && this->treat_first_red_two_way_signal_as_eol;
}
};

/**
Expand Down
23 changes: 18 additions & 5 deletions src/pathfinder/yapf/yapf_costrail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ class CYapfCostRailT : public CYapfCostBase {
* @note maximum cost doesn't work with caching enabled
* @todo fix maximum cost failing with caching (e.g. FS#2900)
*/
int max_cost;
bool disable_cache;
std::vector<int> sig_look_ahead_costs;
int max_cost = 0;
bool disable_cache = false;
std::vector<int> sig_look_ahead_costs = {};
bool treat_first_red_two_way_signal_as_eol = false;

public:
bool stopped_on_first_two_way_signal;
bool stopped_on_first_two_way_signal = false;

protected:
static constexpr int MAX_SEGMENT_COST = 10000;

CYapfCostRailT() : max_cost(0), disable_cache(false), stopped_on_first_two_way_signal(false)
CYapfCostRailT()
{
/* pre-compute look-ahead penalties into array */
int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0;
Expand All @@ -75,6 +76,18 @@ class CYapfCostRailT : public CYapfCostBase {
}

public:
/** Sets whether the first two-way signal should be treated as a dead end */
void SetTreatFirstRedTwoWaySignalAsEOL(bool enabled)
{
this->treat_first_red_two_way_signal_as_eol = enabled;
}

/** Returns whether the first two-way signal should be treated as a dead end */
inline bool TreatFirstRedTwoWaySignalAsEOL()
{
return Yapf().PfGetSettings().rail_firstred_twoway_eol && this->treat_first_red_two_way_signal_as_eol;
}

inline int SlopeCost(TileIndex tile, Trackdir td)
{
if (!stSlopeCost(tile, td)) return 0;
Expand Down
10 changes: 7 additions & 3 deletions src/pathfinder/yapf/yapf_rail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ class CYapfFollowAnyDepotRailT
inline FindDepotData FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
{
/* set origin and destination nodes */
Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty);
Yapf().SetTreatFirstRedTwoWaySignalAsEOL(true);
Yapf().SetDestination(v);
Yapf().SetMaxCost(max_penalty);

Expand Down Expand Up @@ -351,6 +352,7 @@ class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
{
/* Set origin and destination. */
Yapf().SetOrigin(t1, td);
Yapf().SetTreatFirstRedTwoWaySignalAsEOL(false);
Yapf().SetDestination(v, override_railtype);

if (!Yapf().FindPath(v)) return false;
Expand Down Expand Up @@ -437,7 +439,8 @@ class CYapfFollowRailT : public CYapfReserveTrack<Types>

/* set origin and destination nodes */
PBSTileInfo origin = FollowTrainReservation(v);
Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1);
Yapf().SetTreatFirstRedTwoWaySignalAsEOL(true);
Yapf().SetDestination(v);

/* find the best path */
Expand Down Expand Up @@ -501,7 +504,8 @@ class CYapfFollowRailT : public CYapfReserveTrack<Types>
{
/* create pathfinder instance
* set origin and destination nodes */
Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty);
Yapf().SetTreatFirstRedTwoWaySignalAsEOL(false);
Yapf().SetDestination(v);

/* find the best path */
Expand Down

0 comments on commit 701cb2e

Please sign in to comment.