Loading

Paste #pungzsg9l

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

Comments