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->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 coupled 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 (ost->airport.HasHangar() ||
/* Helicopters can be serviced at helipads as long as there is no pending replace and serviceathelipad is enabled */
(v->subtype == AIR_HELICOPTER && !v->HasPendingReplace() && _settings_game.order.serviceathelipad && ost->airport.GetFTA()->num_helipads)) {
hangar_in_o = true;
break;
}
}
}
if (!hangar_in_o) {
/* there's no airport coupled with a hangar in the orders */
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 &&
/* 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);
}
}
}
}