Skip to content

Commit

Permalink
Codechange: Use FindVehiclesWithOrder when removing a road stop. (#12144
Browse files Browse the repository at this point in the history
)
  • Loading branch information
PeterN committed Mar 28, 2024
1 parent 907cb4f commit 8746be8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/station_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "stdafx.h"
#include "aircraft.h"
#include "bridge_map.h"
#include "vehiclelist_func.h"
#include "viewport_func.h"
#include "viewport_kdtree.h"
#include "command_func.h"
Expand Down Expand Up @@ -2174,13 +2175,18 @@ static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags, int repla

delete cur_stop;

/* Make sure no vehicle is going to the old roadstop */
for (RoadVehicle *v : RoadVehicle::Iterate()) {
if (v->First() == v && v->current_order.IsType(OT_GOTO_STATION) &&
v->dest_tile == tile) {
v->SetDestTile(v->GetOrderStationLocation(st->index));
/* Make sure no vehicle is going to the old roadstop. Narrow the search to any road vehicles with an order to
* this station, then look for any currently heading to the tile. */
StationID station_id = st->index;
FindVehiclesWithOrder(
[](const Vehicle *v) { return v->type == VEH_ROAD; },
[station_id](const Order *order) { return order->IsType(OT_GOTO_STATION) && order->GetDestination() == station_id; },
[station_id, tile](Vehicle *v) {
if (v->dest_tile == tile) {
v->SetDestTile(v->GetOrderStationLocation(station_id));
}
}
}
);

st->rect.AfterRemoveTile(st, tile);

Expand Down
2 changes: 1 addition & 1 deletion src/vehiclelist_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, V
for (const OrderList *orderlist : OrderList::Iterate()) {

/* We assume all vehicles sharing an order list match the condition. */
const Vehicle *v = orderlist->GetFirstSharedVehicle();
Vehicle *v = orderlist->GetFirstSharedVehicle();
if (!veh_pred(v)) continue;

/* Vehicle is a candidate, search for a matching order. */
Expand Down

0 comments on commit 8746be8

Please sign in to comment.