Changeset - r27872:bb4e800961d7
[Not reviewed]
master
0 5 0
PeterN - 9 months ago 2023-09-09 13:15:53
peter1138@openttd.org
Codechange: Using alias and std::array for company expense storage. (#11273)

This simplifies passing yearly expenses to functions and use of std algorithms.
5 files changed with 12 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/company_base.h
Show inline comments
 
@@ -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<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 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.
src/company_cmd.cpp
Show inline comments
 
@@ -750,8 +750,9 @@ static IntervalTimer<TimerGameCalendar> 
 
{
 
	/* 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);
 
	}
 

	
src/company_gui.cpp
Show inline comments
 
@@ -229,7 +229,7 @@ static void DrawPrice(Money amount, int 
 
 * 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 Rec
 
 * @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;
src/economy_type.h
Show inline comments
 
@@ -173,6 +173,11 @@ enum ExpensesType : byte {
 
};
 

	
 
/**
 
 * Data type for storage of Money for each #ExpensesType category.
 
 */
 
using Expenses = std::array<Money, EXPENSES_END>;
 

	
 
/**
 
 * Categories of a price bases.
 
 */
 
enum PriceCategory {
src/network/network_admin.cpp
Show inline comments
 
@@ -383,10 +383,7 @@ NetworkRecvStatus ServerNetworkAdminSock
 
{
 
	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);
 

	
0 comments (0 inline, 0 general)