Loading

Paste #p4dec4avi

  1. static void CheckIfAircraftNeedsService(Aircraft *v)
  2. {
  3.     if (Company::Get(v->owner)->settings.vehicle.servint_aircraft == 0 || !v->NeedsAutomaticServicing()) return;
  4.     if (v->IsChainInDepot()) {
  5.         VehicleServiceInDepot(v);
  6.         return;
  7.     }
  8.  
  9.     /* When we're parsing conditional orders and the like
  10.      * we don't want to consider going to a depot too. */
  11.     if (!v->current_order.IsType(OT_GOTO_DEPOT) && !v->current_order.IsType(OT_GOTO_STATION)) return;
  12.  
  13.     const Station *st = Station::Get(v->current_order.GetDestination());
  14.  
  15.     assert(st != NULL);
  16.  
  17.     /* only goto depot if the target airport has a depot */
  18.     if (st->airport.HasHangar() && CanVehicleUseStation(v, st)) {
  19.         v->current_order.MakeGoToDepot(st->index, ODTFB_SERVICE);
  20.         SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
  21.     } else if (v->current_order.IsType(OT_GOTO_DEPOT)) {
  22.         v->current_order.MakeDummy();
  23.         SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
  24.     } else {
  25.         bool hangar_in_o = false;
  26.         const Order *o;
  27.  
  28.         /* Is there at least an airport coupled with a hangar in the orders? */
  29.         FOR_VEHICLE_ORDERS(v, o) {
  30.             if (o->IsType(OT_GOTO_STATION)) {
  31.                 const Station *ost = Station::Get(o->GetDestination());
  32.                 if (ost->airport.HasHangar() ||
  33.                         /* Helicopters can be serviced at helipads as long as there is no pending replace and serviceathelipad is enabled */
  34.                         (v->subtype == AIR_HELICOPTER && !v->HasPendingReplace() && _settings_game.order.serviceathelipad && ost->airport.GetFTA()->num_helipads)) {
  35.                     hangar_in_o = true;
  36.                     break;
  37.                 }
  38.             }
  39.         }
  40.  
  41.         if (!hangar_in_o) {
  42.             /* there's no airport coupled with a hangar in the orders */
  43.             const StationID nearest_hangar = FindNearestHangar(v);
  44.  
  45.             /* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
  46.             TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
  47.  
  48.             if (nearest_hangar != INVALID_STATION &&
  49.                     /* is nearest hangar closer than destination? */
  50.                     DistanceSquare(vtile, Station::Get(nearest_hangar)->airport.tile) <= DistanceSquare(vtile, st->airport.tile)) {
  51.                 /* defer destination, service aircraft at that hangar now */
  52.                 v->current_order.MakeGoToDepot(nearest_hangar, ODTFB_SERVICE);
  53.                 v->dest_tile = v->GetOrderStationLocation(nearest_hangar);
  54.                 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
  55.             }
  56.         }
  57.     }
  58. }

Comments