Changeset - r9659:f5984e9c0ecd
src/ai/ai.cpp
Show inline comments
 
@@ -190,13 +190,13 @@ void AI_RunGameLoop()
 
	/* Check for AI-client (so joining a network with an AI) */
 
	if (!_networking || _network_server) {
 
		/* Check if we want to run AIs (server or SP only) */
 
		const Player* p;
 

	
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active && p->is_ai) {
 
			if (p->is_ai) {
 
				/* This should always be true, else something went wrong... */
 
				assert(_ai_player[p->index].active);
 

	
 
				/* Run the script */
 
				AI_DequeueCommands(p->index);
 
				AI_RunTick(p->index);
 
@@ -246,9 +246,9 @@ void AI_Initialize()
 
 */
 
void AI_Uninitialize()
 
{
 
	const Player* p;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && p->is_ai) AI_PlayerDied(p->index);
 
		if (p->is_ai) AI_PlayerDied(p->index);
 
	}
 
}
src/ai/default/default.cpp
Show inline comments
 
@@ -3894,14 +3894,13 @@ static void AiHandleTakeover(Player *p)
 
		uint asked = p->bankrupt_asked;
 
		Player *pp, *best_pl = NULL;
 
		int32 best_val = -1;
 

	
 
		// Ask the guy with the highest performance hist.
 
		FOR_ALL_PLAYERS(pp) {
 
			if (pp->is_active &&
 
					!(asked & 1) &&
 
			if (!(asked & 1) &&
 
					pp->bankrupt_asked == 0 &&
 
					best_val < pp->old_economy[1].performance_history) {
 
				best_val = pp->old_economy[1].performance_history;
 
				best_pl = pp;
 
			}
 
			asked >>= 1;
src/airport_gui.cpp
Show inline comments
 
@@ -17,12 +17,13 @@
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "order_func.h"
 
#include "station_type.h"
 
#include "tilehighlight_func.h"
 
#include "player_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
static byte _selected_airport_type;
 

	
src/autoslope.h
Show inline comments
 
@@ -36,11 +36,11 @@ static inline bool AutoslopeCheckForEntr
 
 *
 
 * @return true iff autoslope is enabled.
 
 */
 
static inline bool AutoslopeEnabled()
 
{
 
	return (_settings_game.construction.autoslope &&
 
	        ((IsValidPlayerID(_current_player) && !_is_old_ai_player) ||
 
	        ((_current_player < MAX_PLAYERS && !_is_old_ai_player) ||
 
	         (_current_player == OWNER_NONE && _game_mode == GM_EDITOR)));
 
}
 

	
 
#endif /* AUTOSLOPE_H */
src/cheat_gui.cpp
Show inline comments
 
@@ -41,14 +41,14 @@ static int32 ClickMoneyCheat(int32 p1, i
 
/**
 
 * @param p1 player to set to
 
 * @param p2 is -1 or +1 (down/up)
 
 */
 
static int32 ClickChangePlayerCheat(int32 p1, int32 p2)
 
{
 
	while (IsValidPlayerID((PlayerID)p1)) {
 
		if (_players[p1].is_active) {
 
	while ((uint)p1 < GetPlayerPoolSize()) {
 
		if (IsValidPlayerID((PlayerID)p1)) {
 
			SetLocalPlayer((PlayerID)p1);
 

	
 
			MarkWholeScreenDirty();
 
			return _local_player;
 
		}
 
		p1 += p2;
src/console_cmds.cpp
Show inline comments
 
@@ -591,13 +591,12 @@ DEF_CONSOLE_CMD(ConKick)
 

	
 
	return true;
 
}
 

	
 
DEF_CONSOLE_CMD(ConResetCompany)
 
{
 
	const Player *p;
 
	PlayerID index;
 

	
 
	if (argc == 0) {
 
		IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company <company-id>'");
 
		IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Player 1 is 1, etc.");
 
		return true;
 
@@ -610,18 +609,13 @@ DEF_CONSOLE_CMD(ConResetCompany)
 
	/* Check valid range */
 
	if (!IsValidPlayerID(index)) {
 
		IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_PLAYERS);
 
		return true;
 
	}
 

	
 
	/* Check if company does exist */
 
	p = GetPlayer(index);
 
	if (!p->is_active) {
 
		IConsoleError("Company does not exist.");
 
		return true;
 
	}
 
	const Player *p = GetPlayer(index);
 

	
 
	if (p->is_ai) {
 
		IConsoleError("Company is owned by an AI.");
 
		return true;
 
	}
 

	
 
@@ -1154,14 +1148,12 @@ DEF_CONSOLE_CMD(ConPlayers)
 
	}
 
	NetworkPopulateCompanyInfo();
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		char buffer[512];
 

	
 
		if (!p->is_active) continue;
 

	
 
		const NetworkPlayerInfo *npi = &_network_player_info[p->index];
 

	
 
		GetString(buffer, STR_00D1_DARK_BLUE + _player_colors[p->index], lastof(buffer));
 
		IConsolePrintF(CC_INFO, "#:%d(%s) Company Name: '%s'  Year Founded: %d  Money: %" OTTD_PRINTF64 "d  Loan: %" OTTD_PRINTF64 "d  Value: %" OTTD_PRINTF64 "d  (T:%d, R:%d, P:%d, S:%d) %sprotected",
 
			p->index + 1, buffer, npi->company_name, p->inaugurated_year, (int64)p->player_money, (int64)p->current_loan, (int64)CalculateCompanyValue(p),
 
			/* trains      */ npi->num_vehicle[0],
 
@@ -1182,13 +1174,13 @@ DEF_CONSOLE_CMD(ConSayPlayer)
 
		return true;
 
	}
 

	
 
	if (argc != 3) return false;
 

	
 
	PlayerID player_id = (PlayerID)(atoi(argv[1]) - 1);
 
	if (!IsValidPlayerID(player_id) || !GetPlayer(player_id)->is_active) {
 
	if (!IsValidPlayerID(player_id)) {
 
		IConsolePrintF(CC_DEFAULT, "Unknown player. Player range is between 1 and %d.", MAX_PLAYERS);
 
		return true;
 
	}
 

	
 
	if (!_network_server) {
 
		NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, player_id, argv[2]);
src/core/enum_type.hpp
Show inline comments
 
@@ -91,13 +91,21 @@ struct TinyEnumT
 
		return (enum_type)m_val;
 
	}
 

	
 
	/** Assignment operator (from Tenum_t type) */
 
	FORCEINLINE TinyEnumT& operator = (enum_type e)
 
	{
 
		m_val = (storage_type)e; return *this;
 
		m_val = (storage_type)e;
 
		return *this;
 
	}
 

	
 
	/** Assignment operator (from Tenum_t type) */
 
	FORCEINLINE TinyEnumT& operator = (uint u)
 
	{
 
		m_val = (storage_type)u;
 
		return *this;
 
	}
 

	
 
	/** postfix ++ operator on tiny type */
 
	FORCEINLINE TinyEnumT operator ++ (int)
 
	{
 
		TinyEnumT org = *this;
src/dock_gui.cpp
Show inline comments
 
@@ -18,12 +18,13 @@
 
#include "sound_func.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "slope_func.h"
 
#include "tilehighlight_func.h"
 
#include "player_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
static void ShowBuildDockStationPicker(Window *parent);
 
static void ShowBuildDocksDepotPicker(Window *parent);
src/economy.cpp
Show inline comments
 
@@ -289,13 +289,12 @@ void ChangeOwnershipOfPlayerItems(Player
 
		Player *p;
 
		uint i;
 

	
 
		/* See if the old_player had shares in other companies */
 
		_current_player = old_player;
 
		FOR_ALL_PLAYERS(p) {
 
			if (!p->is_active) continue;
 
			for (i = 0; i < 4; i++) {
 
				if (p->share_owners[i] == old_player) {
 
					/* Sell his shares */
 
					CommandCost 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! */
 
@@ -562,16 +561,17 @@ static void PlayersCheckBankrupt(Player 
 
				ChangeNetworkOwner(p->index, PLAYER_SPECTATOR);
 
			}
 

	
 
			/* Remove the player */
 
			ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
 
			/* Register the player as not-active */
 
			p->is_active = false;
 

	
 
			if (!IsHumanPlayer(p->index) && (!_networking || _network_server) && _ai.enabled)
 
				AI_PlayerDied(p->index);
 

	
 
			delete p;
 
		}
 
	}
 
}
 

	
 
static void PlayersGenStatistics()
 
{
 
@@ -585,24 +585,22 @@ static void PlayersGenStatistics()
 
	}
 

	
 
	if (!HasBit(1<<0|1<<3|1<<6|1<<9, _cur_month))
 
		return;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0]));
 
			p->old_economy[0] = p->cur_economy;
 
			memset(&p->cur_economy, 0, sizeof(p->cur_economy));
 
		memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0]));
 
		p->old_economy[0] = p->cur_economy;
 
		memset(&p->cur_economy, 0, sizeof(p->cur_economy));
 

	
 
			if (p->num_valid_stat_ent != 24) p->num_valid_stat_ent++;
 
		if (p->num_valid_stat_ent != 24) p->num_valid_stat_ent++;
 

	
 
			UpdateCompanyRatingAndValue(p, true);
 
			PlayersCheckBankrupt(p);
 
		UpdateCompanyRatingAndValue(p, true);
 
		PlayersCheckBankrupt(p);
 

	
 
			if (p->block_preview != 0) p->block_preview--;
 
		}
 
		if (p->block_preview != 0) p->block_preview--;
 
	}
 

	
 
	InvalidateWindow(WC_INCOME_GRAPH, 0);
 
	InvalidateWindow(WC_OPERATING_PROFIT, 0);
 
	InvalidateWindow(WC_DELIVERED_CARGO, 0);
 
	InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
 
@@ -675,14 +673,12 @@ static void AddInflation()
 
static void PlayersPayInterest()
 
{
 
	const Player* p;
 
	int interest = _economy.interest_rate * 54;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) continue;
 

	
 
		_current_player = p->index;
 

	
 
		SubtractMoneyFromPlayer(CommandCost(EXPENSES_LOAN_INT, (Money)BigMulSU(p->current_loan, interest, 16)));
 

	
 
		SubtractMoneyFromPlayer(CommandCost(EXPENSES_OTHER, _price.station_value >> 2));
 
	}
 
