1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-26 05:34:12 +01:00

Codechange: Make ScoreID an enum class.

This commit is contained in:
Cyprian Klimaszewski
2026-01-12 18:53:51 +01:00
committed by rubidium42
parent 810dae240a
commit b556dc24b8
5 changed files with 55 additions and 55 deletions

View File

@@ -88,20 +88,20 @@ typedef std::vector<Industry *> SmallIndustryList;
/**
* Score info, values used for computing the detailed performance rating.
*/
const ScoreInfo _score_info[] = {
{ 120, 100}, // SCORE_VEHICLES
{ 80, 100}, // SCORE_STATIONS
{ 10000, 100}, // SCORE_MIN_PROFIT
{ 50000, 50}, // SCORE_MIN_INCOME
{ 100000, 100}, // SCORE_MAX_INCOME
{ 40000, 400}, // SCORE_DELIVERED
{ 8, 50}, // SCORE_CARGO
{10000000, 50}, // SCORE_MONEY
{ 250000, 50}, // SCORE_LOAN
{ 0, 0} // SCORE_TOTAL
const EnumClassIndexContainer<std::array<ScoreInfo, to_underlying(ScoreID::End)>, ScoreID> _score_info = {
ScoreInfo(100, 120), // ScoreID::Vehicles
ScoreInfo(100, 80), // ScoreID::Stations
ScoreInfo(100, 10000), // ScoreID::MinProfit
ScoreInfo(50, 50000), // ScoreID::MinIncome
ScoreInfo(100, 100000), // ScoreID::MaxIncome
ScoreInfo(400, 40000), // ScoreID::Delivered
ScoreInfo(50, 8), // ScoreID::Cargo
ScoreInfo(50, 10000000), // ScoreID::Money
ScoreInfo(50, 250000), // ScoreID::Loan
ScoreInfo(0, 0), // ScoreID::Total
};
TypedIndexContainer<std::array<std::array<int64_t, SCORE_END>, MAX_COMPANIES>, CompanyID> _score_part;
TypedIndexContainer<std::array<EnumClassIndexContainer<std::array<int64_t, to_underlying(ScoreID::End)>, ScoreID>, MAX_COMPANIES>, CompanyID> _score_part;
Economy _economy;
Prices _price;
static PriceMultipliers _price_base_multiplier;
@@ -228,10 +228,10 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
min_profit >>= 8; // remove the fract part
_score_part[owner][SCORE_VEHICLES] = num;
_score_part[owner][ScoreID::Vehicles] = num;
/* Don't allow negative min_profit to show */
if (min_profit > 0) {
_score_part[owner][SCORE_MIN_PROFIT] = min_profit;
_score_part[owner][ScoreID::MinProfit] = min_profit;
}
}
@@ -242,7 +242,7 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
/* Only count stations that are actually serviced */
if (st->owner == owner && (st->time_since_load <= 20 || st->time_since_unload <= 20)) num += st->facilities.Count();
}
_score_part[owner][SCORE_STATIONS] = num;
_score_part[owner][ScoreID::Stations] = num;
}
/* Generate statistics depending on recent income statistics */
@@ -251,8 +251,8 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
if (numec != 0) {
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;
if (min_income > 0) _score_part[owner][ScoreID::MinIncome] = min_income;
_score_part[owner][ScoreID::MaxIncome] = max_income;
}
}
@@ -263,25 +263,25 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
OverflowSafeInt64 total_delivered = 0;
for (auto &ce : c->old_economy | std::views::take(numec)) total_delivered += ce.delivered_cargo.GetSum<OverflowSafeInt64>();
_score_part[owner][SCORE_DELIVERED] = total_delivered;
_score_part[owner][ScoreID::Delivered] = total_delivered;
}
}
/* Generate score for variety of cargo */
{
_score_part[owner][SCORE_CARGO] = c->old_economy[0].delivered_cargo.GetCount();
_score_part[owner][ScoreID::Cargo] = c->old_economy[0].delivered_cargo.GetCount();
}
/* Generate score for company's money */
{
if (c->money > 0) {
_score_part[owner][SCORE_MONEY] = c->money;
_score_part[owner][ScoreID::Money] = c->money;
}
}
/* Generate score for loan */
{
_score_part[owner][SCORE_LOAN] = _score_info[SCORE_LOAN].needed - c->current_loan;
_score_part[owner][ScoreID::Loan] = _score_info[ScoreID::Loan].needed - c->current_loan;
}
/* Now we calculate the score for each item.. */
@@ -289,16 +289,16 @@ int UpdateCompanyRatingAndValue(Company *c, bool update)
int total_score = 0;
int s;
score = 0;
for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
for (ScoreID i = ScoreID::Begin; i < ScoreID::End; i++) {
/* Skip the total */
if (i == SCORE_TOTAL) continue;
if (i == ScoreID::Total) continue;
/* Check the score */
s = Clamp<int64_t>(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
score += s;
total_score += _score_info[i].score;
}
_score_part[owner][SCORE_TOTAL] = score;
_score_part[owner][ScoreID::Total] = score;
/* We always want the score scaled to SCORE_MAX (1000) */
if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score;

View File

@@ -22,8 +22,8 @@
void ResetPriceBaseMultipliers();
void SetPriceBaseMultiplier(Price price, int factor);
extern const ScoreInfo _score_info[];
extern TypedIndexContainer<std::array<std::array<int64_t, SCORE_END>, MAX_COMPANIES>, CompanyID> _score_part;
extern const EnumClassIndexContainer<std::array<ScoreInfo, to_underlying(ScoreID::End)>, ScoreID> _score_info;
extern TypedIndexContainer<std::array<EnumClassIndexContainer<std::array<int64_t, to_underlying(ScoreID::End)>, ScoreID>, MAX_COMPANIES>, CompanyID> _score_part;
extern Economy _economy;
/* Prices and also the fractional part. */
extern Prices _price;

View File

@@ -58,19 +58,19 @@ struct Economy {
};
/** Score categories in the detailed performance rating. */
enum ScoreID : uint8_t {
SCORE_BEGIN = 0, ///< The lowest valid value.
SCORE_VEHICLES = 0, ///< Number of vehicles that turned profit last year.
SCORE_STATIONS = 1, ///< Number of recently-serviced stations.
SCORE_MIN_PROFIT = 2, ///< The profit of the vehicle with the lowest income.
SCORE_MIN_INCOME = 3, ///< Income in the quater with the lowest profit of the last 12 quaters.
SCORE_MAX_INCOME = 4, ///< Income in the quater with the highest profit of the last 12 quaters.
SCORE_DELIVERED = 5, ///< Units of cargo delivered in the last four quaters.
SCORE_CARGO = 6, ///< Number of types of cargo delivered in the last four quaters.
SCORE_MONEY = 7, ///< Amount of money company has in the bank.
SCORE_LOAN = 8, ///< The amount of money company can take as a loan.
SCORE_TOTAL = 9, ///< Total points out of possible points ,must always be the last entry.
SCORE_END = 10, ///< Score ID end marker.
enum class ScoreID : uint8_t {
Begin, ///< The lowest valid value.
Vehicles = ScoreID::Begin, ///< Number of vehicles that turned profit last year.
Stations, ///< Number of recently-serviced stations.
MinProfit, ///< The profit of the vehicle with the lowest income.
MinIncome, ///< Income in the quater with the lowest profit of the last 12 quaters.
MaxIncome, ///< Income in the quater with the highest profit of the last 12 quaters.
Delivered, ///< Units of cargo delivered in the last four quaters.
Cargo, ///< Number of types of cargo delivered in the last four quaters.
Money, ///< Amount of money company has in the bank.
Loan, ///< The amount of money company can take as a loan.
Total, ///< Total points out of possible points ,must always be the last entry.
End, ///< Score ID end marker.
};
DECLARE_INCREMENT_DECREMENT_OPERATORS(ScoreID)
@@ -82,8 +82,8 @@ static constexpr int SCORE_MAX = 1000;
/** Data structure for storing how the score is computed for a single score id. */
struct ScoreInfo {
int needed; ///< How much you need to get the perfect score
int score; ///< How much score it will give
int needed; ///< How much you need to get the perfect score
};
/**

View File

@@ -1493,8 +1493,8 @@ struct PerformanceRatingDetailWindow : Window {
size.height = this->bar_height + WidgetDimensions::scaled.matrix.Vertical();
uint score_info_width = 0;
for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
score_info_width = std::max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
for (ScoreID i = ScoreID::Begin; i < ScoreID::End; i++) {
score_info_width = std::max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + to_underlying(i)).width);
}
score_info_width += GetStringBoundingBox(GetString(STR_JUST_COMMA, GetParamMaxValue(1000))).width + WidgetDimensions::scaled.hsep_wide;
@@ -1565,16 +1565,16 @@ struct PerformanceRatingDetailWindow : Window {
int64_t needed = _score_info[score_type].needed;
int score = _score_info[score_type].score;
/* SCORE_TOTAL has its own rules ;) */
if (score_type == SCORE_TOTAL) {
for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
/* ScoreID::Total has its own rules ;) */
if (score_type == ScoreID::Total) {
for (ScoreID i = ScoreID::Begin; i < ScoreID::End; i++) score += _score_info[i].score;
needed = SCORE_MAX;
}
uint bar_top = CentreBounds(r.top, r.bottom, this->bar_height);
uint text_top = CentreBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL));
DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + to_underlying(score_type));
/* Draw the score */
DrawString(this->score_info_left, this->score_info_right, text_top, GetString(STR_JUST_COMMA, score), TC_BLACK, SA_RIGHT);
@@ -1595,17 +1595,17 @@ struct PerformanceRatingDetailWindow : Window {
/* Draw it */
DrawString(this->bar_left, this->bar_right, text_top, GetString(STR_PERFORMANCE_DETAIL_PERCENT, Clamp<int64_t>(val, 0, needed) * 100 / needed), TC_FROMSTRING, SA_HOR_CENTER);
/* SCORE_LOAN is inverted */
if (score_type == SCORE_LOAN) val = needed - val;
/* ScoreID::Loan is inverted */
if (score_type == ScoreID::Loan) val = needed - val;
/* Draw the amount we have against what is needed
* For some of them it is in currency format */
switch (score_type) {
case SCORE_MIN_PROFIT:
case SCORE_MIN_INCOME:
case SCORE_MAX_INCOME:
case SCORE_MONEY:
case SCORE_LOAN:
case ScoreID::MinProfit:
case ScoreID::MinIncome:
case ScoreID::MaxIncome:
case ScoreID::Money:
case ScoreID::Loan:
DrawString(this->score_detail_left, this->score_detail_right, text_top, GetString(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY, val, needed));
break;
default:
@@ -2017,7 +2017,7 @@ static std::unique_ptr<NWidgetBase> MakePerformanceDetailPanels()
STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
};
static_assert(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
static_assert(lengthof(performance_tips) == to_underlying(ScoreID::End));
auto vert = std::make_unique<NWidgetVertical>(NWidContainerFlag::EqualSize);
for (WidgetID widnum = WID_PRD_SCORE_FIRST; widnum <= WID_PRD_SCORE_LAST; widnum++) {

View File

@@ -45,7 +45,7 @@ enum GraphWidgets : WidgetID {
/** Widget of the #PerformanceRatingDetailWindow class. */
enum PerformanceRatingDetailsWidgets : WidgetID {
WID_PRD_SCORE_FIRST, ///< First entry in the score list.
WID_PRD_SCORE_LAST = WID_PRD_SCORE_FIRST + (SCORE_END - SCORE_BEGIN) - 1, ///< Last entry in the score list.
WID_PRD_SCORE_LAST = WID_PRD_SCORE_FIRST + to_underlying(ScoreID::End) - 1, ///< Last entry in the score list.
WID_PRD_COMPANY_FIRST, ///< First company.
WID_PRD_COMPANY_LAST = WID_PRD_COMPANY_FIRST + MAX_COMPANIES - 1, ///< Last company.