src/players.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6725 23339968083f
--- a/src/players.cpp	Sat Jun 02 19:59:29 2007 +0000
+++ b/src/players.cpp	Sat Jul 14 19:42:58 2007 +0000
@@ -173,12 +173,12 @@
 	InvalidateWindow(WC_FINANCES, pid);
 }
 
-bool CheckPlayerHasMoney(int32 cost)
+bool CheckPlayerHasMoney(CommandCost cost)
 {
-	if (cost > 0) {
+	if (cost.GetCost() > 0) {
 		PlayerID pid = _current_player;
-		if (IsValidPlayer(pid) && cost > GetPlayer(pid)->player_money) {
-			SetDParam(0, cost);
+		if (IsValidPlayer(pid) && cost.GetCost() > GetPlayer(pid)->player_money) {
+			SetDParam(0, cost.GetCost());
 			_error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
 			return false;
 		}
@@ -186,50 +186,54 @@
 	return true;
 }
 
-static void SubtractMoneyFromAnyPlayer(Player *p, int32 cost)
+static void SubtractMoneyFromAnyPlayer(Player *p, CommandCost cost)
 {
-	p->money64 -= cost;
-	UpdatePlayerMoney32(p);
+	CommandCost tmp(p->player_money);
+	tmp.AddCost(-cost.GetCost());
+	p->player_money = tmp.GetCost();
 
-	p->yearly_expenses[0][_yearly_expenses_type] += cost;
+	tmp = CommandCost(p->yearly_expenses[0][_yearly_expenses_type]);
+	tmp.AddCost(cost);
+	p->yearly_expenses[0][_yearly_expenses_type] = tmp.GetCost();
 
-	if (HASBIT(1<<7|1<<8|1<<9|1<<10, _yearly_expenses_type)) {
-		p->cur_economy.income -= cost;
-	} else if (HASBIT(1<<2|1<<3|1<<4|1<<5|1<<6|1<<11, _yearly_expenses_type)) {
-		p->cur_economy.expenses -= cost;
+	if (HASBIT(1 << EXPENSES_TRAIN_INC    |
+	           1 << EXPENSES_ROADVEH_INC  |
+	           1 << EXPENSES_AIRCRAFT_INC |
+	           1 << EXPENSES_SHIP_INC, _yearly_expenses_type)) {
+		tmp = CommandCost(p->cur_economy.income);
+		tmp.AddCost(-cost.GetCost());
+		p->cur_economy.income = tmp.GetCost();
+	} else if (HASBIT(1 << EXPENSES_TRAIN_RUN    |
+	                  1 << EXPENSES_ROADVEH_RUN  |
+	                  1 << EXPENSES_AIRCRAFT_RUN |
+	                  1 << EXPENSES_SHIP_RUN     |
+	                  1 << EXPENSES_PROPERTY     |
+	                  1 << EXPENSES_LOAN_INT, _yearly_expenses_type)) {
+		tmp = CommandCost(p->cur_economy.expenses);
+		tmp.AddCost(-cost.GetCost());
+		p->cur_economy.expenses = tmp.GetCost();
 	}
 
 	InvalidatePlayerWindows(p);
 }
 
-void SubtractMoneyFromPlayer(int32 cost)
+void SubtractMoneyFromPlayer(CommandCost cost)
 {
 	PlayerID pid = _current_player;
 
 	if (IsValidPlayer(pid)) SubtractMoneyFromAnyPlayer(GetPlayer(pid), cost);
 }
 
-void SubtractMoneyFromPlayerFract(PlayerID player, int32 cost)
+void SubtractMoneyFromPlayerFract(PlayerID player, CommandCost cst)
 {
 	Player *p = GetPlayer(player);
 	byte m = p->player_money_fraction;
+	Money cost = cst.GetCost();
 
 	p->player_money_fraction = m - (byte)cost;
 	cost >>= 8;
 	if (p->player_money_fraction > m) cost++;
-	if (cost != 0) SubtractMoneyFromAnyPlayer(p, cost);
-}
-
-/** the player_money field is kept as it is, but money64 contains the actual amount of money. */
-void UpdatePlayerMoney32(Player *p)
-{
-	if (p->money64 < -2000000000) {
-		p->player_money = -2000000000;
-	} else if (p->money64 > 2000000000) {
-		p->player_money = 2000000000;
-	} else {
-		p->player_money = (int32)p->money64;
-	}
+	if (cost != 0) SubtractMoneyFromAnyPlayer(p, CommandCost(cost));
 }
 
 void GetNameOfOwner(Owner owner, TileIndex tile)
@@ -242,8 +246,8 @@
 		} else {
 			const Player* p = GetPlayer(owner);
 
-			SetDParam(0, p->name_1);
-			SetDParam(1, p->name_2);
+			SetDParam(0, STR_COMPANY_NAME);
+			SetDParam(1, p->index);
 		}
 	} else {
 		const Town* t = ClosestTownFromTile(tile, (uint)-1);
@@ -410,15 +414,15 @@
 		p->president_name_2 = Random();
 		p->president_name_1 = SPECSTR_PRESIDENT_NAME;
 
-		SetDParam(0, p->president_name_2);
-		GetString(buffer, p->president_name_1, lastof(buffer));
+		SetDParam(0, p->index);
+		GetString(buffer, STR_PLAYER_NAME, lastof(buffer));
 		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94)
 			continue;
 
 		FOR_ALL_PLAYERS(pp) {
 			if (pp->is_active && p != pp) {
-				SetDParam(0, pp->president_name_2);
-				GetString(buffer2, pp->president_name_1, lastof(buffer2));
+				SetDParam(0, pp->index);
+				GetString(buffer2, STR_PLAYER_NAME, lastof(buffer2));
 				if (strcmp(buffer2, buffer) == 0)
 					goto restart;
 			}
@@ -465,7 +469,7 @@
 	p->name_1 = STR_SV_UNNAMED;
 	p->is_active = true;
 
-	p->money64 = p->player_money = p->current_loan = 100000;
+	p->player_money = p->current_loan = 100000;
 
 	p->is_ai = is_ai;
 	p->ai.state = 5; // AIS_WANT_NEW_ROUTE
@@ -551,16 +555,6 @@
 		MaybeStartNewPlayer();
 }
 
-/** index is the next parameter in _decode_parameters to set up */
-StringID GetPlayerNameString(PlayerID player, uint index)
-{
-	if (IsHumanPlayer(player) && IsValidPlayer(player)) {
-		SetDParam(index, player+1);
-		return STR_7002_PLAYER;
-	}
-	return STR_EMPTY;
-}
-
 extern void ShowPlayerFinances(PlayerID player);
 
 void PlayersYearlyLoop()
@@ -668,7 +662,7 @@
  * if p1 = 5, then
  * - p2 = enable renew_keep_length
  */
-int32 CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	Player *p;
 	if (!IsValidPlayer(_current_player)) return CMD_ERROR;
@@ -715,7 +709,7 @@
 			EngineID old_engine_type = GB(p2, 0, 16);
 			EngineID new_engine_type = GB(p2, 16, 16);
 			GroupID id_g = GB(p1, 16, 16);
-			int32 cost;
+			CommandCost cost;
 
 			if (!IsValidGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
 			if (new_engine_type != INVALID_ENGINE) {
@@ -742,7 +736,7 @@
 				cost = RemoveEngineReplacementForPlayer(p, old_engine_type,id_g, flags);
 			}
 
-			if (IsLocalPlayer()) InvalidateAutoreplaceWindow(old_engine_type);
+			if (IsLocalPlayer()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
 
 			return cost;
 		}
@@ -774,7 +768,7 @@
 		break;
 
 	}
-	return 0;
+	return CommandCost();
 }
 
 /** Control the players: add, delete, etc.
@@ -797,7 +791,7 @@
  * @arg - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)@n
  * @arg - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received
  */
-int32 CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+CommandCost CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	if (flags & DC_EXEC) _current_player = OWNER_NONE;
 
@@ -819,9 +813,9 @@
 		if (!_networking) return CMD_ERROR;
 
 		/* Has the network client a correct ClientID? */
-		if (!(flags & DC_EXEC)) return 0;
+		if (!(flags & DC_EXEC)) return CommandCost();
 #ifdef ENABLE_NETWORK
-		if (cid >= MAX_CLIENT_INFO) return 0;
+		if (cid >= MAX_CLIENT_INFO) return CommandCost();
 #endif /* ENABLE_NETWORK */
 
 		/* Delete multiplayer progress bar */
@@ -836,12 +830,10 @@
 				NetworkClientInfo *ci = &_network_client_info[cid];
 				ci->client_playas = PLAYER_SPECTATOR;
 				NetworkUpdateClientInfo(ci->client_index);
-			} else
+			} else if (_local_player == PLAYER_SPECTATOR) {
+				_network_playas = PLAYER_SPECTATOR;
+			}
 #endif /* ENABLE_NETWORK */
