From c04f0af19b8164c690c83f6c8ac1e23cac30c039 Mon Sep 17 00:00:00 2001 From: truelight Date: Tue, 10 Apr 2007 09:49:41 +0000 Subject: [PATCH] (svn r9583) [0.5] -Backport from trunk (r9529, r9533, r9540, r9541): - Fix: when 2 clients joined together, the second asserted on the NewCompany command of the first (r9529) - Fix: when company is removed, sell all shares he has first, then sell all shares other people might have in this company (r9533) - Fix: bankrupt AIs no longer buy over themselves (also added safeguards to prevent in future) (r9540 / r9541) --- ai/default/default.c | 2 +- economy.c | 62 +++++++++++++++++++++++++++++--------------- players.c | 4 +-- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/ai/default/default.c b/ai/default/default.c index f53198e2e9..28ddfde58a 100644 --- a/ai/default/default.c +++ b/ai/default/default.c @@ -3838,7 +3838,7 @@ static void AiHandleTakeover(Player *p) if (best_pl->player_money >> 1 >= p->bankrupt_value) { // Computer wants to buy it. old_p = _current_player; - _current_player = p->index; + _current_player = best_pl->index; DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY); _current_player = old_p; } diff --git a/economy.c b/economy.c index 14909b6e10..da81cef3c0 100644 --- a/economy.c +++ b/economy.c @@ -246,6 +246,41 @@ void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player) { Town *t; PlayerID old = _current_player; + + assert(old_player != new_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 @@ -338,25 +373,6 @@ void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player) /* 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(); @@ -1695,12 +1711,16 @@ int32 CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) int32 CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { Player *p; + PlayerID pid = (PlayerID)p1; /* Disable takeovers in multiplayer games */ - if (!IsValidPlayer((PlayerID)p1) || _networking) return CMD_ERROR; + if (!IsValidPlayer(pid) || _networking) return CMD_ERROR; + + /* Do not allow players to take over themselves */ + if (pid == _current_player) return CMD_ERROR; SET_EXPENSES_TYPE(EXPENSES_OTHER); - p = GetPlayer(p1); + p = GetPlayer(pid); if (!p->is_ai) return CMD_ERROR; diff --git a/players.c b/players.c index 38b5fa5182..e023984dc1 100644 --- a/players.c +++ b/players.c @@ -871,8 +871,8 @@ int32 CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) } /* This is the joining client who wants a new company */ - if (_local_player != _network_playas) { - assert(_local_player == PLAYER_SPECTATOR && _network_playas == p->index); + if (_local_player != _network_playas && _network_playas == p->index) { + assert(_local_player == PLAYER_SPECTATOR); SetLocalPlayer(p->index); MarkWholeScreenDirty(); }