@@ -1764,42 +1760,38 @@ static void DoAcquireCompany(Player *p)
 
			_current_player = p->share_owners[i];
 
			SubtractMoneyFromPlayer(CommandCost(EXPENSES_OTHER, -value));
 
		}
 
	}
 
	_current_player = old_player;
 

	
 
	p->is_active = false;
 

	
 
	DeletePlayerWindows(pi);
 
	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
 
	InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
 
	InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
 
	InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
 

	
 
	delete p;
 
}
 

	
 
extern int GetAmountOwnedBy(const Player *p, PlayerID owner);
 

	
 
/** Acquire shares in an opposing company.
 
 * @param tile unused
 
 * @param flags type of operation
 
 * @param p1 player to buy the shares from
 
 * @param p2 unused
 
 */
 
CommandCost CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Player *p;
 
	CommandCost cost(EXPENSES_OTHER);
 

	
 
	/* Check if buying shares is allowed (protection against modified clients) */
 
	/* Cannot buy own shares */
 
	if (!IsValidPlayerID((PlayerID)p1) || !_settings_game.economy.allow_shares || _current_player == (PlayerID)p1) return CMD_ERROR;
 

	
 
	p = GetPlayer((PlayerID)p1);
 

	
 
	/* Cannot buy shares of non-existent nor bankrupted company */
 
	if (!p->is_active) return CMD_ERROR;
 
	Player *p = GetPlayer((PlayerID)p1);
 

	
 
	/* Protect new companies from hostile takeovers */
 
	if (_cur_year - p->inaugurated_year < 6) return_cmd_error(STR_PROTECTED);
 

	
 
	/* Those lines are here for network-protection (clients can be slow) */
 
	if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 0) return cost;
 
@@ -1832,29 +1824,23 @@ CommandCost CmdBuyShareInCompany(TileInd
 
 * @param flags type of operation
 
 * @param p1 player to sell the shares from
 
 * @param p2 unused
 
 */
 
CommandCost CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Player *p;
 
	Money cost;
 

	
 
	/* Check if selling shares is allowed (protection against modified clients) */
 
	/* Cannot sell own shares */
 
	if (!IsValidPlayerID((PlayerID)p1) || !_settings_game.economy.allow_shares || _current_player == (PlayerID)p1) return CMD_ERROR;
 

	
 
	p = GetPlayer((PlayerID)p1);
 

	
 
	/* Cannot sell shares of non-existent nor bankrupted company */
 
	if (!p->is_active) return CMD_ERROR;
 
	Player *p = GetPlayer((PlayerID)p1);
 

	
 
	/* Those lines are here for network-protection (clients can be slow) */
 
	if (GetAmountOwnedBy(p, _current_player) == 0) return CommandCost();
 

	
 
	/* adjust it a little to make it less profitable to sell and buy */
 
	cost = CalculateCompanyValue(p) >> 2;
 
	Money cost = CalculateCompanyValue(p) >> 2;
 
	cost = -(cost - (cost >> 7));
 

	
 
	if (flags & DC_EXEC) {
 
		PlayerByte* b = p->share_owners;
 
		while (*b != _current_player) b++; // share owners is guaranteed to contain player
 
		*b = PLAYER_SPECTATOR;
 
@@ -1871,22 +1857,21 @@ CommandCost CmdSellShareInCompany(TileIn
 
 * @param flags type of operation
 
 * @param p1 player/company to buy up
 
 * @param p2 unused
 
 */
 
CommandCost CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Player *p;
 
	PlayerID pid = (PlayerID)p1;
 

	
 
	/* Disable takeovers in multiplayer games */
 
	if (!IsValidPlayerID(pid) || _networking) return CMD_ERROR;
 

	
 
	/* Do not allow players to take over themselves */
 
	if (pid == _current_player) return CMD_ERROR;
 

	
 
	p = GetPlayer(pid);
 
	Player *p = GetPlayer(pid);
 

	
 
	if (!p->is_ai) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DoAcquireCompany(p);
 
	}
src/engine.cpp
Show inline comments
 
@@ -310,13 +310,13 @@ static PlayerID GetBestPlayer(uint8 pp)
 
	uint mask = 0;
 

	
 
	do {
 
		best_hist = -1;
 
		best_player = PLAYER_SPECTATOR;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active && p->block_preview == 0 && !HasBit(mask, p->index) &&
 
			if (p->block_preview == 0 && !HasBit(mask, p->index) &&
 
					p->old_economy[0].performance_history > best_hist) {
 
				best_hist = p->old_economy[0].performance_history;
 
				best_player = p->index;
 
			}
 
		}
 

	
 
@@ -425,20 +425,16 @@ static void NewVehicleAvailable(Engine *
 
	if (IsWagon(index)) return;
 

	
 
	if (e->type == VEH_TRAIN) {
 
		/* maybe make another rail type available */
 
		RailType railtype = e->u.rail.railtype;
 
		assert(railtype < RAILTYPE_END);
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) SetBit(p->avail_railtypes, railtype);
 
		}
 
		FOR_ALL_PLAYERS(p) SetBit(p->avail_railtypes, railtype);
 
	} else if (e->type == VEH_ROAD) {
 
		/* maybe make another road type available */
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) SetBit(p->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
 
		}
 
		FOR_ALL_PLAYERS(p) SetBit(p->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
 
	}
 

	
 
	SetDParam(0, GetEngineCategoryName(index));
 
	SetDParam(1, index);
 
	AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
 
}
src/genworld.cpp
Show inline comments
 
