From 6b8f9b50b99bc09fd35a9a0bbffdb6f306aa97c7 Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Sun, 21 Feb 2021 15:35:15 +0000 Subject: Fix: Vehicle list windows did not update when this year's profit changed Remove caching from vehicle group object. and recalculate it whenever required instead. --- src/vehicle_gui.cpp | 28 +++++++++------------------- src/vehicle_gui_base.h | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 8e2088815..5960f0be9 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -185,7 +185,7 @@ void BaseVehicleListWindow::BuildVehicleList() if (this->grouping == GB_NONE) { uint max_unitnumber = 0; for (auto it = this->vehicles.begin(); it != this->vehicles.end(); ++it) { - this->vehgroups.emplace_back(it, it + 1, (*it)->GetDisplayProfitThisYear(), (*it)->GetDisplayProfitLastYear(), (*it)->age); + this->vehgroups.emplace_back(it, it + 1); max_unitnumber = std::max(max_unitnumber, (*it)->unitnumber); } @@ -204,17 +204,7 @@ void BaseVehicleListWindow::BuildVehicleList() return v->FirstShared() == first_shared; }); - Money display_profit_this_year = 0; - Money display_profit_last_year = 0; - Date age = 0; - for (auto it = begin; it != end; ++it) { - const Vehicle * const v = (*it); - display_profit_this_year += v->GetDisplayProfitThisYear(); - display_profit_last_year += v->GetDisplayProfitLastYear(); - age = std::max(age, v->age); - } - - this->vehgroups.emplace_back(begin, end, display_profit_this_year, display_profit_last_year, age); + this->vehgroups.emplace_back(begin, end); max_num_vehicles = std::max(max_num_vehicles, static_cast(end - begin)); @@ -1183,25 +1173,25 @@ static bool VehicleGroupLengthSorter(const GUIVehicleGroup &a, const GUIVehicleG /** Sort vehicle groups by the total profit this year */ static bool VehicleGroupTotalProfitThisYearSorter(const GUIVehicleGroup &a, const GUIVehicleGroup &b) { - return a.display_profit_this_year < b.display_profit_this_year; + return a.GetDisplayProfitThisYear() < b.GetDisplayProfitThisYear(); } /** Sort vehicle groups by the total profit last year */ static bool VehicleGroupTotalProfitLastYearSorter(const GUIVehicleGroup &a, const GUIVehicleGroup &b) { - return a.display_profit_last_year < b.display_profit_last_year; + return a.GetDisplayProfitLastYear() < b.GetDisplayProfitLastYear(); } /** Sort vehicle groups by the average profit this year */ static bool VehicleGroupAverageProfitThisYearSorter(const GUIVehicleGroup &a, const GUIVehicleGroup &b) { - return a.display_profit_this_year * static_cast(b.NumVehicles()) < b.display_profit_this_year * static_cast(a.NumVehicles()); + return a.GetDisplayProfitThisYear() * static_cast(b.NumVehicles()) < b.GetDisplayProfitThisYear() * static_cast(a.NumVehicles()); } /** Sort vehicle groups by the average profit last year */ static bool VehicleGroupAverageProfitLastYearSorter(const GUIVehicleGroup &a, const GUIVehicleGroup &b) { - return a.display_profit_last_year * static_cast(b.NumVehicles()) < b.display_profit_last_year * static_cast(a.NumVehicles()); + return a.GetDisplayProfitLastYear() * static_cast(b.NumVehicles()) < b.GetDisplayProfitLastYear() * static_cast(a.NumVehicles()); } /** Sort vehicles by their number */ @@ -1538,11 +1528,11 @@ void BaseVehicleListWindow::DrawVehicleListItems(VehicleID selected_vehicle, int const GUIVehicleGroup &vehgroup = this->vehgroups[i]; - SetDParam(0, vehgroup.display_profit_this_year); - SetDParam(1, vehgroup.display_profit_last_year); + SetDParam(0, vehgroup.GetDisplayProfitThisYear()); + SetDParam(1, vehgroup.GetDisplayProfitLastYear()); DrawString(text_left, text_right, y + line_height - FONT_HEIGHT_SMALL - WD_FRAMERECT_BOTTOM - 1, STR_VEHICLE_LIST_PROFIT_THIS_YEAR_LAST_YEAR); - DrawVehicleProfitButton(vehgroup.age, vehgroup.display_profit_last_year, vehgroup.NumVehicles(), vehicle_button_x, y + FONT_HEIGHT_NORMAL + 3); + DrawVehicleProfitButton(vehgroup.GetOldestVehicleAge(), vehgroup.GetDisplayProfitLastYear(), vehgroup.NumVehicles(), vehicle_button_x, y + FONT_HEIGHT_NORMAL + 3); switch (this->grouping) { case GB_NONE: { diff --git a/src/vehicle_gui_base.h b/src/vehicle_gui_base.h index f89b81ffb..4bbfbf2a8 100644 --- a/src/vehicle_gui_base.h +++ b/src/vehicle_gui_base.h @@ -14,32 +14,54 @@ #include "date_type.h" #include "economy_type.h" #include "sortlist_type.h" +#include "vehicle_base.h" #include "vehiclelist.h" #include "window_gui.h" #include "widgets/dropdown_type.h" #include +#include typedef GUIList GUIVehicleList; struct GUIVehicleGroup { VehicleList::const_iterator vehicles_begin; ///< Pointer to beginning element of this vehicle group. VehicleList::const_iterator vehicles_end; ///< Pointer to past-the-end element of this vehicle group. - Money display_profit_this_year; ///< Total profit for the vehicle group this year. - Money display_profit_last_year; ///< Total profit for the vehicle group laste year. - Date age; ///< Age in days of oldest vehicle in the group. - GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end, Money display_profit_this_year, Money display_profit_last_year, Date age) - : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end), display_profit_this_year(display_profit_this_year), display_profit_last_year(display_profit_last_year), age(age) {} + GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end) + : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {} std::ptrdiff_t NumVehicles() const { - return std::distance(vehicles_begin, vehicles_end); + return std::distance(this->vehicles_begin, this->vehicles_end); } + const Vehicle *GetSingleVehicle() const { - assert(NumVehicles() == 1); - return vehicles_begin[0]; + assert(this->NumVehicles() == 1); + return this->vehicles_begin[0]; + } + + Money GetDisplayProfitThisYear() const + { + return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) { + return acc + v->GetDisplayProfitThisYear(); + }); + } + + Money GetDisplayProfitLastYear() const + { + return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) { + return acc + v->GetDisplayProfitLastYear(); + }); + } + + Date GetOldestVehicleAge() const + { + const Vehicle *oldest = *std::max_element(this->vehicles_begin, this->vehicles_end, [](const Vehicle *v_a, const Vehicle *v_b) { + return v_a->age < v_b->age; + }); + return oldest->age; } }; -- cgit v1.2.3-54-g00ecf