1
0
Fork 0

(svn r13941) -Codechange [YAPP]: Added YAPP-related penalties to YAPF. (michi_cc)

release/0.7
rubidium 2008-08-02 22:51:07 +00:00
parent abc46b1e86
commit 8c7983727e
4 changed files with 82 additions and 40 deletions

View File

@ -1725,6 +1725,8 @@ const SettingDesc _patch_settings[] = {
SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p0, SLE_INT, 28, SL_MAX_VERSION, 0, 0, 500, -1000000, 1000000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p0, SLE_INT, 28, SL_MAX_VERSION, 0, 0, 500, -1000000, 1000000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p1, SLE_INT, 28, SL_MAX_VERSION, 0, 0, -100, -1000000, 1000000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p1, SLE_INT, 28, SL_MAX_VERSION, 0, 0, -100, -1000000, 1000000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p2, SLE_INT, 28, SL_MAX_VERSION, 0, 0, 5, -1000000, 1000000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_look_ahead_signal_p2, SLE_INT, 28, SL_MAX_VERSION, 0, 0, 5, -1000000, 1000000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_pbs_cross_penalty, SLE_UINT,100, SL_MAX_VERSION, 0, 0, 3 * YAPF_TILE_LENGTH, 0, 1000000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_pbs_signal_back_penalty, SLE_UINT,100, SL_MAX_VERSION, 0, 0, 15 * YAPF_TILE_LENGTH, 0, 1000000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_longer_platform_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 8 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_longer_platform_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 8 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_longer_platform_per_tile_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 0 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_longer_platform_per_tile_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 0 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL),
SDT_CONDVAR(GameSettings, pf.yapf.rail_shorter_platform_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 40 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL), SDT_CONDVAR(GameSettings, pf.yapf.rail_shorter_platform_penalty, SLE_UINT, 33, SL_MAX_VERSION, 0, 0, 40 * YAPF_TILE_LENGTH, 0, 20000, 0, STR_NULL, NULL),

View File