@@ -142,13 +142,13 @@ static void _GenerateWorld(void *arg)
 
				RunTileLoop();
 
				IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
 
			}
 
		}
 

	
 
		ResetObjectToPlace();
 
		SetLocalPlayer(_gw.lp);
 
		_local_player = _gw.lp;
 

	
 
		SetGeneratingWorldProgress(GWP_GAME_START, 1);
 
		/* Call any callback */
 
		if (_gw.proc != NULL) _gw.proc();
 
		IncreaseGeneratingWorldProgress(GWP_GAME_START);
 

	
src/graph_gui.cpp
Show inline comments
 
@@ -42,26 +42,23 @@ struct GraphLegendWindow : Window {
 

	
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const Player *p;
 
		for (PlayerID p = PLAYER_FIRST; p < MAX_PLAYERS; p++) {
 
			if (IsValidPlayerID(p)) continue;
 

	
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) continue;
 

	
 
			SetBit(_legend_excluded_players, p->index);
 
			this->RaiseWidget(p->index + 3);
 
			SetBit(_legend_excluded_players, p);
 
			this->RaiseWidget(p + 3);
 
		}
 

	
 
		this->DrawWidgets();
 

	
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (!p->is_active) continue;
 

	
 
			DrawPlayerIcon(p->index, 4, 18 + p->index * 12);
 

	
 
			SetDParam(0, p->index);
 
			SetDParam(1, p->index);
 
			DrawString(21, 17 + p->index * 12, STR_7021, HasBit(_legend_excluded_players, p->index) ? TC_BLACK : TC_WHITE);
 
		}
 
@@ -357,22 +354,22 @@ public:
 
	{
 
		this->DrawWidgets();
 

	
 
		uint excluded_players = _legend_excluded_players;
 

	
 
		/* Exclude the players which aren't valid */
 
		const Player* p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (!p->is_active) SetBit(excluded_players, p->index);
 
		for (PlayerID p = PLAYER_FIRST; p < MAX_PLAYERS; p++) {
 
			if (!IsValidPlayerID(p)) SetBit(excluded_players, p);
 
		}
 
		this->excluded_data = excluded_players;
 
		this->num_vert_lines = 24;
 

	
 
		byte nums = 0;
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) nums = max(nums, p->num_valid_stat_ent);
 
			nums = max(nums, p->num_valid_stat_ent);
 
		}
 
		this->num_on_x_axis = min(nums, 24);
 

	
 
		int mo = (_cur_month / 3 - nums) * 3;
 
		int yr = _cur_year;
 
		while (mo < 0) {
 
@@ -381,14 +378,15 @@ public:
 
		}
 

	
 
		this->year = yr;
 
		this->month = mo;
 

	
 
		int numd = 0;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) {
 
		for (PlayerID k = PLAYER_FIRST; k < MAX_PLAYERS; k++) {
 
			if (IsValidPlayerID(k)) {
 
				p = GetPlayer(k);
 
				this->colors[numd] = _colour_gradient[p->player_color][6];
 
				for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
 
					this->cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(p, j);
 
					i++;
 
				}
 
			}
 
@@ -771,15 +769,13 @@ private:
 
		if (!this->players.NeedRebuild()) return;
 

	
 
		this->players.Clear();
 

	
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) {
 
				*this->players.Append() = p;
 
			}
 
			*this->players.Append() = p;
 
		}
 

	
 
		this->players.Compact();
 
		this->players.RebuildDone();
 
	}
 

	
 
@@ -864,13 +860,13 @@ struct PerformanceRatingDetailWindow : W
 
	int timeout;
 

	
 
	PerformanceRatingDetailWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
 
	{
 
		/* Disable the players who are not active */
 
		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
 
			this->SetWidgetDisabledState(i + 13, !GetPlayer(i)->is_active);
 
			this->SetWidgetDisabledState(i + 13, !IsValidPlayerID(i));
 
		}
 

	
 
		this->UpdatePlayerStats();
 

	
 
		if (player != INVALID_PLAYER) this->LowerWidget(player + 13);
 

	
 
@@ -880,13 +876,13 @@ struct PerformanceRatingDetailWindow : W
 
	void UpdatePlayerStats()
 
	{
 
		/* Update all player stats with the current data
 
		 * (this is because _score_info is not saved to a savegame) */
 
		Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) UpdateCompanyRatingAndValue(p, false);
 
			UpdateCompanyRatingAndValue(p, false);
 
		}
 

	
 
		this->timeout = DAY_TICKS * 5;
 

	
 
	}
 

	
 
