From d6eff806f0a84a0f80656a4f58d9391b9833b79b Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 16 Oct 2025 21:54:43 +0100 Subject: [PATCH] Codechange: Use single vector for next station order. Pass vector to insert into to avoid handling multiple vectors. This may change the order of returned stations. --- src/aircraft_cmd.cpp | 3 ++- src/economy.cpp | 6 ++++-- src/order_base.h | 2 +- src/order_cmd.cpp | 15 +++++++-------- src/vehicle_base.h | 5 +++-- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index dce6e04f57..94d976e112 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -135,7 +135,8 @@ static StationID FindNearestHangar(const Aircraft *v) next_dest = Station::GetIfValid(v->current_order.GetDestination().ToStationID()); } else { last_dest = GetTargetAirportIfValid(v); - std::vector next_station = v->GetNextStoppingStation(); + std::vector next_station; + v->GetNextStoppingStation(next_station); if (!next_station.empty()) next_dest = Station::GetIfValid(next_station.back()); } } diff --git a/src/economy.cpp b/src/economy.cpp index 8001d3afd8..d61372de7e 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1270,7 +1270,8 @@ void PrepareUnload(Vehicle *front_v) assert(CargoPayment::CanAllocateItem()); front_v->cargo_payment = new CargoPayment(front_v); - std::vector next_station = front_v->GetNextStoppingStation(); + std::vector next_station; + front_v->GetNextStoppingStation(next_station); if (front_v->orders == nullptr || (front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) { Station *st = Station::Get(front_v->last_station_visited); for (Vehicle *v = front_v; v != nullptr; v = v->Next()) { @@ -1618,7 +1619,8 @@ static void LoadUnloadVehicle(Vehicle *front) StationID last_visited = front->last_station_visited; Station *st = Station::Get(last_visited); - std::vector next_station = front->GetNextStoppingStation(); + std::vector next_station; + front->GetNextStoppingStation(next_station); bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CARGO_AUTO_REFIT; CargoArray consist_capleft{}; if (_settings_game.order.improved_load && use_autorefit ? diff --git a/src/order_base.h b/src/order_base.h index 5a2511bae0..5dac764838 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -367,7 +367,7 @@ public: */ inline VehicleOrderID GetNumManualOrders() const { return this->num_manual_orders; } - std::vector GetNextStoppingStation(const Vehicle *v, VehicleOrderID first = INVALID_VEH_ORDER_ID, uint hops = 0) const; + void GetNextStoppingStation(std::vector &next_station, const Vehicle *v, VehicleOrderID first = INVALID_VEH_ORDER_ID, uint hops = 0) const; VehicleOrderID GetNextDecisionNode(VehicleOrderID next, uint hops) const; void InsertOrderAt(Order &&order, VehicleOrderID index); diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 32df9ffd30..8754c96874 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -361,14 +361,14 @@ VehicleOrderID OrderList::GetNextDecisionNode(VehicleOrderID next, uint hops) co * @pre The vehicle is currently loading and v->last_station_visited is meaningful. * @note This function may draw a random number. Don't use it from the GUI. */ -std::vector OrderList::GetNextStoppingStation(const Vehicle *v, VehicleOrderID first, uint hops) const +void OrderList::GetNextStoppingStation(std::vector &next_station, const Vehicle *v, VehicleOrderID first, uint hops) const { VehicleOrderID next = first; if (first == INVALID_VEH_ORDER_ID) { next = v->cur_implicit_order_index; if (next >= this->GetNumOrders()) { next = this->GetFirstOrder(); - if (next == INVALID_VEH_ORDER_ID) return {}; + if (next == INVALID_VEH_ORDER_ID) return; } else { /* GetNext never returns INVALID_VEH_ORDER_ID if there is a valid station in the list. * As the given "next" is already valid and a station in the list, we @@ -392,10 +392,9 @@ std::vector OrderList::GetNextStoppingStation(const Vehicle *v, Vehic } else if (skip_to == INVALID_VEH_ORDER_ID || skip_to == first) { next = (advance == first) ? INVALID_VEH_ORDER_ID : advance; } else { - std::vector st1 = this->GetNextStoppingStation(v, skip_to, hops); - std::vector st2 = this->GetNextStoppingStation(v, advance, hops); - std::copy(st2.rbegin(), st2.rend(), std::back_inserter(st1)); - return st1; + this->GetNextStoppingStation(next_station, v, skip_to, hops); + this->GetNextStoppingStation(next_station, v, advance, hops); + return; } ++hops; } @@ -404,11 +403,11 @@ std::vector OrderList::GetNextStoppingStation(const Vehicle *v, Vehic if (next == INVALID_VEH_ORDER_ID || ((orders[next].IsType(OT_GOTO_STATION) || orders[next].IsType(OT_IMPLICIT)) && orders[next].GetDestination() == v->last_station_visited && (orders[next].GetUnloadType() & (OUFB_TRANSFER | OUFB_UNLOAD)) != 0)) { - return {}; + return; } } while (orders[next].IsType(OT_GOTO_DEPOT) || orders[next].GetDestination() == v->last_station_visited); - return {orders[next].GetDestination().ToStationID()}; + next_station.push_back(orders[next].GetDestination().ToStationID()); } /** diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 642669ae8e..6e9cc5e20f 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -714,9 +714,10 @@ public: * Get the next station the vehicle will stop at. * @return ID of the next station the vehicle will stop at or StationID::Invalid(). */ - inline std::vector GetNextStoppingStation() const + inline void GetNextStoppingStation(std::vector &next_station) const { - return (this->orders == nullptr) ? std::vector{} : this->orders->GetNextStoppingStation(this); + if (this->orders == nullptr) return; + this->orders->GetNextStoppingStation(next_station, this); } void ResetRefitCaps();