(svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
authorrubidium
Mon, 18 Jun 2007 22:49:55 +0000
changeset 6955 da3431cb25a9
parent 6954 76ad0c521767
child 6956 3579bfc5157b
(svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
src/economy.cpp
src/openttd.h
src/player.h
src/players.cpp
src/saveload.cpp
src/station_cmd.cpp
src/vehicle.cpp
--- a/src/economy.cpp	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/economy.cpp	Mon Jun 18 22:49:55 2007 +0000
@@ -93,7 +93,7 @@
 	value.AddCost(-p->current_loan);
 	value.AddCost(p->player_money);
 
-	return max(value.GetCost(), 1);
+	return max(value.GetCost(), 1LL);
 }
 
 /** if update is set to true, the economy is updated with this score
@@ -1451,7 +1451,7 @@
 	}
 
 	/* Ensure a negative total is only applied to the vehicle if there is value to reduce. */
-	front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0);
+	front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0LL);
 
 	if (virtual_profit_total > 0) {
 		ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit_total);
@@ -1920,7 +1920,8 @@
 /** Prices */
 static void SaveLoad_PRIC()
 {
-	SlArray(&_price,      NUM_PRICES, SLE_INT32);
+	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
+	SlArray(&_price,      NUM_PRICES, vt);
 	SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
 }
 
@@ -1928,18 +1929,21 @@
 static void SaveLoad_CAPR()
 {
 	uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
-	SlArray(&_cargo_payment_rates,      num_cargo, SLE_INT32);
+	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
+	SlArray(&_cargo_payment_rates,      num_cargo, vt);
 	SlArray(&_cargo_payment_rates_frac, num_cargo, SLE_UINT16);
 }
 
 static const SaveLoad _economy_desc[] = {
-	SLE_VAR(Economy, max_loan,         SLE_INT32),
-	SLE_VAR(Economy, max_loan_unround, SLE_INT32),
-	SLE_VAR(Economy, fluct,            SLE_FILE_I16 | SLE_VAR_I32),
-	SLE_VAR(Economy, interest_rate,    SLE_UINT8),
-	SLE_VAR(Economy, infl_amount,      SLE_UINT8),
-	SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),
-	SLE_END()
+	SLE_CONDVAR(Economy, max_loan,         SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
+	SLE_CONDVAR(Economy, max_loan,         SLE_INT64,                  65, SL_MAX_VERSION),
+	SLE_CONDVAR(Economy, max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
+	SLE_CONDVAR(Economy, max_loan_unround, SLE_INT64,                  65, SL_MAX_VERSION),
+	    SLE_VAR(Economy, fluct,            SLE_FILE_I16 | SLE_VAR_I32),
+	    SLE_VAR(Economy, interest_rate,    SLE_UINT8),
+	    SLE_VAR(Economy, infl_amount,      SLE_UINT8),
+	    SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),
+	    SLE_END()
 };
 
 /** Economy variables */
--- a/src/openttd.h	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/openttd.h	Mon Jun 18 22:49:55 2007 +0000
@@ -69,7 +69,7 @@
 typedef uint16 GroupID;
 typedef uint16 EngineRenewID;
 typedef uint16 DestinationID;
-typedef int32 Money;
+typedef int64 Money;
 
 /* DestinationID must be at least as large as every these below, because it can
  * be any of them
@@ -386,8 +386,6 @@
 	 * @param cst the initial cost of this command
 	 */
 	CommandCost(Money cst) : cost(cst), message(INVALID_STRING_ID), success(true) {}
-	/** "Hack" to make everything compile nicely, not needed when cost is int64 */
-	CommandCost(uint cst) : cost(cst), message(INVALID_STRING_ID), success(true) {}
 
 	/**
 	 * Adds the cost of the given command return value to this cost.
--- a/src/player.h	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/player.h	Mon Jun 18 22:49:55 2007 +0000
@@ -16,7 +16,7 @@
 	Money expenses;
 	int32 delivered_cargo;
 	int32 performance_history; ///< player score (scale 0-1000)
-	int64 company_value;
+	Money company_value;
 };
 
 struct AiBuildRec {
@@ -166,7 +166,7 @@
 
 	PlayerFace face;
 
-	int64 player_money;
+	Money player_money;
 	Money current_loan;
 
 	byte player_color;
@@ -197,7 +197,7 @@
 	PlayerAI ai;
 	PlayerAiNew ainew;
 
-	int64 yearly_expenses[3][13];
+	Money yearly_expenses[3][13];
 	PlayerEconomyEntry cur_economy;
 	PlayerEconomyEntry old_economy[24];
 	EngineRenewList engine_renew_list; ///< Defined later
--- a/src/players.cpp	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/players.cpp	Mon Jun 18 22:49:55 2007 +0000
@@ -188,11 +188,11 @@
 
 static void SubtractMoneyFromAnyPlayer(Player *p, CommandCost cost)
 {
-	CommandCost tmp((int32)p->player_money);
+	CommandCost tmp(p->player_money);
 	tmp.AddCost(-cost.GetCost());
 	p->player_money = tmp.GetCost();
 
-	tmp = CommandCost((int32)p->yearly_expenses[0][_yearly_expenses_type]);
+	tmp = CommandCost(p->yearly_expenses[0][_yearly_expenses_type]);
 	tmp.AddCost(cost);
 	p->yearly_expenses[0][_yearly_expenses_type] = tmp.GetCost();
 
@@ -1130,10 +1130,11 @@
 	    SLE_VAR(Player, face,            SLE_UINT32),
 
 	/* money was changed to a 64 bit field in savegame version 1. */