@@ -898,24 +894,24 @@ struct PerformanceRatingDetailWindow : W
 
		int color_done, color_notdone;
 

	
 
		/* Draw standard stuff */
 
		this->DrawWidgets();
 

	
 
		/* Check if the currently selected player is still active. */
 
		if (player == INVALID_PLAYER || !GetPlayer(player)->is_active) {
 
		if (player == INVALID_PLAYER || !IsValidPlayerID(player)) {
 
			if (player != INVALID_PLAYER) {
 
				/* Raise and disable the widget for the previous selection. */
 
				this->RaiseWidget(player + 13);
 
				this->DisableWidget(player + 13);
 
				this->SetDirty();
 

	
 
				player = INVALID_PLAYER;
 
			}
 

	
 
			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
 
				if (GetPlayer(i)->is_active) {
 
				if (IsValidPlayerID(i)) {
 
					/* Lower the widget corresponding to this player. */
 
					this->LowerWidget(i + 13);
 
					this->SetDirty();
 

	
 
					player = i;
 
					break;
 
@@ -925,13 +921,13 @@ struct PerformanceRatingDetailWindow : W
 

	
 
		/* If there are no active players, don't display anything else. */
 
		if (player == INVALID_PLAYER) return;
 

	
 
		/* Paint the player icons */
 
		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
 
			if (!GetPlayer(i)->is_active) {
 
			if (!IsValidPlayerID(i)) {
 
				/* Check if we have the player as an active player */
 
				if (!this->IsWidgetDisabled(i + 13)) {
 
					/* Bah, player gone :( */
 
					this->DisableWidget(i + 13);
 

	
 
					/* We need a repaint */
src/industry_gui.cpp
Show inline comments
 
@@ -25,12 +25,13 @@
 
#include "player_func.h"
 
#include "settings_type.h"
 
#include "tilehighlight_func.h"
 
#include "string_func.h"
 
#include "sortlist_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "player_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
bool _ignore_restrictions;
 

	
src/misc.cpp
Show inline comments
 
@@ -97,14 +97,14 @@ void InitializeGame(uint size_x, uint si
 
	InitializeIndustries();
 
	InitializeBuildingCounts();
 

	
 
	InitializeTrains();
 
	InitializeNPF();
 

	
 
	InitializePlayers();
 
	AI_Initialize();
 
	InitializePlayers();
 
	InitializeCheats();
 

	
 
	InitTextEffects();
 
	InitChatMessage();
 
	InitializeAnimatedTiles();
 

	
src/misc_cmd.cpp
Show inline comments
 
@@ -66,13 +66,13 @@ CommandCost CmdSetPlayerColor(TileIndex 
 
	Player *p = GetPlayer(_current_player);
 

	
 
	/* Ensure no two companies have the same primary colour */
 
	if (scheme == LS_DEFAULT && state == 0) {
 
		const Player *pp;
 
		FOR_ALL_PLAYERS(pp) {
 
			if (pp->is_active && pp != p && pp->player_color == colour) return CMD_ERROR;
 
			if (pp != p && pp->player_color == colour) return CMD_ERROR;
 
		}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		switch (state) {
 
			case 0:
 
@@ -205,13 +205,12 @@ CommandCost CmdDecreaseLoan(TileIndex ti
 
static bool IsUniqueCompanyName(const char *name)
 
{
 
	const Player *p;
 
	char buf[512];
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) continue;
 
		SetDParam(0, p->index);
 
		GetString(buf, STR_COMPANY_NAME, lastof(buf));
 
		if (strcmp(buf, name) == 0) return false;
 
	}
 

	
 
	return true;
 
@@ -242,13 +241,12 @@ CommandCost CmdChangeCompanyName(TileInd
 
static bool IsUniquePresidentName(const char *name)
 
{
 
	const Player *p;
 
	char buf[512];
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) continue;
 
		SetDParam(0, p->index);
 
		GetString(buf, STR_PLAYER_NAME, lastof(buf));
 
		if (strcmp(buf, name) == 0) return false;
 
	}
 

	
 
	return true;
src/misc_gui.cpp
Show inline comments
 
@@ -42,12 +42,13 @@
 
#include "player_gui.h"
 
#include "settings_type.h"
 
#include "newgrf_cargo.h"
 
#include "rail_gui.h"
 
#include "tilehighlight_func.h"
 
#include "querystring_gui.h"
 
#include "player_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
/* Variables to display file lists */
 
SaveLoadDialogMode _saveload_mode;
src/network/network.cpp
Show inline comments
 
@@ -37,12 +37,13 @@
 
#include "../landscape_type.h"
 
#include "../rev.h"
 
#ifdef DEBUG_DUMP_COMMANDS
 
	#include "../core/alloc_func.hpp"
 
#endif /* DEBUG_DUMP_COMMANDS */
 
#include "table/strings.h"
 
#include "../player_base.h"
 

	
 
bool _network_server;     ///< network-server is active
 
bool _network_available;  ///< is network mode available?
 
bool _network_dedicated;  ///< are we a dedicated server?
 
bool _is_network_server;  ///< Does this client wants to be a network-server?
 
NetworkServerGameInfo _network_game_info;
src/network/network_client.cpp
Show inline comments
 
@@ -621,15 +621,13 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 

	
 
		// Say we received the map and loaded it correctly!
 
		SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
 

	
 
		/* New company/spectator (invalid player) or company we want to join is not active
 
		 * Switch local player to spectator and await the server's judgement */
 
		if (_network_playas == PLAYER_NEW_COMPANY || !IsValidPlayerID(_network_playas) ||
 
				!GetPlayer(_network_playas)->is_active) {
 

	
 
		if (_network_playas == PLAYER_NEW_COMPANY || !IsValidPlayerID(_network_playas)) {
 
			SetLocalPlayer(PLAYER_SPECTATOR);
 

	
 
			if (_network_playas != PLAYER_SPECTATOR) {
 
				/* We have arrived and ready to start playing; send a command to make a new player;
 
				 * the server will give us a client-id and let us in */
 
				_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
src/network/network_gui.cpp
Show inline comments
 
@@ -28,12 +28,13 @@
 
#include "../gfx_func.h"
 
#include "../player_func.h"
 
#include "../settings_type.h"
 
#include "../widgets/dropdown_func.h"
 
#include "../querystring_gui.h"
 
#include "../sortlist_type.h"
 
#include "../player_base.h"
 

	
 
#include "table/strings.h"
 
#include "../table/sprites.h"
 

	
 
#define BGC 5
 
#define BTC 15
src/network/network_server.cpp
Show inline comments
 
@@ -86,14 +86,12 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_CO
 
		return;
 
	}
 

	
 
	NetworkPopulateCompanyInfo();
 

	
 
	FOR_ALL_PLAYERS(player) {
 
		if (!player->is_active) continue;
 

	
 
		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
 

	
 
		p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
 
		p->Send_uint8 (active);
 
		p->Send_uint8 (player->index);
 

	
 
@@ -1296,18 +1294,15 @@ void NetworkPopulateCompanyInfo()
 
	const Station *s;
 
	NetworkTCPSocketHandler *cs;
 
	const NetworkClientInfo *ci;
 
	uint i;
 
	uint16 months_empty;
 

	
 
	memset(_network_player_info, 0, sizeof(_network_player_info));
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) {
 
			memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
 
			continue;
 
		}
 

	
 
		// Clean the info but not the password
 
		ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password));
 
		months_empty = _network_player_info[p->index].months_empty;
 
		memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
 
		_network_player_info[p->index].months_empty = months_empty;
 
		ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password));
 
@@ -1433,13 +1428,13 @@ static void NetworkAutoCleanCompanies()
 
		if (IsValidPlayerID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
 
	}
 

	
 
	/* Go through all the comapnies */
 
	FOR_ALL_PLAYERS(p) {
 
		/* Skip the non-active once */
 
		if (!p->is_active || p->is_ai) continue;
 
		if (p->is_ai) continue;
 

	
 
		if (!clients_in_company[p->index]) {
 
			/* The company is empty for one month more */
 
			_network_player_info[p->index].months_empty++;
 

	
 
			/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
src/network/network_udp.cpp
Show inline comments
 
@@ -121,15 +121,12 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_U
 
	NetworkPopulateCompanyInfo();
 

	
 
	Player *player;
 
	byte current = 0;
 
	/* Go through all the players */
 
	FOR_ALL_PLAYERS(player) {
 
		/* Skip non-active players */
 
		if (!player->is_active) continue;
 

	
 
		current++;
 

	
 
		/* Send the information */
 
		packet.Send_uint8 (current);
 

	
 
		packet.Send_string(_network_player_info[player->index].company_name);
src/newgrf_house.cpp
Show inline comments
 
@@ -20,12 +20,13 @@
 
#include "newgrf_commons.h"
 
#include "transparency.h"
 
#include "functions.h"
 
#include "player_func.h"
 
#include "animated_tile_func.h"
 
#include "date_func.h"
 
#include "player_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 
#include "table/town_land.h"
 

	
 
static BuildingCounts    _building_counts;
src/oldloader.cpp
Show inline comments
 
@@ -998,20 +998,20 @@ static const OldChunks player_chunk[] = 
 

	
 
	OCL_END()
 
};
 

	
 
static bool LoadOldPlayer(LoadgameState *ls, int num)
 
{
 
	Player *p = GetPlayer((PlayerID)num);
 
	Player *p = new (num) Player();
 

	
 
	_current_player_id = (PlayerID)num;
 

	
 
	if (!LoadChunk(ls, p, player_chunk)) return false;
 

	
 
	if (_old_string_id == 0) {
 
		p->is_active = false;
 
		delete p;
 
		return true;
 
	}
 

	
 
	p->name_1 = RemapOldStringID(_old_string_id);
 
	p->president_name_1 = RemapOldStringID(_old_string_id_2);
 
	_players_ai[_current_player_id].tick = ai_tick;
src/openttd.cpp
Show inline comments
 
@@ -299,12 +299,13 @@ static void ParseResolution(Dimension *r
 

	
 
static void InitializeDynamicVariables()
 
{
 
	/* Dynamic stuff needs to be initialized somewhere... */
 
	_industry_mngr.ResetMapping();
 
	_industile_mngr.ResetMapping();
 
	_Player_pool.AddBlockToPool();
 
}
 

	
 

	
 
/** Unitializes drivers, frees allocated memory, cleans pools, ...
 
 * Generally, prepares the game for shutting down
 
 */
 
@@ -332,12 +333,13 @@ static void ShutdownGame()
 
	_Vehicle_pool.CleanPool();
 
	_Sign_pool.CleanPool();
 
	_Order_pool.CleanPool();
 
	_Group_pool.CleanPool();
 
	_CargoPacket_pool.CleanPool();
 
	_Engine_pool.CleanPool();
 
	_Player_pool.CleanPool();
 

	
 
	free(_config_file);
 

	
 
	/* Close all and any open filehandles */
 
	FioCloseAll();
 
}
 
@@ -521,15 +523,12 @@ int ttd_main(int argc, char *argv[])
 
	/* initialize airport state machines */
 
	InitializeAirports();
 

	
 
	/* initialize all variables that are allocated dynamically */
 
	InitializeDynamicVariables();
 

	
 
	/* start the AI */
 
	AI_Initialize();
 

	
 
	/* Sample catalogue */
 
	DEBUG(misc, 1, "Loading sound effects...");
 
	MxInitialize(11025);
 
	SoundInitialize("sample.cat");
 

	
 
	/* Initialize FreeType */
 
@@ -1162,22 +1161,12 @@ static void ConvertTownOwner()
 

	
 
			default: break;
 
		}
 
	}
 
}
 

	
 
/* before savegame version 4, the name of the company determined if it existed */
 
static void CheckIsPlayerActive()
 
{
 
	Player *p;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->name_1 != 0) p->is_active = true;
 
	}
 
}
 

	
 
/* since savegame version 4.1, exclusive transport rights are stored at towns */
 
static void UpdateExclusiveRights()
 
{
 
	Town *t;
 

	
 
	FOR_ALL_TOWNS(t) {
 
@@ -1401,27 +1390,23 @@ bool AfterLoadGame()
 
	/* make sure there is a town in the game */
 
	if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, (uint)-1)) {
 
		SetSaveLoadError(STR_NO_TOWN_IN_SCENARIO);
 
		return false;
 
	}
 

	
 
	/* in version 4.1 of the savegame, is_active was introduced to determine
 
	 * if a player does exist, rather then checking name_1 */
 
	if (CheckSavegameVersionOldStyle(4, 1)) CheckIsPlayerActive();
 

	
 
	/* The void tiles on the southern border used to belong to a wrong class (pre 4.3).
 
	 * This problem appears in savegame version 21 too, see r3455. But after loading the
 
	 * savegame and saving again, the buggy map array could be converted to new savegame
 
	 * version. It didn't show up before r12070. */
 
	if (CheckSavegameVersion(87)) UpdateVoidTiles();
 

	
 
	/* If Load Scenario / New (Scenario) Game is used,
 
	 *  a player does not exist yet. So create one here.
 
	 * 1 exeption: network-games. Those can have 0 players
 
	 *   But this exeption is not true for non dedicated network_servers! */
 
	if (!_players[0].is_active && (!_networking || (_networking && _network_server && !_network_dedicated)))
 
	if (!IsValidPlayerID(PLAYER_FIRST) && (!_networking || (_networking && _network_server && !_network_dedicated)))
 
		DoStartupNewPlayer(false);
 

	
 
	if (CheckSavegameVersion(72)) {
 
		/* Locks/shiplifts in very old savegames had OWNER_WATER as owner */
 
		for (TileIndex t = 0; t < MapSize(); t++) {
 
			switch (GetTileType(t)) {
 
@@ -2313,20 +2298,16 @@ bool AfterLoadGame()
 
		 *     (when inactive players were stored in the savegame - TTD, TTDP and some
 
		 *      *really* old revisions of OTTD; else it is already set in InitializePlayers())
 
		 * 2) shares that are owned by inactive players or self
 
		 *     (caused by cheating players in earlier revisions) */
 
		Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (!p->is_active) {
 
				for (uint i = 0; i < 4; i++) { p->share_owners[i] = PLAYER_SPECTATOR; }
 
			} else {
 
				for (uint i = 0; i < 4; i++) {
 
					PlayerID o = p->share_owners[i];
 
					if (o == PLAYER_SPECTATOR) continue;
 
					if (!IsValidPlayerID(o) || o == p->index || !GetPlayer(o)->is_active) p->share_owners[i] = PLAYER_SPECTATOR;
 
				}
 
			for (uint i = 0; i < 4; i++) {
 
				PlayerID o = p->share_owners[i];
 
				if (o == PLAYER_SPECTATOR) continue;
 
				if (!IsValidPlayerID(o) || o == p->index) p->share_owners[i] = PLAYER_SPECTATOR;
 
			}
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(86)) {
 
		for (TileIndex t = 0; t < map_size; t++) {
 
@@ -2371,13 +2352,13 @@ bool AfterLoadGame()
 
				 * This conversion has to be done before buoys with invalid owner are removed. */
 
				SetWaterClass(t, WATER_CLASS_SEA);
 
			}
 

	
 
			if (IsBuoyTile(t) || IsDriveThroughStopTile(t) || IsTileType(t, MP_WATER)) {
 
				Owner o = GetTileOwner(t);
 
				if (IsValidPlayerID(o) && !GetPlayer(o)->is_active) {
 
				if (o < MAX_PLAYERS && !IsValidPlayerID(o)) {
 
					_current_player = o;
 
					ChangeTileOwner(t, o, PLAYER_SPECTATOR);
 
				}
 
				if (IsBuoyTile(t)) {
 
					/* reset buoy owner to OWNER_NONE in the station struct
 
					 * (even if it is owned by active player) */
 
@@ -2385,17 +2366,17 @@ bool AfterLoadGame()
 
				}
 
			} else if (IsTileType(t, MP_ROAD)) {
 
				/* works for all RoadTileType */
 
				for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
 
					/* update even non-existing road types to update tile owner too */
 
					Owner o = GetRoadOwner(t, rt);
 
					if (IsValidPlayerID(o) && !GetPlayer(o)->is_active) SetRoadOwner(t, rt, OWNER_NONE);
 
					if (o < MAX_PLAYERS && !IsValidPlayerID(o)) SetRoadOwner(t, rt, OWNER_NONE);
 
				}
 
				if (IsLevelCrossing(t)) {
 
					Owner o = GetTileOwner(t);
 
					if (!GetPlayer(o)->is_active) {
 
					if (!IsValidPlayerID(o)) {
 
						/* remove leftover rail piece from crossing (from very old savegames) */
 
						_current_player = o;
 
						DoCommand(t, 0, GetCrossingRailTrack(t), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
 
					}
 
				}
 
			}
src/player_base.h
Show inline comments
 
@@ -2,12 +2,14 @@
 

	
 
/** @file player_base.h Definition of stuff that is very close to a player, like the player struct itself. */
 

	
 
#ifndef PLAYER_BASE_H
 
#define PLAYER_BASE_H
 

	
 
#include "player_type.h"
 
#include "oldpool.h"
 
#include "road_type.h"
 
#include "rail_type.h"
 
#include "date_type.h"
 
#include "engine_type.h"
 
#include "livery.h"
 
#include "autoreplace_type.h"
 
@@ -19,13 +21,18 @@ struct PlayerEconomyEntry {
 
	Money expenses;
 
	int32 delivered_cargo;
 
	int32 performance_history; ///< player score (scale 0-1000)
 
	Money company_value;
 
};
 

	
 
struct Player {
 
DECLARE_OLD_POOL(Player, Player, 1, MAX_PLAYERS)
 

	
 
struct Player : PoolItem<Player, PlayerByte, &_Player_pool> {
 
	Player(uint16 name_1 = 0, bool is_ai = false);
 
	~Player();
 

	
 
	uint32 name_2;
 
	uint16 name_1;
 
	char *name;
 

	
 
	uint16 president_name_1;
 
	uint32 president_name_2;
 
@@ -39,13 +46,12 @@ struct Player {
 
	byte player_color;
 
	Livery livery[LS_END];
 
	byte player_money_fraction;
 
	RailTypes avail_railtypes;
 
	RoadTypes avail_roadtypes;
 
	byte block_preview;
 
	PlayerByte index;
 

	
 
	uint32 cargo_types; ///< which cargo types were transported the last year
 

	
 
	TileIndex location_of_house;
 
	TileIndex last_build_coordinate;
 

	
 
@@ -56,56 +62,56 @@ struct Player {
 

	
 
	byte quarters_of_bankrupcy;
 
	byte bankrupt_asked; ///< which players were asked about buying it?
 
	int16 bankrupt_timeout;
 
	Money bankrupt_value;
 

	
 
	bool is_active;
 
	bool is_ai;
 

	
 
	Money yearly_expenses[3][EXPENSES_END];
 
	PlayerEconomyEntry cur_economy;
 
	PlayerEconomyEntry old_economy[24];
 
	EngineRenewList engine_renew_list; ///< Defined later
 
	bool engine_renew;
 
	bool renew_keep_length;
 
	int16 engine_renew_months;
 
	uint32 engine_renew_money;
 
	uint16 *num_engines; ///< caches the number of engines of each type the player owns (no need to save this)
 

	
 
	inline bool IsValid() const { return this->name_1 != 0; }
 
};
 

	
 
inline bool operator < (PlayerID p, uint u) {return (uint)p < u;}
 

	
 
static inline bool IsValidPlayerID(PlayerID index)
 
{
 
	return index < GetPlayerPoolSize() && GetPlayer(index)->IsValid();
 
}
 

	
 
#define FOR_ALL_PLAYERS_FROM(d, start) for (d = GetPlayer(start); d != NULL; d = (d->index + 1U < GetPlayerPoolSize()) ? GetPlayer(d->index + 1U) : NULL) if (d->IsValid())
 
#define FOR_ALL_PLAYERS(d) FOR_ALL_PLAYERS_FROM(d, 0)
 

	
 
struct PlayerMoneyBackup {
 
private:
 
	Money backup_yearly_expenses[EXPENSES_END];
 
	PlayerEconomyEntry backup_cur_economy;
 
	Player *p;
 

	
 
public:
 
	PlayerMoneyBackup(Player *player);
 

	
 
	void Restore();
 
};
 

	
 
extern Player _players[MAX_PLAYERS];
 
#define FOR_ALL_PLAYERS(p) for (p = _players; p != endof(_players); p++)
 

	
 
static inline byte ActivePlayerCount()
 
{
 
	const Player *p;
 
	byte count = 0;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) count++;
 
	}
 
	FOR_ALL_PLAYERS(p) count++;
 

	
 
	return count;
 
}
 

	
 
static inline Player *GetPlayer(PlayerID i)
 
{
 
	assert(IsInsideBS(i, PLAYER_FIRST, lengthof(_players)));
 
	return &_players[i];
 
}
 

	
 
Money CalculateCompanyValue(const Player *p);
 

	
 
#endif /* PLAYER_BASE_H */
src/player_func.h
Show inline comments
 
@@ -24,17 +24,12 @@ bool IsHumanPlayer(PlayerID pi);
 

	
 
static inline bool IsLocalPlayer()
 
{
 
	return _local_player == _current_player;
 
}
 

	
 
static inline bool IsValidPlayerID(PlayerID pi)
 
{
 
	return IsInsideBS(pi, PLAYER_FIRST, MAX_PLAYERS);
 
}
 

	
 
static inline bool IsInteractivePlayer(PlayerID pi)
 
{
 
	return pi == _local_player;
 
}
 

	
 

	
src/player_gui.cpp
Show inline comments
 
@@ -343,13 +343,13 @@ private:
 
		LiveryScheme scheme;
 

	
 
		/* Disallow other player colours for the primary colour */
 
		if (HasBit(this->sel, LS_DEFAULT) && widget == PLW_WIDGET_PRI_COL_DROPDOWN) {
 
			const Player *p;
 
			FOR_ALL_PLAYERS(p) {
 
				if (p->is_active && p->index != _local_player) SetBit(used_colours, p->player_color);
 
				if (p->index != _local_player) SetBit(used_colours, p->player_color);
 
			}
 
		}
 

	
 
		/* Get the first selected livery to use as the default dropdown item */
 
		for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
 
			if (HasBit(this->sel, scheme)) break;
src/players.cpp
Show inline comments
 
@@ -35,24 +35,43 @@
 
#include "ai/default/default.h"
 
#include "ai/trolly/trolly.h"
 
#include "road_func.h"
 
#include "rail.h"
 
#include "sprite.h"
 
#include "debug.h"
 
#include "oldpool_func.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
Player _players[MAX_PLAYERS];
 
PlayerByte _local_player;
 
PlayerByte _current_player;
 
/* NOSAVE: can be determined from player structs */
 
byte _player_colors[MAX_PLAYERS];
 
PlayerFace _player_face; ///< for player face storage in openttd.cfg
 
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
 

	
 
DEFINE_OLD_POOL_GENERIC(Player, Player)
 

	
 
Player::Player(uint16 name_1, bool is_ai) : name_1(name_1), is_ai(is_ai)
 
{
 
	for (uint j = 0; j < 4; j++) this->share_owners[j] = PLAYER_SPECTATOR;
 
}
 

	
 
Player::~Player()
 
{
 
	free(this->name);
 
	free(this->president_name);
 
	free(this->num_engines);
 

	
 
	if (CleaningPool()) return;
 

	
 
	DeletePlayerWindows(this->index);
 
	this->name_1 = 0;
 
}
 

	
 
/**
 
 * Sets the local player and updates the patch settings that are set on a
 
 * per-company (player) basis to reflect the core's state in the GUI.
 
 * @param new_player the new player
 
 * @pre IsValidPlayerID(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE
 
 */
 
@@ -422,14 +441,12 @@ static byte GeneratePlayerColour()
 
		}
 
	};
 

	
 
	/* Move the colors that look similar to each player's color to the side */
 
	Player *p;
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) continue;
 

	
 
		Colours pcolour = (Colours)p->player_color;
 

	
 
		for (uint i = 0; i < COLOUR_END; i++) {
 
			if (colours[i] == pcolour) {
 
				colours[i] = INVALID_COLOUR;
 
				break;
 
@@ -468,42 +485,23 @@ restart:;
 
		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) {
 
			if (p != pp) {
 
				SetDParam(0, pp->index);
 
				GetString(buffer2, STR_PLAYER_NAME, lastof(buffer2));
 
				if (strcmp(buffer2, buffer) == 0)
 
					goto restart;
 
			}
 
		}
 
		return;
 
	}
 
}
 

	
 
static Player *AllocatePlayer()
 
{
 
	Player *p;
 
	/* Find a free slot */
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) {
 
			free(p->name);
 
			free(p->president_name);
 
			PlayerID i = p->index;
 
			memset(p, 0, sizeof(Player));
 
			memset(&_players_ai[i], 0, sizeof(PlayerAI));
 
			memset(&_players_ainew[i], 0, sizeof(PlayerAiNew));
 
			p->index = i;
 
			return p;
 
		}
 
	}
 
	return NULL;
 
}
 

	
 
void ResetPlayerLivery(Player *p)
 
{
 
	for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
 
		p->livery[scheme].in_use  = false;
 
		p->livery[scheme].colour1 = p->player_color;
 
		p->livery[scheme].colour2 = p->player_color;
 
@@ -515,27 +513,26 @@ void ResetPlayerLivery(Player *p)
 
 *
 
 * @param is_ai is a ai player?
 
 * @return the player struct
 
 */
 
Player *DoStartupNewPlayer(bool is_ai)
 
{
 
	Player *p;
 
	Player *p = new Player(STR_SV_UNNAMED, is_ai);
 

	
 
	p = AllocatePlayer();
 
	if (p == NULL) return NULL;
 

	
 
	memset(&_players_ai[p->index], 0, sizeof(PlayerAI));
 
	memset(&_players_ainew[p->index], 0, sizeof(PlayerAiNew));
 

	
 
	/* Make a color */
 
	p->player_color = GeneratePlayerColour();
 
	ResetPlayerLivery(p);
 
	_player_colors[p->index] = p->player_color;
 
	p->name_1 = STR_SV_UNNAMED;
 
	p->is_active = true;
 

	
 
	p->player_money = p->current_loan = 100000;
 

	
 
	p->is_ai = is_ai;
 
	_players_ai[p->index].state = 5; // AIS_WANT_NEW_ROUTE
 
	p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = PLAYER_SPECTATOR;
 

	
 
	p->avail_railtypes = GetPlayerRailtypes(p->index);
 
	p->avail_roadtypes = GetPlayerRoadtypes(p->index);
 
	p->inaugurated_year = _cur_year;
 
@@ -554,13 +551,12 @@ Player *DoStartupNewPlayer(bool is_ai)
 
	InvalidateWindow(WC_TOOLBAR_MENU, 0);
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	if (is_ai && (!_networking || _network_server) && _ai.enabled)
 
		AI_StartNewAI(p->index);
 

	
 
	free(p->num_engines);
 
	p->num_engines = CallocT<uint16>(GetEnginePoolSize());
 

	
 
	return p;
 
}
 

	
 
void StartupPlayers()
 
@@ -574,13 +570,13 @@ static void MaybeStartNewPlayer()
 
	uint n;
 
	Player *p;
 

	
 
	/* count number of competitors */
 
	n = 0;
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && p->is_ai) n++;
 
		if (p->is_ai) n++;
 
	}
 

	
 
	/* when there's a lot of computers in game, the probability that a new one starts is lower */
 
	if (n < (uint)_settings_game.difficulty.max_no_competitors &&
 
			n < (_network_server ?
 
				InteractiveRandomRange(_settings_game.difficulty.max_no_competitors + 2) :
 
@@ -595,45 +591,42 @@ static void MaybeStartNewPlayer()
 
	_next_competitor_start = _settings_game.difficulty.competitor_start_time * 90 * DAY_TICKS + 1;
 
	_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
 
}
 

	
 
void InitializePlayers()
 
{
 
	memset(_players, 0, sizeof(_players));
 
	for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
 
		_players[i].index = i;
 
		for (uint j = 0; j < 4; j++) _players[i].share_owners[j] = PLAYER_SPECTATOR;
 
	}
 
	_Player_pool.CleanPool();
 
	_Player_pool.AddBlockToPool();
 
	_cur_player_tick_index = 0;
 
}
 

	
 
void OnTick_Players()
 
{
 
	Player *p;
 

	
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	p = GetPlayer((PlayerID)_cur_player_tick_index);
 
	if (IsValidPlayerID((PlayerID)_cur_player_tick_index)) {
 
		Player *p = GetPlayer((PlayerID)_cur_player_tick_index);
 
		if (p->name_1 != 0) GenerateCompanyName(p);
 

	
 
		if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start) {
 
			MaybeStartNewPlayer();
 
		}
 
	}
 

	
 
	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
 
	if (p->name_1 != 0) GenerateCompanyName(p);
 

	
 
	if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start)
 
		MaybeStartNewPlayer();
 
}
 

	
 
void PlayersYearlyLoop()
 
{
 
	Player *p;
 

	
 
	/* Copy statistics */
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
 
			memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
 
			InvalidateWindow(WC_FINANCES, p->index);
 
		}
 
		memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
 
		memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
 
		InvalidateWindow(WC_FINANCES, p->index);
 
	}
 

	
 
	if (_settings_client.gui.show_finances && _local_player != PLAYER_SPECTATOR) {
 
		ShowPlayerFinances(_local_player);
 
		p = GetPlayer(_local_player);
 
		if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
 
@@ -641,26 +634,12 @@ void PlayersYearlyLoop()
 
		} else {
 
			SndPlayFx(SND_00_GOOD_YEAR);
 
		}
 
	}
 
}
 

	
 
static void DeletePlayerStuff(PlayerID pi)
 
{
 
	Player *p;
 

	
 
	DeletePlayerWindows(pi);
 
	p = GetPlayer(pi);
 
	p->name_1 = STR_NULL;
 
	p->president_name_1 = STR_NULL;
 
	free(p->name);
 
	free(p->president_name);
 
	p->name = NULL;
 
	p->president_name = NULL;
 
}
 

	
 
/** Change engine renewal parameters
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 bits 0-3 command
 
 * - p1 = 0 - change auto renew bool
 
 * - p1 = 1 - change auto renew months
 
@@ -969,26 +948,27 @@ CommandCost CmdPlayerCtrl(TileIndex tile
 
			SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
 
			SetDParamStr(2, cni->company_name);
 
			AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
 

	
 
			/* Remove the company */
 
			ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
 
			p->is_active = false;
 

	
 
			delete p;
 
		}
 
	} break;
 

	
 
	case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */
 
		PlayerID pid_old = (PlayerID)GB(p2,  0, 16);
 
		PlayerID pid_new = (PlayerID)GB(p2, 16, 16);
 

	
 
		if (!IsValidPlayerID(pid_old) || !IsValidPlayerID(pid_new)) return CMD_ERROR;
 

	
 
		if (!(flags & DC_EXEC)) return CMD_ERROR;
 

	
 
		ChangeOwnershipOfPlayerItems(pid_old, pid_new);
 
		DeletePlayerStuff(pid_old);
 
		delete GetPlayer(pid_old);
 
	} break;
 

	
 
	default: return CMD_ERROR;
 
	}
 

	
 
	return CommandCost();
 
