changeset 6403:8c022dabd50e draft

(svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
author truelight <truelight@openttd.org>
date Sat, 31 Mar 2007 12:19:22 +0000
parents b0b11d9fe257
children e01ad45f9fd9
files src/economy.cpp
diffstat 1 files changed, 33 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -254,6 +254,39 @@
 {
 	Town *t;
 	PlayerID old = _current_player;
+
+	{
+		Player *p;
+		uint i;
+
+		/* See if the old_player had shares in other companies */
+		_current_player = old_player;
+		FOR_ALL_PLAYERS(p) {
+			for (i = 0; i < 4; i++) {
+				if (p->share_owners[i] == old_player) {
+					/* Sell his shares */
+					int32 res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
+					/* Because we are in a DoCommand, we can't just execute an other one and
+					 *  expect the money to be removed. We need to do it ourself! */
+					SubtractMoneyFromPlayer(res);
+				}
+			}
+		}
+
+		/* Sell all the shares that people have on this company */
+		p = GetPlayer(old_player);
+		for (i = 0; i < 4; i++) {
+			_current_player = p->share_owners[i];
+			if (_current_player != PLAYER_SPECTATOR) {
+				/* Sell the shares */
+				int32 res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
+				/* Because we are in a DoCommand, we can't just execute an other one and
+				 *  expect the money to be removed. We need to do it ourself! */
+				SubtractMoneyFromPlayer(res);
+			}
+		}
+	}
+
 	_current_player = old_player;
 
 	/* Temporarily increase the player's money, to be sure that
@@ -346,25 +379,6 @@
 	/* Change color of existing windows */
 	if (new_player != PLAYER_SPECTATOR) ChangeWindowOwner(old_player, new_player);
 
-	{
-		Player *p;
-		uint i;
-
-		/* Check for shares */
-		FOR_ALL_PLAYERS(p) {
-			for (i = 0; i < 4; i++) {
-				/* 'Sell' the share if this player has any */
-				if (p->share_owners[i] == _current_player) {
-					p->share_owners[i] = PLAYER_SPECTATOR;
-				}
-			}
-		}
-		p = GetPlayer(_current_player);
-		/* Sell all the shares that people have on this company */
-		for (i = 0; i < 4; i++)
-			p->share_owners[i] = PLAYER_SPECTATOR;
-	}
-
 	_current_player = old;
 
 	MarkWholeScreenDirty();