summaryrefslogtreecommitdiff
path: root/src/pathfinder/npf
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-12-02 10:44:38 +0000
committerrubidium <rubidium@openttd.org>2009-12-02 10:44:38 +0000
commitc10e95314156cfaf856a3075118b1bd2c698d139 (patch)
tree542edd8f84d6ac34d3012f25d783cefceaafcd97 /src/pathfinder/npf
parentc56c892b2c9a30d7a5d4e7a59053e13471f038ab (diff)
downloadopenttd-c10e95314156cfaf856a3075118b1bd2c698d139.tar.xz
(svn r18373) -Codechange: unify some road pathfinder functions
Diffstat (limited to 'src/pathfinder/npf')
-rw-r--r--src/pathfinder/npf/npf.cpp45
-rw-r--r--src/pathfinder/npf/npf_func.h20
2 files changed, 63 insertions, 2 deletions
diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp
index f50106fb2..38c966cfd 100644
--- a/src/pathfinder/npf/npf.cpp
+++ b/src/pathfinder/npf/npf.cpp
@@ -18,8 +18,9 @@
#include "../../functions.h"
#include "../../tunnelbridge.h"
#include "../../pbs.h"
-#include "../../train.h"
+#include "../../roadveh.h"
#include "../../ship.h"
+#include "../../train.h"
#include "../pathfinder_func.h"
#include "../pathfinder_type.h"
#include "npf.h"
@@ -1097,6 +1098,46 @@ void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool
fstd->v = v;
}
+/*** Road vehicles ***/
+
+FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_distance)
+{
+ Trackdir trackdir = v->GetVehicleTrackdir();
+
+ NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, v->tile, ReverseTrackdir(trackdir), false, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0);
+
+ if (ftd.best_bird_dist != 0) return FindDepotData();
+
+ /* Found target */
+ /* Our caller expects a number of tiles, so we just approximate that
+ * number by this. It might not be completely what we want, but it will
+ * work for now :-) We can possibly change this when the old pathfinder
+ * is removed. */
+ return FindDepotData(ftd.node.tile, ftd.best_path_dist / NPF_TILE_LENGTH);
+}
+
+Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs)
+{
+ NPFFindStationOrTileData fstd;
+
+ NPFFillWithOrderData(&fstd, v);
+ Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
+
+ NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES);
+ if (ftd.best_trackdir == INVALID_TRACKDIR) {
+ /* We are already at our target. Just do something
+ * @todo: maybe display error?
+ * @todo: go straight ahead if possible? */
+ return (Trackdir)FindFirstBit2x64(trackdirs);
+ }
+
+ /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
+ * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
+ * we did not find our target, but ftd.best_trackdir contains the direction leading
+ * to the tile closest to our target. */
+ return ftd.best_trackdir;
+}
+
/*** Ships ***/
Track NPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
@@ -1127,7 +1168,7 @@ FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_distance)
assert(trackdir != INVALID_TRACKDIR);
NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes, NPF_INFINITE_PENALTY);
- if (ftd.best_bird_dist != 0) FindDepotData();
+ if (ftd.best_bird_dist != 0) return FindDepotData();
/* Found target */
/* Our caller expects a number of tiles, so we just approximate that
diff --git a/src/pathfinder/npf/npf_func.h b/src/pathfinder/npf/npf_func.h
index 027e8c794..14c4d2c53 100644
--- a/src/pathfinder/npf/npf_func.h
+++ b/src/pathfinder/npf/npf_func.h
@@ -17,6 +17,26 @@
#include "../pathfinder_type.h"
/**
+ * Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF.
+ * @param v vehicle that needs to go to some depot
+ * @param max_distance max distance (number of track tiles) from the current vehicle position
+ * (used also as optimization - the pathfinder can stop path finding if max_distance
+ * was reached and no depot was seen)
+ * @return the data about the depot
+ */
+FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_distance);
+
+/**
+ * Finds the best path for given road vehicle using NPF.
+ * @param v the RV that needs to find a path
+ * @param tile the tile to find the path from (should be next tile the RV is about to enter)
+ * @param enterdir diagonal direction which the RV will enter this new tile from
+ * @param trackdirs available trackdirs on the new tile (to choose from)
+ * @return the best trackdir for next turn or INVALID_TRACKDIR if the path could not be found
+ */
+Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs);
+
+/**
* Finds the best path for given ship using NPF.
* @param v the ship that needs to find a path
* @param tile the tile to find the path from (should be next tile the ship is about to enter)