Skip to content

Commit

Permalink
Fix OpenTTD#5405: Aircraft could route to depots outside their range (O…
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuXarick authored and douiwby committed Apr 16, 2020
1 parent ce01213 commit 38e1f82
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/aircraft_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ static StationID FindNearestHangar(const Aircraft *v)
StationID index = INVALID_STATION;
TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
uint max_range = v->acache.cached_max_range_sqr;

/* Determine destinations where it's coming from and where it's heading to */
const Station *last_dest;
const Station *next_dest;
if (max_range != 0) {
if (v->current_order.IsType(OT_GOTO_STATION) ||
(v->current_order.IsType(OT_GOTO_DEPOT) && v->current_order.GetDepotActionType() != ODATFB_NEAREST_DEPOT)) {
last_dest = Station::GetIfValid(v->last_station_visited);
next_dest = Station::GetIfValid(v->current_order.GetDestination());
} else {
last_dest = GetTargetAirportIfValid(v);
next_dest = Station::GetIfValid(v->GetNextStoppingStation().value);
}
}

FOR_ALL_STATIONS(st) {
if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT) || !st->airport.HasHangar()) continue;
Expand All @@ -138,13 +153,15 @@ static StationID FindNearestHangar(const Aircraft *v)
/* the plane won't land at any helicopter station */
if (!(afc->flags & AirportFTAClass::AIRPLANES) && (avi->subtype & AIR_CTOL)) continue;

/* Check if our last and next destinations can be reached from the depot airport. */
if (max_range != 0) {
uint last_dist = last_dest != nullptr && last_dest->airport.tile != INVALID_TILE ? DistanceSquare(st->airport.tile, last_dest->airport.tile) : 0;
uint next_dist = next_dest != nullptr && next_dest->airport.tile != INVALID_TILE ? DistanceSquare(st->airport.tile, next_dest->airport.tile) : 0;
if (last_dist > max_range || next_dist > max_range) continue;
}

/* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
uint distance = DistanceSquare(vtile, st->airport.tile);
if (v->acache.cached_max_range_sqr != 0) {
/* Check if our current destination can be reached from the depot airport. */
const Station *cur_dest = GetTargetAirportIfValid(v);
if (cur_dest != nullptr && DistanceSquare(st->airport.tile, cur_dest->airport.tile) > v->acache.cached_max_range_sqr) continue;
}
if (distance < best || index == INVALID_STATION) {
best = distance;
index = st->index;
Expand Down

0 comments on commit 38e1f82

Please sign in to comment.