@ -218,6 +218,8 @@ struct YAPFSettings {
int32 rail_look_ahead_signal_p0; ///< constant in polynomial penalty function int32 rail_look_ahead_signal_p0; ///< constant in polynomial penalty function
int32 rail_look_ahead_signal_p1; ///< constant in polynomial penalty function int32 rail_look_ahead_signal_p1; ///< constant in polynomial penalty function
int32 rail_look_ahead_signal_p2; ///< constant in polynomial penalty function int32 rail_look_ahead_signal_p2; ///< constant in polynomial penalty function
uint32 rail_pbs_cross_penalty; ///< penalty for crossing a reserved tile
uint32 rail_pbs_signal_back_penalty; ///< penalty for passing a pbs signal from the backside
uint32 rail_longer_platform_penalty; ///< penalty for longer station platform than train uint32 rail_longer_platform_penalty; ///< penalty for longer station platform than train
uint32 rail_longer_platform_per_tile_penalty; ///< penalty for longer station platform than train (per tile) uint32 rail_longer_platform_per_tile_penalty; ///< penalty for longer station platform than train (per tile)

View File

@ -589,6 +589,19 @@ static inline bool TracksOverlap(TrackBits bits)
return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT; return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
} }
/**
* Check if a given track is contained within or overlaps some other tracks.
*
* @param tracks Tracks to be testet against
* @param track The track to test
* @return true if the track is already in the tracks or overlaps the tracks.
*/
static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
{
if (HasBit(tracks, track)) return true;
return TracksOverlap(tracks | TrackToTrackBits(track));
}
/** /**
* Checks whether the trackdir means that we are reversing. * Checks whether the trackdir means that we are reversing.
* @param dir the trackdir to check * @param dir the trackdir to check

View File

@ -5,6 +5,7 @@
#ifndef YAPF_COSTRAIL_HPP #ifndef YAPF_COSTRAIL_HPP
#define YAPF_COSTRAIL_HPP #define YAPF_COSTRAIL_HPP
#include "../pbs.h"
template <class Types> template <class Types>
class CYapfCostRailT class CYapfCostRailT
@ -128,6 +129,19 @@ public:
return cost; return cost;
} }
/** The cost for reserved tiles, including skipped ones. */
FORCEINLINE int ReservationCost(Node& n, TileIndex& tile, Trackdir trackdir, int skipped)
{
if (n.m_num_signals_passed >= m_sig_look_ahead_costs.Size() / 2) return 0;
if (TrackOverlapsTracks(GetReservedTrackbits(tile), TrackdirToTrack(trackdir))) {
int cost = Yapf().PfGetSettings().rail_pbs_cross_penalty;
if (!IsDiagonalTrackdir(trackdir)) cost = (cost * YAPF_TILE_CORNER_LENGTH) / YAPF_TILE_LENGTH;
return cost * (skipped + 1);
}
return 0;
}
int SignalCost(Node& n, TileIndex tile, Trackdir trackdir) int SignalCost(Node& n, TileIndex tile, Trackdir trackdir)
{ {
int cost = 0; int cost = 0;
@ -139,50 +153,58 @@ public:
if (has_signal_against && !has_signal_along && IsOnewaySignal(tile, TrackdirToTrack(trackdir))) { if (has_signal_against && !has_signal_along && IsOnewaySignal(tile, TrackdirToTrack(trackdir))) {
// one-way signal in opposite direction // one-way signal in opposite direction
n.m_segment->m_end_segment_reason |= ESRB_DEAD_END; n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
} else if (has_signal_along) { } else {
SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir); if (has_signal_along) {
// cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0; // cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
if (sig_state != SIGNAL_STATE_RED) { int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0;
// green signal if (sig_state != SIGNAL_STATE_RED) {
n.flags_u.flags_s.m_last_signal_was_red = false; // green signal
// negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal n.flags_u.flags_s.m_last_signal_was_red = false;
if (look_ahead_cost < 0) { // negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal
// add its negation to the cost if (look_ahead_cost < 0) {
cost -= look_ahead_cost; // add its negation to the cost
} cost -= look_ahead_cost;
} else { }
// we have a red signal in our direction } else {
// was it first signal which is two-way? SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir));
if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) { // we have a red signal in our direction
// yes, the first signal is two-way red signal => DEAD END // was it first signal which is two-way?
n.m_segment->m_end_segment_reason |= ESRB_DEAD_END; if (!IsPbsSignal(sig_type) && Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
Yapf().m_stopped_on_first_two_way_signal = true; // yes, the first signal is two-way red signal => DEAD END
return -1; n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
} Yapf().m_stopped_on_first_two_way_signal = true;
SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir)); return -1;
n.m_last_red_signal_type = sig_type; }
n.flags_u.flags_s.m_last_signal_was_red = true; n.m_last_red_signal_type = sig_type;
n.flags_u.flags_s.m_last_signal_was_red = true;
// look-ahead signal penalty // look-ahead signal penalty
if (look_ahead_cost > 0) { if (!IsPbsSignal(sig_type) && look_ahead_cost > 0) {
// add the look ahead penalty only if it is positive // add the look ahead penalty only if it is positive
cost += look_ahead_cost; cost += look_ahead_cost;
}
// special signal penalties
if (n.m_num_signals_passed == 0) {
switch (sig_type) {
case SIGTYPE_COMBO:
case SIGTYPE_EXIT: cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break; // first signal is red pre-signal-exit
case SIGTYPE_NORMAL:
case SIGTYPE_ENTRY: cost += Yapf().PfGetSettings().rail_firstred_penalty; break;
default: break;
};
}
} }
// special signal penalties n.m_num_signals_passed++;
if (n.m_num_signals_passed == 0) { n.m_segment->m_last_signal_tile = tile;
switch (sig_type) { n.m_segment->m_last_signal_td = trackdir;
case SIGTYPE_COMBO: }
case SIGTYPE_EXIT: cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break; // first signal is red pre-signal-exit
case SIGTYPE_NORMAL: if (has_signal_against && IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) {
case SIGTYPE_ENTRY: cost += Yapf().PfGetSettings().rail_firstred_penalty; break; cost += n.m_num_signals_passed < Yapf().PfGetSettings().rail_look_ahead_max_signals ? Yapf().PfGetSettings().rail_pbs_signal_back_penalty : 0;
};
}
} }
n.m_num_signals_passed++;
n.m_segment->m_last_signal_tile = tile;
n.m_segment->m_last_signal_td = trackdir;
} }
} }
return cost; return cost;
@ -327,6 +349,9 @@ no_entry_cost: // jump here at the beginning if the node has no parent (it is th
/* Slope cost. */ /* Slope cost. */
segment_cost += Yapf().SlopeCost(cur.tile, cur.td); segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
/* Reserved tiles. */
segment_cost += Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped);
/* Signal cost (routine can modify segment data). */ /* Signal cost (routine can modify segment data). */
segment_cost += Yapf().SignalCost(n, cur.tile, cur.td); segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
end_segment_reason = segment.m_end_segment_reason; end_segment_reason = segment.m_end_segment_reason;