Changeset - r9806:1fcdbff0d7b2
[Not reviewed]
master
0 2 0
rubidium - 16 years ago 2008-08-02 22:52:50
rubidium@openttd.org
(svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
2 files changed with 54 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/npf.cpp
Show inline comments
 
@@ -453,12 +453,23 @@ static int32 NPFFindDepot(AyStar* as, Op
 
	/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
 
	 * since checking the cache not that much faster than the actual check */
 
	return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
 
		AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
 
}
 

	
 
/** Find any safe and free tile. */
 
static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current)
 
{
 
	const Vehicle *v = ((NPFFindStationOrTileData*)as->user_target)->v;
 

	
 
	return
 
		IsSafeWaitingPosition(v, current->path.node.tile, (Trackdir)current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
 
		IsWaitingPositionFree(v, current->path.node.tile, (Trackdir)current->path.node.direction, _settings_game.pf.forbid_90_deg) ?
 
			AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
 
}
 

	
 
/* Will find a station identified using the NPFFindStationOrTileData */
 
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
 
{
 
	NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
 
	AyStarNode *node = &current->path.node;
 
	TileIndex tile = node->tile;
 
@@ -817,12 +828,24 @@ static void NPFFollowTrack(AyStar* aysta
 
			src_trackdir = ReverseTrackdir(src_trackdir);
 

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

	
 
	if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
 
		/* Mask out any reserved tracks. */
 
		TrackBits reserved = GetReservedTrackbits(dst_tile);
 
		trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
 

	
 
		uint bits = TrackdirBitsToTrackBits(trackdirbits);
 
		int i;
 
		FOR_EACH_SET_BIT(i, bits) {
 
			if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) trackdirbits &= ~TrackToTrackdirBits((Track)i);
 
		}
 
	}
 

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

	
 
@@ -1062,12 +1085,36 @@ NPFFoundTargetData NPFRouteToDepotTrialE
 
	if (result.best_bird_dist != 0) {
 
		DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
 
	}
 
	return best_result;
 
}
 

	
 
NPFFoundTargetData NPFRouteToSafeTile(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
 
{
 
	assert(v->type == VEH_TRAIN);
 

	
 
	NPFFindStationOrTileData fstd;
 
	fstd.v = v;
 
	fstd.reserve_path = true;
 

	
 
	AyStarNode start1;
 
	start1.tile = tile;
 
	/* We set this in case the target is also the start tile, we will just
 
	 * return a not found then */
 
	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
 
	start1.direction = trackdir;
 
	NPFSetFlag(&start1, NPF_FLAG_IGNORE_RESERVED, true);
 

	
 
	RailTypes railtypes = v->u.rail.compatible_railtypes;
 
	if (override_railtype) railtypes |= GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes;
 

	
 
	/* perform a breadth first search. Target is NULL,
 
	 * since we are just looking for any safe tile...*/
 
	return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, TRANSPORT_RAIL, 0, v->owner, railtypes, 0);
 
}
 

	
 
void InitializeNPF()
 
{
 
	static bool first_init = true;
 
	if (first_init) {
 
		first_init = false;
 
		init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
src/npf.h
Show inline comments
 
@@ -69,12 +69,13 @@ enum NPFNodeFlag {
 
	NPF_FLAG_2ND_SIGNAL,        ///< Used to mark that two signals were seen, rail only
 
	NPF_FLAG_3RD_SIGNAL,        ///< Used to mark that three signals were seen, rail only
 
	NPF_FLAG_REVERSE,           ///< Used to mark that this node was reached from the second start node, if applicable
 
	NPF_FLAG_LAST_SIGNAL_RED,   ///< Used to mark that the last signal on this path was red
 
	NPF_FLAG_IGNORE_START_TILE, ///< Used to mark that the start tile is invalid, and searching should start from the second tile on
 
	NPF_FLAG_TARGET_RESERVED,   ///< Used to mark that the possible reservation target is already reserved
 
	NPF_FLAG_IGNORE_RESERVED,   ///< Used to mark that reserved tiles should be considered impassable
 
};
 

	
 
/* Meant to be stored in AyStar.userpath */
 
struct NPFFoundTargetData {
 
	uint best_bird_dist;    ///< The best heuristic found. Is 0 if the target was found
 
	uint best_path_dist;    ///< The shortest path. Is (uint)-1 if no path is found
 
@@ -108,12 +109,18 @@ NPFFoundTargetData NPFRouteToDepotBreadt
 
 */
 
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty);
 
/* Search by trying each depot in order of Manhattan Distance. Good for lots
 
 * of choices and accurate heuristics, such as water. */
 
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes);
 

	
 
/**
 
 * Search for any safe tile using a breadth first search and try to reserve a path.
 
 */
 
NPFFoundTargetData NPFRouteToSafeTile(const Vehicle *v, TileIndex tile, Trackdir trackdir,bool override_railtype);
 

	
 

	
 
void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, Vehicle *v, bool reserve_path = false);
 

	
 

	
 
/*
 
 * Functions to manipulate the various NPF related flags on an AyStarNode.
 
 */
0 comments (0 inline, 0 general)