Loading

Paste #phdne9rqy

  1. /**
  2.  * Find the nearest hangar to v
  3.  * INVALID_STATION is returned, if the company does not have any suitable
  4.  * airports (like helipads only)
  5.  * @param v vehicle looking for a hangar
  6.  * @return the StationID if one is found, otherwise, INVALID_STATION
  7.  */
  8. static StationID FindNearestHangar(const Aircraft *v)
  9. {
  10.     const Station *st;
  11.     uint best = 0;
  12.     StationID index = INVALID_STATION;
  13.     TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
  14.     const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
  15.  
  16.     FOR_ALL_STATIONS(st) {
  17.         if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT) || !st->airport.HasHangar()) continue;
  18.  
  19.         const AirportFTAClass *afc = st->airport.GetFTA();
  20.  
  21.         /* don't crash the plane if we know it can't land at the airport */
  22.         if ((afc->flags & AirportFTAClass::SHORT_STRIP) && (avi->subtype & AIR_FAST) &&
  23.                 (!_cheats.no_jetcrash.value || !_settings_game.vehicle.large_plane_on_short_runway)) continue;
  24.  
  25.         /* the plane won't land at any helicopter station */
  26.         if (!(afc->flags & AirportFTAClass::AIRPLANES) && (avi->subtype & AIR_CTOL)) continue;
  27.  
  28.         /* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
  29.         uint distance = DistanceSquare(vtile, st->airport.tile);
  30.         uint max_range = v->acache.cached_max_range_sqr;
  31.         if (max_range != 0) {
  32.             /* Determine destinations */
  33.             const Station *cur_dest = GetTargetAirportIfValid(v);
  34.             const Station *next_dest;
  35.             if (v->current_order.IsType(OT_GOTO_STATION) ||
  36.                     v->current_order.IsType(OT_GOTO_DEPOT) && v->current_order.GetDepotActionType() != ODATFB_NEAREST_DEPOT) {
  37.                 next_dest = Station::GetIfValid(v->current_order.GetDestination());
  38.                 if (next_dest == cur_dest) next_dest = Station::GetIfValid(v->last_station_visited);
  39.             } else {
  40.                 next_dest = Station::GetIfValid(v->GetNextStoppingStation().value);
  41.             }
  42.  
  43.             /* Check if our current and next (or previous) destinations can be reached from the depot airport. */
  44.             uint cur_dist = cur_dest != NULL ? DistanceSquare(st->airport.tile, cur_dest->airport.tile) : UINT_MAX;
  45.             uint next_dist = next_dest != NULL || next_dest->airport.tile != INVALID_TILE ? DistanceSquare(st->airport.tile, next_dest->airport.tile) : UINT_MAX;
  46.             if (cur_dist != UINT_MAX && next_dist != UINT_MAX && (cur_dist > max_range || next_dist > max_range)) continue;
  47.             if (cur_dist != UINT_MAX && next_dist == UINT_MAX && cur_dist > max_range) continue;
  48.             if (cur_dist == UINT_MAX && next_dist != UINT_MAX && next_dist > max_range) continue;
  49.         }
  50.         if (distance < best || index == INVALID_STATION) {
  51.             best = distance;
  52.             index = st->index;
  53.         }
  54.     }
  55.     return index;
  56. }

Version history

Revision # Author Created at
p13ckykxj Anonymous 15 Nov 2017, 15:21:52 UTC Diff

Comments