From 431a6a1262ed4a9d133f6f66f0aff6ccf1900f7b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 26 May 2024 11:27:49 +0100 Subject: [PATCH] Allow sorting by average order occupancy in shared order group mode See: #698 --- src/vehicle_gui.cpp | 42 +++++++++++++++++++++++++++++++++++++++--- src/vehicle_gui_base.h | 6 ++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 14d2c5bf5ad..c127418bca2 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -74,6 +74,7 @@ static BaseVehicleListWindow::VehicleGroupSortFunction VehicleGroupTotalProfitTh static BaseVehicleListWindow::VehicleGroupSortFunction VehicleGroupTotalProfitLastYearSorter; static BaseVehicleListWindow::VehicleGroupSortFunction VehicleGroupAverageProfitThisYearSorter; static BaseVehicleListWindow::VehicleGroupSortFunction VehicleGroupAverageProfitLastYearSorter; +static BaseVehicleListWindow::VehicleGroupSortFunction VehicleGroupAverageOrderOccupancySorter; /** Wrapper to convert a VehicleIndividualSortFunction to a VehicleGroupSortFunction */ template @@ -102,6 +103,16 @@ enum VehicleSortType VST_MAX_SPEED_LOADED, }; +enum VehicleGroupSortType +{ + VGST_LENGTH, + VGST_TOTAL_PROFIT_THIS_YEAR, + VGST_TOTAL_PROFIT_LAST_YEAR, + VGST_AVERAGE_PROFIT_THIS_YEAR, + VGST_AVERAGE_PROFIT_LAST_YEAR, + VGST_AVERAGE_ORDER_OCCUPANCY, +}; + BaseVehicleListWindow::VehicleGroupSortFunction * const BaseVehicleListWindow::vehicle_group_none_sorter_funcs[] = { &VehicleIndividualToGroupSorterWrapper, &VehicleIndividualToGroupSorterWrapper, @@ -147,6 +158,7 @@ BaseVehicleListWindow::VehicleGroupSortFunction * const BaseVehicleListWindow::v &VehicleGroupTotalProfitLastYearSorter, &VehicleGroupAverageProfitThisYearSorter, &VehicleGroupAverageProfitLastYearSorter, + &VehicleGroupAverageOrderOccupancySorter, }; const StringID BaseVehicleListWindow::vehicle_group_shared_orders_sorter_names[] = { @@ -155,6 +167,7 @@ const StringID BaseVehicleListWindow::vehicle_group_shared_orders_sorter_names[] STR_SORT_BY_TOTAL_PROFIT_LAST_YEAR, STR_SORT_BY_AVERAGE_PROFIT_THIS_YEAR, STR_SORT_BY_AVERAGE_PROFIT_LAST_YEAR, + STR_SORT_BY_AVG_ORDER_OCCUPANCY, INVALID_STRING_ID }; @@ -1507,6 +1520,12 @@ static bool VehicleGroupAverageProfitLastYearSorter(const GUIVehicleGroup &a, co return a.GetDisplayProfitLastYear() * static_cast(b.NumVehicles()) < b.GetDisplayProfitLastYear() * static_cast(a.NumVehicles()); } +/** Sort vehicle groups by the average vehicle occupancy */ +static bool VehicleGroupAverageOrderOccupancySorter(const GUIVehicleGroup &a, const GUIVehicleGroup &b) +{ + return a.GetOrderOccupancyAverage() < b.GetOrderOccupancyAverage(); +} + /** Sort vehicles by their number */ static bool VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const &b) { @@ -2002,9 +2021,26 @@ void BaseVehicleListWindow::DrawVehicleListItems(VehicleID selected_vehicle, int GfxFillRect((tr.right - 1) - (GetCharacterHeight(FS_SMALL) - 2), ir.top + 1, tr.right - 1, (ir.top + 1) + (GetCharacterHeight(FS_SMALL) - 2), ccolour, FILLRECT_OPAQUE); } } else { - SetDParam(0, vehgroup.GetDisplayProfitThisYear()); - SetDParam(1, vehgroup.GetDisplayProfitLastYear()); - DrawString(tr.left, tr.right, ir.bottom - GetCharacterHeight(FS_SMALL) - WidgetDimensions::scaled.framerect.bottom, STR_VEHICLE_LIST_PROFIT_THIS_YEAR_LAST_YEAR); + StringID str = STR_JUST_STRING2; + SetDParam(0, STR_VEHICLE_LIST_PROFIT_THIS_YEAR_LAST_YEAR); + SetDParam(1, vehgroup.GetDisplayProfitThisYear()); + SetDParam(2, vehgroup.GetDisplayProfitLastYear()); + + switch (this->vehgroups.SortType()) { + case VGST_AVERAGE_ORDER_OCCUPANCY: { + uint8_t occupancy_average = vehgroup.GetOrderOccupancyAverage(); + if (occupancy_average >= 16) { + str = STR_VEHICLE_LIST_ORDER_OCCUPANCY_AVERAGE; + SetDParam(3, occupancy_average - 16); + } + break; + } + + default: + break; + } + + DrawString(tr.left, tr.right, ir.bottom - GetCharacterHeight(FS_SMALL) - WidgetDimensions::scaled.framerect.bottom, str); } DrawVehicleProfitButton(vehgroup.GetOldestVehicleAge(), vehgroup.GetDisplayProfitLastYear(), vehgroup.NumVehicles(), vehicle_button_x, ir.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal); diff --git a/src/vehicle_gui_base.h b/src/vehicle_gui_base.h index 20c3da57555..f8631a66b35 100644 --- a/src/vehicle_gui_base.h +++ b/src/vehicle_gui_base.h @@ -62,6 +62,12 @@ struct GUIVehicleGroup { }); return oldest->economy_age; } + + uint8_t GetOrderOccupancyAverage() const + { + if (this->NumVehicles() < 1) return 0; + return this->vehicles_begin[0]->GetOrderOccupancyAverage(); + } }; typedef GUIList GUIVehicleGroupList;