1
0
Fork 0

Codechange: make statistics from CompanyEconomyEntry use C++ constructs

pull/13641/head
Rubidium 2025-02-17 21:31:28 +01:00 committed by rubidium42
parent 24a7cde9cc
commit b264a4864b
1 changed files with 4 additions and 18 deletions

View File

@ -8,6 +8,7 @@
/** @file economy.cpp Handling of the economy. */
#include "stdafx.h"
#include <ranges>
#include "company_func.h"
#include "command_func.h"
#include "industry.h"
@ -246,36 +247,21 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
/* Generate statistics depending on recent income statistics */
{
static_assert(MAX_HISTORY_QUARTERS >= 12u);
int numec = std::min<uint>(c->num_valid_stat_ent, 12u);
if (numec != 0) {
auto cee = c->old_economy.begin();
Money min_income = cee->income + cee->expenses;
Money max_income = cee->income + cee->expenses;
do {
min_income = std::min(min_income, cee->income + cee->expenses);
max_income = std::max(max_income, cee->income + cee->expenses);
} while (++cee, --numec);
if (min_income > 0) {
_score_part[owner][SCORE_MIN_INCOME] = min_income;
}
auto [min_income, max_income] = std::ranges::minmax(c->old_economy | std::views::take(numec) | std::views::transform([](const auto &ce) { return ce.income + ce.expenses; }));
if (min_income > 0) _score_part[owner][SCORE_MIN_INCOME] = min_income;
_score_part[owner][SCORE_MAX_INCOME] = max_income;
}
}
/* Generate score depending on amount of transported cargo */
{
static_assert(MAX_HISTORY_QUARTERS >= 4u);
int numec = std::min<uint>(c->num_valid_stat_ent, 4u);
if (numec != 0) {
auto cee = c->old_economy.begin();
OverflowSafeInt64 total_delivered = 0;
do {
total_delivered += cee->delivered_cargo.GetSum<OverflowSafeInt64>();
} while (++cee, --numec);
for (auto &ce : c->old_economy | std::views::take(numec)) total_delivered += ce.delivered_cargo.GetSum<OverflowSafeInt64>();
_score_part[owner][SCORE_DELIVERED] = total_delivered;
}