Loading

Paste #plmhkfy3k

  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) || !(v->state >= TAKEOFF && v->state <= FLYING))) {
  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 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 (CanVehicleUseStation(v, ost) && ost->airport.HasHangar()) {
  33.                     hangar_in_o = true;
  34.                     break;
  35.                 }
  36.             }
  37.         }
  38.  
  39.         if (!hangar_in_o || !CanVehicleUseStation(v, st) && v->state >= TAKEOFF && v->state <= FLYING) {
  40.             /* there's no airport with a hangar in the orders, or the aircraft
  41.              * can't use the airport, so look for a nearby hangar */
  42.             const StationID nearest_hangar = FindNearestHangar(v);
  43.  
  44.             /* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
  45.             TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
  46.  
  47.             if (nearest_hangar != INVALID_STATION && (!CanVehicleUseStation(v, st) && v->state >= TAKEOFF && v->state <= FLYING || !st->airport.HasHangar() &&
  48.                     /* is nearest hangar closer than destination? */
  49.                     DistanceSquare(vtile, Station::Get(nearest_hangar)->airport.tile) <= DistanceSquare(vtile, st->airport.tile))) {
  50.                 /* defer destination, service aircraft at that hangar now */
  51.                 v->current_order.MakeGoToDepot(nearest_hangar, ODTFB_SERVICE);
  52.                 v->dest_tile = v->GetOrderStationLocation(nearest_hangar);
  53.                 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
  54.             }
  55.         }
  56.     }
  57. }

Version history

Revision # Author Created at
pb2hvgyfg Anonymous 08 Nov 2017, 15:17:03 UTC Diff

Comments