diff options
author | SamuXarick <43006711+SamuXarick@users.noreply.github.com> | 2019-11-02 11:26:04 +0000 |
---|---|---|
committer | Charles Pigott <charlespigott@googlemail.com> | 2019-11-02 11:26:04 +0000 |
commit | ccb4c3797fe5a98e9c40eb2c4e3da004c449c301 (patch) | |
tree | 1cbd01dcd41206861668e9f168433e8ed485c56c | |
parent | 4884dcacbae72b265e7bfab180b923a48d65913b (diff) | |
download | openttd-ccb4c3797fe5a98e9c40eb2c4e3da004c449c301.tar.xz |
Fix #5405: Aircraft could route to depots outside their range (#7104)
-rw-r--r-- | src/aircraft_cmd.cpp | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index cf176668b..c3347c528 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -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; @@ -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; |