mirror of https://github.com/OpenTTD/OpenTTD
(svn r10301) -Fix [FS#901, YAPF]: another assert violation in some special cases (immeR)
parent
c600158c1d
commit
ef4c335cb4
|
@ -252,7 +252,7 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
|
/** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
|
||||||
int GetSpeedLimit(int *pmin_speed = NULL)
|
int GetSpeedLimit(int *pmin_speed = NULL) const
|
||||||
{
|
{
|
||||||
int min_speed = 0;
|
int min_speed = 0;
|
||||||
int max_speed = INT_MAX; // no limit
|
int max_speed = INT_MAX; // no limit
|
||||||
|
|
|
@ -213,7 +213,7 @@ public:
|
||||||
m_stats_cache_hits++;
|
m_stats_cache_hits++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bValid = Yapf().PfCalcCost(n, tf);
|
bool bValid = Yapf().PfCalcCost(n, &tf);
|
||||||
|
|
||||||
if (bCached) {
|
if (bCached) {
|
||||||
Yapf().PfNodeCacheFlush(n);
|
Yapf().PfNodeCacheFlush(n);
|
||||||
|
|
|
@ -18,9 +18,44 @@ public:
|
||||||
typedef typename Node::Key Key; ///< key to hash tables
|
typedef typename Node::Key Key; ///< key to hash tables
|
||||||
typedef typename Node::CachedData CachedData;
|
typedef typename Node::CachedData CachedData;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
/* Structure used inside PfCalcCost() to keep basic tile information. */
|
||||||
|
struct TILE {
|
||||||
|
TileIndex tile;
|
||||||
|
Trackdir td;
|
||||||
|
TileType tile_type;
|
||||||
|
RailType rail_type;
|
||||||
|
|
||||||
|
TILE()
|
||||||
|
{
|
||||||
|
tile = INVALID_TILE;
|
||||||
|
td = INVALID_TRACKDIR;
|
||||||
|
tile_type = MP_VOID;
|
||||||
|
rail_type = INVALID_RAILTYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
TILE(TileIndex tile, Trackdir td)
|
||||||
|
{
|
||||||
|
this->tile = tile;
|
||||||
|
this->td = td;
|
||||||
|
this->tile_type = GetTileType(tile);
|
||||||
|
this->rail_type = GetTileRailType(tile);
|
||||||
|
}
|
||||||
|
|
||||||
|
TILE(const TILE &src)
|
||||||
|
{
|
||||||
|
tile = src.tile;
|
||||||
|
td = src.td;
|
||||||
|
tile_type = src.tile_type;
|
||||||
|
rail_type = src.rail_type;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int m_max_cost;
|
int m_max_cost;
|
||||||
CBlobT<int> m_sig_look_ahead_costs;
|
CBlobT<int> m_sig_look_ahead_costs;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool m_stopped_on_first_two_way_signal;
|
bool m_stopped_on_first_two_way_signal;
|
||||||
protected:
|
protected:
|
||||||
|
@ -65,8 +100,8 @@ public:
|
||||||
return cost;
|
return cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** return one tile cost. If tile is a tunnel entry, it is moved to the end of tunnel */
|
/** Return one tile cost (base cost + level crossing penalty). */
|
||||||
FORCEINLINE int OneTileCost(TileIndex prev_tile, TileIndex& tile, Trackdir trackdir)
|
FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
|
||||||
{
|
{
|
||||||
int cost = 0;
|
int cost = 0;
|
||||||
// set base cost
|
// set base cost
|
||||||
|
@ -99,7 +134,7 @@ public:
|
||||||
bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
|
bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
|
||||||
if (has_signal_against && !has_signal_along) {
|
if (has_signal_against && !has_signal_along) {
|
||||||
// one-way signal in opposite direction
|
// one-way signal in opposite direction
|
||||||
n.m_segment->flags_u.flags_s.m_end_of_line = true;
|
n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
|
||||||
} else if (has_signal_along) {
|
} else if (has_signal_along) {
|
||||||
SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
|
SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
|
||||||
// cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
|
// cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
|
||||||
|
@ -117,7 +152,7 @@ public:
|
||||||
// was it first signal which is two-way?
|
// was it first signal which is two-way?
|
||||||
if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
|
if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
|
||||||
// yes, the first signal is two-way red signal => DEAD END
|
// yes, the first signal is two-way red signal => DEAD END
|
||||||
n.m_segment->flags_u.flags_s.m_end_of_line = true;
|
n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
|
||||||
Yapf().m_stopped_on_first_two_way_signal = true;
|
Yapf().m_stopped_on_first_two_way_signal = true;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -170,182 +205,241 @@ public:
|
||||||
public:
|
public:
|
||||||
FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
|
FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Called by YAPF to calculate the cost from the origin to the given node.
|
/** Called by YAPF to calculate the cost from the origin to the given node.
|
||||||
* Calculates only the cost of given node, adds it to the parent node cost
|
* Calculates only the cost of given node, adds it to the parent node cost
|
||||||
* and stores the result into Node::m_cost member */
|
* and stores the result into Node::m_cost member */
|
||||||
FORCEINLINE bool PfCalcCost(Node &n, const TrackFollower &tf)
|
FORCEINLINE bool PfCalcCost(Node &n, const TrackFollower *tf)
|
||||||
{
|
{
|
||||||
assert(!n.flags_u.flags_s.m_targed_seen);
|
assert(!n.flags_u.flags_s.m_targed_seen);
|
||||||
|
assert(tf->m_new_tile == n.m_key.m_tile);
|
||||||
|
assert((TrackdirToTrackdirBits(n.m_key.m_td) & tf->m_new_td_bits) != TRACKDIR_BIT_NONE);
|
||||||
|
|
||||||
CPerfStart perf_cost(Yapf().m_perf_cost);
|
CPerfStart perf_cost(Yapf().m_perf_cost);
|
||||||
int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
|
|
||||||
int first_tile_cost = 0;
|
/* Does the node have some parent node? */
|
||||||
int segment_cost = 0;
|
bool has_parent = (n.m_parent != NULL);
|
||||||
|
|
||||||
|
/* Do we already have a cached segment? */
|
||||||
|
CachedData &segment = *n.m_segment;
|
||||||
|
bool is_cached_segment = (segment.m_cost >= 0);
|
||||||
|
|
||||||
|
int parent_cost = has_parent ? n.m_parent->m_cost : 0;
|
||||||
|
|
||||||
|
/* Each node cost contains 2 or 3 main components:
|
||||||
|
* 1. Transition cost - cost of the move from previous node (tile):
|
||||||
|
* - curve cost (or zero for straight move)
|
||||||
|
* 2. Tile cost:
|
||||||
|
* - base tile cost
|
||||||
|
* - YAPF_TILE_LENGTH for diagonal tiles
|
||||||
|
* - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles
|
||||||
|
* - tile penalties
|
||||||
|
* - tile slope penalty (upward slopes)
|
||||||
|
* - red signal penalty
|
||||||
|
* - level crossing penalty
|
||||||
|
* - speed-limit penalty (bridges)
|
||||||
|
* - station platform penalty
|
||||||
|
* - penalty for reversing in the depot
|
||||||
|
* - etc.
|
||||||
|
* 3. Extra cost (applies to the last node only)
|
||||||
|
* - last red signal penalty
|
||||||
|
* - penalty for too long or too short platform on the destination station
|
||||||
|
*/
|
||||||
|
int transition_cost = 0;
|
||||||
|
int tile_cost = 0;
|
||||||
int extra_cost = 0;
|
int extra_cost = 0;
|
||||||
|
|
||||||
|
/* Segment: one or more tiles connected by contiguous tracks of the same type.
|
||||||
|
* Each segment cost includes 'Tile cost' for all its tiles (including the first
|
||||||
|
* and last), and the 'Transition cost' between its tiles. The first transition
|
||||||
|
* cost of segment entry (move from the 'parent' node) is not included!
|
||||||
|
*/
|
||||||
|
int segment_entry_cost = 0;
|
||||||
|
int segment_cost = 0;
|
||||||
|
|
||||||
const Vehicle* v = Yapf().GetVehicle();
|
const Vehicle* v = Yapf().GetVehicle();
|
||||||
|
|
||||||
// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
|
// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
|
||||||
TileIndex prev_tile = (n.m_parent != NULL) ? n.m_parent->GetLastTile() : INVALID_TILE;
|
TILE cur(n.m_key.m_tile, n.m_key.m_td);
|
||||||
Trackdir prev_trackdir = (n.m_parent != NULL) ? n.m_parent->GetLastTrackdir() : INVALID_TRACKDIR;
|
|
||||||
TileType prev_tile_type = (n.m_parent != NULL) ? GetTileType(n.m_parent->GetLastTile()) : MP_VOID;
|
|
||||||
|
|
||||||
TileIndex tile = n.m_key.m_tile;
|
// the previous tile will be needed for transition cost calculations
|
||||||
Trackdir trackdir = n.m_key.m_td;
|
TILE prev = has_parent ? TILE() : TILE(n.m_parent->GetLastTile(), n.m_parent->GetLastTrackdir());
|
||||||
TileType tile_type = GetTileType(tile);
|
|
||||||
|
|
||||||
RailType rail_type = GetTileRailType(tile);
|
EndSegmentReasonBits end_segment_reason = ESRB_NONE;
|
||||||
|
|
||||||
bool target_seen = Yapf().PfDetectDestination(tile, trackdir);
|
TrackFollower tf_local(v, &Yapf().m_perf_ts_cost);
|
||||||
bool end_by_target_seen = false;
|
|
||||||
|
|
||||||
if (tf.m_is_station) {
|
if (!has_parent) {
|
||||||
// station tiles have an extra penalty
|
/* We will jump to the middle of the cost calculator assuming that segment cache is not used. */
|
||||||
segment_cost += Yapf().PfGetSettings().rail_station_penalty * (tf.m_tiles_skipped + 1);
|
assert(!is_cached_segment);
|
||||||
|
/* Skip the first transition cost calculation. */
|
||||||
|
goto no_entry_cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
for (;;) {
|
||||||
segment_cost += Yapf().OneTileCost(prev_tile, tile, trackdir);
|
/* Transition cost (cost of the move from previous tile) */
|
||||||
segment_cost += Yapf().CurveCost(prev_trackdir, trackdir);
|
transition_cost = Yapf().CurveCost(prev.td, cur.td);
|
||||||
segment_cost += Yapf().SlopeCost(tile, trackdir);
|
|
||||||
segment_cost += Yapf().SignalCost(n, tile, trackdir);
|
|
||||||
if (n.m_segment->flags_u.flags_s.m_end_of_line) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// finish if we have reached the destination
|
/* First transition cost counts against segment entry cost, other transitions
|
||||||
if (target_seen) {
|
* inside segment will come to segment cost (and will be cached) */
|
||||||
end_by_target_seen = true;
|
if (segment_cost == 0) {
|
||||||
break;
|
/* We just entered the loop. First transition cost goes to segment entry cost)*/
|
||||||
}
|
segment_entry_cost = transition_cost;
|
||||||
|
transition_cost = 0;
|
||||||
|
|
||||||
// finish on first station tile - segment should end here to avoid target skipping
|
/* It is the right time now to look if we can reuse the cached segment cost. */
|
||||||
// when cached segments are used
|
if (is_cached_segment) {
|
||||||
if (tile_type == MP_STATION && prev_tile_type != MP_STATION) {
|
/* Yes, we already know the segment cost. */
|
||||||
break;
|
segment_cost = segment.m_cost;
|
||||||
}
|
/* We know also the reason why the segment ends. */
|
||||||
|
end_segment_reason = segment.m_end_segment_reason;
|
||||||
// finish also on waypoint - same workaround as for first station tile
|
/* No further calculation needed. */
|
||||||
if (tile_type == MP_RAILWAY && IsRailWaypoint(tile)) {
|
cur = TILE(n.GetLastTile(), n.GetLastTrackdir());
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there are no reachable trackdirs on the next tile, we have end of road
|
|
||||||
TrackFollower F(v, &Yapf().m_perf_ts_cost);
|
|
||||||
if (!F.Follow(tile, trackdir)) {
|
|
||||||
// we can't continue?
|
|
||||||
// n.m_segment->flags_u.flags_s.m_end_of_line = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there are more trackdirs available & reachable, we are at the end of segment
|
|
||||||
if (KillFirstBit2x64(F.m_new_td_bits) != 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Trackdir new_td = (Trackdir)FindFirstBit2x64(F.m_new_td_bits);
|
|
||||||
|
|
||||||
{
|
|
||||||
// end segment if train is about to enter simple loop with no junctions
|
|
||||||
// so next time it should stop on the next if
|
|
||||||
if (segment_cost > s_max_segment_cost && IsTileType(F.m_new_tile, MP_RAILWAY))
|
|
||||||
break;
|
|
||||||
|
|
||||||
// stop if train is on simple loop with no junctions
|
|
||||||
if (F.m_new_tile == n.m_key.m_tile && new_td == n.m_key.m_td)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if tail type changes, finish segment (cached segment can't contain more rail types)
|
|
||||||
{
|
|
||||||
RailType new_rail_type = GetTileRailType(F.m_new_tile);
|
|
||||||
if (new_rail_type != rail_type) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
rail_type = new_rail_type;
|
} else {
|
||||||
|
/* Other than first transition cost count as the regular segment cost. */
|
||||||
|
segment_cost += transition_cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
// move to the next tile
|
no_entry_cost: // jump here at the beginning if the node has no parent (it is the first node)
|
||||||
prev_tile = tile;
|
|
||||||
prev_trackdir = trackdir;
|
|
||||||
prev_tile_type = tile_type;
|
|
||||||
|
|
||||||
tile = F.m_new_tile;
|
/* All other tile costs will be calculated here. */
|
||||||
trackdir = new_td;
|
segment_cost += Yapf().OneTileCost(cur.tile, cur.td);
|
||||||
tile_type = GetTileType(tile);
|
|
||||||
|
|
||||||
target_seen = Yapf().PfDetectDestination(tile, trackdir);
|
/* If we skipped some tunnel/bridge/station tiles, add their base cost */
|
||||||
|
segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
|
||||||
|
|
||||||
// reversing in depot penalty
|
/* Slope cost. */
|
||||||
if (tile == prev_tile) {
|
segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
|
||||||
|
|
||||||
|
/* Signal cost (routine can modify segment data). */
|
||||||
|
segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
|
||||||
|
end_segment_reason = segment.m_end_segment_reason;
|
||||||
|
|
||||||
|
/* Tests for 'potential target' reasons to close the segment. */
|
||||||
|
if (cur.tile == prev.tile) {
|
||||||
|
/* Penalty for reversing in a depot. */
|
||||||
|
assert(IsRailDepot(cur.tile));
|
||||||
segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
|
segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
|
||||||
|
/* We will end in this pass (depot is possible target) */
|
||||||
|
end_segment_reason |= ESRB_DEPOT;
|
||||||
|
|
||||||
|
} else if (tf->m_is_station) {
|
||||||
|
/* Station penalties. */
|
||||||
|
uint platform_length = tf->m_tiles_skipped + 1;
|
||||||
|
/* We don't know yet if the station is our target or not. Act like
|
||||||
|
* if it is pass-through station (not our destination). */
|
||||||
|
segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
|
||||||
|
/* We will end in this pass (station is possible target) */
|
||||||
|
end_segment_reason |= ESRB_STATION;
|
||||||
|
|
||||||
|
} else if (cur.tile_type == MP_RAILWAY && IsRailWaypoint(cur.tile)) {
|
||||||
|
/* Waypoint is also a good reason to finish. */
|
||||||
|
end_segment_reason |= ESRB_WAYPOINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise
|
||||||
|
* it would cause desync in MP. */
|
||||||
|
if (n.m_num_signals_passed < m_sig_look_ahead_costs.Size())
|
||||||
|
{
|
||||||
|
int min_speed = 0;
|
||||||
|
int max_speed = tf->GetSpeedLimit(&min_speed);
|
||||||
|
if (max_speed < v->max_speed)
|
||||||
|
extra_cost += YAPF_TILE_LENGTH * (v->max_speed - max_speed) * (4 + tf->m_tiles_skipped) / v->max_speed;
|
||||||
|
if (min_speed > v->max_speed)
|
||||||
|
extra_cost += YAPF_TILE_LENGTH * (min_speed - v->max_speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Finish if we already exceeded the maximum path cost (i.e. when
|
||||||
|
* searching for the nearest depot). */
|
||||||
|
if (m_max_cost > 0 && (parent_cost + segment_entry_cost + segment_cost) > m_max_cost) {
|
||||||
|
end_segment_reason |= ESRB_PATH_TOO_LONG;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Move to the next tile/trackdir. */
|
||||||
|
tf = &tf_local;
|
||||||
|
tf_local.Init(v, &Yapf().m_perf_ts_cost);
|
||||||
|
|
||||||
|
if (!tf_local.Follow(cur.tile, cur.td)) {
|
||||||
|
/* Can't move to the next tile (EOL?). */
|
||||||
|
end_segment_reason |= ESRB_DEAD_END;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we skipped some tunnel tiles, add their cost
|
/* Check if the next tile is not a choice. */
|
||||||
segment_cost += YAPF_TILE_LENGTH * F.m_tiles_skipped;
|
if (KillFirstBit2x64(tf_local.m_new_td_bits) != 0) {
|
||||||
|
/* More than one segment will follow. Close this one. */
|
||||||
// add penalty for skipped station tiles
|
end_segment_reason |= ESRB_CHOICE_FOLLOWS;
|
||||||
if (F.m_is_station)
|
break;
|
||||||
{
|
|
||||||
uint platform_length = F.m_tiles_skipped + 1;
|
|
||||||
if (target_seen) {
|
|
||||||
// it is our destination station
|
|
||||||
segment_cost += PlatformLengthPenalty(platform_length);
|
|
||||||
} else {
|
|
||||||
// station is not our destination station, apply penalty for skipped platform tiles
|
|
||||||
segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add min/max speed penalties
|
/* Gather the next tile/trackdir/tile_type/rail_type. */
|
||||||
int min_speed = 0;
|
TILE next(tf_local.m_new_tile, (Trackdir)FindFirstBit2x64(tf_local.m_new_td_bits));
|
||||||
int max_speed = F.GetSpeedLimit(&min_speed);
|
|
||||||
if (max_speed < v->max_speed)
|
|
||||||
segment_cost += YAPF_TILE_LENGTH * (v->max_speed - max_speed) / v->max_speed;
|
|
||||||
if (min_speed > v->max_speed)
|
|
||||||
segment_cost += YAPF_TILE_LENGTH * (min_speed - v->max_speed);
|
|
||||||
|
|
||||||
// finish if we already exceeded the maximum cost
|
/* Check the next tile for the rail type. */
|
||||||
if (m_max_cost > 0 && (parent_cost + first_tile_cost + segment_cost) > m_max_cost) {
|
if (next.rail_type != cur.rail_type) {
|
||||||
return false;
|
/* Segment must consist from the same rail_type tiles. */
|
||||||
|
end_segment_reason |= ESRB_RAIL_TYPE;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (first_tile_cost == 0) {
|
/* Avoid infinite looping. */
|
||||||
// we just have done first tile
|
if (next.tile == n.m_key.m_tile && next.td == n.m_key.m_td) {
|
||||||
first_tile_cost = segment_cost;
|
end_segment_reason |= ESRB_INFINITE_LOOP;
|
||||||
segment_cost = 0;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// look if we can reuse existing (cached) segment cost
|
if (segment_cost > s_max_segment_cost) {
|
||||||
if (n.m_segment->m_cost >= 0) {
|
/* Potentially in the infinite loop (or only very long segment?). We should
|
||||||
// reuse the cached segment cost
|
* not force it to finish prematurely unless we are on a regular tile. */
|
||||||
|
if (IsTileType(tf->m_new_tile, MP_RAILWAY)) {
|
||||||
|
end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// segment cost was not filled yes, we have not cached it yet
|
|
||||||
n.SetLastTileTrackdir(tile, trackdir);
|
|
||||||
|
|
||||||
} // while (true)
|
/* Any other reason bit set? */
|
||||||
|
if (end_segment_reason != ESRB_NONE) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (first_tile_cost == 0) {
|
/* For the next loop set new prev and cur tile info. */
|
||||||
// we have just finished first tile
|
prev = cur;
|
||||||
first_tile_cost = segment_cost;
|
cur = next;
|
||||||
segment_cost = 0;
|
|
||||||
|
} // for (;;)
|
||||||
|
|
||||||
|
bool target_seen = false;
|
||||||
|
if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) {
|
||||||
|
/* Depot, station or waypoint. */
|
||||||
|
if (Yapf().PfDetectDestination(cur.tile, cur.td)) {
|
||||||
|
/* Destination found. */
|
||||||
|
target_seen = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// do we have cached segment cost?
|
/* Update the segment if needed. */
|
||||||
if (n.m_segment->m_cost >= 0) {
|
if (!is_cached_segment) {
|
||||||
// reuse the cached segment cost
|
/* Write back the segment information so it can be reused the next time. */
|
||||||
segment_cost = n.m_segment->m_cost;
|
segment.m_cost = segment_cost;
|
||||||
} else {
|
segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
|
||||||
// save segment cost
|
assert(segment.m_end_segment_reason != ESRB_NONE);
|
||||||
n.m_segment->m_cost = segment_cost;
|
/* Save end of segment back to the node. */
|
||||||
|
n.SetLastTileTrackdir(cur.tile, cur.td);
|
||||||
// save end of segment back to the node
|
|
||||||
n.SetLastTileTrackdir(tile, trackdir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// special costs for the case we have reached our target
|
/* Do we have an excuse why not to continue pathfinding in this direction? */
|
||||||
if (end_by_target_seen) {
|
if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
|
||||||
|
/* Reason to not continue. Stop this PF branch. */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Special costs for the case we have reached our target. */
|
||||||
|
if (target_seen) {
|
||||||
n.flags_u.flags_s.m_targed_seen = true;
|
n.flags_u.flags_s.m_targed_seen = true;
|
||||||
|
/* Last-red and last-red-exit penalties. */
|
||||||
if (n.flags_u.flags_s.m_last_signal_was_red) {
|
if (n.flags_u.flags_s.m_last_signal_was_red) {
|
||||||
if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
|
if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
|
||||||
// last signal was red pre-signal-exit
|
// last signal was red pre-signal-exit
|
||||||
|
@ -355,12 +449,23 @@ public:
|
||||||
extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
|
extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Station platform-length penalty. */
|
||||||
|
if ((end_segment_reason & ESRB_STATION) != ESRB_NONE) {
|
||||||
|
Station *st = GetStationByTile(n.GetLastTile());
|
||||||
|
assert(st != NULL);
|
||||||
|
uint platform_length = st->GetPlatformLength(n.GetLastTile(), ReverseDiagDir(TrackdirToExitdir(n.GetLastTrackdir())));
|
||||||
|
/* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */
|
||||||
|
extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length;
|
||||||
|
/* Add penalty for the inappropriate platform length. */
|
||||||
|
extra_cost += PlatformLengthPenalty(platform_length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// total node cost
|
// total node cost
|
||||||
n.m_cost = parent_cost + first_tile_cost + segment_cost + extra_cost;
|
n.m_cost = parent_cost + segment_entry_cost + segment_cost + extra_cost;
|
||||||
|
|
||||||
return !n.m_segment->flags_u.flags_s.m_end_of_line || end_by_target_seen;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE bool CanUseGlobalCache(Node& n) const
|
FORCEINLINE bool CanUseGlobalCache(Node& n) const
|
||||||
|
|
|
@ -22,6 +22,60 @@ struct CYapfRailSegmentKey
|
||||||
FORCEINLINE bool operator == (const CYapfRailSegmentKey& other) const {return m_value == other.m_value;}
|
FORCEINLINE bool operator == (const CYapfRailSegmentKey& other) const {return m_value == other.m_value;}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Enum used in PfCalcCost() to see why was the segment closed. */
|
||||||
|
enum EndSegmentReason {
|
||||||
|
/* The following reasons can be saved into cached segment */
|
||||||
|
ESR_DEAD_END = 0, ///< track ends here
|
||||||
|
ESR_RAIL_TYPE, ///< the next tile has a different rail type than our tiles
|
||||||
|
ESR_INFINITE_LOOP, ///< infinite loop detected
|
||||||
|
ESR_SEGMENT_TOO_LONG, ///< the segment is too long (possible infinite loop)
|
||||||
|
ESR_CHOICE_FOLLOWS, ///< the next tile contains a choice (the track splits to more than one segments)
|
||||||
|
ESR_DEPOT, ///< stop in the depot (could be a target next time)
|
||||||
|
ESR_WAYPOINT, ///< waypoint encountered (could be a target next time)
|
||||||
|
ESR_STATION, ///< station encountered (could be a target next time)
|
||||||
|
|
||||||
|
/* The following reasons are used only internally by PfCalcCost().
|
||||||
|
* They should not be found in the cached segment. */
|
||||||
|
ESR_PATH_TOO_LONG, ///< the path is too long (searching for the nearest depot in the given radius)
|
||||||
|
ESR_FIRST_TWO_WAY_RED, ///< first signal was 2-way and it was red
|
||||||
|
ESR_LOOK_AHEAD_END, ///< we have just passed the last look-ahead signal
|
||||||
|
ESR_TARGET_REACHED, ///< we have just reached the destination
|
||||||
|
|
||||||
|
/* Special values */
|
||||||
|
ESR_NONE = 0xFF, ///< no reason to end the segment here
|
||||||
|
};
|
||||||
|
|
||||||
|
enum EndSegmentReasonBits {
|
||||||
|
ESRB_NONE = 0,
|
||||||
|
|
||||||
|
ESRB_DEAD_END = 1 << ESR_DEAD_END,
|
||||||
|
ESRB_RAIL_TYPE = 1 << ESR_RAIL_TYPE,
|
||||||
|
ESRB_INFINITE_LOOP = 1 << ESR_INFINITE_LOOP,
|
||||||
|
ESRB_SEGMENT_TOO_LONG = 1 << ESR_SEGMENT_TOO_LONG,
|
||||||
|
ESRB_CHOICE_FOLLOWS = 1 << ESR_CHOICE_FOLLOWS,
|
||||||
|
ESRB_DEPOT = 1 << ESR_DEPOT,
|
||||||
|
ESRB_WAYPOINT = 1 << ESR_WAYPOINT,
|
||||||
|
ESRB_STATION = 1 << ESR_STATION,
|
||||||
|
|
||||||
|
ESRB_PATH_TOO_LONG = 1 << ESR_PATH_TOO_LONG,
|
||||||
|
ESRB_FIRST_TWO_WAY_RED = 1 << ESR_FIRST_TWO_WAY_RED,
|
||||||
|
ESRB_LOOK_AHEAD_END = 1 << ESR_LOOK_AHEAD_END,
|
||||||
|
ESRB_TARGET_REACHED = 1 << ESR_TARGET_REACHED,
|
||||||
|
|
||||||
|
/* Additional (composite) values. */
|
||||||
|
|
||||||
|
/* What reasons mean that the target can be fond and needs to be detected. */
|
||||||
|
ESRB_POSSIBLE_TARGET = ESRB_DEPOT | ESRB_WAYPOINT | ESRB_STATION,
|
||||||
|
|
||||||
|
/* What reasons can be stored back into cached segment. */
|
||||||
|
ESRB_CACHED_MASK = ESRB_DEAD_END | ESRB_RAIL_TYPE | ESRB_INFINITE_LOOP | ESRB_SEGMENT_TOO_LONG | ESRB_CHOICE_FOLLOWS | ESRB_DEPOT | ESRB_WAYPOINT | ESRB_STATION,
|
||||||
|
|
||||||
|
/* Reasons to abort pathfinding in this direction. */
|
||||||
|
ESRB_ABORT_PF_MASK = ESRB_DEAD_END | ESRB_PATH_TOO_LONG | ESRB_INFINITE_LOOP | ESRB_FIRST_TWO_WAY_RED,
|
||||||
|
};
|
||||||
|
|
||||||
|
DECLARE_ENUM_AS_BIT_SET(EndSegmentReasonBits);
|
||||||
|
|
||||||
/** cached segment cost for rail YAPF */
|
/** cached segment cost for rail YAPF */
|
||||||
struct CYapfRailSegment
|
struct CYapfRailSegment
|
||||||
{
|
{
|
||||||
|
@ -33,14 +87,8 @@ struct CYapfRailSegment
|
||||||
int m_cost;
|
int m_cost;
|
||||||
TileIndex m_last_signal_tile;
|
TileIndex m_last_signal_tile;
|
||||||
Trackdir m_last_signal_td;
|
Trackdir m_last_signal_td;
|
||||||
|
EndSegmentReasonBits m_end_segment_reason;
|
||||||
CYapfRailSegment* m_hash_next;
|
CYapfRailSegment* m_hash_next;
|
||||||
union {
|
|
||||||
byte m_flags;
|
|
||||||
struct {
|
|
||||||
bool m_end_of_line : 1;
|
|
||||||
} flags_s;
|
|
||||||
} flags_u;
|
|
||||||
byte m_reserve[3];
|
|
||||||
|
|
||||||
FORCEINLINE CYapfRailSegment(const CYapfRailSegmentKey& key)
|
FORCEINLINE CYapfRailSegment(const CYapfRailSegmentKey& key)
|
||||||
: m_key(key)
|
: m_key(key)
|
||||||
|
@ -49,10 +97,9 @@ struct CYapfRailSegment
|
||||||
, m_cost(-1)
|
, m_cost(-1)
|
||||||
, m_last_signal_tile(INVALID_TILE)
|
, m_last_signal_tile(INVALID_TILE)
|
||||||
, m_last_signal_td(INVALID_TRACKDIR)
|
, m_last_signal_td(INVALID_TRACKDIR)
|
||||||
|
, m_end_segment_reason(ESRB_NONE)
|
||||||
, m_hash_next(NULL)
|
, m_hash_next(NULL)
|
||||||
{
|
{}
|
||||||
flags_u.m_flags = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
FORCEINLINE const Key& GetKey() const {return m_key;}
|
FORCEINLINE const Key& GetKey() const {return m_key;}
|
||||||
FORCEINLINE TileIndex GetTile() const {return m_key.GetTile();}
|
FORCEINLINE TileIndex GetTile() const {return m_key.GetTile();}
|
||||||
|
|
|
@ -72,7 +72,7 @@ public:
|
||||||
/** Called by YAPF to calculate the cost from the origin to the given node.
|
/** Called by YAPF to calculate the cost from the origin to the given node.
|
||||||
* Calculates only the cost of given node, adds it to the parent node cost
|
* Calculates only the cost of given node, adds it to the parent node cost
|
||||||
* and stores the result into Node::m_cost member */
|
* and stores the result into Node::m_cost member */
|
||||||
FORCEINLINE bool PfCalcCost(Node& n, const TrackFollower &tf)
|
FORCEINLINE bool PfCalcCost(Node& n, const TrackFollower *tf)
|
||||||
{
|
{
|
||||||
int segment_cost = 0;
|
int segment_cost = 0;
|
||||||
// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
|
// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
|
||||||
|
|
|
@ -100,7 +100,7 @@ public:
|
||||||
/** Called by YAPF to calculate the cost from the origin to the given node.
|
/** Called by YAPF to calculate the cost from the origin to the given node.
|
||||||
* Calculates only the cost of given node, adds it to the parent node cost
|
* Calculates only the cost of given node, adds it to the parent node cost
|
||||||
* and stores the result into Node::m_cost member */
|
* and stores the result into Node::m_cost member */
|
||||||
FORCEINLINE bool PfCalcCost(Node& n, const TrackFollower &tf)
|
FORCEINLINE bool PfCalcCost(Node& n, const TrackFollower *tf)
|
||||||
{
|
{
|
||||||
// base tile cost depending on distance
|
// base tile cost depending on distance
|
||||||
int c = IsDiagonalTrackdir(n.GetTrackdir()) ? 10 : 7;
|
int c = IsDiagonalTrackdir(n.GetTrackdir()) ? 10 : 7;
|
||||||
|
|
Loading…
Reference in New Issue