Changeset - r26468:408717343c14
[Not reviewed]
master
0 3 0
Nicolas Chappe - 2 years ago 2022-07-27 16:34:01
74881848+nchappe@users.noreply.github.com
Codechange: [YAPF] Allow to retrieve the final tile of the calculated path
3 files changed with 22 insertions and 14 deletions:
0 comments (0 inline, 0 general)
src/pathfinder/yapf/yapf.h
Show inline comments
 
@@ -53,15 +53,16 @@ Trackdir YapfRoadVehicleChooseTrack(cons
 
 * @param tile     the tile to find the path from (should be next tile the train is about to enter)
 
 * @param enterdir diagonal direction which the RV will enter this new tile from
 
 * @param tracks   available trackdirs on the new tile (to choose from)
 
 * @param path_found [out] Whether a path has been found (true) or has been guessed (false)
 
 * @param reserve_track indicates whether YAPF should try to reserve the found path
 
 * @param target   [out] the target tile of the reservation, free is set to true if path was reserved
 
 * @param dest     [out] the final tile of the best path found
 
 * @return         the best track for next turn
 
 */
 
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, struct PBSTileInfo *target);
 
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, struct PBSTileInfo *target, TileIndex *dest);
 

	
 
/**
 
 * Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using YAPF.
 
 * @param v            vehicle that needs to go to some depot
 
 * @param max_penalty  max distance (in pathfinder penalty) from the current vehicle position
 
 *                     (used also as optimization - the pathfinder can stop path finding if max_penalty
src/pathfinder/yapf/yapf_rail.cpp
Show inline comments
 
@@ -392,37 +392,38 @@ public:
 
	/** return debug report character to identify the transportation type */
 
	inline char TransportTypeChar() const
 
	{
 
		return 't';
 
	}
 

	
 
	static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
 
	static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
 
	{
 
		/* create pathfinder instance */
 
		Tpf pf1;
 
		Trackdir result1;
 

	
 
		if (_debug_desync_level < 2) {
 
			result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
 
			result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
 
		} else {
 
			result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, nullptr);
 
			result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, nullptr, nullptr);
 
			Tpf pf2;
 
			pf2.DisableCache(true);
 
			Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
 
			Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
 
			if (result1 != result2) {
 
				Debug(desync, 2, "CACHE ERROR: ChooseRailTrack() = [{}, {}]", result1, result2);
 
				DumpState(pf1, pf2);
 
			}
 
		}
 

	
 
		return result1;
 
	}
 

	
 
	inline Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
 
	inline Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
 
	{
 
		if (target != nullptr) target->tile = INVALID_TILE;
 
		if (dest != nullptr) *dest = INVALID_TILE;
 

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

	
 
@@ -446,13 +447,16 @@ public:
 
				this->FindSafePositionOnNode(pPrev);
 
			}
 
			/* return trackdir from the best origin node (one of start nodes) */
 
			Node &best_next_node = *pPrev;
 
			next_trackdir = best_next_node.GetTrackdir();
 

	
 
			if (reserve_track && path_found) this->TryReservePath(target, pNode->GetLastTile());
 
			if (reserve_track && path_found) {
 
				if (dest != nullptr) *dest = Yapf().GetBestNode()->GetLastTile();
 
				this->TryReservePath(target, pNode->GetLastTile());
 
			}
 
		}
 

	
 
		/* Treat the path as found if stopped on the first two way signal(s). */
 
		path_found |= Yapf().m_stopped_on_first_two_way_signal;
 
		return next_trackdir;
 
	}
 
@@ -525,24 +529,24 @@ struct CYapfAnyDepotRail1 : CYapfT<CYapf
 
struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
 

	
 
struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail    , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
 
struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
 

	
 

	
 
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
 
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
 
{
 
	/* default is YAPF type 2 */
 
	typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*);
 
	typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*, TileIndex*);
 
	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
 

	
 
	/* check if non-default YAPF type needed */
 
	if (_settings_game.pf.forbid_90_deg) {
 
		pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
 
	}
 

	
 
	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
 
	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
 
	return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
 
}
 

	
 
bool YapfTrainCheckReverse(const Train *v)
 
{
 
	const Train *last_veh = v->Last();
src/train_cmd.cpp
Show inline comments
 
@@ -2315,19 +2315,22 @@ static const byte _initial_tile_subcoord
 
 * @param tile The tile the train is about to enter
 
 * @param enterdir Diagonal direction the train is coming from
 
 * @param tracks Usable tracks on the new tile
 
 * @param[out] path_found Whether a path has been found or not.
 
 * @param do_track_reservation Path reservation is requested
 
 * @param[out] dest State and destination of the requested path
 
 * @param[out] final_dest Final tile of the best path found
 
 * @return The best track the train should follow
 
 */
 
static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest)
 
static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest, TileIndex *final_dest)
 
{
 
	if (final_dest != nullptr) *final_dest = INVALID_TILE;
 

	
 
	switch (_settings_game.pf.pathfinder_for_trains) {
 
		case VPF_NPF: return NPFTrainChooseTrack(v, path_found, do_track_reservation, dest);
 
		case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest);
 
		case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest, final_dest);
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
@@ -2593,13 +2596,13 @@ static Track ChooseTrainTrack(Train *v, 
 

	
 
	if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
 
		/* Pathfinders are able to tell that route was only 'guessed'. */
 
		bool      path_found = true;
 
		TileIndex new_tile = res_dest.tile;
 

	
 
		Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest);
 
		Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest, nullptr);
 
		if (new_tile == tile) best_track = next_track;
 
		v->HandlePathfindingResult(path_found);
 
	}
 

	
 
	/* No track reservation requested -> finished. */
 
	if (!do_track_reservation) return best_track;
 
@@ -2641,13 +2644,13 @@ static Track ChooseTrainTrack(Train *v, 
 
		}
 

	
 
		/* Get next order with destination. */
 
		if (orders.SwitchToNextOrder(true)) {
 
			PBSTileInfo cur_dest;
 
			bool path_found;
 
			DoTrainPathfind(v, next_tile, exitdir, reachable, path_found, true, &cur_dest);
 
			DoTrainPathfind(v, next_tile, exitdir, reachable, path_found, true, &cur_dest, nullptr);
 
			if (cur_dest.tile != INVALID_TILE) {
 
				res_dest = cur_dest;
 
				if (res_dest.okay) continue;
 
				/* Path found, but could not be reserved. */
 
				FreeTrainTrackReservation(v);
 
				if (mark_stuck) MarkTrainAsStuck(v);
0 comments (0 inline, 0 general)