changeset 13489:afa5c1a70640 draft

(svn r18008) -Codechange: Rename NUM_PRICES to PR_END, and use the Price enum some more.
author frosch <frosch@openttd.org>
date Sun, 08 Nov 2009 12:23:02 +0000
parents aba01968c037
children 369cd73e98d3
files src/economy.cpp src/economy_func.h src/economy_type.h src/newgrf.cpp src/saveload/economy_sl.cpp src/table/pricebase.h
diffstat 6 files changed, 23 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -109,7 +109,7 @@
 Economy _economy;
 Prices _price;
 Money _additional_cash_required;
-static byte _price_base_multiplier[NUM_PRICES];
+static byte _price_base_multiplier[PR_END];
 
 Money CalculateCompanyValue(const Company *c)
 {
@@ -625,10 +625,8 @@
 	/* Setup maximum loan */
 	_economy.max_loan = (_settings_game.difficulty.max_loan * _economy.inflation_prices >> 16) / 50000 * 50000;
 
-	assert_compile(sizeof(_price) == NUM_PRICES * sizeof(Money));
-
 	/* Setup price bases */
-	for (uint i = 0; i < NUM_PRICES; i++) {
+	for (Price i = PR_BEGIN; i < PR_END; i++) {
 		Money price = _price_base_specs[i].start_price;
 
 		/* Apply difficulty settings */
@@ -662,7 +660,7 @@
 		}
 
 		/* Store value */
-		((Money *)&_price)[i] = price;
+		_price[i] = price;
 	}
 
 	/* Setup cargo payment */
@@ -730,10 +728,8 @@
  */
 void ResetPriceBaseMultipliers()
 {
-	uint i;
-
 	/* 8 means no multiplier. */
-	for (i = 0; i < NUM_PRICES; i++)
+	for (Price i = PR_BEGIN; i < PR_END; i++)
 		_price_base_multiplier[i] = 8;
 }
 
@@ -744,9 +740,9 @@
  * @param price Index of price base to change.
  * @param factor Amount to change by.
  */
-void SetPriceBaseMultiplier(uint price, byte factor)
+void SetPriceBaseMultiplier(Price price, byte factor)
 {
-	assert(price < NUM_PRICES);
+	assert(price < PR_END);
 	_price_base_multiplier[price] = min(factor, MAX_PRICE_MODIFIER);
 }
 
@@ -800,7 +796,7 @@
  */
 Money GetPriceByIndex(Price index)
 {
-	if (index >= NUM_PRICES) return 0;
+	if (index >= PR_END) return 0;
 
 	return _price[index];
 }
--- a/src/economy_func.h
+++ b/src/economy_func.h
@@ -23,7 +23,7 @@
 #include "station_type.h"
 
 void ResetPriceBaseMultipliers();
-void SetPriceBaseMultiplier(uint price, byte factor);
+void SetPriceBaseMultiplier(Price price, byte factor);
 
 extern const ScoreInfo _score_info[];
 extern int _score_part[MAX_COMPANIES][SCORE_END];
--- a/src/economy_type.h
+++ b/src/economy_type.h
@@ -59,7 +59,12 @@
 	int score;  ///< How much score it will give
 };
 
+/**
+ * Enumeration of all base prices for use with #Prices.
+ * The prices are ordered as they are expected by NewGRF cost multipliers, so don't shuffle them.
+ */
 enum Price {
+	PR_BEGIN = 0,
 	PR_STATION_VALUE = 0,
 	PR_BUILD_RAIL,
 	PR_BUILD_ROAD,
@@ -110,11 +115,12 @@
 	PR_RUNNING_SHIP,
 	PR_BUILD_INDUSTRY,
 
-	NUM_PRICES,
+	PR_END,
 	INVALID_PRICE = 0xFF
 };
+DECLARE_POSTFIX_INCREMENT(Price)
 
-typedef Money Prices[NUM_PRICES];
+typedef Money Prices[PR_END];
 
 enum ExpensesType {
 	EXPENSES_CONSTRUCTION =  0,
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -435,7 +435,7 @@
 	static const uint32 start = 0x4B34; ///< Position of first base price
 	static const uint32 size  = 6;      ///< Size of each base price record
 
-	if (base_pointer < start || (base_pointer - start) % size != 0 || (base_pointer - start) / size >= NUM_PRICES) {
+	if (base_pointer < start || (base_pointer - start) % size != 0 || (base_pointer - start) / size >= PR_END) {
 		grfmsg(1, "%s: Unsupported running cost base 0x%04X, ignoring", error_location, base_pointer);
 		return;
 	}
@@ -1668,8 +1668,8 @@
 				byte factor = grf_load_byte(&buf);
 				uint price = gvid + i;
 
-				if (price < NUM_PRICES) {
-					SetPriceBaseMultiplier(price, factor);
+				if (price < PR_END) {
+					SetPriceBaseMultiplier((Price)price, factor);
 				} else {
 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
 				}
--- a/src/saveload/economy_sl.cpp
+++ b/src/saveload/economy_sl.cpp
@@ -18,9 +18,10 @@
 /** Prices in pre 126 savegames */
 static void Load_PRIC()
 {
+	/* Old games store 49 base prices, very old games store them as int32 */
 	int vt = CheckSavegameVersion(65) ? SLE_FILE_I32 : SLE_FILE_I64;
-	SlArray(NULL, NUM_PRICES, vt | SLE_VAR_NULL);
-	SlArray(NULL, NUM_PRICES, SLE_FILE_U16 | SLE_VAR_NULL);
+	SlArray(NULL, 49, vt | SLE_VAR_NULL);
+	SlArray(NULL, 49, SLE_FILE_U16 | SLE_VAR_NULL);
 }
 
 /** Cargo payment rates in pre 126 savegames */
--- a/src/table/pricebase.h
+++ b/src/table/pricebase.h
@@ -9,7 +9,7 @@
 
 /** @file pricebase.h Price Bases */
 
-static const PriceBaseSpec _price_base_specs[NUM_PRICES] = {
+static const PriceBaseSpec _price_base_specs[PR_END] = {
 	{    100, PCAT_NONE        }, ///< PR_STATION_VALUE
 	{    100, PCAT_CONSTRUCTION}, ///< PR_BUILD_RAIL
 	{     95, PCAT_CONSTRUCTION}, ///< PR_BUILD_ROAD