1
0
Fork 0

Codechange: Using alias and std::array for company expense storage. (#11273)

This simplifies passing yearly expenses to functions and use of std algorithms.
pull/11272/head
PeterN 2023-09-09 14:15:53 +01:00 committed by GitHub
parent 00f13282a9
commit afc1ea8135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 9 deletions

View File

@ -92,7 +92,7 @@ struct CompanyProperties {
*/ */
bool is_ai; bool is_ai;
Money yearly_expenses[3][EXPENSES_END]; ///< Expenses of the company for the last three years, in every #ExpensesType category. std::array<Expenses, 3> yearly_expenses{}; ///< Expenses of the company for the last three years.
CompanyEconomyEntry cur_economy; ///< Economic data of the company of this quarter. CompanyEconomyEntry cur_economy; ///< Economic data of the company of this quarter.
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]; ///< Economic data of the company of the last #MAX_HISTORY_QUARTERS quarters. CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]; ///< Economic data of the company of the last #MAX_HISTORY_QUARTERS quarters.
byte num_valid_stat_ent; ///< Number of valid statistical entries in #old_economy. byte num_valid_stat_ent; ///< Number of valid statistical entries in #old_economy.

View File

@ -750,8 +750,9 @@ static IntervalTimer<TimerGameCalendar> _companies_yearly({TimerGameCalendar::YE
{ {
/* Copy statistics */ /* Copy statistics */
for (Company *c : Company::Iterate()) { for (Company *c : Company::Iterate()) {
memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0])); /* Move expenses to previous years. */
memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0])); std::rotate(std::rbegin(c->yearly_expenses), std::rbegin(c->yearly_expenses) + 1, std::rend(c->yearly_expenses));
c->yearly_expenses[0] = {};
SetWindowDirty(WC_FINANCES, c->index); SetWindowDirty(WC_FINANCES, c->index);
} }

View File

@ -229,7 +229,7 @@ static void DrawPrice(Money amount, int left, int right, int top, TextColour col
* Draw a category of expenses/revenues in the year column. * Draw a category of expenses/revenues in the year column.
* @return The income sum of the category. * @return The income sum of the category.
*/ */
static Money DrawYearCategory (const Rect &r, int start_y, ExpensesList list, const Money(&tbl)[EXPENSES_END]) static Money DrawYearCategory(const Rect &r, int start_y, ExpensesList list, const Expenses &tbl)
{ {
int y = start_y; int y = start_y;
ExpensesType et; ExpensesType et;
@ -260,7 +260,7 @@ static Money DrawYearCategory (const Rect &r, int start_y, ExpensesList list, co
* @param tbl Reference to table of amounts for \a year. * @param tbl Reference to table of amounts for \a year.
* @note The environment must provide padding at the left and right of \a r. * @note The environment must provide padding at the left and right of \a r.
*/ */
static void DrawYearColumn(const Rect &r, TimerGameCalendar::Year year, const Money (&tbl)[EXPENSES_END]) static void DrawYearColumn(const Rect &r, TimerGameCalendar::Year year, const Expenses &tbl)
{ {
int y = r.top; int y = r.top;
Money sum; Money sum;

View File

@ -172,6 +172,11 @@ enum ExpensesType : byte {
INVALID_EXPENSES = 0xFF, ///< Invalid expense type. INVALID_EXPENSES = 0xFF, ///< Invalid expense type.
}; };
/**
* Data type for storage of Money for each #ExpensesType category.
*/
using Expenses = std::array<Money, EXPENSES_END>;
/** /**
* Categories of a price bases. * Categories of a price bases.
*/ */

View File

@ -383,10 +383,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyEconomy()
{ {
for (const Company *company : Company::Iterate()) { for (const Company *company : Company::Iterate()) {
/* Get the income. */ /* Get the income. */
Money income = 0; Money income = -std::reduce(std::begin(company->yearly_expenses[0]), std::end(company->yearly_expenses[0]));
for (uint i = 0; i < lengthof(company->yearly_expenses[0]); i++) {
income -= company->yearly_expenses[0][i];
}
Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_ECONOMY); Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_ECONOMY);