static void CheckIfAircraftNeedsService(Aircraft *v)
{
if (Company::Get(v->owner)->settings.vehicle.servint_aircraft == 0 || !v->NeedsAutomaticServicing()) return;
if (v->IsChainInDepot()) {
VehicleServiceInDepot(v);
return;
}
/* When we're parsing conditional orders and the like
* we don't want to consider going to a depot too. */
if (!v->current_order.IsType(OT_GOTO_DEPOT) && !v->current_order.IsType(OT_GOTO_STATION)) return;
const Station *st = Station::Get(v->current_order.GetDestination());
assert(st != NULL);
/* only goto depot if the target airport has a depot */
if (st->airport.HasHangar() && (CanVehicleUseStation(v, st) || !(v->state >= TAKEOFF && v->state <= FLYING))) {
v->current_order.MakeGoToDepot(st->index, ODTFB_SERVICE);
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
} else if (v->current_order.IsType(OT_GOTO_DEPOT)) {
v->current_order.MakeDummy();
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
} else {
bool hangar_in_o = false;
const Order *o;
/* Is there at least an airport with a hangar in the orders? */
FOR_VEHICLE_ORDERS(v, o) {
if (o->IsType(OT_GOTO_STATION)) {
const Station *ost = Station::Get(o->GetDestination());
if (CanVehicleUseStation(v, ost) && ost->airport.HasHangar()) {
hangar_in_o = true;
break;
}
}
}
if (!hangar_in_o || !CanVehicleUseStation(v, st) && v->state >= TAKEOFF && v->state <= FLYING) {
/* there's no airport with a hangar in the orders, or the aircraft
* can't use the airport, so look for a nearby hangar */
const StationID nearest_hangar = FindNearestHangar(v);
/* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
if (nearest_hangar != INVALID_STATION && (!CanVehicleUseStation(v, st) && v->state >= TAKEOFF && v->state <= FLYING || !st->airport.HasHangar() &&
/* is nearest hangar closer than destination? */
DistanceSquare(vtile, Station::Get(nearest_hangar)->airport.tile) <= DistanceSquare(vtile, st->airport.tile))) {
/* defer destination, service aircraft at that hangar now */
v->current_order.MakeGoToDepot(nearest_hangar, ODTFB_SERVICE);
v->dest_tile = v->GetOrderStationLocation(nearest_hangar);
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
}
}
}
}