static void CheckIfAircraftNeedsService(Aircraft *v)
{
bool needs_automatic_servicing = v->NeedsAutomaticServicing();
bool has_pending_replace = v->HasPendingReplace();
if (Company::Get(v->owner)->settings.vehicle.servint_aircraft == 0 || (!needs_automatic_servicing &&
(!has_pending_replace || v->subtype != AIR_HELICOPTER || !_settings_game.order.serviceathelipad))) 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 && !has_pending_replace && _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, 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 && (has_pending_replace && v->subtype == AIR_HELICOPTER && _settings_game.order.serviceathelipad && !needs_automatic_servicing ||
/* 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->SetDestTile(v->GetOrderStationLocation(nearest_hangar));
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
}
}
}
}