(svn r27845) -Change: (Yapf) Use FindDepotData struct to simplify depot finding code and remove need to return fake path distance. (juanjo)

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
peter1138 8 years ago
parent 9bc27c9a72
commit 1eef97674c

@ -223,7 +223,7 @@ public:
return 't'; return 't';
} }
static bool stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed) static FindDepotData stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
{ {
Tpf pf1; Tpf pf1;
/* /*
@ -236,16 +236,16 @@ public:
* depot orders and you do not disable automatic servicing. * depot orders and you do not disable automatic servicing.
*/ */
if (max_penalty != 0) pf1.DisableCache(true); if (max_penalty != 0) pf1.DisableCache(true);
bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, depot_tile, reversed); FindDepotData result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
if (_debug_desync_level >= 2) { if (_debug_desync_level >= 2) {
Tpf pf2; Tpf pf2;
TileIndex depot_tile2 = INVALID_TILE;
bool reversed2 = false;
pf2.DisableCache(true); pf2.DisableCache(true);
bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, &depot_tile2, &reversed2); FindDepotData result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) { if (result1.tile != result2.tile || (result1.reverse != result2.reverse)) {
DEBUG(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F"); DEBUG(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]",
result1.tile != INVALID_TILE ? "T" : "F",
result2.tile != INVALID_TILE ? "T" : "F");
DumpState(pf1, pf2); DumpState(pf1, pf2);
} }
} }
@ -253,7 +253,7 @@ public:
return result1; return result1;
} }
inline bool FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed) 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 */ /* set origin and destination nodes */
Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true); Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
@ -261,13 +261,10 @@ public:
Yapf().SetMaxCost(max_penalty); Yapf().SetMaxCost(max_penalty);
/* find the best path */ /* find the best path */
bool bFound = Yapf().FindPath(v); if (!Yapf().FindPath(v)) return FindDepotData();
if (!bFound) return false;
/* some path found /* Some path found. */
* get found depot tile */
Node *n = Yapf().GetBestNode(); Node *n = Yapf().GetBestNode();
*depot_tile = n->GetLastTile();
/* walk through the path back to the origin */ /* walk through the path back to the origin */
Node *pNode = n; Node *pNode = n;
@ -277,9 +274,7 @@ public:
/* if the origin node is our front vehicle tile/Trackdir then we didn't reverse /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
* but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */ * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
*reversed = (pNode->m_cost != 0); return FindDepotData(n->GetLastTile(), n->m_cost, pNode->m_cost != 0);
return true;
} }
}; };
@ -611,15 +606,13 @@ bool YapfTrainCheckReverse(const Train *v)
FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty) FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
{ {
FindDepotData fdd;
const Train *last_veh = v->Last(); const Train *last_veh = v->Last();
PBSTileInfo origin = FollowTrainReservation(v); PBSTileInfo origin = FollowTrainReservation(v);
TileIndex last_tile = last_veh->tile; TileIndex last_tile = last_veh->tile;
Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir()); Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
typedef bool (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*); typedef FindDepotData (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int);
PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay; PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
/* check if non-default YAPF type needed */ /* check if non-default YAPF type needed */
@ -627,9 +620,7 @@ FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
} }
bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY, &fdd.tile, &fdd.reverse); return pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY);
fdd.best_length = ret ? max_penalty / 2 : UINT_MAX; // some fake distance or NOT_FOUND
return fdd;
} }
bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype) bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)

Loading…
Cancel
Save