-			{
-				_network_playas = PLAYER_SPECTATOR;
-				SetLocalPlayer(PLAYER_SPECTATOR);
-			}
 			break;
 		}
 
@@ -895,7 +887,7 @@
 	} break;
 
 	case 1: /* Make a new AI player */
-		if (!(flags & DC_EXEC)) return 0;
+		if (!(flags & DC_EXEC)) return CommandCost();
 
 		DoStartupNewPlayer(true);
 		break;
@@ -905,7 +897,7 @@
 
 		if (!IsValidPlayer((PlayerID)p2)) return CMD_ERROR;
 
-		if (!(flags & DC_EXEC)) return 0;
+		if (!(flags & DC_EXEC)) return CommandCost();
 
 		p = GetPlayer((PlayerID)p2);
 
@@ -915,8 +907,7 @@
 			DeletePlayerWindows(p->index);
 
 			/* Show the bankrupt news */
-			SetDParam(0, p->name_1);
-			SetDParam(1, p->name_2);
+			SetDParam(0, p->index);
 			AddNewsItem( (StringID)(p->index | NB_BBANKRUPT), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
 
 			/* Remove the company */
@@ -943,7 +934,7 @@
 	default: return CMD_ERROR;
 	}
 
-	return 0;
+	return CommandCost();
 }
 
 static const StringID _endgame_perf_titles[] = {
@@ -1000,10 +991,8 @@
 		if (hs[i].score <= score) {
 			/* move all elements one down starting from the replaced one */
 			memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
-			SetDParam(0, p->president_name_1);
-			SetDParam(1, p->president_name_2);
-			SetDParam(2, p->name_1);
-			SetDParam(3, p->name_2);
+			SetDParam(0, p->index);
+			SetDParam(1, p->index);
 			GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
 			hs[i].score = score;
 			hs[i].title = EndGameGetPerformanceTitleFromValue(score);
@@ -1045,10 +1034,8 @@
 		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
 			HighScore* hs = &_highscore_table[LAST_HS_ITEM][i];
 
-			SetDParam(0, pl[i]->president_name_1);
-			SetDParam(1, pl[i]->president_name_2);
-			SetDParam(2, pl[i]->name_1);
-			SetDParam(3, pl[i]->name_2);
+			SetDParam(0, pl[i]->index);
+			SetDParam(1, pl[i]->index);
 			GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
 			hs->score = pl[i]->old_economy[0].performance_history;
 			hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
@@ -1126,10 +1113,11 @@
 	    SLE_VAR(Player, face,            SLE_UINT32),
 
 	/* money was changed to a 64 bit field in savegame version 1. */
-	SLE_CONDVAR(Player, money64,               SLE_VAR_I64 | SLE_FILE_I32, 0, 0),
-	SLE_CONDVAR(Player, money64,               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),
@@ -1151,7 +1139,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),
@@ -1176,11 +1165,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),
@@ -1309,7 +1298,6 @@
 		Player *p = GetPlayer((PlayerID)index);
 		SaveLoad_PLYR(p);
 		_player_colors[index] = p->player_color;
-		UpdatePlayerMoney32(p);
 
 		/* This is needed so an AI is attached to a loaded AI */
 		if (p->is_ai && (!_networking || _network_server) && _ai.enabled)