@@ -1060,13 +1040,13 @@ int8 SaveHighScoreValueNetwork()
 
	const Player* p;
 
	const Player* pl[MAX_PLAYERS];
 
	uint count = 0;
 
	int8 player = -1;
 

	
 
	/* Sort all active players with the highest score first */
 
	FOR_ALL_PLAYERS(p) if (p->is_active) pl[count++] = p;
 
	FOR_ALL_PLAYERS(p) pl[count++] = p;
 

	
 
	GSortT(pl, count, &HighScoreSorter);
 

	
 
	{
 
		uint i;
 

	
 
@@ -1197,13 +1177,13 @@ static const SaveLoad _player_desc[] = {
 

	
 
	/* 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),
 
	SLE_CONDARR(Player, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
 

	
 
	SLE_CONDVAR(Player, is_ai,                 SLE_BOOL, 2, SL_MAX_VERSION),
 
	SLE_CONDVAR(Player, is_active,             SLE_BOOL, 4, SL_MAX_VERSION),
 
	SLE_CONDNULL(1, 4, 99),
 

	
 
	/* Engine renewal settings */
 
	SLE_CONDNULL(512, 16, 18),
 
	SLE_CONDREF(Player, engine_renew_list,     REF_ENGINE_RENEWS,          19, SL_MAX_VERSION),
 
	SLE_CONDVAR(Player, engine_renew,          SLE_BOOL,                   16, SL_MAX_VERSION),
 
	SLE_CONDVAR(Player, engine_renew_months,   SLE_INT16,                  16, SL_MAX_VERSION),
 
@@ -1278,24 +1258,22 @@ static void SaveLoad_PLYR(Player* p)
 
}
 

	
 
static void Save_PLYR()
 
{
 
	Player *p;
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			SlSetArrayIndex(p->index);
 
			SlAutolength((AutolengthProc*)SaveLoad_PLYR, p);
 
		}
 
		SlSetArrayIndex(p->index);
 
		SlAutolength((AutolengthProc*)SaveLoad_PLYR, p);
 
	}
 
}
 

	
 
