From e937c4dcfd14cef270d70a0836fbc15f19676618 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 2 Feb 2025 21:48:07 +0100 Subject: [PATCH] Codechange: change DestinationID into class with conversion helpers A DestinationID is either a DepotID or StationID, where the aircraft hangar being conceptually a depot is actually a StationID. When making those types stronger, a lot of casts would need to be added, but this shows the intent much better. --- src/aircraft_cmd.cpp | 8 ++--- src/industry_cmd.cpp | 2 +- src/linkgraph/refresh.cpp | 4 +-- src/newgrf_engine.cpp | 2 +- src/order_base.h | 10 +++--- src/order_cmd.cpp | 42 +++++++++++------------ src/order_cmd.h | 4 +-- src/order_gui.cpp | 2 +- src/order_type.h | 23 ++++++++++++- src/pathfinder/yapf/yapf_costrail.hpp | 2 +- src/pathfinder/yapf/yapf_destrail.hpp | 6 ++-- src/pathfinder/yapf/yapf_road.cpp | 4 +-- src/pathfinder/yapf/yapf_ship.cpp | 2 +- src/pathfinder/yapf/yapf_ship_regions.cpp | 2 +- src/saveload/oldloader_sl.cpp | 2 +- src/saveload/station_sl.cpp | 4 +-- src/script/api/script_order.cpp | 8 ++--- src/script/api/script_stationlist.cpp | 2 +- src/script/api/script_waypointlist.cpp | 2 +- src/ship_cmd.cpp | 2 +- src/train_cmd.cpp | 2 +- src/vehicle.cpp | 2 +- src/vehicle_base.h | 11 +++--- 23 files changed, 84 insertions(+), 64 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index ab1547b429..9d630d3dc0 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -136,7 +136,7 @@ static StationID FindNearestHangar(const Aircraft *v) if (v->current_order.IsType(OT_GOTO_STATION) || (v->current_order.IsType(OT_GOTO_DEPOT) && (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0)) { last_dest = Station::GetIfValid(v->last_station_visited); - next_dest = Station::GetIfValid(v->current_order.GetDestination()); + next_dest = Station::GetIfValid(v->current_order.GetDestination().ToStationID()); } else { last_dest = GetTargetAirportIfValid(v); next_dest = Station::GetIfValid(v->GetNextStoppingStation().value); @@ -424,7 +424,7 @@ static void CheckIfAircraftNeedsService(Aircraft *v) * 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()); + const Station *st = Station::Get(v->current_order.GetDestination().ToStationID()); assert(st != nullptr); @@ -1447,7 +1447,7 @@ static void AircraftLandAirplane(Aircraft *v) void AircraftNextAirportPos_and_Order(Aircraft *v) { if (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_DEPOT)) { - v->targetairport = v->current_order.GetDestination(); + v->targetairport = v->current_order.GetDestination().ToStationID(); } const Station *st = GetTargetAirportIfValid(v); @@ -2101,7 +2101,7 @@ static bool AircraftEventHandler(Aircraft *v, int loop) /* Check the distance to the next destination. This code works because the target * airport is only updated after take off and not on the ground. */ Station *cur_st = Station::GetIfValid(v->targetairport); - Station *next_st = v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_DEPOT) ? Station::GetIfValid(v->current_order.GetDestination()) : nullptr; + Station *next_st = v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_DEPOT) ? Station::GetIfValid(v->current_order.GetDestination().ToStationID()) : nullptr; if (cur_st != nullptr && cur_st->airport.tile != INVALID_TILE && next_st != nullptr && next_st->airport.tile != INVALID_TILE) { uint dist = DistanceSquare(cur_st->airport.tile, next_st->airport.tile); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 9889aaa5c6..68bad28303 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2741,7 +2741,7 @@ int WhoCanServiceIndustry(Industry *ind) for (const Order *o : v->Orders()) { if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) { /* Vehicle visits a station to load or unload */ - Station *st = Station::Get(o->GetDestination()); + Station *st = Station::Get(o->GetDestination().ToStationID()); assert(st != nullptr); /* Same cargo produced by industry is dropped here => not serviced by vehicle v */ diff --git a/src/linkgraph/refresh.cpp b/src/linkgraph/refresh.cpp index 1d56e080cc..a363305aa1 100644 --- a/src/linkgraph/refresh.cpp +++ b/src/linkgraph/refresh.cpp @@ -198,8 +198,8 @@ const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next */ void LinkRefresher::RefreshStats(const Order *cur, const Order *next) { - StationID next_station = next->GetDestination(); - Station *st = Station::GetIfValid(cur->GetDestination()); + StationID next_station = next->GetDestination().ToStationID(); + Station *st = Station::GetIfValid(cur->GetDestination().ToStationID()); if (st != nullptr && next_station != INVALID_STATION && next_station != st->index) { Station *st_to = Station::Get(next_station); for (CargoType c = 0; c < NUM_CARGO; c++) { diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 98e31571b7..ac0e00cc4d 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -734,7 +734,7 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0x08: break; // not implemented case 0x09: break; // not implemented case 0x0A: return v->current_order.MapOldOrder(); - case 0x0B: return v->current_order.GetDestination(); + case 0x0B: return v->current_order.GetDestination().value; case 0x0C: return v->GetNumOrders(); case 0x0D: return v->cur_real_order_index; case 0x0E: break; // not implemented diff --git a/src/order_base.h b/src/order_base.h index dd333ea923..2239b80885 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -47,7 +47,7 @@ private: uint8_t type = 0; ///< The type of order + non-stop flags uint8_t flags = 0; ///< Load/unload types, depot order/action types. - DestinationID dest = 0; ///< The destination of the order. + DestinationID dest{}; ///< The destination of the order. CargoType refit_cargo = CARGO_NO_REFIT; ///< Refit CargoType @@ -145,13 +145,13 @@ public: /** What are we going to do when in the depot. */ inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 3, 4); } /** What variable do we have to compare? */ - inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); } + inline OrderConditionVariable GetConditionVariable() const { return static_cast(GB(this->dest.value, 11, 5)); } /** What is the comparator to use? */ inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); } /** Get the order to skip to. */ inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; } /** Get the value to base the skip on. */ - inline uint16_t GetConditionValue() const { return GB(this->dest, 0, 11); } + inline uint16_t GetConditionValue() const { return GB(this->dest.value, 0, 11); } /** Set how the consist must be loaded. */ inline void SetLoadType(OrderLoadFlags load_type) { SB(this->flags, 4, 3, load_type); } @@ -166,13 +166,13 @@ public: /** Set what we are going to do in the depot. */ inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 3, 4, depot_service_type); } /** Set variable we have to compare. */ - inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); } + inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest.value, 11, 5, condition_variable); } /** Set the comparator to use. */ inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); } /** Get the order to skip to. */ inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; } /** Set the value to base the skip on. */ - inline void SetConditionValue(uint16_t value) { SB(this->dest, 0, 11, value); } + inline void SetConditionValue(uint16_t value) { SB(this->dest.value, 0, 11, value); } /* As conditional orders write their "skip to" order all over the flags, we cannot check the * flags to find out if timetabling is enabled. However, as conditional orders are never diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 28531a723a..d1cb513d83 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -51,7 +51,7 @@ Order::~Order() /* We can visit oil rigs and buoys that are not our own. They will be shown in * the list of stations. So, we need to invalidate that window if needed. */ if (this->IsType(OT_GOTO_STATION) || this->IsType(OT_GOTO_WAYPOINT)) { - BaseStation *bs = BaseStation::GetIfValid(this->GetDestination()); + BaseStation *bs = BaseStation::GetIfValid(this->GetDestination().ToStationID()); if (bs != nullptr && bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0); } } @@ -202,12 +202,12 @@ uint16_t Order::MapOldOrder() const if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5); if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6); if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7); - order |= GB(this->GetDestination(), 0, 8) << 8; + order |= GB(this->GetDestination().value, 0, 8) << 8; break; case OT_GOTO_DEPOT: if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6); SetBit(order, 7); - order |= GB(this->GetDestination(), 0, 8) << 8; + order |= GB(this->GetDestination().value, 0, 8) << 8; break; case OT_LOADING: if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6); @@ -434,7 +434,7 @@ StationIDStack OrderList::GetNextStoppingStation(const Vehicle *v, const Order * } } while (next->IsType(OT_GOTO_DEPOT) || next->GetDestination() == v->last_station_visited); - return next->GetDestination(); + return next->GetDestination().ToStationID(); } /** @@ -469,7 +469,7 @@ void OrderList::InsertOrderAt(Order *new_order, int index) /* We can visit oil rigs and buoys that are not our own. They will be shown in * the list of stations. So, we need to invalidate that window if needed. */ if (new_order->IsType(OT_GOTO_STATION) || new_order->IsType(OT_GOTO_WAYPOINT)) { - BaseStation *bs = BaseStation::Get(new_order->GetDestination()); + BaseStation *bs = BaseStation::Get(new_order->GetDestination().ToStationID()); if (bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0); } @@ -630,12 +630,12 @@ TileIndex Order::GetLocation(const Vehicle *v, bool airport) const case OT_GOTO_WAYPOINT: case OT_GOTO_STATION: case OT_IMPLICIT: - if (airport && v->type == VEH_AIRCRAFT) return Station::Get(this->GetDestination())->airport.tile; - return BaseStation::Get(this->GetDestination())->xy; + if (airport && v->type == VEH_AIRCRAFT) return Station::Get(this->GetDestination().ToStationID())->airport.tile; + return BaseStation::Get(this->GetDestination().ToStationID())->xy; case OT_GOTO_DEPOT: if (this->GetDestination() == INVALID_DEPOT) return INVALID_TILE; - return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy; + return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination().ToStationID())->xy : Depot::Get(this->GetDestination().ToDepotID())->xy; default: return INVALID_TILE; @@ -694,7 +694,7 @@ CommandCost CmdInsertOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se * and has the correct flags if any */ switch (new_order.GetType()) { case OT_GOTO_STATION: { - const Station *st = Station::GetIfValid(new_order.GetDestination()); + const Station *st = Station::GetIfValid(new_order.GetDestination().ToStationID()); if (st == nullptr) return CMD_ERROR; if (st->owner != OWNER_NONE) { @@ -749,7 +749,7 @@ CommandCost CmdInsertOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se case OT_GOTO_DEPOT: { if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) { if (v->type == VEH_AIRCRAFT) { - const Station *st = Station::GetIfValid(new_order.GetDestination()); + const Station *st = Station::GetIfValid(new_order.GetDestination().ToStationID()); if (st == nullptr) return CMD_ERROR; @@ -760,7 +760,7 @@ CommandCost CmdInsertOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se return CMD_ERROR; } } else { - const Depot *dp = Depot::GetIfValid(new_order.GetDestination()); + const Depot *dp = Depot::GetIfValid(new_order.GetDestination().ToDepotID()); if (dp == nullptr) return CMD_ERROR; @@ -802,7 +802,7 @@ CommandCost CmdInsertOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se } case OT_GOTO_WAYPOINT: { - const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination()); + const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination().ToStationID()); if (wp == nullptr) return CMD_ERROR; switch (v->type) { @@ -1536,7 +1536,7 @@ CommandCost CmdCloneOrder(DoCommandFlag flags, CloneOptions action, VehicleID ve /* Allow copying unreachable destinations if they were already unreachable for the source. * This is basically to allow cloning / autorenewing / autoreplacing vehicles, while the stations * are temporarily invalid due to reconstruction. */ - const Station *st = Station::Get(order->GetDestination()); + const Station *st = Station::Get(order->GetDestination().ToStationID()); if (CanVehicleUseStation(src, st) && !CanVehicleUseStation(dst, st)) { return CommandCost(STR_ERROR_CAN_T_COPY_SHARE_ORDER, GetVehicleCannotUseStationReason(dst, st)); } @@ -1583,7 +1583,7 @@ CommandCost CmdCloneOrder(DoCommandFlag flags, CloneOptions action, VehicleID ve * and neither can helicopters and aircraft. */ for (const Order *order : src->Orders()) { if (!OrderGoesToStation(dst, order)) continue; - Station *st = Station::Get(order->GetDestination()); + Station *st = Station::Get(order->GetDestination().ToStationID()); if (!CanVehicleUseStation(dst, st)) { return CommandCost(STR_ERROR_CAN_T_COPY_SHARE_ORDER, GetVehicleCannotUseStationReason(dst, st)); } @@ -1722,7 +1722,7 @@ void CheckOrders(const Vehicle *v) } /* Does station have a load-bay for this vehicle? */ if (order->IsType(OT_GOTO_STATION)) { - const Station *st = Station::Get(order->GetDestination()); + const Station *st = Station::Get(order->GetDestination().ToStationID()); n_st++; if (!CanVehicleUseStation(v, st)) { @@ -1989,7 +1989,7 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool switch (order->GetType()) { case OT_GOTO_STATION: - v->SetDestTile(v->GetOrderStationLocation(order->GetDestination())); + v->SetDestTile(v->GetOrderStationLocation(order->GetDestination().ToStationID())); return true; case OT_GOTO_DEPOT: @@ -2035,13 +2035,13 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool v->IncrementRealOrderIndex(); } else { if (v->type != VEH_AIRCRAFT) { - v->SetDestTile(Depot::Get(order->GetDestination())->xy); + v->SetDestTile(Depot::Get(order->GetDestination().ToStationID())->xy); } else { Aircraft *a = Aircraft::From(v); DestinationID destination = a->current_order.GetDestination(); if (a->targetairport != destination) { /* The aircraft is now heading for a different hangar than the next in the orders */ - a->SetDestTile(a->GetOrderStationLocation(destination)); + a->SetDestTile(a->GetOrderStationLocation(destination.ToStationID())); } } return true; @@ -2049,7 +2049,7 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool break; case OT_GOTO_WAYPOINT: - v->SetDestTile(Waypoint::Get(order->GetDestination())->xy); + v->SetDestTile(Waypoint::Get(order->GetDestination().ToStationID())->xy); return true; case OT_CONDITIONAL: { @@ -2144,7 +2144,7 @@ bool ProcessOrders(Vehicle *v) * the train to stop at this 'via' station if the next order * is a no-non-stop order; in that case not setting the last * visited station will cause the vehicle to still stop. */ - v->last_station_visited = v->current_order.GetDestination(); + v->last_station_visited = v->current_order.GetDestination().ToStationID(); UpdateVehicleTimetable(v, true); v->IncrementImplicitOrderIndex(); } @@ -2174,7 +2174,7 @@ bool ProcessOrders(Vehicle *v) /* If it is unchanged, keep it. */ if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) && - (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->ship_station.tile != INVALID_TILE)) { + (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination().ToStationID())->ship_station.tile != INVALID_TILE)) { return false; } diff --git a/src/order_cmd.h b/src/order_cmd.h index bbc34ee2c7..38bb525458 100644 --- a/src/order_cmd.h +++ b/src/order_cmd.h @@ -35,12 +35,12 @@ DEF_CMD_TRAIT(CMD_CLEAR_ORDER_BACKUP, CmdClearOrderBackup, CMD_CLIENT_ID, CMDT_ template inline EndianBufferWriter &operator <<(EndianBufferWriter &buffer, const Order &order) { - return buffer << order.type << order.flags << order.dest << order.refit_cargo << order.wait_time << order.travel_time << order.max_speed; + return buffer << order.type << order.flags << order.dest.value << order.refit_cargo << order.wait_time << order.travel_time << order.max_speed; } inline EndianBufferReader &operator >>(EndianBufferReader &buffer, Order &order) { - return buffer >> order.type >> order.flags >> order.dest >> order.refit_cargo >> order.wait_time >> order.travel_time >> order.max_speed; + return buffer >> order.type >> order.flags >> order.dest.value >> order.refit_cargo >> order.wait_time >> order.travel_time >> order.max_speed; } #endif /* ORDER_CMD_H */ diff --git a/src/order_gui.cpp b/src/order_gui.cpp index a2dc6156d7..c3ed9250bf 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -262,7 +262,7 @@ void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int case OT_GOTO_STATION: { OrderLoadFlags load = order->GetLoadType(); OrderUnloadFlags unload = order->GetUnloadType(); - bool valid_station = CanVehicleUseStation(v, Station::Get(order->GetDestination())); + bool valid_station = CanVehicleUseStation(v, Station::Get(order->GetDestination().ToStationID())); SetDParam(0, valid_station ? STR_ORDER_GO_TO_STATION : STR_ORDER_GO_TO_STATION_CAN_T_USE_STATION); SetDParam(1, STR_ORDER_GO_TO + (v->IsGroundVehicle() ? order->GetNonStopType() : 0)); diff --git a/src/order_type.h b/src/order_type.h index 6b7e4a4ce5..2701b029fe 100644 --- a/src/order_type.h +++ b/src/order_type.h @@ -11,11 +11,32 @@ #define ORDER_TYPE_H #include "core/enum_type.hpp" +#include "depot_type.h" +#include "station_type.h" typedef uint8_t VehicleOrderID; ///< The index of an order within its current vehicle (not pool related) typedef uint32_t OrderID; typedef uint16_t OrderListID; -typedef uint16_t DestinationID; + +struct DestinationID { + using BaseType = uint16_t; + BaseType value = 0; + + explicit DestinationID() = default; + constexpr DestinationID(size_t index) : value(static_cast(index)) {} + + constexpr DepotID ToDepotID() const noexcept { return static_cast(this->value); } + constexpr StationID ToStationID() const noexcept { return static_cast(this->value); } + constexpr BaseType base() const noexcept { return this->value; } + + constexpr bool operator ==(const DestinationID &destination) const { return this->value == destination.value; } + constexpr bool operator !=(const DestinationID &destination) const { return this->value != destination.value; } + constexpr bool operator ==(const StationID &station) const { return this->value == station; } + constexpr bool operator !=(const StationID &station) const { return this->value != station; } +}; + +constexpr bool operator ==(const StationID &station, const DestinationID &destination) { return destination == station; } +constexpr bool operator !=(const StationID &station, const DestinationID &destination) { return destination != station; } /** Invalid vehicle order index (sentinel) */ static const VehicleOrderID INVALID_VEH_ORDER_ID = 0xFF; diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index 712dc9266a..7c1fa7acb8 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -404,7 +404,7 @@ no_entry_cost: // jump here at the beginning if the node has no parent (it is th } else if (cur.tile_type == MP_STATION && IsRailWaypoint(cur.tile)) { if (v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(cur.tile) == v->current_order.GetDestination() && - !Waypoint::Get(v->current_order.GetDestination())->IsSingleTile()) { + !Waypoint::Get(v->current_order.GetDestination().ToStationID())->IsSingleTile()) { /* This waypoint is our destination; maybe this isn't an unreserved * one, so check that and if so see that as the last signal being * red. This way waypoints near stations should work better. */ diff --git a/src/pathfinder/yapf/yapf_destrail.hpp b/src/pathfinder/yapf/yapf_destrail.hpp index 68e0947ad9..7262060dc7 100644 --- a/src/pathfinder/yapf/yapf_destrail.hpp +++ b/src/pathfinder/yapf/yapf_destrail.hpp @@ -135,7 +135,7 @@ public: this->any_depot = false; switch (v->current_order.GetType()) { case OT_GOTO_WAYPOINT: - if (!Waypoint::Get(v->current_order.GetDestination())->IsSingleTile()) { + if (!Waypoint::Get(v->current_order.GetDestination().ToStationID())->IsSingleTile()) { /* In case of 'complex' waypoints we need to do a look * ahead. This look ahead messes a bit about, which * means that it 'corrupts' the cache. To prevent this @@ -146,8 +146,8 @@ public: [[fallthrough]]; case OT_GOTO_STATION: - this->dest_tile = CalcClosestStationTile(v->current_order.GetDestination(), v->tile, v->current_order.IsType(OT_GOTO_STATION) ? StationType::Rail : StationType::RailWaypoint); - this->dest_station_id = v->current_order.GetDestination(); + this->dest_tile = CalcClosestStationTile(v->current_order.GetDestination().ToStationID(), v->tile, v->current_order.IsType(OT_GOTO_STATION) ? StationType::Rail : StationType::RailWaypoint); + this->dest_station_id = v->current_order.GetDestination().ToStationID(); this->dest_trackdirs = INVALID_TRACKDIR_BIT; break; diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp index 3e99a7d839..448cc7adf4 100644 --- a/src/pathfinder/yapf/yapf_road.cpp +++ b/src/pathfinder/yapf/yapf_road.cpp @@ -241,13 +241,13 @@ public: void SetDestination(const RoadVehicle *v) { if (v->current_order.IsType(OT_GOTO_STATION)) { - this->dest_station = v->current_order.GetDestination(); + this->dest_station = v->current_order.GetDestination().ToStationID(); this->station_type = v->IsBus() ? StationType::Bus : StationType::Truck; this->dest_tile = CalcClosestStationTile(this->dest_station, v->tile, this->station_type); this->non_artic = !v->HasArticulatedPart(); this->dest_trackdirs = INVALID_TRACKDIR_BIT; } else if (v->current_order.IsType(OT_GOTO_WAYPOINT)) { - this->dest_station = v->current_order.GetDestination(); + this->dest_station = v->current_order.GetDestination().ToStationID(); this->station_type = StationType::RoadWaypoint; this->dest_tile = CalcClosestStationTile(this->dest_station, v->tile, this->station_type); this->non_artic = !v->HasArticulatedPart(); diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp index a5b9fc3e13..c439c7abfb 100644 --- a/src/pathfinder/yapf/yapf_ship.cpp +++ b/src/pathfinder/yapf/yapf_ship.cpp @@ -45,7 +45,7 @@ public: void SetDestination(const Ship *v) { if (v->current_order.IsType(OT_GOTO_STATION)) { - this->dest_station = v->current_order.GetDestination(); + this->dest_station = v->current_order.GetDestination().ToStationID(); this->dest_tile = CalcClosestStationTile(this->dest_station, v->tile, StationType::Dock); this->dest_trackdirs = INVALID_TRACKDIR_BIT; } else { diff --git a/src/pathfinder/yapf/yapf_ship_regions.cpp b/src/pathfinder/yapf/yapf_ship_regions.cpp index 36a939ea74..dab1045d96 100644 --- a/src/pathfinder/yapf/yapf_ship_regions.cpp +++ b/src/pathfinder/yapf/yapf_ship_regions.cpp @@ -185,7 +185,7 @@ public: pf.SetDestination(start_water_region_patch); if (v->current_order.IsType(OT_GOTO_STATION)) { - DestinationID station_id = v->current_order.GetDestination(); + StationID station_id = v->current_order.GetDestination().ToStationID(); const BaseStation *station = BaseStation::Get(station_id); TileArea tile_area; station->GetTileArea(&tile_area, StationType::Dock); diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index c78c216483..b73ccb86e0 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1369,7 +1369,7 @@ bool LoadOldVehicle(LoadgameState *ls, int num) v->current_order.AssignOrder(UnpackOldOrder(_old_order)); if (v->type == VEH_DISASTER) { - DisasterVehicle::From(v)->state = UnpackOldOrder(_old_order).GetDestination(); + DisasterVehicle::From(v)->state = UnpackOldOrder(_old_order).GetDestination().value; } v->next = (Vehicle *)(size_t)_old_next_ptr; diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 998e488ab4..7d323a87ad 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -32,10 +32,10 @@ static void UpdateWaypointOrder(Order *o) { if (!o->IsType(OT_GOTO_STATION)) return; - const Station *st = Station::Get(o->GetDestination()); + const Station *st = Station::Get(o->GetDestination().ToStationID()); if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) return; - o->MakeGoToWaypoint(o->GetDestination()); + o->MakeGoToWaypoint(o->GetDestination().ToStationID()); } /** diff --git a/src/script/api/script_order.cpp b/src/script/api/script_order.cpp index 4f373bb26b..17df83d166 100644 --- a/src/script/api/script_order.cpp +++ b/src/script/api/script_order.cpp @@ -252,15 +252,15 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr /* We don't know where the nearest depot is... (yet) */ if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) return INVALID_TILE; - if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination())->xy; + if (v->type != VEH_AIRCRAFT) return ::Depot::Get(order->GetDestination().ToDepotID())->xy; /* Aircraft's hangars are referenced by StationID, not DepotID */ - const Station *st = ::Station::Get(order->GetDestination()); + const Station *st = ::Station::Get(order->GetDestination().ToStationID()); if (!st->airport.HasHangar()) return INVALID_TILE; return st->airport.GetHangarTile(0); } case OT_GOTO_STATION: { - const Station *st = ::Station::Get(order->GetDestination()); + const Station *st = ::Station::Get(order->GetDestination().ToStationID()); if (st->train_station.tile != INVALID_TILE) { for (TileIndex t : st->train_station) { if (st->TileBelongsToRailStation(t)) return t; @@ -282,7 +282,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr } case OT_GOTO_WAYPOINT: { - const Waypoint *wp = ::Waypoint::Get(order->GetDestination()); + const Waypoint *wp = ::Waypoint::Get(order->GetDestination().ToStationID()); if (wp->train_station.tile != INVALID_TILE) { for (TileIndex t : wp->train_station) { if (wp->TileBelongsToRailStation(t)) return t; diff --git a/src/script/api/script_stationlist.cpp b/src/script/api/script_stationlist.cpp index c66d229879..782310846f 100644 --- a/src/script/api/script_stationlist.cpp +++ b/src/script/api/script_stationlist.cpp @@ -35,7 +35,7 @@ ScriptStationList_Vehicle::ScriptStationList_Vehicle(VehicleID vehicle_id) const Vehicle *v = ::Vehicle::Get(vehicle_id); for (Order *o = v->GetFirstOrder(); o != nullptr; o = o->next) { - if (o->IsType(OT_GOTO_STATION)) this->AddItem(o->GetDestination()); + if (o->IsType(OT_GOTO_STATION)) this->AddItem(o->GetDestination().ToStationID()); } } diff --git a/src/script/api/script_waypointlist.cpp b/src/script/api/script_waypointlist.cpp index 2332655c2e..5425ad2925 100644 --- a/src/script/api/script_waypointlist.cpp +++ b/src/script/api/script_waypointlist.cpp @@ -35,6 +35,6 @@ ScriptWaypointList_Vehicle::ScriptWaypointList_Vehicle(VehicleID vehicle_id) const Vehicle *v = ::Vehicle::Get(vehicle_id); for (const Order *o = v->GetFirstOrder(); o != nullptr; o = o->next) { - if (o->IsType(OT_GOTO_WAYPOINT)) this->AddItem(o->GetDestination()); + if (o->IsType(OT_GOTO_WAYPOINT)) this->AddItem(o->GetDestination().ToStationID()); } } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 2e59dbe3a8..37c9d94bca 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -773,7 +773,7 @@ static void ShipController(Ship *v) } } else if (v->current_order.IsType(OT_GOTO_STATION) && IsDockingTile(gp.new_tile)) { /* Process station in the orderlist. */ - Station *st = Station::Get(v->current_order.GetDestination()); + Station *st = Station::Get(v->current_order.GetDestination().ToStationID()); if (st->docking_station.Contains(gp.new_tile) && IsShipDestinationTile(gp.new_tile, st->index)) { v->last_station_visited = st->index; if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index cbe2b66c55..e7aded5e8d 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -4194,7 +4194,7 @@ void Train::OnNewEconomyDay() /* update destination */ if (this->current_order.IsType(OT_GOTO_STATION)) { - TileIndex tile = Station::Get(this->current_order.GetDestination())->train_station.tile; + TileIndex tile = Station::Get(this->current_order.GetDestination().ToStationID())->train_station.tile; if (tile != INVALID_TILE) this->dest_tile = tile; } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index a368c722ba..e0c1f9fcc1 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -2635,7 +2635,7 @@ CommandCost Vehicle::SendToDepot(DoCommandFlag flags, DepotCommandFlags command) } this->SetDestTile(closestDepot.location); - this->current_order.MakeGoToDepot(closestDepot.destination, ODTF_MANUAL); + this->current_order.MakeGoToDepot(closestDepot.destination.ToDepotID(), ODTF_MANUAL); if (!command.Test(DepotCommandFlag::Service)) this->current_order.SetDepotActionType(ODATFB_HALT); SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP); diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 22b6e2222f..1aa29bbd2b 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -228,13 +228,12 @@ struct RefitDesc { * and whether it could be found. */ struct ClosestDepot { - TileIndex location; - DestinationID destination; ///< The DestinationID as used for orders. - bool reverse; - bool found; + TileIndex location = INVALID_TILE; + DestinationID destination{}; ///< The DestinationID as used for orders. + bool reverse = false; + bool found = false; - ClosestDepot() : - location(INVALID_TILE), destination(0), reverse(false), found(false) {} + ClosestDepot() = default; ClosestDepot(TileIndex location, DestinationID destination, bool reverse = false) : location(location), destination(destination), reverse(reverse), found(true) {}