Changeset - r22939:fbe30010235d
[Not reviewed]
master
0 8 0
J0anJosep - 6 years ago 2018-04-29 10:23:01
juanjo.ng.83@gmail.com
Codechange: Increase readability of track functions and pathfinders.
8 files changed with 28 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/pathfinder/follow_track.hpp
Show inline comments
 
@@ -236,13 +236,13 @@ protected:
 
		CPerfStart perf(*m_pPerf);
 
		if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
 
			m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
 
		} else {
 
			m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
 

	
 
			if (IsTram() && m_new_td_bits == 0) {
 
			if (IsTram() && m_new_td_bits == TRACKDIR_BIT_NONE) {
 
				/* GetTileTrackStatus() returns 0 for single tram bits.
 
				 * As we cannot change it there (easily) without breaking something, change it here */
 
				switch (GetSingleTramBit(m_new_tile)) {
 
					case DIAGDIR_NE:
 
					case DIAGDIR_SW:
 
						m_new_td_bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
src/pathfinder/npf/npf.cpp
Show inline comments
 
@@ -804,13 +804,13 @@ static bool CanEnterTile(TileIndex tile,
 
 * @return The Trackdirs the vehicle can continue moving on.
 
 */
 
static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
 
{
 
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
 

	
 
	if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
 
	if (trackdirbits == TRACKDIR_BIT_NONE && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
 
		/* GetTileTrackStatus() returns 0 for single tram bits.
 
		 * As we cannot change it there (easily) without breaking something, change it here */
 
		switch (GetSingleTramBit(dst_tile)) {
 
			case DIAGDIR_NE:
 
			case DIAGDIR_SW:
 
				trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
 
@@ -897,13 +897,13 @@ static void NPFFollowTrack(AyStar *aysta
 
			dst_tile = src_tile;
 
			src_trackdir = ReverseTrackdir(src_trackdir);
 
		}
 

	
 
		trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
 

	
 
		if (trackdirbits == 0) {
 
		if (trackdirbits == TRACKDIR_BIT_NONE) {
 
			/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
 
			if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
 

	
 
			dst_tile = src_tile;
 
			src_trackdir = ReverseTrackdir(src_trackdir);
 

	
 
@@ -921,13 +921,13 @@ static void NPFFollowTrack(AyStar *aysta
 
			if (TracksOverlap(reserved | TrackToTrackBits(t))) trackdirbits &= ~TrackToTrackdirBits(t);
 
		}
 
	}
 

	
 
	/* Enumerate possible track */
 
	uint i = 0;
 
	while (trackdirbits != 0) {
 
	while (trackdirbits != TRACKDIR_BIT_NONE) {
 
		Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
 
		DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
 

	
 
		/* Tile with signals? */
 
		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
 
			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir))) {
src/pathfinder/opf/opf_ship.cpp
Show inline comments
 
@@ -180,35 +180,42 @@ bad:;
 

	
 
	*track = best_track;
 
	return best_bird_dist;
 
}
 

	
 
/**
 
 * returns the track to choose on the next tile, or -1 when it's better to
 
 * reverse. The tile given is the tile we are about to enter, enterdir is the
 
 * direction in which we are entering the tile
 
 * Finds the best track to choose on the next tile and
 
 * returns INVALID_TRACK when it is better to reverse.
 
 * @param v The ship.
 
 * @param tile The tile we are about to enter.
 
 * @param enterdir The direction entering the tile.
 
 * @param tracks The tracks available on new tile.
 
 * @param[out] path_found Whether a path has been found.
 
 * @return Best track on next tile or INVALID_TRACK when better to reverse.
 
 */
 
Track OPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found)
 
{
 
	assert(IsValidDiagDirection(enterdir));
 

	
 
	TileIndex tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
 
	Track track;
 

	
 
	/* Let's find out how far it would be if we would reverse first */
 
	Trackdir trackdir = v->GetVehicleTrackdir();
 
	TrackBits b = TrackStatusToTrackBits(GetTileTrackStatus(tile2, TRANSPORT_WATER, 0)) & DiagdirReachesTracks(ReverseDiagDir(enterdir)) & TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));
 
	uint rev_dist = UINT_MAX; // distance if we reverse
 
	Track cur_track = TrackdirToTrack(v->GetVehicleTrackdir()); // track on the current tile
 
	DiagDirection rev_enterdir = ReverseDiagDir(enterdir);
 
	TrackBits rev_tracks = TrackStatusToTrackBits(GetTileTrackStatus(tile2, TRANSPORT_WATER, 0)) &
 
			DiagdirReachesTracks(rev_enterdir);
 

	
 
	uint distr = UINT_MAX; // distance if we reversed
 
	if (b != 0) {
 
		distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
 
		if (distr != UINT_MAX) distr++; // penalty for reversing
 
	if ((rev_tracks & TrackToTrackBits(cur_track) != TRACK_BIT_NONE) {
 
		rev_dist = FindShipTrack(v, tile2, rev_enterdir, TrackToTrackBits(cur_track), tile, &track);
 
		if (rev_dist != UINT_MAX) rev_dist++; // penalty for reversing
 
	}
 

	
 
	/* And if we would not reverse? */
 
	uint dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track);
 

	
 
	/* Due to the way this pathfinder works we cannot determine whether we're lost or not. */
 
	path_found = true;
 
	if (dist <= distr) return track;
 
	if (dist <= rev_dist) return track;
 
	return INVALID_TRACK; // We could better reverse
 
}
src/pathfinder/yapf/yapf_common.hpp
Show inline comments
 
@@ -139,14 +139,13 @@ protected:
 
	}
 

	
 
public:
 
	/** Called by YAPF to detect if node ends in the desired destination */
 
	inline bool PfDetectDestination(Node &n)
 
	{
 
		bool bDest = (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
 
		return bDest;
 
		return (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
 
	}
 

	
 
	/**
 
	 * Called by YAPF to calculate cost estimate. Calculates distance to the destination
 
	 *  adds it to the actual cost from origin and stores the sum to the Node::m_estimate
 
	 */
src/pathfinder/yapf/yapf_costrail.hpp
Show inline comments
 
@@ -101,13 +101,13 @@ public:
 
	inline int CurveCost(Trackdir td1, Trackdir td2)
 
	{
 
		assert(IsValidTrackdir(td1));
 
		assert(IsValidTrackdir(td2));
 
		int cost = 0;
 
		if (TrackFollower::Allow90degTurns()
 
				&& ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
 
				&& ((TrackdirToTrackdirBits(td2) & TrackdirCrossesTrackdirs(td1)) != TRACKDIR_BIT_NONE)) {
 
			/* 90-deg curve penalty */
 
			cost += Yapf().PfGetSettings().rail_curve90_penalty;
 
		} else if (td2 != NextTrackdir(td1)) {
 
			/* 45-deg curve penalty */
 
			cost += Yapf().PfGetSettings().rail_curve45_penalty;
 
		}
src/pathfinder/yapf/yapf_destrail.hpp
Show inline comments
 
@@ -163,22 +163,19 @@ public:
 
		return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
 
	}
 

	
 
	/** Called by YAPF to detect if node ends in the desired destination */
 
	inline bool PfDetectDestination(TileIndex tile, Trackdir td)
 
	{
 
		bool bDest;
 
		if (m_dest_station_id != INVALID_STATION) {
 
			bDest = HasStationTileRail(tile)
 
			return HasStationTileRail(tile)
 
				&& (GetStationIndex(tile) == m_dest_station_id)
 
				&& (GetRailStationTrack(tile) == TrackdirToTrack(td));
 
		} else {
 
			bDest = (tile == m_destTile)
 
				&& ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
 
		}
 
		return bDest;
 

	
 
		return (tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
 
	}
 

	
 
	/**
 
	 * Called by YAPF to calculate cost estimate. Calculates distance to the destination
 
	 *  adds it to the actual cost from origin and stores the sum to the Node::m_estimate
 
	 */
src/pathfinder/yapf/yapf_road.cpp
Show inline comments
 
@@ -182,14 +182,13 @@ public:
 
		return *static_cast<Tpf *>(this);
 
	}
 

	
 
	/** Called by YAPF to detect if node ends in the desired destination */
 
	inline bool PfDetectDestination(Node &n)
 
	{
 
		bool bDest = IsRoadDepotTile(n.m_segment_last_tile);
 
		return bDest;
 
		return IsRoadDepotTile(n.m_segment_last_tile);
 
	}
 

	
 
	inline bool PfDetectDestinationTile(TileIndex tile, Trackdir trackdir)
 
	{
 
		return IsRoadDepotTile(tile);
 
	}
src/track_func.h
Show inline comments
 
@@ -58,13 +58,13 @@ static inline bool IsValidTrackdirForRoa
 
 * @param trackdir The value to check
 
 * @return true if the given value is a valid Trackdir
 
 * @note Use this in an assert()
 
 */
 
static inline bool IsValidTrackdir(Trackdir trackdir)
 
{
 
	return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
 
	return (1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE;
 
}
 

	
 
/**
 
 * Convert an Axis to the corresponding Track
 
 * AXIS_X -> TRACK_X
 
 * AXIS_Y -> TRACK_Y
0 comments (0 inline, 0 general)