static void Load_PLYR()
 
{
 
	int index;
 
	while ((index = SlIterateArray()) != -1) {
 
		Player *p = GetPlayer((PlayerID)index);
 
		Player *p = new (index) Player();
 
		SaveLoad_PLYR(p);
 
		_player_colors[index] = p->player_color;
 

	
 
		/* This is needed so an AI is attached to a loaded AI */
 
		if (p->is_ai && (!_networking || _network_server) && _ai.enabled) {
 
			/* Clear the memory of the new AI, otherwise we might be doing wrong things. */
src/road_gui.cpp
Show inline comments
 
@@ -21,12 +21,13 @@
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "player_func.h"
 
#include "settings_type.h"
 
#include "tunnelbridge.h"
 
#include "tilehighlight_func.h"
 
#include "player_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
static void ShowRVStationPicker(Window *parent, RoadStopType rs);
 
static void ShowRoadDepotPicker(Window *parent);
src/smallmap_gui.cpp
Show inline comments
 
@@ -616,16 +616,14 @@ public:
 
			_owner_colors[OWNER_NONE] = MKCOLOR(0x54545454);
 
			_owner_colors[OWNER_WATER] = MKCOLOR(0xCACACACA);
 
			_owner_colors[OWNER_END]   = MKCOLOR(0x20202020); /* industry */
 

	
 
			/* now fill with the player colors */
 
			FOR_ALL_PLAYERS(p) {
 
				if (p->is_active) {
 
					_owner_colors[p->index] =
 
						_colour_gradient[p->player_color][5] * 0x01010101;
 
				}
 
				_owner_colors[p->index] =
 
					_colour_gradient[p->player_color][5] * 0x01010101;
 
			}
 
		}
 

	
 
		tile_x = this->scroll_x / TILE_SIZE;
 
		tile_y = this->scroll_y / TILE_SIZE;
 

	
src/station_gui.cpp
Show inline comments
 
@@ -23,12 +23,13 @@
 
#include "window_func.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "widgets/dropdown_func.h"
 
#include "newgrf_cargo.h"
 
#include "string_func.h"
 
#include "player_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
bool _station_show_coverage;
 

	
src/strings.cpp
Show inline comments
 
@@ -956,13 +956,13 @@ static char* FormatString(char* buff, co
 
			}
 

	
 
			case SCC_COMPANY_NUM: { // {COMPANYNUM}
 
				PlayerID player = (PlayerID)GetInt32(&argv);
 

	
 
				/* Nothing is added for AI or inactive players */
 
				if (IsHumanPlayer(player) && IsValidPlayerID(player)) {
 
				if (IsValidPlayerID(player) && IsHumanPlayer(player)) {
 
					int64 args[1];
 
					args[0] = player + 1;
 
					buff = GetStringWithArgs(buff, STR_7002_PLAYER, args, last);
 
				}
 
				break;
 
			}
src/terraform_gui.cpp
Show inline comments
 
@@ -559,26 +559,25 @@ static void ResetLandscapeConfirmationCa
 
	if (confirmed) {
 
		Player *p;
 

	
 
		/* Set generating_world to true to get instant-green grass after removing
 
		 * player property. */
 
		_generating_world = true;
 
		/* Delete all players */
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) {
 
				ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
 
				p->is_active = false;
 
			}
 
		}
 
		_generating_world = false;
 

	
 
		/* Delete all stations owned by a player */
 
		Station *st;
 
		FOR_ALL_STATIONS(st) {
 
			if (IsValidPlayerID(st->owner)) delete st;
 
		}
 

	
 
		/* Delete all players */
 
		FOR_ALL_PLAYERS(p) {
 
			ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
 
			delete p;
 
		}
 
		_generating_world = false;
 
	}
 
}
 

	
 
