summaryrefslogtreecommitdiff
path: root/src/pathfinder/yapf
diff options
context:
space:
mode:
Diffstat (limited to 'src/pathfinder/yapf')
-rw-r--r--src/pathfinder/yapf/yapf.h44
-rw-r--r--src/pathfinder/yapf/yapf_rail.cpp22
2 files changed, 39 insertions, 27 deletions
diff --git a/src/pathfinder/yapf/yapf.h b/src/pathfinder/yapf/yapf.h
index 2f39cd6c5..bfb442a2a 100644
--- a/src/pathfinder/yapf/yapf.h
+++ b/src/pathfinder/yapf/yapf.h
@@ -14,6 +14,7 @@
#include "../../direction_type.h"
#include "../../station_type.h"
+#include "../pathfinder_type.h"
/**
* Finds the best path for given ship using YAPF.
@@ -33,7 +34,8 @@ Track YapfChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir,
*/
Trackdir YapfChooseRoadTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir);
-/** Finds the best path for given train.
+/**
+ * Finds the best path for given train using YAPF.
* @param v the train that needs to find a path
* @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
@@ -41,9 +43,9 @@ Trackdir YapfChooseRoadTrack(const Vehicle *v, TileIndex tile, DiagDirection ent
* @param path_not_found [out] true is returned if no path can be found (returned Trackdir is only a 'guess')
* @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
- * @return the best trackdir for next turn or INVALID_TRACKDIR if the path could not be found
+ * @return the best track for next turn
*/
-Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, struct PBSTileInfo *target);
+Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, struct PBSTileInfo *target);
/** Used by RV multistop feature to find the nearest road stop that has a free slot.
* @param v RV (its current tile will be the origin)
@@ -70,31 +72,33 @@ bool YapfFindNearestRoadVehicleCompatibleStop(const RoadVehicle *v, StationID st
*/
bool YapfFindNearestRoadDepot(const Vehicle *v, int max_distance, TileIndex *depot_tile);
-/** Used when user sends train to the nearest depot or if train needs servicing.
+/**
+ * Used when user sends train to the nearest depot or if train needs servicing using YAPF.
* @param v train that needs to go to some depot
* @param max_distance max distance (number of track tiles) from the current train position
* (used also as optimization - the pathfinder can stop path finding if max_distance
* was reached and no depot was seen)
- * @param reverse_penalty penalty that should be added for the path that requires reversing the train first
- * @param depot_tile receives the depot tile if depot was found
- * @param reversed receives true if train needs to reversed first
- * @return true if depot was found.
+ * @return the data about the depot
*/
-bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed);
+FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_distance);
-/** Returns true if it is better to reverse the train before leaving station */
-bool YapfCheckReverseTrain(const Vehicle *v);
+/**
+ * Returns true if it is better to reverse the train before leaving station using YAPF.
+ * @param v the train leaving the station
+ * @return true if reversing is better
+ */
+bool YapfTrainCheckReverse(const Train *v);
/**
- * Try to extend the reserved path of a train to the nearest safe tile.
+ * Try to extend the reserved path of a train to the nearest safe tile using YAPF.
*
* @param v The train that needs to find a safe tile.
* @param tile Last tile of the current reserved path.
* @param td Last trackdir of the current reserved path.
- * @param override_railtype Should all physically compabtible railtypes be searched, even if the vehicle can't on them on it own?
+ * @param override_railtype Should all physically compatible railtypes be searched, even if the vehicle can't run on them on its own?
* @return True if the path could be extended to a safe tile.
*/
-bool YapfRailFindNearestSafeTile(const Vehicle *v, TileIndex tile, Trackdir td, bool override_railtype);
+bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype);
/** Use this function to notify YAPF that track layout (or signal configuration) has change */
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track);
@@ -111,7 +115,17 @@ extern int _aystar_stats_closed_size;
/** Base tile length units */
enum {
YAPF_TILE_LENGTH = 100,
- YAPF_TILE_CORNER_LENGTH = 71
+ YAPF_TILE_CORNER_LENGTH = 71,
+
+ /**
+ * This penalty is the equivalent of "infite", which means that paths that
+ * get this penalty will be chosen, but only if there is no other route
+ * without it. Be careful with not applying this penalty to often, or the
+ * total path cost might overflow..
+ * For now, this is just a Very Big Penalty, we might actually implement
+ * this in a nicer way :-)
+ */
+ YAPF_INFINITE_PENALTY = 1000 * YAPF_TILE_LENGTH,
};
#endif /* YAPF_H */
diff --git a/src/pathfinder/yapf/yapf_rail.cpp b/src/pathfinder/yapf/yapf_rail.cpp
index f17b90262..54ea1a916 100644
--- a/src/pathfinder/yapf/yapf_rail.cpp
+++ b/src/pathfinder/yapf/yapf_rail.cpp
@@ -524,7 +524,7 @@ struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CF
struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
-Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
+Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
{
/* default is YAPF type 2 */
typedef Trackdir (*PfnChooseRailTrack)(const Vehicle*, TileIndex, DiagDirection, TrackBits, bool*, bool, PBSTileInfo*);
@@ -536,13 +536,11 @@ Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection ent
}
Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
-
- return td_ret;
+ return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
}
-bool YapfCheckReverseTrain(const Vehicle *vt)
+bool YapfTrainCheckReverse(const Train *v)
{
- const Train *v = Train::From(vt);
const Train *last_veh = v->Last();
/* get trackdirs of both ends */
@@ -600,12 +598,11 @@ bool YapfCheckReverseTrain(const Vehicle *vt)
return reverse;
}
-bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
+FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_distance)
{
- *depot_tile = INVALID_TILE;
- *reversed = false;
+ FindDepotData fdd;
- const Vehicle *last_veh = v->Last();
+ const Train *last_veh = v->Last();
PBSTileInfo origin = FollowTrainReservation(Train::From(v));
TileIndex last_tile = last_veh->tile;
@@ -619,11 +616,12 @@ bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reve
pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
}
- bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
- return ret;
+ bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_distance, YAPF_INFINITE_PENALTY, &fdd.tile, &fdd.reverse);
+ fdd.best_length = ret ? max_distance / 2 : UINT_MAX; // some fake distance or NOT_FOUND
+ return fdd;
}
-bool YapfRailFindNearestSafeTile(const Vehicle *v, TileIndex tile, Trackdir td, bool override_railtype)
+bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
{
typedef bool (*PfnFindNearestSafeTile)(const Vehicle*, TileIndex, Trackdir, bool);
PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;