|
|
@ -33,8 +33,7 @@ public:
|
|
|
|
typedef typename Node::Key Key; ///< key to hash tables.
|
|
|
|
typedef typename Node::Key Key; ///< key to hash tables.
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
TileIndex dest_tile;
|
|
|
|
std::span<TileIndex> dest_tiles;
|
|
|
|
TrackdirBits dest_trackdirs;
|
|
|
|
|
|
|
|
StationID dest_station;
|
|
|
|
StationID dest_station;
|
|
|
|
|
|
|
|
|
|
|
|
bool has_intermediate_dest = false;
|
|
|
|
bool has_intermediate_dest = false;
|
|
|
@ -42,16 +41,13 @@ protected:
|
|
|
|
WaterRegionPatchDesc intermediate_dest_region_patch;
|
|
|
|
WaterRegionPatchDesc intermediate_dest_region_patch;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void SetDestination(const Ship *v)
|
|
|
|
void SetDestination(const Ship *v, const std::span<TileIndex> destination_tiles)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
this->dest_tiles = destination_tiles;
|
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION)) {
|
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION)) {
|
|
|
|
this->dest_station = v->current_order.GetDestination().ToStationID();
|
|
|
|
this->dest_station = v->current_order.GetDestination().ToStationID();
|
|
|
|
this->dest_tile = CalcClosestStationTile(this->dest_station, v->tile, StationType::Dock);
|
|
|
|
|
|
|
|
this->dest_trackdirs = INVALID_TRACKDIR_BIT;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this->dest_station = StationID::Invalid();
|
|
|
|
this->dest_station = StationID::Invalid();
|
|
|
|
this->dest_tile = v->dest_tile;
|
|
|
|
|
|
|
|
this->dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_WATER, 0));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -73,11 +69,8 @@ public:
|
|
|
|
/** Called by YAPF to detect if node ends in the desired destination. */
|
|
|
|
/** Called by YAPF to detect if node ends in the desired destination. */
|
|
|
|
inline bool PfDetectDestination(Node &n)
|
|
|
|
inline bool PfDetectDestination(Node &n)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return this->PfDetectDestinationTile(n.segment_last_tile, n.segment_last_td);
|
|
|
|
const TileIndex tile = n.segment_last_tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool PfDetectDestinationTile(TileIndex tile, Trackdir trackdir)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (this->has_intermediate_dest) {
|
|
|
|
if (this->has_intermediate_dest) {
|
|
|
|
/* GetWaterRegionInfo is much faster than GetWaterRegionPatchInfo so we try that first. */
|
|
|
|
/* GetWaterRegionInfo is much faster than GetWaterRegionPatchInfo so we try that first. */
|
|
|
|
if (GetWaterRegionInfo(tile) != this->intermediate_dest_region_patch) return false;
|
|
|
|
if (GetWaterRegionInfo(tile) != this->intermediate_dest_region_patch) return false;
|
|
|
@ -86,23 +79,14 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
if (this->dest_station != StationID::Invalid()) return IsDockingTile(tile) && IsShipDestinationTile(tile, this->dest_station);
|
|
|
|
if (this->dest_station != StationID::Invalid()) return IsDockingTile(tile) && IsShipDestinationTile(tile, this->dest_station);
|
|
|
|
|
|
|
|
|
|
|
|
return tile == this->dest_tile && ((this->dest_trackdirs & TrackdirToTrackdirBits(trackdir)) != TRACKDIR_BIT_NONE);
|
|
|
|
assert(this->dest_tiles.size() == 1);
|
|
|
|
|
|
|
|
return tile == this->dest_tiles.front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
static inline int CalcEstimate(Node &n, TileIndex destination_tile)
|
|
|
|
* 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::estimate.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline bool PfCalcEstimate(Node &n)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const TileIndex destination_tile = this->has_intermediate_dest ? this->intermediate_dest_tile : this->dest_tile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const int dg_dir_to_x_offs[] = { -1, 0, 1, 0 };
|
|
|
|
static const int dg_dir_to_x_offs[] = { -1, 0, 1, 0 };
|
|
|
|
static const int dg_dir_to_y_offs[] = { 0, 1, 0, -1 };
|
|
|
|
static const int dg_dir_to_y_offs[] = { 0, 1, 0, -1 };
|
|
|
|
if (this->PfDetectDestination(n)) {
|
|
|
|
|
|
|
|
n.estimate = n.cost;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TileIndex tile = n.segment_last_tile;
|
|
|
|
TileIndex tile = n.segment_last_tile;
|
|
|
|
DiagDirection exitdir = TrackdirToExitdir(n.segment_last_td);
|
|
|
|
DiagDirection exitdir = TrackdirToExitdir(n.segment_last_td);
|
|
|
@ -115,8 +99,33 @@ public:
|
|
|
|
int dmin = std::min(dx, dy);
|
|
|
|
int dmin = std::min(dx, dy);
|
|
|
|
int dxy = abs(dx - dy);
|
|
|
|
int dxy = abs(dx - dy);
|
|
|
|
int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
|
|
|
|
int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
|
|
|
|
n.estimate = n.cost + d;
|
|
|
|
int estimate = n.cost + d;
|
|
|
|
assert(n.estimate >= n.parent->estimate);
|
|
|
|
assert(estimate >= n.parent->estimate);
|
|
|
|
|
|
|
|
return estimate;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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::estimate.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline bool PfCalcEstimate(Node &n)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (this->PfDetectDestination(n)) {
|
|
|
|
|
|
|
|
n.estimate = n.cost;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int shortest_estimate = std::numeric_limits<int>::max();
|
|
|
|
|
|
|
|
if (this->has_intermediate_dest) {
|
|
|
|
|
|
|
|
shortest_estimate = this->CalcEstimate(n, this->intermediate_dest_tile);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (const TileIndex &destination_tile : this->dest_tiles) {
|
|
|
|
|
|
|
|
int estimate = this->CalcEstimate(n, destination_tile);
|
|
|
|
|
|
|
|
if (estimate < shortest_estimate) shortest_estimate = estimate;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n.estimate = shortest_estimate;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -211,10 +220,10 @@ public:
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, TrackdirBits forward_dirs, TrackdirBits reverse_dirs,
|
|
|
|
static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, TrackdirBits forward_dirs, TrackdirBits reverse_dirs, const std::span<TileIndex> dest_tiles,
|
|
|
|
bool &path_found, ShipPathCache &path_cache, Trackdir &best_origin_dir)
|
|
|
|
bool &path_found, ShipPathCache &path_cache, Trackdir &best_origin_dir)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1);
|
|
|
|
const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1, dest_tiles);
|
|
|
|
if (high_level_path.empty()) {
|
|
|
|
if (high_level_path.empty()) {
|
|
|
|
path_found = false;
|
|
|
|
path_found = false;
|
|
|
|
/* Make the ship move around aimlessly. This prevents repeated pathfinder calls and clearly indicates that the ship is lost. */
|
|
|
|
/* Make the ship move around aimlessly. This prevents repeated pathfinder calls and clearly indicates that the ship is lost. */
|
|
|
@ -229,7 +238,7 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
/* Set origin and destination nodes */
|
|
|
|
/* Set origin and destination nodes */
|
|
|
|
pf.SetOrigin(v->tile, forward_dirs | reverse_dirs);
|
|
|
|
pf.SetOrigin(v->tile, forward_dirs | reverse_dirs);
|
|
|
|
pf.SetDestination(v);
|
|
|
|
pf.SetDestination(v, dest_tiles);
|
|
|
|
const bool is_intermediate_destination = static_cast<int>(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1;
|
|
|
|
const bool is_intermediate_destination = static_cast<int>(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1;
|
|
|
|
if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back());
|
|
|
|
if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back());
|
|
|
|
|
|
|
|
|
|
|
@ -297,9 +306,10 @@ public:
|
|
|
|
* Called when leaving depot.
|
|
|
|
* Called when leaving depot.
|
|
|
|
* @param v Ship.
|
|
|
|
* @param v Ship.
|
|
|
|
* @param trackdir [out] the best of all possible reversed trackdirs.
|
|
|
|
* @param trackdir [out] the best of all possible reversed trackdirs.
|
|
|
|
|
|
|
|
* @param dest_tiles list of destination tiles.
|
|
|
|
* @return true if the reverse direction is better.
|
|
|
|
* @return true if the reverse direction is better.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static bool CheckShipReverse(const Ship *v, Trackdir *trackdir)
|
|
|
|
static bool CheckShipReverse(const Ship *v, Trackdir *trackdir, const std::span<TileIndex> dest_tiles)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool path_found = false;
|
|
|
|
bool path_found = false;
|
|
|
|
ShipPathCache dummy_cache;
|
|
|
|
ShipPathCache dummy_cache;
|
|
|
@ -310,13 +320,13 @@ public:
|
|
|
|
const Trackdir reverse_dir = ReverseTrackdir(v->GetVehicleTrackdir());
|
|
|
|
const Trackdir reverse_dir = ReverseTrackdir(v->GetVehicleTrackdir());
|
|
|
|
const TrackdirBits forward_dirs = TrackdirToTrackdirBits(v->GetVehicleTrackdir());
|
|
|
|
const TrackdirBits forward_dirs = TrackdirToTrackdirBits(v->GetVehicleTrackdir());
|
|
|
|
const TrackdirBits reverse_dirs = TrackdirToTrackdirBits(reverse_dir);
|
|
|
|
const TrackdirBits reverse_dirs = TrackdirToTrackdirBits(reverse_dir);
|
|
|
|
(void)ChooseShipTrack(v, v->tile, forward_dirs, reverse_dirs, path_found, dummy_cache, best_origin_dir);
|
|
|
|
(void)ChooseShipTrack(v, v->tile, forward_dirs, reverse_dirs, dest_tiles, path_found, dummy_cache, best_origin_dir);
|
|
|
|
return path_found && best_origin_dir == reverse_dir;
|
|
|
|
return path_found && best_origin_dir == reverse_dir;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
/* This gets called when a ship suddenly can't move forward, e.g. due to terraforming. */
|
|
|
|
/* This gets called when a ship suddenly can't move forward, e.g. due to terraforming. */
|
|
|
|
const DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state));
|
|
|
|
const DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state));
|
|
|
|
const TrackdirBits reverse_dirs = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(v->tile, TRANSPORT_WATER, 0, entry));
|
|
|
|
const TrackdirBits reverse_dirs = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(v->tile, TRANSPORT_WATER, 0, entry));
|
|
|
|
(void)ChooseShipTrack(v, v->tile, TRACKDIR_BIT_NONE, reverse_dirs, path_found, dummy_cache, best_origin_dir);
|
|
|
|
(void)ChooseShipTrack(v, v->tile, TRACKDIR_BIT_NONE, reverse_dirs, dest_tiles, path_found, dummy_cache, best_origin_dir);
|
|
|
|
*trackdir = path_found && best_origin_dir != INVALID_TRACKDIR ? best_origin_dir : GetRandomTrackdir(reverse_dirs);
|
|
|
|
*trackdir = path_found && best_origin_dir != INVALID_TRACKDIR ? best_origin_dir : GetRandomTrackdir(reverse_dirs);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -420,13 +430,15 @@ struct CYapfShip : CYapfT<CYapfShip_TypesT<CYapfShip, CFollowTrackWater, CShipNo
|
|
|
|
/** Ship controller helper - path finder invoker. */
|
|
|
|
/** Ship controller helper - path finder invoker. */
|
|
|
|
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, bool &path_found, ShipPathCache &path_cache)
|
|
|
|
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, bool &path_found, ShipPathCache &path_cache)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<TileIndex> dest_tiles = GetShipDestinationTiles(v);
|
|
|
|
Trackdir best_origin_dir = INVALID_TRACKDIR;
|
|
|
|
Trackdir best_origin_dir = INVALID_TRACKDIR;
|
|
|
|
const TrackdirBits origin_dirs = TrackdirToTrackdirBits(v->GetVehicleTrackdir());
|
|
|
|
const TrackdirBits origin_dirs = TrackdirToTrackdirBits(v->GetVehicleTrackdir());
|
|
|
|
const Trackdir td_ret = CYapfShip::ChooseShipTrack(v, tile, origin_dirs, TRACKDIR_BIT_NONE, path_found, path_cache, best_origin_dir);
|
|
|
|
const Trackdir td_ret = CYapfShip::ChooseShipTrack(v, tile, origin_dirs, TRACKDIR_BIT_NONE, dest_tiles, path_found, path_cache, best_origin_dir);
|
|
|
|
return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
|
|
|
|
return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir)
|
|
|
|
bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return CYapfShip::CheckShipReverse(v, trackdir);
|
|
|
|
std::vector<TileIndex> dest_tiles = GetShipDestinationTiles(v);
|
|
|
|
|
|
|
|
return CYapfShip::CheckShipReverse(v, trackdir, dest_tiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|