From afc1ea8135d9a082f6051db6e5b1bacd4d8c85f2 Mon Sep 17 00:00:00 2001 From: PeterN Date: Sat, 9 Sep 2023 14:15:53 +0100 Subject: [PATCH] Codechange: Using alias and std::array for company expense storage. (#11273) This simplifies passing yearly expenses to functions and use of std algorithms. --- src/company_base.h | 2 +- src/company_cmd.cpp | 5 +++-- src/company_gui.cpp | 4 ++-- src/economy_type.h | 5 +++++ src/network/network_admin.cpp | 5 +---- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/company_base.h b/src/company_base.h index 53949fa500..eea9974089 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -92,7 +92,7 @@ struct CompanyProperties { */ bool is_ai; - Money yearly_expenses[3][EXPENSES_END]; ///< Expenses of the company for the last three years, in every #ExpensesType category. + std::array yearly_expenses{}; ///< Expenses of the company for the last three years. 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. byte num_valid_stat_ent; ///< Number of valid statistical entries in #old_economy. diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 2647b334f4..901fdbc063 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -750,8 +750,9 @@ static IntervalTimer _companies_yearly({TimerGameCalendar::YE { /* Copy statistics */ for (Company *c : Company::Iterate()) { - memmove(&c->yearly_expenses[1], &c->yearly_expenses[0], sizeof(c->yearly_expenses) - sizeof(c->yearly_expenses[0])); - memset(&c->yearly_expenses[0], 0, sizeof(c->yearly_expenses[0])); + /* Move expenses to previous years. */ + 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); } diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 45ca97f8e3..960ae8d37c 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -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. * @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; 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. * @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; Money sum; diff --git a/src/economy_type.h b/src/economy_type.h index 1c754476b1..763634ac3b 100644 --- a/src/economy_type.h +++ b/src/economy_type.h @@ -172,6 +172,11 @@ enum ExpensesType : byte { INVALID_EXPENSES = 0xFF, ///< Invalid expense type. }; +/** + * Data type for storage of Money for each #ExpensesType category. + */ +using Expenses = std::array; + /** * Categories of a price bases. */ diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp index 40679352d6..8a33b97c54 100644 --- a/src/network/network_admin.cpp +++ b/src/network/network_admin.cpp @@ -383,10 +383,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyEconomy() { for (const Company *company : Company::Iterate()) { /* Get the income. */ - Money income = 0; - for (uint i = 0; i < lengthof(company->yearly_expenses[0]); i++) { - income -= company->yearly_expenses[0][i]; - } + Money income = -std::reduce(std::begin(company->yearly_expenses[0]), std::end(company->yearly_expenses[0])); Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_ECONOMY);