summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-08-02 22:52:50 +0000
committerrubidium <rubidium@openttd.org>2008-08-02 22:52:50 +0000
commitbf33da38ebfcc2f346529fd0751d6fe5fde3155a (patch)
tree7a3fe24c3a2e126c9847d3db6de32680ed7fbaea
parentc05c10ae71703467898e18d062771d733f60e004 (diff)
downloadopenttd-bf33da38ebfcc2f346529fd0751d6fe5fde3155a.tar.xz
(svn r13948) -Add [YAPP]: Extend NPF with a function to find a safe tile and reserve a path. (michi_cc)
-rw-r--r--src/npf.cpp47
-rw-r--r--src/npf.h7
2 files changed, 54 insertions, 0 deletions
diff --git a/src/npf.cpp b/src/npf.cpp
index 4aa817b70..d5f431b43 100644
--- a/src/npf.cpp
+++ b/src/npf.cpp
@@ -456,6 +456,17 @@ static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
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)
{
@@ -820,6 +831,18 @@ static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
}
}
+ 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) {
@@ -1065,6 +1088,30 @@ NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir,
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;
diff --git a/src/npf.h b/src/npf.h
index 9ab9a456b..bf19b62d5 100644
--- a/src/npf.h
+++ b/src/npf.h
@@ -72,6 +72,7 @@ enum NPFNodeFlag {
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 */
@@ -111,6 +112,12 @@ NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir t
* 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);