struct ScenarioEditorLandscapeGenerationWindow : Window {
 
	ScenarioEditorLandscapeGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
 
	{
src/toolbar_gui.cpp
Show inline comments
 
@@ -1479,13 +1479,13 @@ static void PopupMainToolbMenu(Window *p
 
static int GetPlayerIndexFromMenu(int index)
 
{
 
	if (index >= 0) {
 
		const Player *p;
 

	
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active && --index < 0) return p->index;
 
			if (--index < 0) return p->index;
 
		}
 
	}
 
	return -1;
 
}
 

	
 
struct ToolbarPlayerMenuWindow : Window {
 
@@ -1555,25 +1555,24 @@ struct ToolbarPlayerMenuWindow : Window 
 
			}
 
			DrawString(x + 19, y, STR_NETWORK_CLIENT_LIST, TC_FROMSTRING);
 
			y += 10;
 
			sel--;
 
		}
 

	
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) {
 
				if (p->index == sel) {
 
		for (PlayerID p = PLAYER_FIRST; p < MAX_PLAYERS; p++) {
 
			if (IsValidPlayerID(p)) {
 
				if (p == sel) {
 
					GfxFillRect(x, y, x + 238, y + 9, 0);
 
				}
 

	
 
				DrawPlayerIcon(p->index, x + 2, y + 1);
 
				DrawPlayerIcon(p, x + 2, y + 1);
 

	
 
				SetDParam(0, p->index);
 
				SetDParam(1, p->index);
 
				SetDParam(0, p);
 
				SetDParam(1, p);
 

	
 
				TextColour color = (p->index == sel) ? TC_WHITE : TC_BLACK;
 
				TextColour color = (p == sel) ? TC_WHITE : TC_BLACK;
 
				if (gray & 1) color = TC_GREY;
 
				DrawString(x + 19, y, STR_7021, color);
 

	
 
				y += 10;
 
			}
 
			gray >>= 1;
src/town_cmd.cpp
Show inline comments
 
@@ -2303,13 +2303,13 @@ CommandCost CmdDoTownAction(TileIndex ti
 

	
 
static void UpdateTownGrowRate(Town *t)
 
{
 
	/* Increase player ratings if they're low */
 
	const Player *p;
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && t->ratings[p->index] < RATING_GROWTH_MAXIMUM) {
 
		if (t->ratings[p->index] < RATING_GROWTH_MAXIMUM) {
 
			t->ratings[p->index] = min((int)RATING_GROWTH_MAXIMUM, t->ratings[p->index] + RATING_GROWTH_UP_STEP);
 
		}
 
	}
 

	
 
	int n = 0;
 

	
src/town_gui.cpp
Show inline comments
 
@@ -179,13 +179,13 @@ public:
 

	
 
		/* Draw list of players */
 
		int y = 25;
 

	
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active && (HasBit(this->town->have_ratings, p->index) || this->town->exclusivity == p->index)) {
 
			if ((HasBit(this->town->have_ratings, p->index) || this->town->exclusivity == p->index)) {
 
				DrawPlayerIcon(p->index, 2, y);
 

	
 
				SetDParam(0, p->index);
 
				SetDParam(1, p->index);
 

	
 
				int r = this->town->ratings[p->index];
src/tree_cmd.cpp
Show inline comments
 
@@ -20,12 +20,13 @@
 
#include "player_func.h"
 
#include "sound_func.h"
 
#include "settings_type.h"
 
#include "water_map.h"
 
#include "water.h"
 
#include "landscape_type.h"
 
#include "player_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 
#include "table/tree_land.h"
 

	
 
/**
src/tree_gui.cpp
Show inline comments
 
@@ -5,12 +5,13 @@
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "window_gui.h"
 
#include "gfx_func.h"
 
#include "tilehighlight_func.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "command_func.h"
 
#include "sound_func.h"
 
#include "settings_type.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
0 comments (0 inline, 0 general)