Skip to content

Instantly share code, notes, and snippets.

@SamuXarick
Created March 2, 2024 15:50
Show Gist options
  • Save SamuXarick/31d62faf40efeff8aee1e2d96126b56f to your computer and use it in GitHub Desktop.
Save SamuXarick/31d62faf40efeff8aee1e2d96126b56f to your computer and use it in GitHub Desktop.
/** Original code: https://github.com/OpenTTD/OpenTTD/blob/master/src/pathfinder/yapf/yapf_ship.cpp#L97-L123 */
/**
* 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.
*/
inline bool PfCalcEstimate(Node &n)
{
if (PfDetectDestination(n)) {
n.m_estimate = n.m_cost;
return true;
}
const TileIndex destination_tile = m_has_intermediate_dest ? m_intermediate_dest_tile : m_destTile;
extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
static const int YAPF_HALF_TILE_LENGTH = YAPF_TILE_LENGTH >> 1;
TileIndex tile = n.m_segment_last_tile;
DiagDirection exitdir = TrackdirToExitdir(n.m_segment_last_td);
int dx = std::abs(((int)TileX(tile) << 1) + _tileoffs_by_diagdir[exitdir].x - ((int)TileX(destination_tile) << 1));
int dy = std::abs(((int)TileY(tile) << 1) + _tileoffs_by_diagdir[exitdir].y - ((int)TileY(destination_tile) << 1));
n.m_estimate = n.m_cost + std::min(dx, dy) * YAPF_TILE_CORNER_LENGTH + (std::abs(dx - dy) - 1) * YAPF_HALF_TILE_LENGTH;
assert(n.m_estimate >= n.m_parent->m_estimate);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment