changeset 18397:2403d22fbd81 draft

(svn r23233) -Codechange: Refactor maximum and actually transported cargo amount of towns into a reusable struct.
author michi_cc <michi_cc@openttd.org>
date Tue, 15 Nov 2011 20:47:53 +0000
parents 28b350769e53
children 78922d7138a3
files src/ai/api/ai_town.cpp src/economy.cpp src/newgrf_town.cpp src/saveload/oldloader_sl.cpp src/saveload/town_sl.cpp src/town.h src/town_cmd.cpp src/town_gui.cpp src/town_type.h
diffstat 9 files changed, 110 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/api/ai_town.cpp
+++ b/src/ai/api/ai_town.cpp
@@ -71,8 +71,8 @@
 	const Town *t = ::Town::Get(town_id);
 
 	switch (AICargo::GetTownEffect(cargo_id)) {
-		case AICargo::TE_PASSENGERS: return t->max_pass;
-		case AICargo::TE_MAIL:       return t->max_mail;
+		case AICargo::TE_PASSENGERS: return t->pass.old_max;
+		case AICargo::TE_MAIL:       return t->mail.old_max;
 		default: return -1;
 	}
 }
@@ -85,8 +85,8 @@
 	const Town *t = ::Town::Get(town_id);
 
 	switch (AICargo::GetTownEffect(cargo_id)) {
-		case AICargo::TE_PASSENGERS: return t->act_pass;
-		case AICargo::TE_MAIL:       return t->act_mail;
+		case AICargo::TE_PASSENGERS: return t->pass.old_act;
+		case AICargo::TE_MAIL:       return t->mail.old_act;
 		default: return -1;
 	}
 }
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -990,8 +990,8 @@
 
 	/* Increase town's counter for some special goods types */
 	const CargoSpec *cs = CargoSpec::Get(cargo_type);
-	if (cs->town_effect == TE_FOOD) st->town->new_act_food += accepted;
-	if (cs->town_effect == TE_WATER) st->town->new_act_water += accepted;
+	if (cs->town_effect == TE_FOOD) st->town->food.new_act += accepted;
+	if (cs->town_effect == TE_WATER) st->town->water.new_act += accepted;
 
 	/* Determine profit */
 	Money profit = GetTransportedGoodsIncome(accepted, DistanceManhattan(source_tile, st->xy), days_in_transit, cargo_type);
--- a/src/newgrf_town.cpp
+++ b/src/newgrf_town.cpp
@@ -91,32 +91,32 @@
 		case 0xB2: return t->statues;
 		case 0xB6: return ClampToU16(t->num_houses);
 		case 0xB9: return t->growth_rate;
-		case 0xBA: return ClampToU16(t->new_max_pass);
-		case 0xBB: return GB(ClampToU16(t->new_max_pass), 8, 8);
-		case 0xBC: return ClampToU16(t->new_max_mail);
-		case 0xBD: return GB(ClampToU16(t->new_max_mail), 8, 8);
-		case 0xBE: return ClampToU16(t->new_act_pass);
-		case 0xBF: return GB(ClampToU16(t->new_act_pass), 8, 8);
-		case 0xC0: return ClampToU16(t->new_act_mail);
-		case 0xC1: return GB(ClampToU16(t->new_act_mail), 8, 8);
-		case 0xC2: return ClampToU16(t->max_pass);
-		case 0xC3: return GB(ClampToU16(t->max_pass), 8, 8);
-		case 0xC4: return ClampToU16(t->max_mail);
-		case 0xC5: return GB(ClampToU16(t->max_mail), 8, 8);
-		case 0xC6: return ClampToU16(t->act_pass);
-		case 0xC7: return GB(ClampToU16(t->act_pass), 8, 8);
-		case 0xC8: return ClampToU16(t->act_mail);
-		case 0xC9: return GB(ClampToU16(t->act_mail), 8, 8);
+		case 0xBA: return ClampToU16(t->pass.new_max);
+		case 0xBB: return GB(ClampToU16(t->pass.new_max), 8, 8);
+		case 0xBC: return ClampToU16(t->mail.new_max);
+		case 0xBD: return GB(ClampToU16(t->mail.new_max), 8, 8);
+		case 0xBE: return ClampToU16(t->pass.new_act);
+		case 0xBF: return GB(ClampToU16(t->pass.new_act), 8, 8);
+		case 0xC0: return ClampToU16(t->mail.new_act);
+		case 0xC1: return GB(ClampToU16(t->mail.new_act), 8, 8);
+		case 0xC2: return ClampToU16(t->pass.old_max);
+		case 0xC3: return GB(ClampToU16(t->pass.old_max), 8, 8);
+		case 0xC4: return ClampToU16(t->mail.old_max);
+		case 0xC5: return GB(ClampToU16(t->mail.old_max), 8, 8);
+		case 0xC6: return ClampToU16(t->pass.old_act);
+		case 0xC7: return GB(ClampToU16(t->pass.old_act), 8, 8);
+		case 0xC8: return ClampToU16(t->mail.old_act);
+		case 0xC9: return GB(ClampToU16(t->mail.old_act), 8, 8);
 		case 0xCA: return t->GetPercentPassTransported();
 		case 0xCB: return t->GetPercentMailTransported();
-		case 0xCC: return t->new_act_food;
-		case 0xCD: return GB(t->new_act_food, 8, 8);
-		case 0xCE: return t->new_act_water;
-		case 0xCF: return GB(t->new_act_water, 8, 8);
-		case 0xD0: return t->act_food;
-		case 0xD1: return GB(t->act_food, 8, 8);
-		case 0xD2: return t->act_water;
-		case 0xD3: return GB(t->act_water, 8, 8);
+		case 0xCC: return t->food.new_act;
+		case 0xCD: return GB(t->food.new_act, 8, 8);
+		case 0xCE: return t->water.new_act;
+		case 0xCF: return GB(t->water.new_act, 8, 8);
+		case 0xD0: return t->food.old_act;
+		case 0xD1: return GB(t->food.old_act, 8, 8);
+		case 0xD2: return t->water.old_act;
+		case 0xD3: return GB(t->water.old_act, 8, 8);
 		case 0xD4: return t->road_build_months;
 		case 0xD5: return t->fund_buildings_months;
 	}
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -568,21 +568,21 @@
 	OCL_SVAR(  OC_FILE_U8 | OC_VAR_U16, Town, time_until_rebuild ),
 	OCL_SVAR(  OC_FILE_U8 | OC_VAR_I16, Town, growth_rate ),
 
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, new_max_pass ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, new_max_mail ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, new_act_pass ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, new_act_mail ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, max_pass ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, max_mail ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, act_pass ),
-	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, act_mail ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, pass.new_max ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, mail.new_max ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, pass.new_act ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, mail.new_act ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, pass.old_max ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, mail.old_max ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, pass.old_act ),
+	OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Town, mail.old_act ),
 
 	OCL_NULL( 2 ),         ///< pct_pass_transported / pct_mail_transported, now computed on the fly
 
-	OCL_SVAR( OC_TTD | OC_UINT16, Town, new_act_food ),
-	OCL_SVAR( OC_TTD | OC_UINT16, Town, new_act_water ),
-	OCL_SVAR( OC_TTD | OC_UINT16, Town, act_food ),
-	OCL_SVAR( OC_TTD | OC_UINT16, Town, act_water ),
+	OCL_SVAR( OC_TTD | OC_UINT16, Town, food.new_act ),
+	OCL_SVAR( OC_TTD | OC_UINT16, Town, water.new_act ),
+	OCL_SVAR( OC_TTD | OC_UINT16, Town, food.old_act ),
+	OCL_SVAR( OC_TTD | OC_UINT16, Town, water.old_act ),
 
 	OCL_SVAR(  OC_UINT8, Town, road_build_months ),
 	OCL_SVAR(  OC_UINT8, Town, fund_buildings_months ),
--- a/src/saveload/town_sl.cpp
+++ b/src/saveload/town_sl.cpp
@@ -128,30 +128,30 @@
 	SLE_CONDARR(Town, unwanted,              SLE_INT8,  8,               4, 103),
 	SLE_CONDARR(Town, unwanted,              SLE_INT8,  MAX_COMPANIES, 104, SL_MAX_VERSION),
 
-	SLE_CONDVAR(Town, max_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, max_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, new_max_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, new_max_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, act_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, act_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, new_act_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
-	SLE_CONDVAR(Town, new_act_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, pass.old_max,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, mail.old_max,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, pass.new_max,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, mail.new_max,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, pass.old_act,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, mail.old_act,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, pass.new_act,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
+	SLE_CONDVAR(Town, mail.new_act,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
 
-	SLE_CONDVAR(Town, max_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, max_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, new_max_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, new_max_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, act_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, act_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, new_act_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
-	SLE_CONDVAR(Town, new_act_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, pass.old_max,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, mail.old_max,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, pass.new_max,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, mail.new_max,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, pass.old_act,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, mail.old_act,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, pass.new_act,          SLE_UINT32,                 9, SL_MAX_VERSION),
+	SLE_CONDVAR(Town, mail.new_act,          SLE_UINT32,                 9, SL_MAX_VERSION),
 
 	SLE_CONDNULL(2, 0, 163),                 ///< pct_pass_transported / pct_mail_transported, now computed on the fly
 
-	    SLE_VAR(Town, act_food,              SLE_UINT16),
-	    SLE_VAR(Town, act_water,             SLE_UINT16),
-	    SLE_VAR(Town, new_act_food,          SLE_UINT16),
-	    SLE_VAR(Town, new_act_water,         SLE_UINT16),
+	    SLE_VAR(Town, food.old_act,          SLE_UINT16),
+	    SLE_VAR(Town, water.old_act,         SLE_UINT16),
+	    SLE_VAR(Town, food.new_act,          SLE_UINT16),
+	    SLE_VAR(Town, water.new_act,         SLE_UINT16),
 
 	SLE_CONDVAR(Town, time_until_rebuild,    SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
 	SLE_CONDVAR(Town, grow_counter,          SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
--- a/src/town.h
+++ b/src/town.h
@@ -66,25 +66,16 @@
 	uint8 exclusive_counter;       ///< months till the exclusivity expires
 	int16 ratings[MAX_COMPANIES];  ///< ratings of each company for this town
 
-	uint32 max_pass;               ///< maximum amount of passengers that can be transported
-	uint32 max_mail;               ///< maximum amount of mail that can be transported
-	uint32 new_max_pass;
-	uint32 new_max_mail;
-	uint32 act_pass;
-	uint32 act_mail;
-	uint32 new_act_pass;
-	uint32 new_act_mail;
+	TransportedCargoStat<uint32> pass;  ///< Passenger cargo statistics.
+	TransportedCargoStat<uint32> mail;  ///< Mail cargo statistics.
+	TransportedCargoStat<uint16> food;  ///< Food cargo statistics.
+	TransportedCargoStat<uint16> water; ///< Water cargo statistics.
 
 	/** Percentage of passengers transported last month (0xFF=100%) */
-	inline byte GetPercentPassTransported() const { return this->act_pass * 256 / (this->max_pass + 1); }
+	inline byte GetPercentPassTransported() const { return this->pass.old_act * 256 / (this->pass.old_max + 1); }
 
 	/** Percentage of mail transported last month (0xFF=100%) */
-	inline byte GetPercentMailTransported() const { return this->act_mail * 256 / (this->max_mail + 1); }
-
-	uint16 act_food;               ///< amount of food that was transported
-	uint16 act_water;              ///< amount of water that was transported
-	uint16 new_act_food;
-	uint16 new_act_water;
+	inline byte GetPercentMailTransported() const { return this->mail.old_act * 256 / (this->mail.old_max + 1); }
 
 	uint16 time_until_rebuild;     ///< time until we rebuild a house
 
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -487,13 +487,13 @@
 			const CargoSpec *cs = CargoSpec::Get(cargo);
 			switch (cs->town_effect) {
 				case TE_PASSENGERS:
-					t->new_max_pass += amt;
-					t->new_act_pass += moved;
+					t->pass.new_max += amt;
+					t->pass.new_act += moved;
 					break;
 
 				case TE_MAIL:
-					t->new_max_mail += amt;
-					t->new_act_mail += moved;
+					t->mail.new_max += amt;
+					t->mail.new_act += moved;
 					break;
 
 				default:
@@ -505,16 +505,16 @@
 			uint amt = GB(r, 0, 8) / 8 + 1;
 
 			if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
-			t->new_max_pass += amt;
-			t->new_act_pass += MoveGoodsToStation(CT_PASSENGERS, amt, ST_TOWN, t->index, stations.GetStations());
+			t->pass.new_max += amt;
+			t->pass.new_act += MoveGoodsToStation(CT_PASSENGERS, amt, ST_TOWN, t->index, stations.GetStations());
 		}
 
 		if (GB(r, 8, 8) < hs->mail_generation) {
 			uint amt = GB(r, 8, 8) / 8 + 1;
 
 			if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
-			t->new_max_mail += amt;
-			t->new_act_mail += MoveGoodsToStation(CT_MAIL, amt, ST_TOWN, t->index, stations.GetStations());
+			t->mail.new_max += amt;
+			t->mail.new_act += MoveGoodsToStation(CT_MAIL, amt, ST_TOWN, t->index, stations.GetStations());
 		}
 	}
 
@@ -1401,8 +1401,8 @@
 
 void UpdateTownMaxPass(Town *t)
 {
-	t->max_pass = t->population >> 3;
-	t->max_mail = t->population >> 4;
+	t->pass.old_max = t->population >> 3;
+	t->mail.old_max = t->population >> 4;
 }
 
 /**
@@ -1426,20 +1426,8 @@
 	t->population = 0;
 	t->grow_counter = 0;
 	t->growth_rate = 250;
-	t->new_max_pass = 0;
-	t->new_max_mail = 0;
-	t->new_act_pass = 0;
-	t->new_act_mail = 0;
-	t->max_pass = 0;
-	t->max_mail = 0;
-	t->act_pass = 0;
-	t->act_mail = 0;
 
 	t->fund_buildings_months = 0;
-	t->new_act_food = 0;
-	t->new_act_water = 0;
-	t->act_food = 0;
-	t->act_water = 0;
 
 	for (uint i = 0; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
 
@@ -2783,10 +2771,10 @@
 	}
 
 	if (_settings_game.game_creation.landscape == LT_ARCTIC) {
-		if (TileHeight(t->xy) >= GetSnowLine() && t->act_food == 0 && t->population > 90) return;
+		if (TileHeight(t->xy) >= GetSnowLine() && t->food.old_act == 0 && t->population > 90) return;
 
 	} else if (_settings_game.game_creation.landscape == LT_TROPIC) {
-		if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->act_food == 0 || t->act_water == 0) && t->population > 60) return;
+		if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->food.old_act == 0 || t->water.old_act == 0) && t->population > 60) return;
 	}
 
 	/* Use the normal growth rate values if new buildings have been funded in
@@ -2806,13 +2794,10 @@
 
 static void UpdateTownAmounts(Town *t)
 {
-	t->max_pass = t->new_max_pass; t->new_max_pass = 0;
-	t->act_pass = t->new_act_pass; t->new_act_pass = 0;
-	t->act_food = t->new_act_food; t->new_act_food = 0;
-	t->act_water = t->new_act_water; t->new_act_water = 0;
-
-	t->max_mail = t->new_max_mail; t->new_max_mail = 0;
-	t->act_mail = t->new_act_mail; t->new_act_mail = 0;
+	t->pass.NewMonth();
+	t->mail.NewMonth();
+	t->food.NewMonth();
+	t->water.NewMonth();
 
 	SetWindowDirty(WC_TOWN_VIEW, t->index);
 }
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -368,12 +368,12 @@
 		SetDParam(1, this->town->num_houses);
 		DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
 
-		SetDParam(0, this->town->act_pass);
-		SetDParam(1, this->town->max_pass);
+		SetDParam(0, this->town->pass.old_act);
+		SetDParam(1, this->town->pass.old_max);
 		DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
 
-		SetDParam(0, this->town->act_mail);
-		SetDParam(1, this->town->max_mail);
+		SetDParam(0, this->town->mail.old_act);
+		SetDParam(1, this->town->mail.old_max);
 		DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
 
 		StringID required_text = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
@@ -406,9 +406,9 @@
 			CargoID first_water_cargo = (water != NULL) ? water->Index() : (CargoID)CT_INVALID;
 			StringID water_name       = (water != NULL) ? water->name    : STR_CARGO_PLURAL_WATER;
 
-			if (first_food_cargo != CT_INVALID && this->town->act_food > 0) {
+			if (first_food_cargo != CT_INVALID && this->town->food.old_act > 0) {
 				SetDParam(0, first_food_cargo);
-				SetDParam(1, this->town->act_food);
+				SetDParam(1, this->town->food.old_act);
 				DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
 			} else {
 				SetDParam(0, food_name);
@@ -416,9 +416,9 @@
 			}
 
 			if (cargo_needed_for_growth > 1) {
-				if (first_water_cargo != CT_INVALID && this->town->act_water > 0) {
+				if (first_water_cargo != CT_INVALID && this->town->water.old_act > 0) {
 					SetDParam(0, first_water_cargo);
-					SetDParam(1, this->town->act_water);
+					SetDParam(1, this->town->water.old_act);
 					DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
 				} else {
 					SetDParam(0, water_name);
--- a/src/town_type.h
+++ b/src/town_type.h
@@ -107,4 +107,22 @@
 
 static const uint MAX_LENGTH_TOWN_NAME_CHARS = 32; ///< The maximum length of a town name in characters including '\0'
 
+/** Store the maximum and actually transported cargo amount for the current and the last month. */
+template <typename Tstorage>
+struct TransportedCargoStat {
+	Tstorage old_max;  ///< Maximum amount last month
+	Tstorage new_max;  ///< Maximum amount this month
+	Tstorage old_act;  ///< Actually transported last month
+	Tstorage new_act;  ///< Actually transported this month
+
+	TransportedCargoStat() : old_max(0), new_max(0), old_act(0), new_act(0) {}
+
+	/** Update stats for a new month. */
+	void NewMonth()
+	{
+		this->old_max = this->new_max; this->new_max = 0;
+		this->old_act = this->new_act; this->new_act = 0;
+	}
+};
+
 #endif /* TOWN_TYPE_H */