Skip to content

Commit

Permalink
Fix #10511: Delay 'go to nearest depot' orders
Browse files Browse the repository at this point in the history
Delay the nearest depot order search for a day if the destination has been set to zero, which happens when it has already attempted to do so and failed to find a valid destination.
  • Loading branch information
SamuXarick committed Dec 7, 2023
1 parent 90e386a commit dcf026b
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,34 +1961,37 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool
}

if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
/* We need to search for the nearest depot (hangar). */
ClosestDepot closestDepot = v->FindClosestDepot();

if (closestDepot.found) {
/* PBS reservations cannot reverse */
if (pbs_look_ahead && closestDepot.reverse) return false;

v->SetDestTile(closestDepot.location);
v->current_order.SetDestination(closestDepot.destination);

/* If there is no depot in front, reverse automatically (trains only) */
if (v->type == VEH_TRAIN && closestDepot.reverse) Command<CMD_REVERSE_TRAIN_DIRECTION>::Do(DC_EXEC, v->index, false);

if (v->type == VEH_AIRCRAFT) {
Aircraft *a = Aircraft::From(v);
if (a->state == FLYING && a->targetairport != closestDepot.destination) {
/* The aircraft is now heading for a different hangar than the next in the orders */
AircraftNextAirportPos_and_Order(a);
/* Try to delay the search for a bit if the previous destination is invalid. */
if (v->dest_tile != 0 || TimerGameTick::counter % Ticks::DAY_TICKS == 0) {
/* We need to search for the nearest depot (hangar). */
ClosestDepot closestDepot = v->FindClosestDepot();

if (closestDepot.found) {
/* PBS reservations cannot reverse */
if (pbs_look_ahead && closestDepot.reverse) return false;

v->SetDestTile(closestDepot.location);
v->current_order.SetDestination(closestDepot.destination);

/* If there is no depot in front, reverse automatically (trains only) */
if (v->type == VEH_TRAIN && closestDepot.reverse) Command<CMD_REVERSE_TRAIN_DIRECTION>::Do(DC_EXEC, v->index, false);

if (v->type == VEH_AIRCRAFT) {
Aircraft *a = Aircraft::From(v);
if (a->state == FLYING && a->targetairport != closestDepot.destination) {
/* The aircraft is now heading for a different hangar than the next in the orders */
AircraftNextAirportPos_and_Order(a);
}
}
return true;
}
return true;
}

/* If there is no depot, we cannot help PBS either. */
if (pbs_look_ahead) return false;
/* If there is no depot, we cannot help PBS either. */
if (pbs_look_ahead) return false;

UpdateVehicleTimetable(v, true);
v->IncrementRealOrderIndex();
UpdateVehicleTimetable(v, true);
v->IncrementRealOrderIndex();
}
} else {
if (v->type != VEH_AIRCRAFT) {
v->SetDestTile(Depot::Get(order->GetDestination())->xy);
Expand Down

0 comments on commit dcf026b

Please sign in to comment.