FindDepotData OPFShipFindNearestDepot(const Ship *v, int max_distance) // todo
{
Trackdir trackdir = v->GetVehicleTrackdir();
DiagDirection enterdir = TrackdirToExitdir(trackdir);
TrackBits tracks = TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(enterdir));
FindDepotData fdd;
Depot *depot;
uint best_dist = max_distance == 0 ? UINT_MAX : max_distance;
FOR_ALL_DEPOTS(depot) {
TileIndex depottile = depot->xy;
if (IsShipDepotTile(depottile) && IsTileOwner(depottile, v->owner)) {
uint dist = DistanceManhattan(v->tile, depottile);
if (dist <= best_dist) {
bool path_found;
Track track = OPFShipChooseTrack(v, v->tile, enterdir, tracks, path_found, depottile);
if (track != INVALID_TRACK) {
fdd.best_length = dist;
fdd.tile = depottile;
break;
}
}
}
}
return fdd;
}