Skip to content

Commit

Permalink
Change OpenTTD#8001: Don't add docking tile cost when ships are still…
Browse files Browse the repository at this point in the history
… too far from their destination
  • Loading branch information
SamuXarick committed Sep 1, 2021
1 parent 63116bd commit 699f684
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/pathfinder/npf/npf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,15 @@ static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *par
}

if (IsDockingTile(current->tile)) {
/* Check docking tile for occupancy */
uint count = 0;
HasVehicleOnPos(current->tile, &count, &CountShipProc);
cost += count * 3 * _trackdir_length[trackdir];
NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
if (IsShipDestinationTile(current->tile, fstd->station_index)) {
/* Check docking tile for occupancy */
uint count = 0;
if (DistanceManhattan(fstd->dest_coords, fstd->v->tile) < NPF_SHIP_DOCKING_TILE_DESTINATION_LIMIT) {
HasVehicleOnPos(current->tile, &count, &CountShipProc);
}
cost += count * 3 * _trackdir_length[trackdir];
}
}

/* @todo More penalties? */
Expand Down
6 changes: 6 additions & 0 deletions src/pathfinder/pathfinder_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ static const int NPF_TILE_LENGTH = 100;
*/
static const int NPF_INFINITE_PENALTY = 1000 * NPF_TILE_LENGTH;

/** Distance from destination dock to account for docking tile occupancy penalty */
static const int NPF_SHIP_DOCKING_TILE_DESTINATION_LIMIT = 16;


/** Length (penalty) of one tile with YAPF */
static const int YAPF_TILE_LENGTH = 100;
Expand All @@ -41,6 +44,9 @@ static const int YAPF_INFINITE_PENALTY = 1000 * YAPF_TILE_LENGTH;
/** Maximum length of ship path cache */
static const int YAPF_SHIP_PATH_CACHE_LENGTH = 32;

/** Distance from destination dock to account for docking tile occupancy penalty */
static const int YAPF_SHIP_DOCKING_TILE_DESTINATION_LIMIT = 16;

/** Maximum segments of road vehicle path cache */
static const int YAPF_ROADVEH_PATH_CACHE_SEGMENTS = 8;

Expand Down
18 changes: 16 additions & 2 deletions src/pathfinder/yapf/yapf_ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CYapfDestinationTileWaterT
TileIndex m_destTile;
TrackdirBits m_destTrackdirs;
StationID m_destStation;
uint m_destDistance; ///< Manhattan distance

public:
void SetDestination(const Ship *v)
Expand All @@ -43,6 +44,7 @@ class CYapfDestinationTileWaterT
m_destTile = v->dest_tile;
m_destTrackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_WATER, 0));
}
m_destDistance = DistanceManhattan(m_destTile, v->tile);
}

protected:
Expand All @@ -68,6 +70,16 @@ class CYapfDestinationTileWaterT
return tile == m_destTile && ((m_destTrackdirs & TrackdirToTrackdirBits(trackdir)) != TRACKDIR_BIT_NONE);
}

inline StationID GetDestinationStation()
{
return m_destStation;
}

inline uint GetDestinationDistanceManhattan()
{
return m_destDistance;
}

/**
* 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::m_estimate
Expand Down Expand Up @@ -285,10 +297,12 @@ class CYapfCostShipT
/* additional penalty for curves */
c += CurveCost(n.m_parent->GetTrackdir(), n.GetTrackdir());

if (IsDockingTile(n.GetTile())) {
if (IsDockingTile(n.GetTile()) && IsShipDestinationTile(n.GetTile(), Yapf().GetDestinationStation())) {
/* Check docking tile for occupancy */
uint count = 0;
HasVehicleOnPos(n.GetTile(), &count, &CountShipProc);
if (Yapf().GetDestinationDistanceManhattan() < YAPF_SHIP_DOCKING_TILE_DESTINATION_LIMIT) {
HasVehicleOnPos(n.GetTile(), &count, &CountShipProc);
}
c += count * 3 * YAPF_TILE_LENGTH;
}

Expand Down
1 change: 1 addition & 0 deletions src/ship_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ static bool ShipMoveUpDownOnLock(Ship *v)
bool IsShipDestinationTile(TileIndex tile, StationID station)
{
assert(IsDockingTile(tile));
if (!Station::IsValidID(station)) return false;
/* Check each tile adjacent to docking tile. */
for (DiagDirection d = DIAGDIR_BEGIN; d != DIAGDIR_END; d++) {
TileIndex t = tile + TileOffsByDiagDir(d);
Expand Down

0 comments on commit 699f684

Please sign in to comment.