-	SLE_CONDVAR(Player, player_money,          SLE_VAR_I64 | SLE_FILE_I32, 0, 0),
-	SLE_CONDVAR(Player, player_money,          SLE_INT64, 1, SL_MAX_VERSION),
+	SLE_CONDVAR(Player, player_money,          SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
+	SLE_CONDVAR(Player, player_money,          SLE_INT64,                   1, SL_MAX_VERSION),
 
-	    SLE_VAR(Player, current_loan,          SLE_INT32),
+	SLE_CONDVAR(Player, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
+	SLE_CONDVAR(Player, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
 
 	    SLE_VAR(Player, player_color,          SLE_UINT8),
 	    SLE_VAR(Player, player_money_fraction, SLE_UINT8),
@@ -1155,7 +1156,8 @@
 	    SLE_VAR(Player, quarters_of_bankrupcy, SLE_UINT8),
 	    SLE_VAR(Player, bankrupt_asked,        SLE_UINT8),
 	    SLE_VAR(Player, bankrupt_timeout,      SLE_INT16),
-	    SLE_VAR(Player, bankrupt_value,        SLE_INT32),
+	SLE_CONDVAR(Player, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
+	SLE_CONDVAR(Player, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
 
 	/* yearly expenses was changed to 64-bit in savegame version 2. */
 	SLE_CONDARR(Player, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
@@ -1180,11 +1182,11 @@
 
 static const SaveLoad _player_economy_desc[] = {
 	/* these were changed to 64-bit in savegame format 2 */
-	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_INT32,                  0, 1),
-	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_INT32,                  0, 1),
+	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
+	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
+	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
+	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
 	SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
-	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_FILE_I64 | SLE_VAR_I32, 2, SL_MAX_VERSION),
-	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_FILE_I64 | SLE_VAR_I32, 2, SL_MAX_VERSION),
 	SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
 
 	    SLE_VAR(PlayerEconomyEntry, delivered_cargo,     SLE_INT32),
--- a/src/saveload.cpp	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/saveload.cpp	Mon Jun 18 22:49:55 2007 +0000
@@ -29,7 +29,7 @@
 #include <setjmp.h>
 #include <list>
 
-extern const uint16 SAVEGAME_VERSION = 64;
+extern const uint16 SAVEGAME_VERSION = 65;
 uint16 _sl_version;       ///< the major savegame version identifier
 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
 
--- a/src/station_cmd.cpp	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/station_cmd.cpp	Mon Jun 18 22:49:55 2007 +0000
@@ -2917,7 +2917,8 @@
 	    SLE_VAR(GoodsEntry, enroute_time,       SLE_UINT8),
 	    SLE_VAR(GoodsEntry, last_speed,         SLE_UINT8),
 	    SLE_VAR(GoodsEntry, last_age,           SLE_UINT8),
-	SLE_CONDVAR(GoodsEntry, feeder_profit,      SLE_INT32,                 14, SL_MAX_VERSION),
+	SLE_CONDVAR(GoodsEntry, feeder_profit,      SLE_FILE_I32 | SLE_VAR_I64,14, 64),
+	SLE_CONDVAR(GoodsEntry, feeder_profit,      SLE_INT64,                 65, SL_MAX_VERSION),
 
 	SLE_END()
 };
--- a/src/vehicle.cpp	Mon Jun 18 22:09:54 2007 +0000
+++ b/src/vehicle.cpp	Mon Jun 18 22:49:55 2007 +0000
@@ -2810,11 +2810,15 @@
 	SLE_CONDVAR(Vehicle, cargo_paid_for,       SLE_UINT16,                45, SL_MAX_VERSION),
 	SLE_CONDVAR(Vehicle, vehicle_flags,        SLE_UINT8,                 40, SL_MAX_VERSION),
 
-	    SLE_VAR(Vehicle, profit_this_year,     SLE_INT32),
-	    SLE_VAR(Vehicle, profit_last_year,     SLE_INT32),
-	SLE_CONDVAR(Vehicle, cargo_feeder_share,   SLE_INT32,                 51, SL_MAX_VERSION),
+	SLE_CONDVAR(Vehicle, profit_this_year,     SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
+	SLE_CONDVAR(Vehicle, profit_this_year,     SLE_INT64,                 65, SL_MAX_VERSION),
+	SLE_CONDVAR(Vehicle, profit_last_year,     SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
+	SLE_CONDVAR(Vehicle, profit_last_year,     SLE_INT64,                 65, SL_MAX_VERSION),
+	SLE_CONDVAR(Vehicle, cargo_feeder_share,   SLE_FILE_I32 | SLE_VAR_I64,51, 64),
+	SLE_CONDVAR(Vehicle, cargo_feeder_share,   SLE_INT64,                 65, SL_MAX_VERSION),
 	SLE_CONDVAR(Vehicle, cargo_loaded_at_xy,   SLE_UINT32,                51, SL_MAX_VERSION),
-	    SLE_VAR(Vehicle, value,                SLE_UINT32),
+	SLE_CONDVAR(Vehicle, value,                SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
+	SLE_CONDVAR(Vehicle, value,                SLE_INT64,                 65, SL_MAX_VERSION),
 
 	    SLE_VAR(Vehicle, random_bits,          SLE_UINT8),
 	    SLE_VAR(Vehicle, waiting_triggers,     SLE_UINT8),