From 4e84d032b85f0d1b6d81d0ff9d2293a814f13d2e Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 2 Feb 2025 08:49:38 +0100 Subject: [PATCH] Codechange: read VLI.index through wrapper to get the right type --- src/group_gui.cpp | 52 +++++++++++++++++++++++---------------------- src/vehicle_gui.cpp | 10 ++++----- src/vehiclelist.cpp | 10 ++++----- src/vehiclelist.h | 23 ++++++++++++++------ 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/group_gui.cpp b/src/group_gui.cpp index ea16a60877..988260741e 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -293,7 +293,7 @@ private: if (g_id == NEW_GROUP) return; /* draw the selected group in white, else we draw it in black */ - TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK; + TextColour colour = g_id == this->vli.ToGroupID() ? TC_WHITE : TC_BLACK; const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype); bool rtl = _current_text_dir == TD_RTL; @@ -503,7 +503,8 @@ public: this->group_rename = INVALID_GROUP; } - if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) { + GroupID group = this->vli.ToGroupID(); + if (!(IsAllGroupID(group) || IsDefaultGroupID(group) || Group::IsValidID(group))) { this->vli.index = ALL_GROUP; this->CloseChildWindows(WC_DROPDOWN_MENU); } @@ -524,16 +525,16 @@ public: case WID_GL_CAPTION: /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption * We list all vehicles or ungrouped vehicles */ - if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) { + if (IsDefaultGroupID(this->vli.ToGroupID()) || IsAllGroupID(this->vli.ToGroupID())) { SetDParam(0, STR_COMPANY_NAME); SetDParam(1, this->vli.company); SetDParam(2, this->vehicles.size()); SetDParam(3, this->vehicles.size()); } else { - uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.index, this->vli.vtype); + uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.ToGroupID(), this->vli.vtype); SetDParam(0, STR_GROUP_NAME); - SetDParam(1, this->vli.index); + SetDParam(1, this->vli.ToGroupID()); SetDParam(2, num_vehicle); SetDParam(3, num_vehicle); } @@ -566,7 +567,8 @@ public: WID_GL_MANAGE_VEHICLES_DROPDOWN); /* Disable the group specific function when we select the default group or all vehicles */ - this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company, + GroupID group = this->vli.ToGroupID(); + this->SetWidgetsDisabledState(IsDefaultGroupID(group) || IsAllGroupID(group) || _local_company != this->vli.company, WID_GL_DELETE_GROUP, WID_GL_RENAME_GROUP, WID_GL_LIVERY_GROUP, @@ -584,7 +586,7 @@ public: /* If not a default group and the group has replace protection, show an enabled replace sprite. */ uint16_t protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN; - if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->flags.Test(GroupFlag::ReplaceProtection)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; + if (!IsDefaultGroupID(group) && !IsAllGroupID(group) && Group::Get(group)->flags.Test(GroupFlag::ReplaceProtection)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; this->GetWidget(WID_GL_REPLACE_PROTECTION)->SetSprite(protect_sprite + this->vli.vtype); /* Set text of "group by" dropdown widget. */ @@ -665,13 +667,13 @@ public: break; case WID_GL_LIST_VEHICLE: - if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) { + if (this->vli.ToGroupID() != ALL_GROUP && this->grouping == GB_NONE) { /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */ Rect mr = r.WithHeight(this->resize.step_height); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->vehgroups); for (auto it = first; it != last; ++it) { const Vehicle *v = it->GetSingleVehicle(); - if (v->group_id != this->vli.index) { + if (v->group_id != this->vli.ToGroupID()) { GfxFillRect(mr.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(COLOUR_GREY, SHADE_DARK), FILLRECT_CHECKER); } mr = mr.Translate(0, this->resize.step_height); @@ -722,7 +724,7 @@ public: break; case WID_GL_ALL_VEHICLES: // All vehicles button - if (!IsAllGroupID(this->vli.index)) { + if (!IsAllGroupID(this->vli.ToGroupID())) { this->vli.index = ALL_GROUP; this->vehgroups.ForceRebuild(); this->SetDirty(); @@ -730,7 +732,7 @@ public: break; case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button - if (!IsDefaultGroupID(this->vli.index)) { + if (!IsDefaultGroupID(this->vli.ToGroupID())) { this->vli.index = DEFAULT_GROUP; this->vehgroups.ForceRebuild(); this->SetDirty(); @@ -749,7 +751,7 @@ public: group_display->pos_x + WidgetDimensions::scaled.framerect.left + it->indent * WidgetDimensions::scaled.hsep_indent; if (click_count > 1 || (pt.x >= x && pt.x < (int)(x + this->column_size[VGC_FOLD].width))) { - GroupID g = this->vli.index; + GroupID g = this->vli.ToGroupID(); if (!IsAllGroupID(g) && !IsDefaultGroupID(g)) { do { g = Group::Get(g)->parent; @@ -832,22 +834,22 @@ public: } case WID_GL_CREATE_GROUP: { // Create a new group - Command::Post(STR_ERROR_GROUP_CAN_T_CREATE, CcCreateGroup, this->vli.vtype, this->vli.index); + Command::Post(STR_ERROR_GROUP_CAN_T_CREATE, CcCreateGroup, this->vli.vtype, this->vli.ToGroupID()); break; } case WID_GL_DELETE_GROUP: { // Delete the selected group - this->group_confirm = this->vli.index; + this->group_confirm = this->vli.ToGroupID(); ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback); break; } case WID_GL_RENAME_GROUP: // Rename the selected roup - this->ShowRenameGroupWindow(this->vli.index, false); + this->ShowRenameGroupWindow(this->vli.ToGroupID(), false); break; case WID_GL_LIVERY_GROUP: // Set group livery - ShowCompanyLiveryWindow(this->owner, this->vli.index); + ShowCompanyLiveryWindow(this->owner, this->vli.ToGroupID()); break; case WID_GL_AVAILABLE_VEHICLES: @@ -855,7 +857,7 @@ public: break; case WID_GL_MANAGE_VEHICLES_DROPDOWN: { - ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index), IsDefaultGroupID(this->vli.index)), -1, WID_GL_MANAGE_VEHICLES_DROPDOWN); + ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.ToGroupID()), IsDefaultGroupID(this->vli.ToGroupID())), -1, WID_GL_MANAGE_VEHICLES_DROPDOWN); break; } @@ -866,9 +868,9 @@ public: } case WID_GL_REPLACE_PROTECTION: { - const Group *g = Group::GetIfValid(this->vli.index); + const Group *g = Group::GetIfValid(this->vli.ToGroupID()); if (g != nullptr) { - Command::Post(this->vli.index, GroupFlag::ReplaceProtection, !g->flags.Test(GroupFlag::ReplaceProtection), _ctrl_pressed); + Command::Post(this->vli.ToGroupID(), GroupFlag::ReplaceProtection, !g->flags.Test(GroupFlag::ReplaceProtection), _ctrl_pressed); } break; } @@ -1013,7 +1015,7 @@ public: switch (index) { case ADI_REPLACE: // Replace window - ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype); + ShowReplaceGroupVehicleWindow(this->vli.ToGroupID(), this->vli.vtype); break; case ADI_SERVICE: // Send for servicing case ADI_DEPOT: { // Send to Depots @@ -1026,14 +1028,14 @@ public: break; case ADI_ADD_SHARED: // Add shared Vehicles - assert(Group::IsValidID(this->vli.index)); + assert(Group::IsValidID(this->vli.ToGroupID())); - Command::Post(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE, this->vli.index, this->vli.vtype); + Command::Post(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE, this->vli.ToGroupID(), this->vli.vtype); break; case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group - assert(Group::IsValidID(this->vli.index)); + assert(Group::IsValidID(this->vli.ToGroupID())); - Command::Post(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES, this->vli.index); + Command::Post(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES, this->vli.ToGroupID()); break; default: NOT_REACHED(); } @@ -1124,7 +1126,7 @@ public: */ void SelectGroup(const GroupID g_id) { - if (g_id == INVALID_GROUP || g_id == this->vli.index) return; + if (g_id == INVALID_GROUP || g_id == this->vli.ToGroupID()) return; this->vli.index = g_id; if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) { diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index b5523a1716..38c1aa906b 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2040,20 +2040,20 @@ public: case VL_STANDARD: // Company Name SetDParam(0, STR_COMPANY_NAME); - SetDParam(1, this->vli.index); + SetDParam(1, this->vli.ToCompanyID()); SetDParam(3, this->vehicles.size()); break; case VL_STATION_LIST: // Station/Waypoint Name - SetDParam(0, Station::IsExpected(BaseStation::Get(this->vli.index)) ? STR_STATION_NAME : STR_WAYPOINT_NAME); - SetDParam(1, this->vli.index); + SetDParam(0, Station::IsExpected(BaseStation::Get(this->vli.ToStationID())) ? STR_STATION_NAME : STR_WAYPOINT_NAME); + SetDParam(1, this->vli.ToStationID()); SetDParam(3, this->vehicles.size()); break; case VL_DEPOT_LIST: SetDParam(0, STR_DEPOT_CAPTION); SetDParam(1, this->vli.vtype); - SetDParam(2, this->vli.index); + SetDParam(2, this->vli.ToDestinationID()); SetDParam(3, this->vehicles.size()); break; default: NOT_REACHED(); @@ -2249,7 +2249,7 @@ public: void OnGameTick() override { if (this->vehgroups.NeedResort()) { - StationID station = (this->vli.type == VL_STATION_LIST) ? this->vli.index : INVALID_STATION; + StationID station = (this->vli.type == VL_STATION_LIST) ? this->vli.ToStationID() : INVALID_STATION; Debug(misc, 3, "Periodic resort {} list company {} at station {}", this->vli.vtype, this->owner, station); this->SetDirty(); diff --git a/src/vehiclelist.cpp b/src/vehiclelist.cpp index 0be67c7edf..e24fd147da 100644 --- a/src/vehiclelist.cpp +++ b/src/vehiclelist.cpp @@ -96,14 +96,14 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli case VL_STATION_LIST: FindVehiclesWithOrder( [&vli](const Vehicle *v) { return v->type == vli.vtype; }, - [&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.index; }, + [&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.ToStationID(); }, [&list](const Vehicle *v) { list->push_back(v); } ); break; case VL_SHARED_ORDERS: { /* Add all vehicles from this vehicle's shared order list */ - const Vehicle *v = Vehicle::GetIfValid(vli.index); + const Vehicle *v = Vehicle::GetIfValid(vli.ToVehicleID()); if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false; for (; v != nullptr; v = v->NextShared()) { @@ -113,10 +113,10 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli } case VL_GROUP_LIST: - if (vli.index != ALL_GROUP) { + if (vli.ToGroupID() != ALL_GROUP) { for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == vli.vtype && v->IsPrimaryVehicle() && - v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) { + v->owner == vli.company && GroupIsInGroup(v->group_id, vli.ToGroupID())) { list->push_back(v); } } @@ -135,7 +135,7 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli case VL_DEPOT_LIST: FindVehiclesWithOrder( [&vli](const Vehicle *v) { return v->type == vli.vtype; }, - [&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index; }, + [&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.ToDestinationID(); }, [&list](const Vehicle *v) { list->push_back(v); } ); break; diff --git a/src/vehiclelist.h b/src/vehiclelist.h index b23cc9e005..82ceb1e0c4 100644 --- a/src/vehiclelist.h +++ b/src/vehiclelist.h @@ -10,18 +10,21 @@ #ifndef VEHICLELIST_H #define VEHICLELIST_H -#include "vehicle_type.h" #include "company_type.h" +#include "group_type.h" +#include "order_type.h" +#include "station_type.h" #include "tile_type.h" +#include "vehicle_type.h" #include "window_type.h" /** Vehicle List type flags */ enum VehicleListType : uint8_t { - VL_STANDARD, - VL_SHARED_ORDERS, - VL_STATION_LIST, - VL_DEPOT_LIST, - VL_GROUP_LIST, + VL_STANDARD, ///< Index is the company. + VL_SHARED_ORDERS, ///< Index is the first vehicle of the shared orders. + VL_STATION_LIST, ///< Index is the station. + VL_DEPOT_LIST, ///< Index is the destination (station for hangar of aircraft, depot for others) + VL_GROUP_LIST, ///< Index is the group. VLT_END }; @@ -36,6 +39,12 @@ struct VehicleListIdentifier { bool Valid() const { return this->type < VLT_END; } + constexpr CompanyID ToCompanyID() const { assert(this->type == VL_STANDARD); return CompanyID(this->index); } + constexpr DestinationID ToDestinationID() const { assert(this->type == VL_DEPOT_LIST); return DestinationID(this->index); } + constexpr GroupID ToGroupID() const { assert(this->type == VL_GROUP_LIST); return GroupID(this->index); } + constexpr StationID ToStationID() const { assert(this->type == VL_STATION_LIST); return StationID(this->index); } + constexpr VehicleID ToVehicleID() const { assert(this->type == VL_SHARED_ORDERS); return VehicleID(this->index); } + /** * Create a simple vehicle list. * @param type List type. @@ -46,7 +55,7 @@ struct VehicleListIdentifier { VehicleListIdentifier(VehicleListType type, VehicleType vtype, CompanyID company, uint index = 0) : type(type), vtype(vtype), company(company), index(index) {} - VehicleListIdentifier() : type(), vtype(), company(), index() {} + VehicleListIdentifier() = default; }; /** A list of vehicles. */