Changeset - r10208:ef8fcc3dc4ca
[Not reviewed]
master
! ! !
rubidium - 16 years ago 2008-09-30 20:51:04
rubidium@openttd.org
(svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
73 files changed:
Changeset was too big and was cut off... Show full diff anyway
0 comments (0 inline, 0 general)
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -668,10 +668,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\players.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\queue.cpp"
 
				>
 
			</File>
 
@@ -876,6 +872,26 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_base.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_func.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_gui.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_manager_face.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_type.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\console_func.h"
 
				>
 
			</File>
 
@@ -1280,26 +1296,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_base.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_face.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_func.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_gui.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_type.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\querystring_gui.h"
 
				>
 
			</File>
 
@@ -1744,6 +1740,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_gui.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\depot_gui.cpp"
 
				>
 
			</File>
 
@@ -1812,10 +1812,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_gui.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\rail_gui.cpp"
 
				>
 
			</File>
 
@@ -1908,6 +1904,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_cmd.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\disaster_cmd.cpp"
 
				>
 
			</File>
projects/openttd_vs90.vcproj
Show inline comments
 
@@ -665,10 +665,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\players.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\queue.cpp"
 
				>
 
			</File>
 
@@ -873,6 +869,26 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_base.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_func.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_gui.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_manager_face.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_type.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\console_func.h"
 
				>
 
			</File>
 
@@ -1277,26 +1293,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_base.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_face.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_func.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_gui.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_type.h"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\querystring_gui.h"
 
				>
 
			</File>
 
@@ -1741,6 +1737,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_gui.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\depot_gui.cpp"
 
				>
 
			</File>
 
@@ -1809,10 +1809,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\player_gui.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\rail_gui.cpp"
 
				>
 
			</File>
 
@@ -1905,6 +1901,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\company_cmd.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\disaster_cmd.cpp"
 
				>
 
			</File>
source.list
Show inline comments
 
@@ -60,7 +60,6 @@ os_timer.cpp
 
#end
 
pathfind.cpp
 
pbs.cpp
 
players.cpp
 
queue.cpp
 
rail.cpp
 
rev.cpp
 
@@ -146,6 +145,11 @@ cheat_type.h
 
cmd_helper.h
 
command_func.h
 
command_type.h
 
company_base.h
 
company_func.h
 
company_gui.h
 
company_manager_face.h
 
company_type.h
 
console_func.h
 
console_gui.h
 
console_internal.h
 
@@ -247,11 +251,6 @@ order_func.h
 
order_type.h
 
pathfind.h
 
pbs.h
 
player_base.h
 
player_face.h
 
player_func.h
 
player_gui.h
 
player_type.h
 
querystring_gui.h
 
queue.h
 
rail.h
 
@@ -379,6 +378,7 @@ autoreplace_gui.cpp
 
bridge_gui.cpp
 
build_vehicle_gui.cpp
 
cheat_gui.cpp
 
company_gui.cpp
 
depot_gui.cpp
 
dock_gui.cpp
 
engine_gui.cpp
 
@@ -396,7 +396,6 @@ newgrf_gui.cpp
 
news_gui.cpp
 
order_gui.cpp
 
osk_gui.cpp
 
player_gui.cpp
 
rail_gui.cpp
 
road_gui.cpp
 
roadveh_gui.cpp
 
@@ -421,6 +420,7 @@ waypoint_gui.cpp
 
aircraft_cmd.cpp
 
autoreplace_cmd.cpp
 
clear_cmd.cpp
 
company_cmd.cpp
 
disaster_cmd.cpp
 
dummy_land.cpp
 
group_cmd.cpp
src/ai/ai.cpp
Show inline comments
 
@@ -8,8 +8,8 @@
 
#include "../command_func.h"
 
#include "../network/network.h"
 
#include "../core/alloc_func.hpp"
 
#include "../player_func.h"
 
#include "../player_base.h"
 
#include "../company_func.h"
 
#include "../company_base.h"
 
#include "ai.h"
 
#include "default/default.h"
 
#include "trolly/trolly.h"
src/ai/default/default.cpp
Show inline comments
 
@@ -26,8 +26,8 @@
 
#include "../../vehicle_func.h"
 
#include "../../functions.h"
 
#include "../../saveload.h"
 
#include "../../player_func.h"
 
#include "../../player_base.h"
 
#include "../../company_func.h"
 
#include "../../company_base.h"
 
#include "../../settings_type.h"
 
#include "default.h"
 
#include "../../tunnelbridge.h"
src/ai/trolly/build.cpp
Show inline comments
 
@@ -14,8 +14,8 @@
 
#include "../../bridge.h"
 
#include "../../vehicle_func.h"
 
#include "../../vehicle_base.h"
 
#include "../../player_base.h"
 
#include "../../player_func.h"
 
#include "../../company_base.h"
 
#include "../../company_func.h"
 
#include "../ai.h"
 
#include "../../tunnelbridge.h"
 

	
src/ai/trolly/pathfinder.cpp
Show inline comments
 
@@ -13,8 +13,8 @@
 
#include "../../tunnelbridge_map.h"
 
#include "../ai.h"
 
#include "../../variables.h"
 
#include "../../player_base.h"
 
#include "../../player_func.h"
 
#include "../../company_base.h"
 
#include "../../company_func.h"
 
#include "../../tunnelbridge.h"
 

	
 

	
src/ai/trolly/shared.cpp
Show inline comments
 
@@ -7,7 +7,7 @@
 
#include "../../debug.h"
 
#include "../../map_func.h"
 
#include "../../vehicle_base.h"
 
#include "../../player_base.h"
 
#include "../../company_base.h"
 
#include "trolly.h"
 

	
 
int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
src/ai/trolly/trolly.cpp
Show inline comments
 
@@ -38,8 +38,8 @@
 
#include "../../vehicle_func.h"
 
#include "../../date_func.h"
 
#include "../ai.h"
 
#include "../../player_base.h"
 
#include "../../player_func.h"
 
#include "../../company_base.h"
 
#include "../../company_func.h"
 

	
 
#include "table/strings.h"
 

	
src/ai/trolly/trolly.h
Show inline comments
 
@@ -6,7 +6,7 @@
 
#define AI_TROLLY_H
 

	
 
#include "../../aystar.h"
 
#include "../../player_type.h"
 
#include "../../company_type.h"
 
#include "../../vehicle_type.h"
 
#include "../../date_type.h"
 
#include "../../engine_type.h"
src/aircraft_cmd.cpp
Show inline comments
 
@@ -33,7 +33,7 @@
 
#include "autoreplace_func.h"
 
#include "autoreplace_gui.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "settings_type.h"
 
#include "order_func.h"
 
#include "effectvehicle_func.h"
src/airport_gui.cpp
Show inline comments
 
@@ -16,11 +16,11 @@
 
#include "settings_type.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "order_func.h"
 
#include "station_type.h"
 
#include "tilehighlight_func.h"
 
#include "player_base.h"
 
#include "company_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -4,7 +4,7 @@
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "debug.h"
 
#include "vehicle_gui.h"
 
#include "train.h"
src/autoreplace_func.h
Show inline comments
 
@@ -6,7 +6,7 @@
 
#define AUTOREPLACE_FUNC_H
 

	
 
#include "autoreplace_type.h"
 
#include "player_base.h"
 
#include "company_base.h"
 

	
 
/**
 
 * Remove all engine replacement settings for the company.
src/autoreplace_gui.cpp
Show inline comments
 
@@ -17,7 +17,7 @@
 
#include "vehicle_func.h"
 
#include "autoreplace_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "engine_func.h"
src/autoslope.h
Show inline comments
 
@@ -6,7 +6,7 @@
 
#define AUTOSLOPE_H
 

	
 
#include "settings_type.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "depot_func.h"
 

	
 
/**
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -13,7 +13,7 @@
 
#include "articulated_vehicles.h"
 
#include "textbuf_gui.h"
 
#include "command_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "airport.h"
 
#include "vehicle_gui.h"
 
#include "newgrf_engine.h"
src/cheat_gui.cpp
Show inline comments
 
@@ -6,8 +6,8 @@
 
#include "openttd.h"
 
#include "command_func.h"
 
#include "cheat_func.h"
 
#include "player_base.h"
 
#include "player_func.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "gfx_func.h"
 
#include "date_func.h"
 
#include "saveload.h"
 
@@ -18,7 +18,7 @@
 
#include "window_func.h"
 
#include "rail_gui.h"
 
#include "gui.h"
 
#include "player_gui.h"
 
#include "company_gui.h"
 
#include "gamelog.h"
 

	
 
#include "table/strings.h"
src/command.cpp
Show inline comments
 
@@ -18,8 +18,8 @@
 
#include "town.h"
 
#include "date_func.h"
 
#include "debug.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "signal_func.h"
 

	
 
#include "table/strings.h"
src/company_base.h
Show inline comments
 
new file 100644
 
/* $Id$ */
 

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

	
 
#ifndef COMPANY_BASE_H
 
#define COMPANY_BASE_H
 

	
 
#include "company_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"
 
#include "economy_type.h"
 
#include "tile_type.h"
 

	
 
struct CompanyEconomyEntry {
 
	Money income;
 
	Money expenses;
 
	int32 delivered_cargo;
 
	int32 performance_history; ///< company score (scale 0-1000)
 
	Money company_value;
 
};
 

	
 
/* The third parameter and the number after >> MUST be the same,
 
 * otherwise more (or less) companies will be allowed to be
 
 * created than what MAX_COMPANIES specifies!
 
 */
 
DECLARE_OLD_POOL(Company, Company, 1, MAX_COMPANIES >> 1)
 

	
 
struct Company : PoolItem<Company, CompanyByte, &_Company_pool> {
 
	Company(uint16 name_1 = 0, bool is_ai = false);
 
	~Company();
 

	
 
	uint32 name_2;
 
	uint16 name_1;
 
	char *name;
 

	
 
	uint16 president_name_1;
 
	uint32 president_name_2;
 
	char *president_name;
 

	
 
	CompanyManagerFace face;
 

	
 
	Money money;
 
	byte money_fraction;
 
	Money current_loan;
 

	
 
	byte colour;
 
	Livery livery[LS_END];
 
	RailTypes avail_railtypes;
 
	RoadTypes avail_roadtypes;
 
	byte block_preview;
 

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

	
 
	TileIndex location_of_HQ;
 
	TileIndex last_build_coordinate;
 

	
 
	OwnerByte share_owners[4];
 

	
 
	Year inaugurated_year;
 
	byte num_valid_stat_ent;
 

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

	
 
	bool is_ai;
 

	
 
	Money yearly_expenses[3][EXPENSES_END];
 
	CompanyEconomyEntry cur_economy;
 
	CompanyEconomyEntry 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 company owns (no need to save this)
 

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

	
 
static inline bool IsValidCompanyID(CompanyID company)
 
{
 
	return (uint)company < GetCompanyPoolSize() && GetCompany(company)->IsValid();
 
}
 

	
 
#define FOR_ALL_COMPANIES_FROM(d, start) for (d = GetCompany(start); d != NULL; d = (d->index + 1U < GetCompanyPoolSize()) ? GetCompany(d->index + 1U) : NULL) if (d->IsValid())
 
#define FOR_ALL_COMPANIES(d) FOR_ALL_COMPANIES_FROM(d, 0)
 

	
 
static inline byte ActiveCompanyCount()
 
{
 
	const Company *c;
 
	byte count = 0;
 

	
 
	FOR_ALL_COMPANIES(c) count++;
 

	
 
	return count;
 
}
 

	
 
Money CalculateCompanyValue(const Company *c);
 

	
 
#endif /* COMPANY_BASE_H */
src/company_cmd.cpp
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_cmd.cpp Handling of companies. */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "engine_func.h"
 
#include "engine_base.h"
 
#include "company_func.h"
 
#include "company_gui.h"
 
#include "town.h"
 
#include "news_func.h"
 
#include "saveload.h"
 
#include "command_func.h"
 
#include "network/network.h"
 
#include "network/network_func.h"
 
#include "variables.h"
 
#include "cheat_func.h"
 
#include "ai/ai.h"
 
#include "company_manager_face.h"
 
#include "group.h"
 
#include "window_func.h"
 
#include "tile_map.h"
 
#include "strings_func.h"
 
#include "gfx_func.h"
 
#include "functions.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "core/alloc_func.hpp"
 
#include "core/sort_func.hpp"
 
#include "autoreplace_func.h"
 
#include "autoreplace_gui.h"
 
#include "string_func.h"
 
#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"
 

	
 
CompanyByte _local_company;
 
CompanyByte _current_company;
 
/* NOSAVE: can be determined from company structs */
 
byte _company_colours[MAX_COMPANIES];
 
CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg
 
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
 

	
 
DEFINE_OLD_POOL_GENERIC(Company, Company)
 

	
 
Company::Company(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] = COMPANY_SPECTATOR;
 
}
 

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

	
 
	if (CleaningPool()) return;
 

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

	
 
/**
 
 * Sets the local company and updates the patch settings that are set on a
 
 * per-company basis to reflect the core's state in the GUI.
 
 * @param new_company the new company
 
 * @pre IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE
 
 */
 
void SetLocalCompany(CompanyID new_company)
 
{
 
	/* company could also be COMPANY_SPECTATOR or OWNER_NONE */
 
	assert(IsValidCompanyID(new_company) || new_company == COMPANY_SPECTATOR || new_company == OWNER_NONE);
 

	
 
	_local_company = new_company;
 

	
 
	/* Do not update the patches if we are in the intro GUI */
 
	if (IsValidCompanyID(new_company) && _game_mode != GM_MENU) {
 
		const Company *p = GetCompany(new_company);
 
		_settings_client.gui.autorenew        = p->engine_renew;
 
		_settings_client.gui.autorenew_months = p->engine_renew_months;
 
		_settings_client.gui.autorenew_money  = p->engine_renew_money;
 
		InvalidateWindow(WC_GAME_OPTIONS, 0);
 
	}
 
}
 

	
 
bool IsHumanCompany(CompanyID company)
 
{
 
	return !GetCompany(company)->is_ai;
 
}
 

	
 

	
 
uint16 GetDrawStringCompanyColor(CompanyID company)
 
{
 
	/* Get the color for DrawString-subroutines which matches the color
 
	 * of the company */
 
	if (!IsValidCompanyID(company)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOR;
 
	return (_colour_gradient[_company_colours[company]][4]) | IS_PALETTE_COLOR;
 
}
 

	
 
void DrawCompanyIcon(CompanyID p, int x, int y)
 
{
 
	DrawSprite(SPR_PLAYER_ICON, COMPANY_SPRITE_COLOR(p), x, y);
 
}
 

	
 
/**
 
 * Converts an old company manager's face format to the new company manager's face format
 
 *
 
 * Meaning of the bits in the old face (some bits are used in several times):
 
 * - 4 and 5: chin
 
 * - 6 to 9: eyebrows
 
 * - 10 to 13: nose
 
 * - 13 to 15: lips (also moustache for males)
 
 * - 16 to 19: hair
 
 * - 20 to 22: eye color
 
 * - 20 to 27: tie, ear rings etc.
 
 * - 28 to 30: glasses
 
 * - 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white)
 
 * - 31: gender (0 = male, 1 = female)
 
 *
 
 * @param face the face in the old format
 
 * @return the face in the new format
 
 */
 
CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
 
{
 
	CompanyManagerFace cmf = 0;
 
	GenderEthnicity ge = GE_WM;
 

	
 
	if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
 
	if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
 

	
 
	SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN,    ge, ge);
 
	SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
 
	SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR,  ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
 
	SetCompanyManagerFaceBits(cmf, CMFV_CHIN,        ge, ScaleCompanyManagerFaceValue(CMFV_CHIN,     ge, GB(face,  4, 2)));
 
	SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS,    ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face,  6, 4)));
 
	SetCompanyManagerFaceBits(cmf, CMFV_HAIR,        ge, ScaleCompanyManagerFaceValue(CMFV_HAIR,     ge, GB(face, 16, 4)));
 
	SetCompanyManagerFaceBits(cmf, CMFV_JACKET,      ge, ScaleCompanyManagerFaceValue(CMFV_JACKET,   ge, GB(face, 20, 2)));
 
	SetCompanyManagerFaceBits(cmf, CMFV_COLLAR,      ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR,   ge, GB(face, 22, 2)));
 
	SetCompanyManagerFaceBits(cmf, CMFV_GLASSES,     ge, GB(face, 28, 1));
 

	
 
	uint lips = GB(face, 10, 4);
 
	if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
 
		SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
 
		SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
 
	} else {
 
		if (!HasBit(ge, GENDER_FEMALE)) {
 
			lips = lips * 15 / 16;
 
			lips -= 3;
 
			if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
 
		} else {
 
			lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
 
		}
 
		SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
 

	
 
		uint nose = GB(face, 13, 3);
 
		if (ge == GE_WF) {
 
			nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
 
		} else {
 
			nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
 
		}
 
		SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
 
	}
 

	
 
	uint tie_earring = GB(face, 24, 4);
 
	if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
 
		if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
 
		SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
 
	}
 

	
 
	return cmf;
 
}
 

	
 
/**
 
 * Checks whether a company manager's face is a valid encoding.
 
 * Unused bits are not enforced to be 0.
 
 * @param cmf the fact to check
 
 * @return true if and only if the face is valid
 
 */
 
bool IsValidCompanyManagerFace(CompanyManagerFace cmf)
 
{
 
	if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_GEN_ETHN, GE_WM)) return false;
 

	
 
	GenderEthnicity ge   = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
 
	bool has_moustache   = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE,   ge) != 0;
 
	bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
 
	bool has_glasses     = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
 

	
 
	if (!AreCompanyManagerFaceBitsValid(cmf, CMFV_EYE_COLOUR, ge)) return false;
 
	for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
 
		switch (cmfv) {
 
			case CMFV_MOUSTACHE:   if (!has_moustache)   continue; break;
 
			case CMFV_LIPS:        /* FALL THROUGH */
 
			case CMFV_NOSE:        if (has_moustache)    continue; break;
 
			case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
 
			case CMFV_GLASSES:     if (!has_glasses)     continue; break;
 
			default: break;
 
		}
 
		if (!AreCompanyManagerFaceBitsValid(cmf, cmfv, ge)) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
void InvalidateCompanyWindows(const Company *company)
 
{
 
	CompanyID cid = company->index;
 

	
 
	if (cid == _local_company) InvalidateWindow(WC_STATUS_BAR, 0);
 
	InvalidateWindow(WC_FINANCES, cid);
 
}
 

	
 
bool CheckCompanyHasMoney(CommandCost cost)
 
{
 
	if (cost.GetCost() > 0) {
 
		CompanyID company = _current_company;
 
		if (IsValidCompanyID(company) && cost.GetCost() > GetCompany(company)->money) {
 
			SetDParam(0, cost.GetCost());
 
			_error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
 
			return false;
 
		}
 
	}
 
	return true;
 
}
 

	
 
static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
 
{
 
	if (cost.GetCost() == 0) return;
 
	assert(cost.GetExpensesType() != INVALID_EXPENSES);
 

	
 
	c->money -= cost.GetCost();
 
	c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
 

	
 
	if (HasBit(1 << EXPENSES_TRAIN_INC    |
 
	           1 << EXPENSES_ROADVEH_INC  |
 
	           1 << EXPENSES_AIRCRAFT_INC |
 
	           1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
 
		c->cur_economy.income -= cost.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, cost.GetExpensesType())) {
 
		c->cur_economy.expenses -= cost.GetCost();
 
	}
 

	
 
	InvalidateCompanyWindows(c);
 
}
 

	
 
void SubtractMoneyFromCompany(CommandCost cost)
 
{
 
	CompanyID cid = _current_company;
 

	
 
	if (IsValidCompanyID(cid)) SubtractMoneyFromAnyCompany(GetCompany(cid), cost);
 
}
 

	
 
void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst)
 
{
 
	Company *c = GetCompany(company);
 
	byte m = c->money_fraction;
 
	Money cost = cst.GetCost();
 

	
 
	c->money_fraction = m - (byte)cost;
 
	cost >>= 8;
 
	if (c->money_fraction > m) cost++;
 
	if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost));
 
}
 

	
 
void GetNameOfOwner(Owner owner, TileIndex tile)
 
{
 
	SetDParam(2, owner);
 

	
 
	if (owner != OWNER_TOWN) {
 
		if (!IsValidCompanyID(owner)) {
 
			SetDParam(0, STR_0150_SOMEONE);
 
		} else {
 
			SetDParam(0, STR_COMPANY_NAME);
 
			SetDParam(1, owner);
 
		}
 
	} else {
 
		const Town *t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
		SetDParam(0, STR_TOWN);
 
		SetDParam(1, t->index);
 
	}
 
}
 

	
 

	
 
bool CheckOwnership(Owner owner)
 
{
 
	assert(owner < OWNER_END);
 

	
 
	if (owner == _current_company) return true;
 
	_error_message = STR_013B_OWNED_BY;
 
	GetNameOfOwner(owner, 0);
 
	return false;
 
}
 

	
 
bool CheckTileOwnership(TileIndex tile)
 
{
 
	Owner owner = GetTileOwner(tile);
 

	
 
	assert(owner < OWNER_END);
 

	
 
	if (owner == _current_company) return true;
 
	_error_message = STR_013B_OWNED_BY;
 

	
 
	/* no need to get the name of the owner unless we're the local company (saves some time) */
 
	if (IsLocalCompany()) GetNameOfOwner(owner, tile);
 
	return false;
 
}
 

	
 
static void GenerateCompanyName(Company *c)
 
{
 
	TileIndex tile;
 
	Town *t;
 
	StringID str;
 
	Company *cc;
 
	uint32 strp;
 
	char buffer[100];
 

	
 
	if (c->name_1 != STR_SV_UNNAMED) return;
 

	
 
	tile = c->last_build_coordinate;
 
	if (tile == 0) return;
 

	
 
	t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
	if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
 
		str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
 
		strp = t->townnameparts;
 

	
 
verify_name:;
 
		/* No companies must have this name already */
 
		FOR_ALL_COMPANIES(cc) {
 
			if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
 
		}
 

	
 
		GetString(buffer, str, lastof(buffer));
 
		if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
 

	
 
set_name:;
 
		c->name_1 = str;
 
		c->name_2 = strp;
 

	
 
		MarkWholeScreenDirty();
 

	
 
		if (!IsHumanCompany(c->index)) {
 
			CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
 
			cni->FillData(c);
 
			SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
 
			SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
 
			SetDParamStr(2, cni->company_name);
 
			SetDParam(3, t->index);
 
			AddNewsItem(STR_02B6, NS_COMPANY_NEW, c->last_build_coordinate, 0, cni);
 
		}
 
		return;
 
	}
 
bad_town_name:;
 

	
 
	if (c->president_name_1 == SPECSTR_PRESIDENT_NAME) {
 
		str = SPECSTR_ANDCO_NAME;
 
		strp = c->president_name_2;
 
		goto set_name;
 
	} else {
 
		str = SPECSTR_ANDCO_NAME;
 
		strp = Random();
 
		goto verify_name;
 
	}
 
}
 

	
 
static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
 
static const Colours _similar_colour[COLOUR_END][2] = {
 
	{ COLOUR_BLUE,       COLOUR_LIGHT_BLUE }, // COLOUR_DARK_BLUE
 
	{ COLOUR_GREEN,      COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN
 
	{ INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_PINK
 
	{ COLOUR_ORANGE,     INVALID_COLOUR    }, // COLOUR_YELLOW
 
	{ INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_RED
 
	{ COLOUR_DARK_BLUE,  COLOUR_BLUE       }, // COLOUR_LIGHT_BLUE
 
	{ COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN
 
	{ COLOUR_PALE_GREEN, COLOUR_GREEN      }, // COLOUR_DARK_GREEN
 
	{ COLOUR_DARK_BLUE,  COLOUR_LIGHT_BLUE }, // COLOUR_BLUE
 
	{ COLOUR_BROWN,      COLOUR_ORANGE     }, // COLOUR_CREAM
 
	{ COLOUR_PURPLE,     INVALID_COLOUR    }, // COLOUR_MAUVE
 
	{ COLOUR_MAUVE,      INVALID_COLOUR    }, // COLOUR_PURPLE
 
	{ COLOUR_YELLOW,     COLOUR_CREAM      }, // COLOUR_ORANGE
 
	{ COLOUR_CREAM,      INVALID_COLOUR    }, // COLOUR_BROWN
 
	{ COLOUR_WHITE,      INVALID_COLOUR    }, // COLOUR_GREY
 
	{ COLOUR_GREY,       INVALID_COLOUR    }, // COLOUR_WHITE
 
};
 

	
 
static byte GenerateCompanyColour()
 
{
 
	Colours colours[COLOUR_END];
 

	
 
	/* Initialize array */
 
	for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
 

	
 
	/* And randomize it */
 
	for (uint i = 0; i < 100; i++) {
 
		uint r = Random();
 
		Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
 
	}
 

	
 
	/* Bubble sort it according to the values in table 1 */
 
	for (uint i = 0; i < COLOUR_END; i++) {
 
		for (uint j = 1; j < COLOUR_END; j++) {
 
			if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
 
				Swap(colours[j - 1], colours[j]);
 
			}
 
		}
 
	};
 

	
 
	/* Move the colors that look similar to each company's color to the side */
 
	Company *c;
 
	FOR_ALL_COMPANIES(c) {
 
		Colours pcolour = (Colours)c->colour;
 

	
 
		for (uint i = 0; i < COLOUR_END; i++) {
 
			if (colours[i] == pcolour) {
 
				colours[i] = INVALID_COLOUR;
 
				break;
 
			}
 
		}
 

	
 
		for (uint j = 0; j < 2; j++) {
 
			Colours similar = _similar_colour[pcolour][j];
 
			if (similar == INVALID_COLOUR) break;
 

	
 
			for (uint i = 1; i < COLOUR_END; i++) {
 
				if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
 
			}
 
		}
 
	}
 

	
 
	/* Return the first available color */
 
	for (uint i = 0; i < COLOUR_END; i++) {
 
		if (colours[i] != INVALID_COLOUR) return colours[i];
 
	}
 

	
 
	NOT_REACHED();
 
}
 

	
 
static void GeneratePresidentName(Company *c)
 
{
 
	Company *cc;
 
	char buffer[100], buffer2[40];
 

	
 
	for (;;) {
 
restart:;
 

	
 
		c->president_name_2 = Random();
 
		c->president_name_1 = SPECSTR_PRESIDENT_NAME;
 

	
 
		SetDParam(0, c->index);
 
		GetString(buffer, STR_PLAYER_NAME, lastof(buffer));
 
		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94)
 
			continue;
 

	
 
		FOR_ALL_COMPANIES(cc) {
 
			if (c != cc) {
 
				SetDParam(0, cc->index);
 
				GetString(buffer2, STR_PLAYER_NAME, lastof(buffer2));
 
				if (strcmp(buffer2, buffer) == 0)
 
					goto restart;
 
			}
 
		}
 
		return;
 
	}
 
}
 

	
 
void ResetCompanyLivery(Company *c)
 
{
 
	for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
 
		c->livery[scheme].in_use  = false;
 
		c->livery[scheme].colour1 = c->colour;
 
		c->livery[scheme].colour2 = c->colour;
 
	}
 
}
 

	
 
/**
 
 * Create a new company and sets all company variables default values
 
 *
 
 * @param is_ai is a ai company?
 
 * @return the company struct
 
 */
 
Company *DoStartupNewCompany(bool is_ai)
 
{
 
	if (!Company::CanAllocateItem()) return NULL;
 

	
 
	Company *c = new Company(STR_SV_UNNAMED, is_ai);
 

	
 
	memset(&_companies_ai[c->index], 0, sizeof(CompanyAI));
 
	memset(&_companies_ainew[c->index], 0, sizeof(CompanyAiNew));
 

	
 
	/* Make a color */
 
	c->colour = GenerateCompanyColour();
 
	ResetCompanyLivery(c);
 
	_company_colours[c->index] = c->colour;
 

	
 
	c->money = c->current_loan = 100000;
 

	
 
	_companies_ai[c->index].state = 5; // AIS_WANT_NEW_ROUTE
 
	c->share_owners[0] = c->share_owners[1] = c->share_owners[2] = c->share_owners[3] = INVALID_OWNER;
 

	
 
	c->avail_railtypes = GetCompanyRailtypes(c->index);
 
	c->avail_roadtypes = GetCompanyRoadtypes(c->index);
 
	c->inaugurated_year = _cur_year;
 
	RandomCompanyManagerFaceBits(c->face, (GenderEthnicity)Random(), false); // create a random company manager face
 

	
 
	/* Engine renewal settings */
 
	c->engine_renew_list = NULL;
 
	c->renew_keep_length = false;
 
	c->engine_renew = _settings_client.gui.autorenew;
 
	c->engine_renew_months = _settings_client.gui.autorenew_months;
 
	c->engine_renew_money = _settings_client.gui.autorenew_money;
 

	
 
	GeneratePresidentName(c);
 

	
 
	InvalidateWindow(WC_GRAPH_LEGEND, 0);
 
	InvalidateWindow(WC_TOOLBAR_MENU, 0);
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

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

	
 
	c->num_engines = CallocT<uint16>(GetEnginePoolSize());
 

	
 
	return c;
 
}
 

	
 
void StartupCompanies()
 
{
 
	/* The AI starts like in the setting with +2 month max */
 
	_next_competitor_start = _settings_game.difficulty.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
 
}
 

	
 
static void MaybeStartNewCompany()
 
{
 
	uint n;
 
	Company *c;
 

	
 
	/* count number of competitors */
 
	n = 0;
 
	FOR_ALL_COMPANIES(c) {
 
		if (c->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) :
 
				RandomRange(_settings_game.difficulty.max_no_competitors + 2)
 
			)) {
 
		/* Send a command to all clients to start up a new AI.
 
		 * Works fine for Multiplayer and Singleplayer */
 
		DoCommandP(0, 1, 0, NULL, CMD_COMPANY_CTRL);
 
	}
 

	
 
	/* The next AI starts like the difficulty setting said, with +2 month max */
 
	_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 InitializeCompanies()
 
{
 
	_Company_pool.CleanPool();
 
	_Company_pool.AddBlockToPool();
 
	_cur_company_tick_index = 0;
 
}
 

	
 
void OnTick_Companies()
 
{
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	if (IsValidCompanyID((CompanyID)_cur_company_tick_index)) {
 
		Company *c = GetCompany((CompanyID)_cur_company_tick_index);
 
		if (c->name_1 != 0) GenerateCompanyName(c);
 

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

	
 
	_cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
 
}
 

	
 
void CompaniesYearlyLoop()
 
{
 
	Company *c;
 

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

	
 
	if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
 
		ShowCompanyFinances(_local_company);
 
		c = GetCompany(_local_company);
 
		if (c->num_valid_stat_ent > 5 && c->old_economy[0].performance_history < c->old_economy[4].performance_history) {
 
			SndPlayFx(SND_01_BAD_YEAR);
 
		} else {
 
			SndPlayFx(SND_00_GOOD_YEAR);
 
		}
 
	}
 
}
 

	
 
/** 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
 
 * - p1 = 2 - change auto renew money
 
 * - p1 = 3 - change auto renew array
 
 * - p1 = 4 - change bool, months & money all together
 
 * - p1 = 5 - change renew_keep_length
 
 * @param p2 value to set
 
 * if p1 = 0, then:
 
 * - p2 = enable engine renewal
 
 * if p1 = 1, then:
 
 * - p2 = months left before engine expires to replace it
 
 * if p1 = 2, then
 
 * - p2 = minimum amount of money available
 
 * if p1 = 3, then:
 
 * - p1 bits 16-31 = engine group
 
 * - p2 bits  0-15 = old engine type
 
 * - p2 bits 16-31 = new engine type
 
 * if p1 = 4, then:
 
 * - p1 bit     15 = enable engine renewal
 
 * - p1 bits 16-31 = months left before engine expires to replace it
 
 * - p2 bits  0-31 = minimum amount of money available
 
 * if p1 = 5, then
 
 * - p2 = enable renew_keep_length
 
 */
 
CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	if (!IsValidCompanyID(_current_company)) return CMD_ERROR;
 

	
 
	Company *c = GetCompany(_current_company);
 
	switch (GB(p1, 0, 3)) {
 
		case 0:
 
			if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				c->engine_renew = HasBit(p2, 0);
 
				if (IsLocalCompany()) {
 
					_settings_client.gui.autorenew = c->engine_renew;
 
					InvalidateWindow(WC_GAME_OPTIONS, 0);
 
				}
 
			}
 
			break;
 

	
 
		case 1:
 
			if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
 
			if (c->engine_renew_months == (int16)p2) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				c->engine_renew_months = (int16)p2;
 
				if (IsLocalCompany()) {
 
					_settings_client.gui.autorenew_months = c->engine_renew_months;
 
					InvalidateWindow(WC_GAME_OPTIONS, 0);
 
				}
 
			}
 
			break;
 

	
 
		case 2:
 
			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
 
			if (c->engine_renew_money == p2) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				c->engine_renew_money = p2;
 
				if (IsLocalCompany()) {
 
					_settings_client.gui.autorenew_money = c->engine_renew_money;
 
					InvalidateWindow(WC_GAME_OPTIONS, 0);
 
				}
 
			}
 
			break;
 

	
 
		case 3: {
 
			EngineID old_engine_type = GB(p2, 0, 16);
 
			EngineID new_engine_type = GB(p2, 16, 16);
 
			GroupID id_g = GB(p1, 16, 16);
 
			CommandCost cost;
 

	
 
			if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
 
			if (new_engine_type != INVALID_ENGINE) {
 
				if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
 

	
 
				cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
 
			} else {
 
				cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
 
			}
 

	
 
			if (IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
 

	
 
			return cost;
 
		}
 

	
 
		case 4:
 
			if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
 
			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				c->engine_renew = HasBit(p1, 15);
 
				c->engine_renew_months = (int16)GB(p1, 16, 16);
 
				c->engine_renew_money = p2;
 

	
 
				if (IsLocalCompany()) {
 
					_settings_client.gui.autorenew = c->engine_renew;
 
					_settings_client.gui.autorenew_months = c->engine_renew_months;
 
					_settings_client.gui.autorenew_money = c->engine_renew_money;
 
					InvalidateWindow(WC_GAME_OPTIONS, 0);
 
				}
 
			}
 
			break;
 

	
 
		case 5:
 
			if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				c->renew_keep_length = HasBit(p2, 0);
 
				if (IsLocalCompany()) {
 
					InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
 
				}
 
			}
 
		break;
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Fill the CompanyNewsInformation struct with the required data.
 
 * @param p the current company.
 
 * @param other the other company.
 
 */
 
void CompanyNewsInformation::FillData(const Company *c, const Company *other)
 
{
 
	SetDParam(0, c->index);
 
	GetString(this->company_name, STR_COMPANY_NAME, lastof(this->company_name));
 

	
 
	if (other == NULL) {
 
		*this->other_company_name = '\0';
 
	} else {
 
		SetDParam(0, other->index);
 
		GetString(this->other_company_name, STR_COMPANY_NAME, lastof(this->other_company_name));
 
		c = other;
 
	}
 

	
 
	SetDParam(0, c->index);
 
	GetString(this->president_name, STR_7058_PRESIDENT, lastof(this->president_name));
 

	
 
	this->colour = c->colour;
 
	this->face = c->face;
 

	
 
}
 

	
 
/** Control the companies: add, delete, etc.
 
 * @param tile unused
 
 * @param flags operation to perform
 
 * @param p1 various functionality
 
 * - p1 = 0 - create a new company, Which company (network) it will be is in p2
 
 * - p1 = 1 - create a new AI company
 
 * - p1 = 2 - delete a company. Company is identified by p2
 
 * - p1 = 3 - merge two companies together. merge #1 with #2. Identified by p2
 
 * @param p2 various functionality, dictated by p1
 
 * - p1 = 0 - ClientID of the newly created client
 
 * - p1 = 2 - CompanyID of the that is getting deleted
 
 * - p1 = 3 - #1 p2 = (bit  0-15) - company to merge (p2 & 0xFFFF)
 
 *          - #2 p2 = (bit 16-31) - company to be merged into ((p2>>16)&0xFFFF)
 
 * @todo In the case of p1=0, create new company, the clientID of the new client is in parameter
 
 * p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
 
 * on the server itself. First of all this is unbelievably ugly; second of all, well,
 
 * it IS ugly! <b>Someone fix this up :)</b> So where to fix?@n
 
 * @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
 
 */
 
CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	if (flags & DC_EXEC) _current_company = OWNER_NONE;
 

	
 
	InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
 

	
 
	switch (p1) {
 
		case 0: { /* Create a new company */
 
			/* This command is only executed in a multiplayer game */
 
			if (!_networking) return CMD_ERROR;
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
			/* Joining Client:
 
			* _local_company: COMPANY_SPECTATOR
 
			* _network_playas/cid = requested company/clientid
 
			*
 
			* Other client(s)/server:
 
			* _local_company/_network_playas: what they play as
 
			* cid = requested company/company of joining client */
 
			uint16 cid = p2; // ClientID
 

	
 
			/* Has the network client a correct ClientID? */
 
			if (!(flags & DC_EXEC)) return CommandCost();
 
			if (cid >= MAX_CLIENT_INFO) return CommandCost();
 

	
 
			/* Delete multiplayer progress bar */
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
			Company *c = DoStartupNewCompany(false);
 

	
 
			/* A new company could not be created, revert to being a spectator */
 
			if (c == NULL) {
 
				if (_network_server) {
 
					NetworkClientInfo *ci = &_network_client_info[cid];
 
					ci->client_playas = COMPANY_SPECTATOR;
 
					NetworkUpdateClientInfo(ci->client_index);
 
				} else if (_local_company == COMPANY_SPECTATOR) {
 
					_network_playas = COMPANY_SPECTATOR;
 
				}
 
				break;
 
			}
 

	
 
			/* This is the joining client who wants a new company */
 
			if (_local_company != _network_playas && _network_playas == c->index) {
 
				assert(_local_company == COMPANY_SPECTATOR);
 
				SetLocalCompany(c->index);
 
				if (!StrEmpty(_settings_client.network.default_company_pass)) {
 
					char *password = _settings_client.network.default_company_pass;
 
					NetworkChangeCompanyPassword(1, &password);
 
				}
 

	
 
				_current_company = _local_company;
 

	
 
				/* Now that we have a new company, broadcast our autorenew settings to
 
				* all clients so everything is in sync */
 
				NetworkSend_Command(0,
 
					(_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4,
 
					_settings_client.gui.autorenew_money,
 
					CMD_SET_AUTOREPLACE,
 
					NULL
 
				);
 

	
 
				MarkWholeScreenDirty();
 
			}
 

	
 
			if (_network_server) {
 
				/* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
 
				* server-side in network_server.c:838, function
 
				* DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
 
				NetworkClientInfo *ci = &_network_client_info[cid];
 
				ci->client_playas = c->index;
 
				NetworkUpdateClientInfo(ci->client_index);
 

	
 
				if (IsValidCompanyID(ci->client_playas)) {
 
					CompanyID company_backup = _local_company;
 
					_network_company_info[c->index].months_empty = 0;
 

	
 
					/* XXX - When a client joins, we automatically set its name to the
 
					* client's name (for some reason). As it stands now only the server
 
					* knows the client's name, so it needs to send out a "broadcast" to
 
					* do this. To achieve this we send a network command. However, it
 
					* uses _local_company to execute the command as.  To prevent abuse
 
					* (eg. only yourself can change your name/company), we 'cheat' by
 
					* impersonation _local_company as the server. Not the best solution;
 
					* but it works.
 
					* TODO: Perhaps this could be improved by when the client is ready
 
					* with joining to let it send itself the command, and not the server?
 
					* For example in network_client.c:534? */
 
					_cmd_text = ci->client_name;
 
					_local_company = ci->client_playas;
 
					NetworkSend_Command(0, 0, 0, CMD_RENAME_PRESIDENT, NULL);
 
					_local_company = company_backup;
 
				}
 
			}
 
#endif /* ENABLE_NETWORK */
 
		} break;
 

	
 
		case 1: /* Make a new AI company */
 
			if (!(flags & DC_EXEC)) return CommandCost();
 

	
 
			DoStartupNewCompany(true);
 
			break;
 

	
 
		case 2: { /* Delete a company */
 
			Company *c;
 

	
 
			if (!IsValidCompanyID((CompanyID)p2)) return CMD_ERROR;
 

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

	
 
			c = GetCompany((CompanyID)p2);
 

	
 
			/* Only allow removal of HUMAN companies */
 
			if (IsHumanCompany(c->index)) {
 
				/* Delete any open window of the company */
 
				DeleteCompanyWindows(c->index);
 

	
 
				CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
 
				cni->FillData(c);
 

	
 
				/* Show the bankrupt news */
 
				SetDParam(0, STR_705C_BANKRUPT);
 
				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 */
 
				ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
 

	
 
				delete c;
 
			}
 
		} break;
 

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

	
 
			if (!IsValidCompanyID(cid_old) || !IsValidCompanyID(cid_new)) return CMD_ERROR;
 

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

	
 
			ChangeOwnershipOfCompanyItems(cid_old, cid_new);
 
			delete GetCompany(cid_old);
 
		} break;
 

	
 
		default: return CMD_ERROR;
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
static const StringID _endgame_perf_titles[] = {
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0213_BUSINESSMAN,
 
	STR_0214_ENTREPRENEUR,
 
	STR_0214_ENTREPRENEUR,
 
	STR_0215_INDUSTRIALIST,
 
	STR_0215_INDUSTRIALIST,
 
	STR_0216_CAPITALIST,
 
	STR_0216_CAPITALIST,
 
	STR_0217_MAGNATE,
 
	STR_0217_MAGNATE,
 
	STR_0218_MOGUL,
 
	STR_0218_MOGUL,
 
	STR_0219_TYCOON_OF_THE_CENTURY
 
};
 

	
 
StringID EndGameGetPerformanceTitleFromValue(uint value)
 
{
 
	value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
 

	
 
	return _endgame_perf_titles[value];
 
}
 

	
 
/** Save the highscore for the company */
 
int8 SaveHighScoreValue(const Company *c)
 
{
 
	HighScore *hs = _highscore_table[_settings_game.difficulty.diff_level];
 
	uint i;
 
	uint16 score = c->old_economy[0].performance_history;
 

	
 
	/* Exclude cheaters from the honour of being in the highscore table */
 
	if (CheatHasBeenUsed()) return -1;
 

	
 
	for (i = 0; i < lengthof(_highscore_table[0]); i++) {
 
		/* You are in the TOP5. Move all values one down and save us there */
 
		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, c->index);
 
			SetDParam(1, c->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);
 
			return i;
 
		}
 
	}
 

	
 
	return -1; // too bad; we did not make it into the top5
 
}
 

	
 
/** Sort all companies given their performance */
 
static int CDECL HighScoreSorter(const Company* const *a, const Company* const *b)
 
{
 
	return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
 
}
 

	
 
/* Save the highscores in a network game when it has ended */
 
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
 
int8 SaveHighScoreValueNetwork()
 
{
 
	const Company *c;
 
	const Company *cl[MAX_COMPANIES];
 
	uint count = 0;
 
	int8 company = -1;
 

	
 
	/* Sort all active companies with the highest score first */
 
	FOR_ALL_COMPANIES(c) cl[count++] = c;
 

	
 
	GSortT(cl, count, &HighScoreSorter);
 

	
 
	{
 
		uint i;
 

	
 
		memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
 

	
 
		/* Copy over Top5 companies */
 
		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
 
			HighScore* hs = &_highscore_table[LAST_HS_ITEM][i];
 

	
 
			SetDParam(0, cl[i]->index);
 
			SetDParam(1, cl[i]->index);
 
			GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
 
			hs->score = cl[i]->old_economy[0].performance_history;
 
			hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 

	
 
			/* get the ranking of the local company */
 
			if (cl[i]->index == _local_company) company = i;
 
		}
 
	}
 

	
 
	/* Add top5 companys to highscore table */
 
	return company;
 
}
 

	
 
/** Save HighScore table to file */
 
void SaveToHighScore()
 
{
 
	FILE *fp = fopen(_highscore_file, "wb");
 

	
 
	if (fp != NULL) {
 
		uint i;
 
		HighScore *hs;
 

	
 
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
 
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
				/* First character is a command character, so strlen will fail on that */
 
				byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
 

	
 
				if (fwrite(&length, sizeof(length), 1, fp)       != 1 || // write away string length
 
						fwrite(hs->company, length, 1, fp)           >  1 || // Yes... could be 0 bytes too
 
						fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
 
						fwrite("  ", 2, 1, fp)                       != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 
					DEBUG(misc, 1, "Could not save highscore.");
 
					i = LAST_HS_ITEM;
 
					break;
 
				}
 
			}
 
		}
 
		fclose(fp);
 
	}
 
}
 

	
 
/** Initialize the highscore table to 0 and if any file exists, load in values */
 
void LoadFromHighScore()
 
{
 
	FILE *fp = fopen(_highscore_file, "rb");
 

	
 
	memset(_highscore_table, 0, sizeof(_highscore_table));
 

	
 
	if (fp != NULL) {
 
		uint i;
 
		HighScore *hs;
 

	
 
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
 
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
				byte length;
 
				if (fread(&length, sizeof(length), 1, fp)       !=  1 ||
 
						fread(hs->company, length, 1, fp)           >   1 || // Yes... could be 0 bytes too
 
						fread(&hs->score, sizeof(hs->score), 1, fp) !=  1 ||
 
						fseek(fp, 2, SEEK_CUR)                      == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 
					DEBUG(misc, 1, "Highscore corrupted");
 
					i = LAST_HS_ITEM;
 
					break;
 
				}
 
				*lastof(hs->company) = '\0';
 
				hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
			}
 
		}
 
		fclose(fp);
 
	}
 

	
 
	/* Initialize end of game variable (when to show highscore chart) */
 
	_settings_client.gui.ending_year = 2051;
 
}
 

	
 
/* Save/load of companies */
 
static const SaveLoad _company_desc[] = {
 
	    SLE_VAR(Company, name_2,          SLE_UINT32),
 
	    SLE_VAR(Company, name_1,          SLE_STRINGID),
 
	SLE_CONDSTR(Company, name,            SLE_STR, 0,                       84, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(Company, president_name_1, SLE_UINT16),
 
	    SLE_VAR(Company, president_name_2, SLE_UINT32),
 
	SLE_CONDSTR(Company, president_name,  SLE_STR, 0,                       84, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(Company, face,            SLE_UINT32),
 

	
 
	/* money was changed to a 64 bit field in savegame version 1. */
 
	SLE_CONDVAR(Company, money,                 SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
 
	SLE_CONDVAR(Company, money,                 SLE_INT64,                   1, SL_MAX_VERSION),
 

	
 
	SLE_CONDVAR(Company, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
 
	SLE_CONDVAR(Company, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(Company, colour,                SLE_UINT8),
 
	    SLE_VAR(Company, money_fraction,        SLE_UINT8),
 
	SLE_CONDVAR(Company, avail_railtypes,       SLE_UINT8,                   0, 57),
 
	    SLE_VAR(Company, block_preview,         SLE_UINT8),
 

	
 
	SLE_CONDVAR(Company, cargo_types,           SLE_FILE_U16 | SLE_VAR_U32,  0, 93),
 
	SLE_CONDVAR(Company, cargo_types,           SLE_UINT32,                 94, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, location_of_HQ,        SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
 
	SLE_CONDVAR(Company, location_of_HQ,        SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
 
	SLE_CONDVAR(Company, last_build_coordinate, SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, inaugurated_year,      SLE_FILE_U8  | SLE_VAR_I32,  0, 30),
 
	SLE_CONDVAR(Company, inaugurated_year,      SLE_INT32,                  31, SL_MAX_VERSION),
 

	
 
	    SLE_ARR(Company, share_owners,          SLE_UINT8, 4),
 

	
 
	    SLE_VAR(Company, num_valid_stat_ent,    SLE_UINT8),
 

	
 
	    SLE_VAR(Company, quarters_of_bankrupcy, SLE_UINT8),
 
	    SLE_VAR(Company, bankrupt_asked,        SLE_UINT8),
 
	    SLE_VAR(Company, bankrupt_timeout,      SLE_INT16),
 
	SLE_CONDVAR(Company, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
 
	SLE_CONDVAR(Company, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
 

	
 
	/* yearly expenses was changed to 64-bit in savegame version 2. */
 
	SLE_CONDARR(Company, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
 
	SLE_CONDARR(Company, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
 

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

	
 
	/* Engine renewal settings */
 
	SLE_CONDNULL(512, 16, 18),
 
	SLE_CONDREF(Company, engine_renew_list,     REF_ENGINE_RENEWS,          19, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, engine_renew,          SLE_BOOL,                   16, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, engine_renew_months,   SLE_INT16,                  16, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, engine_renew_money,    SLE_UINT32,                 16, SL_MAX_VERSION),
 
	SLE_CONDVAR(Company, renew_keep_length,     SLE_BOOL,                    2, SL_MAX_VERSION), // added with 16.1, but was blank since 2
 

	
 
	/* reserve extra space in savegame here. (currently 63 bytes) */
 
	SLE_CONDNULL(63, 2, SL_MAX_VERSION),
 

	
 
	SLE_END()
 
};
 

	
 
static const SaveLoad _company_economy_desc[] = {
 
	/* these were changed to 64-bit in savegame format 2 */
 
	SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
 
	SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
 
	SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
 
	SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
 
	SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
 
	SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(CompanyEconomyEntry, delivered_cargo,     SLE_INT32),
 
	    SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
 

	
 
	SLE_END()
 
};
 

	
 
static const SaveLoad _company_livery_desc[] = {
 
	SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
 
	SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
 
	SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
 
	SLE_END()
 
};
 

	
 
static void SaveLoad_PLYR(Company *c)
 
{
 
	int i;
 

	
 
	SlObject(c, _company_desc);
 

	
 
	/* Write AI? */
 
	if (!IsHumanCompany(c->index)) {
 
		SaveLoad_AI(c->index);
 
	}
 

	
 
	/* Write economy */
 
	SlObject(&c->cur_economy, _company_economy_desc);
 

	
 
	/* Write old economy entries. */
 
	for (i = 0; i < c->num_valid_stat_ent; i++) {
 
		SlObject(&c->old_economy[i], _company_economy_desc);
 
	}
 

	
 
	/* Write each livery entry. */
 
	int num_liveries = CheckSavegameVersion(63) ? LS_END - 4 : (CheckSavegameVersion(85) ? LS_END - 2: LS_END);
 
	for (i = 0; i < num_liveries; i++) {
 
		SlObject(&c->livery[i], _company_livery_desc);
 
	}
 

	
 
	if (num_liveries < LS_END) {
 
		/* We want to insert some liveries somewhere in between. This means some have to be moved. */
 
		memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
 
		c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
 
		c->livery[LS_PASSENGER_WAGON_MAGLEV]   = c->livery[LS_MAGLEV];
 
	}
 

	
 
	if (num_liveries == LS_END - 4) {
 
		/* Copy bus/truck liveries over to trams */
 
		c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
 
		c->livery[LS_FREIGHT_TRAM]   = c->livery[LS_TRUCK];
 
	}
 
}
 

	
 
static void Save_PLYR()
 
{
 
	Company *c;
 
	FOR_ALL_COMPANIES(c) {
 
		SlSetArrayIndex(c->index);
 
		SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
 
	}
 
}
 

	
 
static void Load_PLYR()
 
{
 
	int index;
 
	while ((index = SlIterateArray()) != -1) {
 
		Company *c = new (index) Company();
 
		SaveLoad_PLYR(c);
 
		_company_colours[index] = c->colour;
 

	
 
		/* This is needed so an AI is attached to a loaded AI */
 
		if (c->is_ai && (!_networking || _network_server) && _ai.enabled) {
 
			/* Clear the memory of the new AI, otherwise we might be doing wrong things. */
 
			memset(&_companies_ainew[index], 0, sizeof(CompanyAiNew));
 
			AI_StartNewAI(c->index);
 
		}
 
	}
 
}
 

	
 
extern const ChunkHandler _company_chunk_handlers[] = {
 
	{ 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
 
};
src/company_func.h
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_func.h Functions related to companies. */
 

	
 
#ifndef COMPANY_FUNC_H
 
#define COMPANY_FUNC_H
 

	
 
#include "core/math_func.hpp"
 
#include "company_type.h"
 
#include "tile_type.h"
 
#include "strings_type.h"
 

	
 
void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner);
 
void GetNameOfOwner(Owner owner, TileIndex tile);
 
void SetLocalCompany(CompanyID new_company);
 

	
 
extern CompanyByte _local_company;
 
extern CompanyByte _current_company;
 

	
 
extern byte _company_colours[MAX_COMPANIES];     ///< NOSAVE: can be determined from company structs
 
extern CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg
 

	
 
bool IsHumanCompany(CompanyID company);
 

	
 
static inline bool IsLocalCompany()
 
{
 
	return _local_company == _current_company;
 
}
 

	
 
static inline bool IsInteractiveCompany(CompanyID company)
 
{
 
	return company == _local_company;
 
}
 

	
 

	
 

	
 
struct HighScore {
 
	char company[100];
 
	StringID title; ///< NOSAVE, has troubles with changing string-numbers.
 
	uint16 score;   ///< do NOT change type, will break hs.dat
 
};
 

	
 
extern HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
 
void SaveToHighScore();
 
void LoadFromHighScore();
 
int8 SaveHighScoreValue(const Company *p);
 
int8 SaveHighScoreValueNetwork();
 

	
 
#endif /* COMPANY_FUNC_H */
src/company_gui.cpp
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_gui.cpp Company related GUIs. */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "gui.h"
 
#include "window_gui.h"
 
#include "textbuf_gui.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "command_func.h"
 
#include "network/network.h"
 
#include "network/network_gui.h"
 
#include "variables.h"
 
#include "roadveh.h"
 
#include "train.h"
 
#include "aircraft.h"
 
#include "newgrf.h"
 
#include "company_manager_face.h"
 
#include "strings_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "string_func.h"
 
#include "settings_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "tilehighlight_func.h"
 

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

	
 
enum {
 
	FIRST_GUI_CALL = INT_MAX,  ///< default value to specify thuis is the first call of the resizable gui
 
};
 

	
 
static void DoShowCompanyFinances(CompanyID company, bool show_small, bool show_stickied, int top = FIRST_GUI_CALL, int left = FIRST_GUI_CALL);
 
static void DoSelectCompanyManagerFace(Window *parent, bool show_big, int top =  FIRST_GUI_CALL, int left = FIRST_GUI_CALL);
 

	
 
static void DrawCompanyEconomyStats(const Company *c, bool small)
 
{
 
	int x, y, i, j, year;
 
	const Money (*tbl)[EXPENSES_END];
 
	Money sum, cost;
 
	StringID str;
 

	
 
	if (!small) { // normal sized economics window
 
		/* draw categories */
 
		DrawStringCenterUnderline(61, 15, STR_700F_EXPENDITURE_INCOME, TC_FROMSTRING);
 
		for (i = 0; i != EXPENSES_END; i++)
 
			DrawString(2, 27 + i * 10, STR_7011_CONSTRUCTION + i, TC_FROMSTRING);
 
		DrawStringRightAligned(111, 27 + 10 * EXPENSES_END + 2, STR_7020_TOTAL, TC_FROMSTRING);
 

	
 
		/* draw the price columns */
 
		year = _cur_year - 2;
 
		j = 3;
 
		x = 215;
 
		tbl = c->yearly_expenses + 2;
 
		do {
 
			if (year >= c->inaugurated_year) {
 
				SetDParam(0, year);
 
				DrawStringRightAlignedUnderline(x, 15, STR_7010, TC_FROMSTRING);
 
				sum = 0;
 
				for (i = 0; i != EXPENSES_END; i++) {
 
					/* draw one row in the price column */
 
					cost = (*tbl)[i];
 
					if (cost != 0) {
 
						sum += cost;
 

	
 
						str = STR_701E;
 
						if (cost < 0) { cost = -cost; str++; }
 
						SetDParam(0, cost);
 
						DrawStringRightAligned(x, 27 + i * 10, str, TC_FROMSTRING);
 
					}
 
				}
 

	
 
				str = STR_701E;
 
				if (sum < 0) { sum = -sum; str++; }
 
				SetDParam(0, sum);
 
				DrawStringRightAligned(x, 27 + EXPENSES_END * 10 + 2, str, TC_FROMSTRING);
 

	
 
				GfxFillRect(x - 75, 27 + 10 * EXPENSES_END, x, 27 + 10 * EXPENSES_END, 215);
 
				x += 95;
 
			}
 
			year++;
 
			tbl--;
 
		} while (--j != 0);
 

	
 
		y = 27 + 10 * EXPENSES_END + 14;
 

	
 
		/* draw max loan aligned to loan below (y += 10) */
 
		SetDParam(0, _economy.max_loan);
 
		DrawString(202, y + 10, STR_MAX_LOAN, TC_FROMSTRING);
 
	} else {
 
		y = 15;
 
	}
 

	
 
	DrawString(2, y, STR_7026_BANK_BALANCE, TC_FROMSTRING);
 
	SetDParam(0, c->money);
 
	DrawStringRightAligned(182, y, STR_7028, TC_FROMSTRING);
 

	
 
	y += 10;
 

	
 
	DrawString(2, y, STR_7027_LOAN, TC_FROMSTRING);
 
	SetDParam(0, c->current_loan);
 
	DrawStringRightAligned(182, y, STR_7028, TC_FROMSTRING);
 

	
 
	y += 12;
 

	
 
	GfxFillRect(182 - 75, y - 2, 182, y - 2, 215);
 

	
 
	SetDParam(0, c->money - c->current_loan);
 
	DrawStringRightAligned(182, y, STR_7028, TC_FROMSTRING);
 
}
 

	
 
enum CompanyFinancesWindowWidgets {
 
	CFW_WIDGET_TOGGLE_SIZE   = 2,
 
	CFW_WIDGET_INCREASE_LOAN = 6,
 
	CFW_WIDGET_REPAY_LOAN    = 7,
 
};
 

	
 
static const Widget _company_finances_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   379,     0,    13, STR_700E_FINANCES,      STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{     WWT_IMGBTN,   RESIZE_NONE,  COLOUR_GREY,   380,   394,     0,    13, SPR_LARGE_SMALL_WINDOW, STR_7075_TOGGLE_LARGE_SMALL_WINDOW},
 
{  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_GREY,   395,   406,     0,    13, 0x0,                    STR_STICKY_BUTTON},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   406,    14, 39 + 10 * EXPENSES_END, 0x0,    STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   406, 40 + 10 * EXPENSES_END, 73 + 10 * EXPENSES_END, 0x0, STR_NULL},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   202, 74 + 10 * EXPENSES_END, 85 + 10 * EXPENSES_END, STR_7029_BORROW,        STR_7035_INCREASE_SIZE_OF_LOAN},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   203,   406, 74 + 10 * EXPENSES_END, 85 + 10 * EXPENSES_END, STR_702A_REPAY,         STR_7036_REPAY_PART_OF_LOAN},
 
{   WIDGETS_END},
 
};
 

	
 
static const Widget _company_finances_small_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   253,     0,    13, STR_700E_FINANCES,      STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{     WWT_IMGBTN,   RESIZE_NONE,  COLOUR_GREY,   254,   267,     0,    13, SPR_LARGE_SMALL_WINDOW, STR_7075_TOGGLE_LARGE_SMALL_WINDOW},
 
{  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_GREY,   268,   279,     0,    13, 0x0,                    STR_STICKY_BUTTON},
 
{      WWT_EMPTY,   RESIZE_NONE,  COLOUR_GREY,     0,     0,     0,     0, 0x0,                    STR_NULL},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   279,    14,    47, STR_NULL,               STR_NULL},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   139,    48,    59, STR_7029_BORROW,        STR_7035_INCREASE_SIZE_OF_LOAN},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   140,   279,    48,    59, STR_702A_REPAY,         STR_7036_REPAY_PART_OF_LOAN},
 
{   WIDGETS_END},
 
};
 

	
 
struct CompanyFinancesWindow : Window {
 
	bool small;
 

	
 
	CompanyFinancesWindow(const WindowDesc *desc, CompanyID company, bool show_small,
 
					bool show_stickied, int top, int left) :
 
			Window(desc, company),
 
			small(show_small)
 
	{
 
		this->caption_color = this->window_number;
 

	
 
		if (show_stickied) this->flags4 |= WF_STICKY;
 

	
 
		/* Check if repositioning from default is required */
 
		if (top != FIRST_GUI_CALL && left != FIRST_GUI_CALL) {
 
			this->top = top;
 
			this->left = left;
 
		}
 

	
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		CompanyID company = (CompanyID)this->window_number;
 
		const Company *c = GetCompany(company);
 

	
 
		/* Recheck the size of the window as it might need to be resized due to the local company changing */
 
		int new_height = ((company != _local_company) ? 0 : 12) + ((this->small != 0) ? 48 : 74 + 10 * EXPENSES_END);
 
		if (this->height != new_height) {
 
			/* Make window dirty before and after resizing */
 
			this->SetDirty();
 
			this->height = new_height;
 
			this->SetDirty();
 

	
 
			this->SetWidgetHiddenState(CFW_WIDGET_INCREASE_LOAN, company != _local_company);
 
			this->SetWidgetHiddenState(CFW_WIDGET_REPAY_LOAN,    company != _local_company);
 
		}
 

	
 
		/* Borrow button only shows when there is any more money to loan */
 
		this->SetWidgetDisabledState(CFW_WIDGET_INCREASE_LOAN, c->current_loan == _economy.max_loan);
 

	
 
		/* Repay button only shows when there is any more money to repay */
 
		this->SetWidgetDisabledState(CFW_WIDGET_REPAY_LOAN, company != _local_company || c->current_loan == 0);
 

	
 
		SetDParam(0, c->index);
 
		SetDParam(1, c->index);
 
		SetDParam(2, LOAN_INTERVAL);
 
		this->DrawWidgets();
 

	
 
		DrawCompanyEconomyStats(c, this->small);
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case CFW_WIDGET_TOGGLE_SIZE: {/* toggle size */
 
				bool new_mode = !this->small;
 
				bool stickied = !!(this->flags4 & WF_STICKY);
 
				int oldtop = this->top;   ///< current top position of the window before closing it
 
				int oldleft = this->left; ///< current left position of the window before closing it
 
				CompanyID company = (CompanyID)this->window_number;
 

	
 
				delete this;
 
				/* Open up the (toggled size) Finance window at the same position as the previous */
 
				DoShowCompanyFinances(company, new_mode, stickied, oldtop, oldleft);
 
			}
 
			break;
 

	
 
			case CFW_WIDGET_INCREASE_LOAN: /* increase loan */
 
				DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_INCREASE_LOAN | CMD_MSG(STR_702C_CAN_T_BORROW_ANY_MORE_MONEY));
 
				break;
 

	
 
			case CFW_WIDGET_REPAY_LOAN: /* repay loan */
 
				DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_DECREASE_LOAN | CMD_MSG(STR_702F_CAN_T_REPAY_LOAN));
 
				break;
 
		}
 
	}
 
};
 

	
 
static const WindowDesc _company_finances_desc = {
 
	WDP_AUTO, WDP_AUTO, 407, 86 + 10 * EXPENSES_END, 407, 86 + 10 * EXPENSES_END,
 
	WC_FINANCES, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
 
	_company_finances_widgets,
 
};
 

	
 
static const WindowDesc _company_finances_small_desc = {
 
	WDP_AUTO, WDP_AUTO, 280, 60, 280, 60,
 
	WC_FINANCES, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
 
	_company_finances_small_widgets,
 
};
 

	
 
/**
 
 * Open the small/large finance window of the company
 
 *
 
 * @param company        the company who's finances are requested to be seen
 
 * @param show_small     show large or small version opf the window
 
 * @param show_stickied  previous "stickyness" of the window
 
 * @param top            previous top position of the window
 
 * @param left           previous left position of the window
 
 *
 
 * @pre is company a valid company
 
 */
 
static void DoShowCompanyFinances(CompanyID company, bool show_small, bool show_stickied, int top, int left)
 
{
 
	if (!IsValidCompanyID(company)) return;
 

	
 
	if (BringWindowToFrontById(WC_FINANCES, company)) return;
 
	new CompanyFinancesWindow(show_small ? &_company_finances_small_desc : &_company_finances_desc, company, show_small, show_stickied, top, left);
 
}
 

	
 
void ShowCompanyFinances(CompanyID company)
 
{
 
	DoShowCompanyFinances(company, false, false);
 
}
 

	
 
/* List of colours for the livery window */
 
static const StringID _colour_dropdown[] = {
 
	STR_00D1_DARK_BLUE,
 
	STR_00D2_PALE_GREEN,
 
	STR_00D3_PINK,
 
	STR_00D4_YELLOW,
 
	STR_00D5_RED,
 
	STR_00D6_LIGHT_BLUE,
 
	STR_00D7_GREEN,
 
	STR_00D8_DARK_GREEN,
 
	STR_00D9_BLUE,
 
	STR_00DA_CREAM,
 
	STR_00DB_MAUVE,
 
	STR_00DC_PURPLE,
 
	STR_00DD_ORANGE,
 
	STR_00DE_BROWN,
 
	STR_00DF_GREY,
 
	STR_00E0_WHITE,
 
};
 

	
 
/* Association of liveries to livery classes */
 
static const LiveryClass _livery_class[LS_END] = {
 
	LC_OTHER,
 
	LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL,
 
	LC_ROAD, LC_ROAD,
 
	LC_SHIP, LC_SHIP,
 
	LC_AIRCRAFT, LC_AIRCRAFT, LC_AIRCRAFT,
 
	LC_ROAD, LC_ROAD,
 
};
 

	
 
class DropDownListColourItem : public DropDownListItem {
 
public:
 
	DropDownListColourItem(int result, bool masked) : DropDownListItem(result, masked) {}
 

	
 
	virtual ~DropDownListColourItem() {}
 

	
 
	StringID String() const
 
	{
 
		return _colour_dropdown[this->result];
 
	}
 

	
 
	uint Height(uint width) const
 
	{
 
		return 14;
 
	}
 

	
 
	bool Selectable() const
 
	{
 
		return true;
 
	}
 

	
 
	void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
 
	{
 
		DrawSprite(SPR_VEH_BUS_SIDE_VIEW, PALETTE_RECOLOR_START + this->result, x + 16, y + 7);
 
		DrawStringTruncated(x + 32, y + 3, this->String(), sel ? TC_WHITE : TC_BLACK, width - 30);
 
	}
 
};
 

	
 
struct SelectCompanyLiveryWindow : public Window {
 
private:
 
	uint32 sel;
 
	LiveryClass livery_class;
 

	
 
	enum SelectCompanyLiveryWindowWidgets {
 
		SCLW_WIDGET_CLOSE,
 
		SCLW_WIDGET_CAPTION,
 
		SCLW_WIDGET_CLASS_GENERAL,
 
		SCLW_WIDGET_CLASS_RAIL,
 
		SCLW_WIDGET_CLASS_ROAD,
 
		SCLW_WIDGET_CLASS_SHIP,
 
		SCLW_WIDGET_CLASS_AIRCRAFT,
 
		SCLW_WIDGET_SPACER_CLASS,
 
		SCLW_WIDGET_SPACER_DROPDOWN,
 
		SCLW_WIDGET_PRI_COL_DROPDOWN,
 
		SCLW_WIDGET_SEC_COL_DROPDOWN,
 
		SCLW_WIDGET_MATRIX,
 
	};
 

	
 
	void ShowColourDropDownMenu(uint32 widget)
 
	{
 
		uint32 used_colours = 0;
 
		const Livery *livery;
 
		LiveryScheme scheme;
 

	
 
		/* Disallow other company colours for the primary colour */
 
		if (HasBit(this->sel, LS_DEFAULT) && widget == SCLW_WIDGET_PRI_COL_DROPDOWN) {
 
			const Company *c;
 
			FOR_ALL_COMPANIES(c) {
 
				if (c->index != _local_company) SetBit(used_colours, c->colour);
 
			}
 
		}
 

	
 
		/* 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;
 
		}
 
		if (scheme == LS_END) scheme = LS_DEFAULT;
 
		livery = &GetCompany((CompanyID)this->window_number)->livery[scheme];
 

	
 
		DropDownList *list = new DropDownList();
 
		for (uint i = 0; i < lengthof(_colour_dropdown); i++) {
 
			list->push_back(new DropDownListColourItem(i, HasBit(used_colours, i)));
 
		}
 

	
 
		ShowDropDownList(this, list, widget == SCLW_WIDGET_PRI_COL_DROPDOWN ? livery->colour1 : livery->colour2, widget);
 
	}
 

	
 
public:
 
	SelectCompanyLiveryWindow(const WindowDesc *desc, CompanyID company) : Window(desc, company)
 
	{
 
		this->caption_color = company;
 
		this->livery_class = LC_OTHER;
 
		this->sel = 1;
 
		this->LowerWidget(SCLW_WIDGET_CLASS_GENERAL);
 
		this->OnInvalidateData(_loaded_newgrf_features.has_2CC);
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const Company *c = GetCompany((CompanyID)this->window_number);
 
		LiveryScheme scheme = LS_DEFAULT;
 
		int y = 51;
 

	
 
		/* Disable dropdown controls if no scheme is selected */
 
		this->SetWidgetDisabledState(SCLW_WIDGET_PRI_COL_DROPDOWN, this->sel == 0);
 
		this->SetWidgetDisabledState(SCLW_WIDGET_SEC_COL_DROPDOWN, this->sel == 0);
 

	
 
		if (this->sel != 0) {
 
			for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
 
				if (HasBit(this->sel, scheme)) break;
 
			}
 
			if (scheme == LS_END) scheme = LS_DEFAULT;
 
		}
 

	
 
		SetDParam(0, STR_00D1_DARK_BLUE + c->livery[scheme].colour1);
 
		SetDParam(1, STR_00D1_DARK_BLUE + c->livery[scheme].colour2);
 

	
 
		this->DrawWidgets();
 

	
 
		for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
 
			if (_livery_class[scheme] == this->livery_class) {
 
				bool sel = HasBit(this->sel, scheme) != 0;
 

	
 
				if (scheme != LS_DEFAULT) {
 
					DrawSprite(c->livery[scheme].in_use ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, 2, y);
 
				}
 

	
 
				DrawString(15, y, STR_LIVERY_DEFAULT + scheme, sel ? TC_WHITE : TC_BLACK);
 

	
 
				DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOR(c->livery[scheme].colour1), 152, y);
 
				DrawString(165, y, STR_00D1_DARK_BLUE + c->livery[scheme].colour1, sel ? TC_WHITE : TC_GOLD);
 

	
 
				if (!this->IsWidgetHidden(SCLW_WIDGET_SEC_COL_DROPDOWN)) {
 
					DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOR(c->livery[scheme].colour2), 277, y);
 
					DrawString(290, y, STR_00D1_DARK_BLUE + c->livery[scheme].colour2, sel ? TC_WHITE : TC_GOLD);
 
				}
 

	
 
				y += 14;
 
			}
 
		}
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		/* Number of liveries in each class, used to determine the height of the livery window */
 
		static const byte livery_height[] = {
 
			1,
 
			13,
 
			4,
 
			2,
 
			3,
 
		};
 

	
 
		switch (widget) {
 
			/* Livery Class buttons */
 
			case SCLW_WIDGET_CLASS_GENERAL:
 
			case SCLW_WIDGET_CLASS_RAIL:
 
			case SCLW_WIDGET_CLASS_ROAD:
 
			case SCLW_WIDGET_CLASS_SHIP:
 
			case SCLW_WIDGET_CLASS_AIRCRAFT: {
 
				LiveryScheme scheme;
 

	
 
				this->RaiseWidget(this->livery_class + SCLW_WIDGET_CLASS_GENERAL);
 
				this->livery_class = (LiveryClass)(widget - SCLW_WIDGET_CLASS_GENERAL);
 
				this->sel = 0;
 
				this->LowerWidget(this->livery_class + SCLW_WIDGET_CLASS_GENERAL);
 

	
 
				/* Select the first item in the list */
 
				for (scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
 
					if (_livery_class[scheme] == this->livery_class) {
 
						this->sel = 1 << scheme;
 
						break;
 
					}
 
				}
 
				this->height = 49 + livery_height[this->livery_class] * 14;
 
				this->widget[SCLW_WIDGET_MATRIX].bottom = this->height - 1;
 
				this->widget[SCLW_WIDGET_MATRIX].data = livery_height[this->livery_class] << 8 | 1;
 
				MarkWholeScreenDirty();
 
				break;
 
			}
 

	
 
			case SCLW_WIDGET_PRI_COL_DROPDOWN: /* First colour dropdown */
 
				ShowColourDropDownMenu(SCLW_WIDGET_PRI_COL_DROPDOWN);
 
				break;
 

	
 
			case SCLW_WIDGET_SEC_COL_DROPDOWN: /* Second colour dropdown */
 
				ShowColourDropDownMenu(SCLW_WIDGET_SEC_COL_DROPDOWN);
 
				break;
 

	
 
			case SCLW_WIDGET_MATRIX: {
 
				LiveryScheme scheme;
 
				LiveryScheme j = (LiveryScheme)((pt.y - 48) / 14);
 

	
 
				for (scheme = LS_BEGIN; scheme <= j; scheme++) {
 
					if (_livery_class[scheme] != this->livery_class) j++;
 
					if (scheme >= LS_END) return;
 
				}
 
				if (j >= LS_END) return;
 

	
 
				/* If clicking on the left edge, toggle using the livery */
 
				if (pt.x < 10) {
 
					DoCommandP(0, j | (2 << 8), !GetCompany((CompanyID)this->window_number)->livery[j].in_use, NULL, CMD_SET_COMPANY_COLOR);
 
				}
 

	
 
				if (_ctrl_pressed) {
 
					ToggleBit(this->sel, j);
 
				} else {
 
					this->sel = 1 << j;
 
				}
 
				this->SetDirty();
 
				break;
 
			}
 
		}
 
	}
 

	
 
	virtual void OnDropdownSelect(int widget, int index)
 
	{
 
		for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
 
			if (HasBit(this->sel, scheme)) {
 
				DoCommandP(0, scheme | (widget == SCLW_WIDGET_PRI_COL_DROPDOWN ? 0 : 256), index, NULL, CMD_SET_COMPANY_COLOR);
 
			}
 
		}
 
	}
 

	
 
	virtual void OnInvalidateData(int data = 0)
 
	{
 
		static bool has2cc = true;
 

	
 
		if (has2cc == !!data) return;
 

	
 
		has2cc = !!data;
 

	
 
		int r = this->widget[has2cc ? SCLW_WIDGET_SEC_COL_DROPDOWN : SCLW_WIDGET_PRI_COL_DROPDOWN].right;
 
		this->SetWidgetHiddenState(SCLW_WIDGET_SEC_COL_DROPDOWN, !has2cc);
 
		this->widget[SCLW_WIDGET_CAPTION].right = r;
 
		this->widget[SCLW_WIDGET_SPACER_CLASS].right = r;
 
		this->widget[SCLW_WIDGET_MATRIX].right = r;
 
		this->width = r + 1;
 
	}
 
};
 

	
 
static const Widget _select_company_livery_widgets[] = {
 
{ WWT_CLOSEBOX, RESIZE_NONE,  COLOUR_GREY,   0,  10,   0,  13, STR_00C5,                  STR_018B_CLOSE_WINDOW },
 
{  WWT_CAPTION, RESIZE_NONE,  COLOUR_GREY,  11, 399,   0,  13, STR_7007_NEW_COLOR_SCHEME, STR_018C_WINDOW_TITLE_DRAG_THIS },
 
{   WWT_IMGBTN, RESIZE_NONE,  COLOUR_GREY,   0,  21,  14,  35, SPR_IMG_COMPANY_GENERAL,   STR_LIVERY_GENERAL_TIP },
 
{   WWT_IMGBTN, RESIZE_NONE,  COLOUR_GREY,  22,  43,  14,  35, SPR_IMG_TRAINLIST,         STR_LIVERY_TRAIN_TIP },
 
{   WWT_IMGBTN, RESIZE_NONE,  COLOUR_GREY,  44,  65,  14,  35, SPR_IMG_TRUCKLIST,         STR_LIVERY_ROADVEH_TIP },
 
{   WWT_IMGBTN, RESIZE_NONE,  COLOUR_GREY,  66,  87,  14,  35, SPR_IMG_SHIPLIST,          STR_LIVERY_SHIP_TIP },
 
{   WWT_IMGBTN, RESIZE_NONE,  COLOUR_GREY,  88, 109,  14,  35, SPR_IMG_AIRPLANESLIST,     STR_LIVERY_AIRCRAFT_TIP },
 
{    WWT_PANEL, RESIZE_NONE,  COLOUR_GREY, 110, 399,  14,  35, 0x0,                       STR_NULL },
 
{    WWT_PANEL, RESIZE_NONE,  COLOUR_GREY,   0, 149,  36,  47, 0x0,                       STR_NULL },
 
{ WWT_DROPDOWN, RESIZE_NONE,  COLOUR_GREY, 150, 274,  36,  47, STR_02BD,                  STR_LIVERY_PRIMARY_TIP },
 
{ WWT_DROPDOWN, RESIZE_NONE,  COLOUR_GREY, 275, 399,  36,  47, STR_02E1,                  STR_LIVERY_SECONDARY_TIP },
 
{   WWT_MATRIX, RESIZE_NONE,  COLOUR_GREY,   0, 399,  48,  48 + 1 * 14, (1 << 8) | 1,     STR_LIVERY_PANEL_TIP },
 
{ WIDGETS_END },
 
};
 

	
 
static const WindowDesc _select_company_livery_desc = {
 
	WDP_AUTO, WDP_AUTO, 400, 49 + 1 * 14, 400, 49 + 1 * 14,
 
	WC_COMPANY_COLOR, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 
	_select_company_livery_widgets,
 
};
 

	
 
/**
 
 * Draws the face of a company manager's face.
 
 * @param cmf   the company manager's face
 
 * @param color the (background) color of the gradient
 
 * @param x     x-position to draw the face
 
 * @param y     y-position to draw the face
 
 */
 
void DrawCompanyManagerFace(CompanyManagerFace cmf, int color, int x, int y)
 
{
 
	GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
 

	
 
	bool has_moustache   = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE,   ge) != 0;
 
	bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
 
	bool has_glasses     = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
 
	SpriteID pal;
 

	
 
	/* Modify eye colour palette only if 2 or more valid values exist */
 
	if (_cmf_info[CMFV_EYE_COLOUR].valid_values[ge] < 2) {
 
		pal = PAL_NONE;
 
	} else {
 
		switch (GetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge)) {
 
			default: NOT_REACHED();
 
			case 0: pal = PALETTE_TO_BROWN; break;
 
			case 1: pal = PALETTE_TO_BLUE;  break;
 
			case 2: pal = PALETTE_TO_GREEN; break;
 
		}
 
	}
 

	
 
	/* Draw the gradient (background) */
 
	DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOR(color), x, y);
 

	
 
	for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
 
		switch (cmfv) {
 
			case CMFV_MOUSTACHE:   if (!has_moustache)   continue; break;
 
			case CMFV_LIPS:        /* FALL THROUGH */
 
			case CMFV_NOSE:        if (has_moustache)    continue; break;
 
			case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
 
			case CMFV_GLASSES:     if (!has_glasses)     continue; break;
 
			default: break;
 
		}
 
		DrawSprite(GetCompanyManagerFaceSprite(cmf, cmfv, ge), (cmfv == CMFV_EYEBROWS) ? pal : PAL_NONE, x, y);
 
	}
 
}
 

	
 
/** Widget description for the normal/simple company manager face selection dialog */
 
static const Widget _select_company_manager_face_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                STR_018B_CLOSE_WINDOW},              // SCMFW_WIDGET_CLOSEBOX
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   174,     0,    13, STR_7043_FACE_SELECTION, STR_018C_WINDOW_TITLE_DRAG_THIS},    // SCMFW_WIDGET_CAPTION
 
{     WWT_IMGBTN,   RESIZE_NONE,  COLOUR_GREY,   175,   189,     0,    13, SPR_LARGE_SMALL_WINDOW,  STR_FACE_ADVANCED_TIP},              // SCMFW_WIDGET_TOGGLE_LARGE_SMALL
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   189,    14,   150, 0x0,                     STR_NULL},                           // SCMFW_WIDGET_SELECT_FACE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    94,   151,   162, STR_012E_CANCEL,         STR_7047_CANCEL_NEW_FACE_SELECTION}, // SCMFW_WIDGET_CANCEL
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   189,   151,   162, STR_012F_OK,             STR_7048_ACCEPT_NEW_FACE_SELECTION}, // SCMFW_WIDGET_ACCEPT
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   187,    75,    86, STR_7044_MALE,           STR_7049_SELECT_MALE_FACES},         // SCMFW_WIDGET_MALE
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   187,    87,    98, STR_7045_FEMALE,         STR_704A_SELECT_FEMALE_FACES},       // SCMFW_WIDGET_FEMALE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     2,    93,   137,   148, STR_7046_NEW_FACE,       STR_704B_GENERATE_RANDOM_NEW_FACE},  // SCMFW_WIDGET_RANDOM_NEW_FACE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   187,    16,    27, STR_FACE_ADVANCED,       STR_FACE_ADVANCED_TIP},              // SCMFW_WIDGET_TOGGLE_LARGE_SMALL_BUTTON
 
{   WIDGETS_END},
 
};
 

	
 
/** Widget description for the advanced company manager face selection dialog */
 
static const Widget _select_company_manager_face_adv_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                STR_018B_CLOSE_WINDOW},              // SCMFW_WIDGET_CLOSEBOX
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   204,     0,    13, STR_7043_FACE_SELECTION, STR_018C_WINDOW_TITLE_DRAG_THIS},    // SCMFW_WIDGET_CAPTION
 
{     WWT_IMGBTN,   RESIZE_NONE,  COLOUR_GREY,   205,   219,     0,    13, SPR_LARGE_SMALL_WINDOW,  STR_FACE_SIMPLE_TIP},                // SCMFW_WIDGET_TOGGLE_LARGE_SMALL
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   219,    14,   207, 0x0,                     STR_NULL},                           // SCMFW_WIDGET_SELECT_FACE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    94,   208,   219, STR_012E_CANCEL,         STR_7047_CANCEL_NEW_FACE_SELECTION}, // SCMFW_WIDGET_CANCEL
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   219,   208,   219, STR_012F_OK,             STR_7048_ACCEPT_NEW_FACE_SELECTION}, // SCMFW_WIDGET_ACCEPT
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,    96,   156,    32,    43, STR_7044_MALE,           STR_7049_SELECT_MALE_FACES},         // SCMFW_WIDGET_MALE
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   157,   217,    32,    43, STR_7045_FEMALE,         STR_704A_SELECT_FEMALE_FACES},       // SCMFW_WIDGET_FEMALE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     2,    93,   137,   148, STR_RANDOM,              STR_704B_GENERATE_RANDOM_NEW_FACE},  // SCMFW_WIDGET_RANDOM_NEW_FACE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    95,   217,    16,    27, STR_FACE_SIMPLE,         STR_FACE_SIMPLE_TIP},                // SCMFW_WIDGET_TOGGLE_LARGE_SMALL_BUTTON
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     2,    93,   158,   169, STR_FACE_LOAD,           STR_FACE_LOAD_TIP},                  // SCMFW_WIDGET_LOAD
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     2,    93,   170,   181, STR_FACE_FACECODE,       STR_FACE_FACECODE_TIP},              // SCMFW_WIDGET_FACECODE
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     2,    93,   182,   193, STR_FACE_SAVE,           STR_FACE_SAVE_TIP},                  // SCMFW_WIDGET_SAVE
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,    96,   156,    46,    57, STR_FACE_EUROPEAN,       STR_FACE_SELECT_EUROPEAN},           // SCMFW_WIDGET_ETHNICITY_EUR
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   157,   217,    46,    57, STR_FACE_AFRICAN,        STR_FACE_SELECT_AFRICAN},            // SCMFW_WIDGET_ETHNICITY_AFR
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   175,   217,    60,    71, STR_EMPTY,               STR_FACE_MOUSTACHE_EARRING_TIP},     // SCMFW_WIDGET_HAS_MOUSTACHE_EARRING
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   175,   217,    72,    83, STR_EMPTY,               STR_FACE_GLASSES_TIP},               // SCMFW_WIDGET_HAS_GLASSES
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   110,   121, SPR_ARROW_LEFT,          STR_FACE_EYECOLOUR_TIP},             // SCMFW_WIDGET_EYECOLOUR_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   110,   121, STR_EMPTY,               STR_FACE_EYECOLOUR_TIP},             // SCMFW_WIDGET_EYECOLOUR
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   110,   121, SPR_ARROW_RIGHT,         STR_FACE_EYECOLOUR_TIP},             // SCMFW_WIDGET_EYECOLOUR_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   158,   169, SPR_ARROW_LEFT,          STR_FACE_CHIN_TIP},                  // SCMFW_WIDGET_CHIN_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   158,   169, STR_EMPTY,               STR_FACE_CHIN_TIP},                  // SCMFW_WIDGET_CHIN
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   158,   169, SPR_ARROW_RIGHT,         STR_FACE_CHIN_TIP},                  // SCMFW_WIDGET_CHIN_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,    98,   109, SPR_ARROW_LEFT,          STR_FACE_EYEBROWS_TIP},              // SCMFW_WIDGET_EYEBROWS_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,    98,   109, STR_EMPTY,               STR_FACE_EYEBROWS_TIP},              // SCMFW_WIDGET_EYEBROWS
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,    98,   109, SPR_ARROW_RIGHT,         STR_FACE_EYEBROWS_TIP},              // SCMFW_WIDGET_EYEBROWS_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   146,   157, SPR_ARROW_LEFT,          STR_FACE_LIPS_MOUSTACHE_TIP},        // SCMFW_WIDGET_LIPS_MOUSTACHE_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   146,   157, STR_EMPTY,               STR_FACE_LIPS_MOUSTACHE_TIP},        // SCMFW_WIDGET_LIPS_MOUSTACHE
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   146,   157, SPR_ARROW_RIGHT,         STR_FACE_LIPS_MOUSTACHE_TIP},        // SCMFW_WIDGET_LIPS_MOUSTACHE_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   134,   145, SPR_ARROW_LEFT,          STR_FACE_NOSE_TIP},                  // SCMFW_WIDGET_NOSE_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   134,   145, STR_EMPTY,               STR_FACE_NOSE_TIP},                  // SCMFW_WIDGET_NOSE
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   134,   145, SPR_ARROW_RIGHT,         STR_FACE_NOSE_TIP},                  // SCMFW_WIDGET_NOSE_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,    86,    97, SPR_ARROW_LEFT,          STR_FACE_HAIR_TIP},                  // SCMFW_WIDGET_HAIR_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,    86,    97, STR_EMPTY,               STR_FACE_HAIR_TIP},                  // SCMFW_WIDGET_HAIR
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,    86,    97, SPR_ARROW_RIGHT,         STR_FACE_HAIR_TIP},                  // SCMFW_WIDGET_HAIR_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   170,   181, SPR_ARROW_LEFT,          STR_FACE_JACKET_TIP},                // SCMFW_WIDGET_JACKET_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   170,   181, STR_EMPTY,               STR_FACE_JACKET_TIP},                // SCMFW_WIDGET_JACKET
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   170,   181, SPR_ARROW_RIGHT,         STR_FACE_JACKET_TIP},                // SCMFW_WIDGET_JACKET_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   182,   193, SPR_ARROW_LEFT,          STR_FACE_COLLAR_TIP},                // SCMFW_WIDGET_COLLAR_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   182,   193, STR_EMPTY,               STR_FACE_COLLAR_TIP},                // SCMFW_WIDGET_COLLAR
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   182,   193, SPR_ARROW_RIGHT,         STR_FACE_COLLAR_TIP},                // SCMFW_WIDGET_COLLAR_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   194,   205, SPR_ARROW_LEFT,          STR_FACE_TIE_EARRING_TIP},           // SCMFW_WIDGET_TIE_EARRING_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   194,   205, STR_EMPTY,               STR_FACE_TIE_EARRING_TIP},           // SCMFW_WIDGET_TIE_EARRING
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   194,   205, SPR_ARROW_RIGHT,         STR_FACE_TIE_EARRING_TIP},           // SCMFW_WIDGET_TIE_EARRING_R
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    175,  183,   122,   133, SPR_ARROW_LEFT,          STR_FACE_GLASSES_TIP_2},             // SCMFW_WIDGET_GLASSES_L
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    184,  208,   122,   133, STR_EMPTY,               STR_FACE_GLASSES_TIP_2},             // SCMFW_WIDGET_GLASSES
 
{ WWT_PUSHIMGBTN,   RESIZE_NONE,  COLOUR_GREY,    209,  217,   122,   133, SPR_ARROW_RIGHT,         STR_FACE_GLASSES_TIP_2},             // SCMFW_WIDGET_GLASSES_R
 
{   WIDGETS_END},
 
};
 

	
 
class SelectCompanyManagerFaceWindow : public Window
 
{
 
	CompanyManagerFace face; ///< company manager face bits
 
	bool advanced; ///< advanced company manager face selection window
 

	
 
	GenderEthnicity ge;
 
	bool is_female;
 
	bool is_moust_male;
 

	
 
	/**
 
	 * Names of the widgets. Keep them in the same order as in the widget array.
 
	 * Do not change the order of the widgets from SCMFW_WIDGET_HAS_MOUSTACHE_EARRING to SCMFW_WIDGET_GLASSES_R,
 
	 * this order is needed for the WE_CLICK event of DrawFaceStringLabel().
 
	 */
 
	enum SelectCompanyManagerFaceWidgets {
 
		SCMFW_WIDGET_CLOSEBOX = 0,
 
		SCMFW_WIDGET_CAPTION,
 
		SCMFW_WIDGET_TOGGLE_LARGE_SMALL,
 
		SCMFW_WIDGET_SELECT_FACE,
 
		SCMFW_WIDGET_CANCEL,
 
		SCMFW_WIDGET_ACCEPT,
 
		SCMFW_WIDGET_MALE,
 
		SCMFW_WIDGET_FEMALE,
 
		SCMFW_WIDGET_RANDOM_NEW_FACE,
 
		SCMFW_WIDGET_TOGGLE_LARGE_SMALL_BUTTON,
 
		/* from here is the advanced company manager face selection window */
 
		SCMFW_WIDGET_LOAD,
 
		SCMFW_WIDGET_FACECODE,
 
		SCMFW_WIDGET_SAVE,
 
		SCMFW_WIDGET_ETHNICITY_EUR,
 
		SCMFW_WIDGET_ETHNICITY_AFR,
 
		SCMFW_WIDGET_HAS_MOUSTACHE_EARRING,
 
		SCMFW_WIDGET_HAS_GLASSES,
 
		SCMFW_WIDGET_EYECOLOUR_L,
 
		SCMFW_WIDGET_EYECOLOUR,
 
		SCMFW_WIDGET_EYECOLOUR_R,
 
		SCMFW_WIDGET_CHIN_L,
 
		SCMFW_WIDGET_CHIN,
 
		SCMFW_WIDGET_CHIN_R,
 
		SCMFW_WIDGET_EYEBROWS_L,
 
		SCMFW_WIDGET_EYEBROWS,
 
		SCMFW_WIDGET_EYEBROWS_R,
 
		SCMFW_WIDGET_LIPS_MOUSTACHE_L,
 
		SCMFW_WIDGET_LIPS_MOUSTACHE,
 
		SCMFW_WIDGET_LIPS_MOUSTACHE_R,
 
		SCMFW_WIDGET_NOSE_L,
 
		SCMFW_WIDGET_NOSE,
 
		SCMFW_WIDGET_NOSE_R,
 
		SCMFW_WIDGET_HAIR_L,
 
		SCMFW_WIDGET_HAIR,
 
		SCMFW_WIDGET_HAIR_R,
 
		SCMFW_WIDGET_JACKET_L,
 
		SCMFW_WIDGET_JACKET,
 
		SCMFW_WIDGET_JACKET_R,
 
		SCMFW_WIDGET_COLLAR_L,
 
		SCMFW_WIDGET_COLLAR,
 
		SCMFW_WIDGET_COLLAR_R,
 
		SCMFW_WIDGET_TIE_EARRING_L,
 
		SCMFW_WIDGET_TIE_EARRING,
 
		SCMFW_WIDGET_TIE_EARRING_R,
 
		SCMFW_WIDGET_GLASSES_L,
 
		SCMFW_WIDGET_GLASSES,
 
		SCMFW_WIDGET_GLASSES_R,
 
	};
 
	/**
 
	 * Draw dynamic a label to the left of the button and a value in the button
 
	 *
 
	 * @param widget_index   index of this widget in the window
 
	 * @param str            the label which will be draw
 
	 * @param val            the value which will be draw
 
	 * @param is_bool_widget is it a bool button
 
	 */
 
	void DrawFaceStringLabel(byte widget_index, StringID str, uint8 val, bool is_bool_widget)
 
	{
 
		/* Write the label in gold (0x2) to the left of the button. */
 
		DrawStringRightAligned(this->widget[widget_index].left - (is_bool_widget ? 5 : 14), this->widget[widget_index].top + 1, str, TC_GOLD);
 

	
 
		if (!this->IsWidgetDisabled(widget_index)) {
 
			if (is_bool_widget) {
 
				/* if it a bool button write yes or no */
 
				str = (val != 0) ? STR_FACE_YES : STR_FACE_NO;
 
			} else {
 
				/* else write the value + 1 */
 
				SetDParam(0, val + 1);
 
				str = STR_JUST_INT;
 
			}
 

	
 
			/* Draw the value/bool in white (0xC). If the button clicked adds 1px to x and y text coordinates (IsWindowWidgetLowered()). */
 
			DrawStringCentered(this->widget[widget_index].left + (this->widget[widget_index].right - this->widget[widget_index].left) / 2 +
 
				this->IsWidgetLowered(widget_index), this->widget[widget_index].top + 1 + this->IsWidgetLowered(widget_index), str, TC_WHITE);
 
		}
 
	}
 

	
 
	void UpdateData()
 
	{
 
		this->ge = (GenderEthnicity)GB(this->face, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // get the gender and ethnicity
 
		this->is_female = HasBit(this->ge, GENDER_FEMALE); // get the gender: 0 == male and 1 == female
 
		this->is_moust_male = !is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge) != 0; // is a male face with moustache
 
	}
 

	
 
public:
 
	SelectCompanyManagerFaceWindow(const WindowDesc *desc, Window *parent, bool advanced, int top, int left) : Window(desc, parent->window_number)
 
	{
 
		this->parent = parent;
 
		this->caption_color = this->window_number;
 
		this->face = GetCompany((CompanyID)this->window_number)->face;
 
		this->advanced = advanced;
 

	
 
		this->UpdateData();
 

	
 
		/* Check if repositioning from default is required */
 
		if (top != FIRST_GUI_CALL && left != FIRST_GUI_CALL) {
 
			this->top = top;
 
			this->left = left;
 
		}
 

	
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		/* lower the non-selected gender button */
 
		this->SetWidgetLoweredState(SCMFW_WIDGET_MALE,  !this->is_female);
 
		this->SetWidgetLoweredState(SCMFW_WIDGET_FEMALE, this->is_female);
 

	
 
		/* advanced company manager face selection window */
 
		if (this->advanced) {
 
			/* lower the non-selected ethnicity button */
 
			this->SetWidgetLoweredState(SCMFW_WIDGET_ETHNICITY_EUR, !HasBit(this->ge, ETHNICITY_BLACK));
 
			this->SetWidgetLoweredState(SCMFW_WIDGET_ETHNICITY_AFR,  HasBit(this->ge, ETHNICITY_BLACK));
 

	
 

	
 
			/* Disable dynamically the widgets which CompanyManagerFaceVariable has less than 2 options
 
			* (or in other words you haven't any choice).
 
			* If the widgets depend on a HAS-variable and this is false the widgets will be disabled, too. */
 

	
 
			/* Eye colour buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_EYE_COLOUR].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_EYECOLOUR, SCMFW_WIDGET_EYECOLOUR_L, SCMFW_WIDGET_EYECOLOUR_R, WIDGET_LIST_END);
 

	
 
			/* Chin buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_CHIN].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_CHIN, SCMFW_WIDGET_CHIN_L, SCMFW_WIDGET_CHIN_R, WIDGET_LIST_END);
 

	
 
			/* Eyebrows buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_EYEBROWS].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_EYEBROWS, SCMFW_WIDGET_EYEBROWS_L, SCMFW_WIDGET_EYEBROWS_R, WIDGET_LIST_END);
 

	
 
			/* Lips or (if it a male face with a moustache) moustache buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_LIPS_MOUSTACHE, SCMFW_WIDGET_LIPS_MOUSTACHE_L, SCMFW_WIDGET_LIPS_MOUSTACHE_R, WIDGET_LIST_END);
 

	
 
			/* Nose buttons | male faces with moustache haven't any nose options */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_NOSE].valid_values[this->ge] < 2 || this->is_moust_male,
 
				SCMFW_WIDGET_NOSE, SCMFW_WIDGET_NOSE_L, SCMFW_WIDGET_NOSE_R, WIDGET_LIST_END);
 

	
 
			/* Hair buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_HAIR].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_HAIR, SCMFW_WIDGET_HAIR_L, SCMFW_WIDGET_HAIR_R, WIDGET_LIST_END);
 

	
 
			/* Jacket buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_JACKET].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_JACKET, SCMFW_WIDGET_JACKET_L, SCMFW_WIDGET_JACKET_R, WIDGET_LIST_END);
 

	
 
			/* Collar buttons */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_COLLAR].valid_values[this->ge] < 2,
 
				SCMFW_WIDGET_COLLAR, SCMFW_WIDGET_COLLAR_L, SCMFW_WIDGET_COLLAR_R, WIDGET_LIST_END);
 

	
 
			/* Tie/earring buttons | female faces without earring haven't any earring options */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_TIE_EARRING].valid_values[this->ge] < 2 ||
 
					(this->is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge) == 0),
 
				SCMFW_WIDGET_TIE_EARRING, SCMFW_WIDGET_TIE_EARRING_L, SCMFW_WIDGET_TIE_EARRING_R, WIDGET_LIST_END);
 

	
 
			/* Glasses buttons | faces without glasses haven't any glasses options */
 
			this->SetWidgetsDisabledState(_cmf_info[CMFV_GLASSES].valid_values[this->ge] < 2 || GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge) == 0,
 
				SCMFW_WIDGET_GLASSES, SCMFW_WIDGET_GLASSES_L, SCMFW_WIDGET_GLASSES_R, WIDGET_LIST_END);
 
		}
 

	
 
		this->DrawWidgets();
 

	
 
		/* Draw dynamic button value and labels for the advanced company manager face selection window */
 
		if (this->advanced) {
 
			if (this->is_female) {
 
				/* Only for female faces */
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_HAS_MOUSTACHE_EARRING, STR_FACE_EARRING,   GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge), true );
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_TIE_EARRING,           STR_FACE_EARRING,   GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING,     this->ge), false);
 
			} else {
 
				/* Only for male faces */
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_HAS_MOUSTACHE_EARRING, STR_FACE_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE,   this->ge), true );
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_TIE_EARRING,           STR_FACE_TIE,       GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING,     this->ge), false);
 
			}
 
			if (this->is_moust_male) {
 
				/* Only for male faces with moustache */
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_LIPS_MOUSTACHE,        STR_FACE_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_MOUSTACHE,       this->ge), false);
 
			} else {
 
				/* Only for female faces or male faces without moustache */
 
				this->DrawFaceStringLabel(SCMFW_WIDGET_LIPS_MOUSTACHE,        STR_FACE_LIPS,      GetCompanyManagerFaceBits(this->face, CMFV_LIPS,            this->ge), false);
 
			}
 
			/* For all faces */
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_HAS_GLASSES,           STR_FACE_GLASSES,     GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES,     this->ge), true );
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_HAIR,                  STR_FACE_HAIR,        GetCompanyManagerFaceBits(this->face, CMFV_HAIR,            this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_EYEBROWS,              STR_FACE_EYEBROWS,    GetCompanyManagerFaceBits(this->face, CMFV_EYEBROWS,        this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_EYECOLOUR,             STR_FACE_EYECOLOUR,   GetCompanyManagerFaceBits(this->face, CMFV_EYE_COLOUR,      this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_GLASSES,               STR_FACE_GLASSES,     GetCompanyManagerFaceBits(this->face, CMFV_GLASSES,         this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_NOSE,                  STR_FACE_NOSE,        GetCompanyManagerFaceBits(this->face, CMFV_NOSE,            this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_CHIN,                  STR_FACE_CHIN,        GetCompanyManagerFaceBits(this->face, CMFV_CHIN,            this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_JACKET,                STR_FACE_JACKET,      GetCompanyManagerFaceBits(this->face, CMFV_JACKET,          this->ge), false);
 
			this->DrawFaceStringLabel(SCMFW_WIDGET_COLLAR,                STR_FACE_COLLAR,      GetCompanyManagerFaceBits(this->face, CMFV_COLLAR,          this->ge), false);
 
		}
 

	
 
		/* Draw the company manager face picture */
 
		DrawCompanyManagerFace(this->face, GetCompany((CompanyID)this->window_number)->colour, 2, 16);
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			/* Toggle size, advanced/simple face selection */
 
			case SCMFW_WIDGET_TOGGLE_LARGE_SMALL:
 
			case SCMFW_WIDGET_TOGGLE_LARGE_SMALL_BUTTON: {
 
				DoCommandP(0, 0, this->face, NULL, CMD_SET_COMPANY_MANAGER_FACE);
 

	
 
				/* Backup some data before deletion */
 
				int oldtop = this->top;     ///< current top position of the window before closing it
 
				int oldleft = this->left;   ///< current top position of the window before closing it
 
				bool adv = !this->advanced;
 
				Window *parent = this->parent;
 

	
 
				delete this;
 

	
 
				/* Open up the (toggled size) Face selection window at the same position as the previous */
 
				DoSelectCompanyManagerFace(parent, adv, oldtop, oldleft);
 
			} break;
 

	
 

	
 
			/* OK button */
 
			case SCMFW_WIDGET_ACCEPT:
 
				DoCommandP(0, 0, this->face, NULL, CMD_SET_COMPANY_MANAGER_FACE);
 
				/* Fall-Through */
 

	
 
			/* Cancel button */
 
			case SCMFW_WIDGET_CANCEL:
 
				delete this;
 
				break;
 

	
 
			/* Load button */
 
			case SCMFW_WIDGET_LOAD:
 
				this->face = _company_manager_face;
 
				ScaleAllCompanyManagerFaceBits(this->face);
 
				ShowErrorMessage(INVALID_STRING_ID, STR_FACE_LOAD_DONE, 0, 0);
 
				this->UpdateData();
 
				this->SetDirty();
 
				break;
 

	
 
			/* 'Company manager face number' button, view and/or set company manager face number */
 
			case SCMFW_WIDGET_FACECODE:
 
				SetDParam(0, this->face);
 
				ShowQueryString(STR_JUST_INT, STR_FACE_FACECODE_CAPTION, 10 + 1, 0, this, CS_NUMERAL, QSF_NONE);
 
				break;
 

	
 
			/* Save button */
 
			case SCMFW_WIDGET_SAVE:
 
				_company_manager_face = this->face;
 
				ShowErrorMessage(INVALID_STRING_ID, STR_FACE_SAVE_DONE, 0, 0);
 
				break;
 

	
 
			/* Toggle gender (male/female) button */
 
			case SCMFW_WIDGET_MALE:
 
			case SCMFW_WIDGET_FEMALE:
 
				SetCompanyManagerFaceBits(this->face, CMFV_GENDER, this->ge, widget - SCMFW_WIDGET_MALE);
 
				ScaleAllCompanyManagerFaceBits(this->face);
 
				this->UpdateData();
 
				this->SetDirty();
 
				break;
 

	
 
			/* Randomize face button */
 
			case SCMFW_WIDGET_RANDOM_NEW_FACE:
 
				RandomCompanyManagerFaceBits(this->face, this->ge, this->advanced);
 
				this->UpdateData();
 
				this->SetDirty();
 
				break;
 

	
 
			/* Toggle ethnicity (european/african) button */
 
			case SCMFW_WIDGET_ETHNICITY_EUR:
 
			case SCMFW_WIDGET_ETHNICITY_AFR:
 
				SetCompanyManagerFaceBits(this->face, CMFV_ETHNICITY, this->ge, widget - SCMFW_WIDGET_ETHNICITY_EUR);
 
				ScaleAllCompanyManagerFaceBits(this->face);
 
				this->UpdateData();
 
				this->SetDirty();
 
				break;
 

	
 
			default:
 
				/* For all buttons from SCMFW_WIDGET_HAS_MOUSTACHE_EARRING to SCMFW_WIDGET_GLASSES_R is the same function.
 
				* Therefor is this combined function.
 
				* First it checks which CompanyManagerFaceVariable will be change and then
 
				* a: invert the value for boolean variables
 
				* or b: it checks inside of IncreaseCompanyManagerFaceBits() if a left (_L) butten is pressed and then decrease else increase the variable */
 
				if (this->advanced && widget >= SCMFW_WIDGET_HAS_MOUSTACHE_EARRING && widget <= SCMFW_WIDGET_GLASSES_R) {
 
					CompanyManagerFaceVariable cmfv; // which CompanyManagerFaceVariable shall be edited
 

	
 
					if (widget < SCMFW_WIDGET_EYECOLOUR_L) { // Bool buttons
 
						switch (widget - SCMFW_WIDGET_HAS_MOUSTACHE_EARRING) {
 
							default: NOT_REACHED();
 
							case 0: cmfv = this->is_female ? CMFV_HAS_TIE_EARRING : CMFV_HAS_MOUSTACHE; break; // Has earring/moustache button
 
							case 1: cmfv = CMFV_HAS_GLASSES; break; // Has glasses button
 
						}
 
						SetCompanyManagerFaceBits(this->face, cmfv, this->ge, !GetCompanyManagerFaceBits(this->face, cmfv, this->ge));
 
						ScaleAllCompanyManagerFaceBits(this->face);
 
					} else { // Value buttons
 
						switch ((widget - SCMFW_WIDGET_EYECOLOUR_L) / 3) {
 
							default: NOT_REACHED();
 
							case 0: cmfv = CMFV_EYE_COLOUR; break;  // Eye colour buttons
 
							case 1: cmfv = CMFV_CHIN; break;        // Chin buttons
 
							case 2: cmfv = CMFV_EYEBROWS; break;    // Eyebrows buttons
 
							case 3: cmfv = this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS; break; // Moustache or lips buttons
 
							case 4: cmfv = CMFV_NOSE; break;        // Nose buttons
 
							case 5: cmfv = CMFV_HAIR; break;        // Hair buttons
 
							case 6: cmfv = CMFV_JACKET; break;      // Jacket buttons
 
							case 7: cmfv = CMFV_COLLAR; break;      // Collar buttons
 
							case 8: cmfv = CMFV_TIE_EARRING; break; // Tie/earring buttons
 
							case 9: cmfv = CMFV_GLASSES; break;     // Glasses buttons
 
						}
 
						/* 0 == left (_L), 1 == middle or 2 == right (_R) - button click */
 
						IncreaseCompanyManagerFaceBits(this->face, cmfv, this->ge, (((widget - SCMFW_WIDGET_EYECOLOUR_L) % 3) != 0) ? 1 : -1);
 
					}
 
					this->UpdateData();
 
					this->SetDirty();
 
				}
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (str == NULL) return;
 
		/* Set a new company manager face number */
 
		if (!StrEmpty(str)) {
 
			this->face = strtoul(str, NULL, 10);
 
			ScaleAllCompanyManagerFaceBits(this->face);
 
			ShowErrorMessage(INVALID_STRING_ID, STR_FACE_FACECODE_SET, 0, 0);
 
			this->UpdateData();
 
			this->SetDirty();
 
		} else {
 
			ShowErrorMessage(INVALID_STRING_ID, STR_FACE_FACECODE_ERR, 0, 0);
 
		}
 
	}
 
};
 

	
 
/** normal/simple company manager face selection window description */
 
static const WindowDesc _select_company_manager_face_desc = {
 
	WDP_AUTO, WDP_AUTO, 190, 163, 190, 163,
 
	WC_COMPANY_MANAGER_FACE, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
 
	_select_company_manager_face_widgets,
 
};
 

	
 
/** advanced company manager face selection window description */
 
static const WindowDesc _select_company_manager_face_adv_desc = {
 
	WDP_AUTO, WDP_AUTO, 220, 220, 220, 220,
 
	WC_COMPANY_MANAGER_FACE, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
 
	_select_company_manager_face_adv_widgets,
 
};
 

	
 
/**
 
 * Open the simple/advanced company manager face selection window
 
 *
 
 * @param parent the parent company window
 
 * @param adv    simple or advanced face selection window
 
 * @param top    previous top position of the window
 
 * @param left   previous left position of the window
 
 */
 
static void DoSelectCompanyManagerFace(Window *parent, bool adv, int top, int left)
 
{
 
	if (!IsValidCompanyID((CompanyID)parent->window_number)) return;
 

	
 
	if (BringWindowToFrontById(WC_COMPANY_MANAGER_FACE, parent->window_number)) return;
 
	new SelectCompanyManagerFaceWindow(adv ? &_select_company_manager_face_adv_desc : &_select_company_manager_face_desc, parent, adv, top, left); // simple or advanced window
 
}
 

	
 

	
 
/* Names of the widgets. Keep them in the same order as in the widget array */
 
enum CompanyWindowWidgets {
 
	CW_WIDGET_CLOSEBOX = 0,
 
	CW_WIDGET_CAPTION,
 
	CW_WIDGET_FACE,
 
	CW_WIDGET_NEW_FACE,
 
	CW_WIDGET_COLOR_SCHEME,
 
	CW_WIDGET_PRESIDENT_NAME,
 
	CW_WIDGET_COMPANY_NAME,
 
	CW_WIDGET_BUILD_VIEW_HQ,
 
	CW_WIDGET_RELOCATE_HQ,
 
	CW_WIDGET_BUY_SHARE,
 
	CW_WIDGET_SELL_SHARE,
 
	CW_WIDGET_COMPANY_PASSWORD,
 
};
 

	
 
static const Widget _company_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                          STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   359,     0,    13, STR_7001,                          STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   359,    14,   157, 0x0,                               STR_NULL},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    89,   158,   169, STR_7004_NEW_FACE,                 STR_7030_SELECT_NEW_FACE_FOR_PRESIDENT},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,    90,   179,   158,   169, STR_7005_COLOR_SCHEME,             STR_7031_CHANGE_THE_COMPANY_VEHICLE},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   180,   269,   158,   169, STR_7009_PRESIDENT_NAME,           STR_7032_CHANGE_THE_PRESIDENT_S},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   270,   359,   158,   169, STR_7008_COMPANY_NAME,             STR_7033_CHANGE_THE_COMPANY_NAME},
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,    18,    29, STR_7072_VIEW_HQ,                  STR_7070_BUILD_COMPANY_HEADQUARTERS},
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,    32,    43, STR_RELOCATE_HQ,                   STR_RELOCATE_COMPANY_HEADQUARTERS},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   179,   158,   169, STR_7077_BUY_25_SHARE_IN_COMPANY,  STR_7079_BUY_25_SHARE_IN_THIS_COMPANY},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   180,   359,   158,   169, STR_7078_SELL_25_SHARE_IN_COMPANY, STR_707A_SELL_25_SHARE_IN_THIS_COMPANY},
 
{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   266,   355,   138,   149, STR_COMPANY_PASSWORD,              STR_COMPANY_PASSWORD_TOOLTIP},
 
{   WIDGETS_END},
 
};
 

	
 

	
 
/**
 
 * Draws text "Vehicles:" and number of all vehicle types, or "(none)"
 
 * @param company ID of company to print statistics of
 
 */
 
static void DrawCompanyVehiclesAmount(CompanyID company)
 
{
 
	const int x = 110;
 
	int y = 63;
 
	const Vehicle *v;
 
	uint train = 0;
 
	uint road  = 0;
 
	uint air   = 0;
 
	uint ship  = 0;
 

	
 
	DrawString(x, y, STR_7039_VEHICLES, TC_FROMSTRING);
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->owner == company) {
 
			switch (v->type) {
 
				case VEH_TRAIN:    if (IsFrontEngine(v)) train++; break;
 
				case VEH_ROAD:     if (IsRoadVehFront(v)) road++; break;
 
				case VEH_AIRCRAFT: if (IsNormalAircraft(v)) air++; break;
 
				case VEH_SHIP:     ship++; break;
 
				default: break;
 
			}
 
		}
 
	}
 

	
 
	if (train + road + air + ship == 0) {
 
		DrawString(x + 70, y, STR_7042_NONE, TC_FROMSTRING);
 
	} else {
 
		if (train != 0) {
 
			SetDParam(0, train);
 
			DrawString(x + 70, y, STR_TRAINS, TC_FROMSTRING);
 
			y += 10;
 
		}
 

	
 
		if (road != 0) {
 
			SetDParam(0, road);
 
			DrawString(x + 70, y, STR_ROAD_VEHICLES, TC_FROMSTRING);
 
			y += 10;
 
		}
 

	
 
		if (air != 0) {
 
			SetDParam(0, air);
 
			DrawString(x + 70, y, STR_AIRCRAFT, TC_FROMSTRING);
 
			y += 10;
 
		}
 

	
 
		if (ship != 0) {
 
			SetDParam(0, ship);
 
			DrawString(x + 70, y, STR_SHIPS, TC_FROMSTRING);
 
		}
 
	}
 
}
 

	
 
int GetAmountOwnedBy(const Company *c, Owner owner)
 
{
 
	return (c->share_owners[0] == owner) +
 
				 (c->share_owners[1] == owner) +
 
				 (c->share_owners[2] == owner) +
 
				 (c->share_owners[3] == owner);
 
}
 

	
 
/**
 
 * Draws list of all companies with shares
 
 * @param c pointer to the Company structure
 
 */
 
static void DrawCompanyOwnerText(const Company *c)
 
{
 
	const Company *c2;
 
	uint num = 0;
 
	const byte height = GetCharacterHeight(FS_NORMAL);
 

	
 
	FOR_ALL_COMPANIES(c2) {
 
		uint amt = GetAmountOwnedBy(c, c2->index);
 
		if (amt != 0) {
 
			SetDParam(0, amt * 25);
 
			SetDParam(1, c2->index);
 

	
 
			DrawString(120, (num++) * height + 116, STR_707D_OWNED_BY, TC_FROMSTRING);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Window with general information about a company
 
 */
 
struct CompanyWindow : Window
 
{
 
	CompanyWindowWidgets query_widget;
 

	
 
	CompanyWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
 
	{
 
		this->caption_color = this->window_number;
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const Company *c = GetCompany((CompanyID)this->window_number);
 
		bool local = this->window_number == _local_company;
 

	
 
		this->SetWidgetHiddenState(CW_WIDGET_NEW_FACE,       !local);
 
		this->SetWidgetHiddenState(CW_WIDGET_COLOR_SCHEME,   !local);
 
		this->SetWidgetHiddenState(CW_WIDGET_PRESIDENT_NAME, !local);
 
		this->SetWidgetHiddenState(CW_WIDGET_COMPANY_NAME,   !local);
 
		this->widget[CW_WIDGET_BUILD_VIEW_HQ].data = (local && c->location_of_HQ == 0) ? STR_706F_BUILD_HQ : STR_7072_VIEW_HQ;
 
		if (local && c->location_of_HQ != 0) this->widget[CW_WIDGET_BUILD_VIEW_HQ].type = WWT_PUSHTXTBTN; //HQ is already built.
 
		this->SetWidgetDisabledState(CW_WIDGET_BUILD_VIEW_HQ, !local && c->location_of_HQ == 0);
 
		this->SetWidgetHiddenState(CW_WIDGET_RELOCATE_HQ,      !local || c->location_of_HQ == 0);
 
		this->SetWidgetHiddenState(CW_WIDGET_BUY_SHARE,        local);
 
		this->SetWidgetHiddenState(CW_WIDGET_SELL_SHARE,       local);
 
		this->SetWidgetHiddenState(CW_WIDGET_COMPANY_PASSWORD, !local || !_networking);
 

	
 
		if (!local) {
 
			if (_settings_game.economy.allow_shares) { // Shares are allowed
 
				/* If all shares are owned by someone (none by nobody), disable buy button */
 
				this->SetWidgetDisabledState(CW_WIDGET_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
 
						/* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
 
						(GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
 
						/* Spectators cannot do anything of course */
 
						_local_company == COMPANY_SPECTATOR);
 

	
 
				/* If the company doesn't own any shares, disable sell button */
 
				this->SetWidgetDisabledState(CW_WIDGET_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
 
						/* Spectators cannot do anything of course */
 
						_local_company == COMPANY_SPECTATOR);
 
			} else { // Shares are not allowed, disable buy/sell buttons
 
				this->DisableWidget(CW_WIDGET_BUY_SHARE);
 
				this->DisableWidget(CW_WIDGET_SELL_SHARE);
 
			}
 
		}
 

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

	
 
		this->DrawWidgets();
 

	
 
		/* Company manager's face */
 
		DrawCompanyManagerFace(c->face, c->colour, 2, 16);
 

	
 
		/* "xxx (Manager)" */
 
		SetDParam(0, c->index);
 
		DrawStringMultiCenter(48, 141, STR_7037_PRESIDENT, MAX_LENGTH_PRESIDENT_NAME_PIXELS);
 

	
 
		/* "Inaugurated:" */
 
		SetDParam(0, c->inaugurated_year);
 
		DrawString(110, 23, STR_7038_INAUGURATED, TC_FROMSTRING);
 

	
 
		/* "Colour scheme:" */
 
		DrawString(110, 43, STR_7006_COLOR_SCHEME, TC_FROMSTRING);
 
		/* Draw company-colour bus */
 
		DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOR(c->index), 215, 44);
 

	
 
		/* "Vehicles:" */
 
		DrawCompanyVehiclesAmount((CompanyID)this->window_number);
 

	
 
		/* "Company value:" */
 
		SetDParam(0, CalculateCompanyValue(c));
 
		DrawString(110, 106, STR_7076_COMPANY_VALUE, TC_FROMSTRING);
 

	
 
		/* Shares list */
 
		DrawCompanyOwnerText(c);
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case CW_WIDGET_NEW_FACE: DoSelectCompanyManagerFace(this, false); break;
 

	
 
			case CW_WIDGET_COLOR_SCHEME:
 
				if (BringWindowToFrontById(WC_COMPANY_COLOR, this->window_number)) break;
 
				new SelectCompanyLiveryWindow(&_select_company_livery_desc, (CompanyID)this->window_number);
 
				break;
 

	
 
			case CW_WIDGET_PRESIDENT_NAME:
 
				this->query_widget = CW_WIDGET_PRESIDENT_NAME;
 
				SetDParam(0, this->window_number);
 
				ShowQueryString(STR_PLAYER_NAME, STR_700B_PRESIDENT_S_NAME, MAX_LENGTH_PRESIDENT_NAME_BYTES, MAX_LENGTH_PRESIDENT_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
 
				break;
 

	
 
			case CW_WIDGET_COMPANY_NAME:
 
				this->query_widget = CW_WIDGET_COMPANY_NAME;
 
				SetDParam(0, this->window_number);
 
				ShowQueryString(STR_COMPANY_NAME, STR_700A_COMPANY_NAME, MAX_LENGTH_COMPANY_NAME_BYTES, MAX_LENGTH_COMPANY_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
 
				break;
 

	
 
			case CW_WIDGET_BUILD_VIEW_HQ: {
 
				TileIndex tile = GetCompany((CompanyID)this->window_number)->location_of_HQ;
 
				if (tile == 0) {
 
					if ((byte)this->window_number != _local_company) return;
 
					SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, VHM_RECT, this);
 
					SetTileSelectSize(2, 2);
 
					this->LowerWidget(CW_WIDGET_BUILD_VIEW_HQ);
 
					this->InvalidateWidget(CW_WIDGET_BUILD_VIEW_HQ);
 
				} else {
 
					if (_ctrl_pressed) {
 
						ShowExtraViewPortWindow(tile);
 
					} else {
 
						ScrollMainWindowToTile(tile);
 
					}
 
				}
 
				break;
 
			}
 

	
 
			case CW_WIDGET_RELOCATE_HQ:
 
				SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, VHM_RECT, this);
 
				SetTileSelectSize(2, 2);
 
				this->LowerWidget(CW_WIDGET_RELOCATE_HQ);
 
				this->InvalidateWidget(CW_WIDGET_RELOCATE_HQ);
 
				break;
 

	
 
			case CW_WIDGET_BUY_SHARE:
 
				DoCommandP(0, this->window_number, 0, NULL, CMD_BUY_SHARE_IN_COMPANY | CMD_MSG(STR_707B_CAN_T_BUY_25_SHARE_IN_THIS));
 
				break;
 

	
 
			case CW_WIDGET_SELL_SHARE:
 
				DoCommandP(0, this->window_number, 0, NULL, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_707C_CAN_T_SELL_25_SHARE_IN));
 
				break;
 

	
 
#ifdef ENABLE_NETWORK
 
			case CW_WIDGET_COMPANY_PASSWORD:
 
				if (this->window_number == _local_company) ShowNetworkCompanyPasswordWindow(this);
 
				break;
 
#endif /* ENABLE_NETWORK */
 
		}
 
	}
 

	
 
	virtual void OnHundredthTick()
 
	{
 
		/* redraw the window every now and then */
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnPlaceObject(Point pt, TileIndex tile)
 
	{
 
		if (DoCommandP(tile, 0, 0, NULL, CMD_BUILD_COMPANY_HQ | CMD_NO_WATER | CMD_MSG(STR_7071_CAN_T_BUILD_COMPANY_HEADQUARTERS)))
 
			ResetObjectToPlace();
 
			this->widget[CW_WIDGET_BUILD_VIEW_HQ].type = WWT_PUSHTXTBTN; // this button can now behave as a normal push button
 
			this->RaiseButtons();
 
	}
 

	
 
	virtual void OnPlaceObjectAbort()
 
	{
 
		this->RaiseButtons();
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (str == NULL) return;
 

	
 
		_cmd_text = str;
 
		switch (this->query_widget) {
 
			default: NOT_REACHED();
 

	
 
			case CW_WIDGET_PRESIDENT_NAME:
 
				DoCommandP(0, 0, 0, NULL, CMD_RENAME_PRESIDENT | CMD_MSG(STR_700D_CAN_T_CHANGE_PRESIDENT));
 
				break;
 

	
 
			case CW_WIDGET_COMPANY_NAME:
 
				DoCommandP(0, 0, 0, NULL, CMD_RENAME_COMPANY | CMD_MSG(STR_700C_CAN_T_CHANGE_COMPANY_NAME));
 
				break;
 
		}
 
	}
 
};
 

	
 
static const WindowDesc _company_desc = {
 
	WDP_AUTO, WDP_AUTO, 360, 170, 360, 170,
 
	WC_COMPANY, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
 
	_company_widgets,
 
};
 

	
 
void ShowCompany(CompanyID company)
 
{
 
	if (!IsValidCompanyID(company)) return;
 

	
 
	AllocateWindowDescFront<CompanyWindow>(&_company_desc, company);
 
}
 

	
 

	
 

	
 
struct BuyCompanyWindow : Window {
 
	BuyCompanyWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
 
	{
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		Company *c = GetCompany((CompanyID)this->window_number);
 
		SetDParam(0, STR_COMPANY_NAME);
 
		SetDParam(1, c->index);
 
		this->DrawWidgets();
 

	
 
		DrawCompanyManagerFace(c->face, c->colour, 2, 16);
 

	
 
		SetDParam(0, c->index);
 
		SetDParam(1, c->bankrupt_value);
 
		DrawStringMultiCenter(214, 65, STR_705B_WE_ARE_LOOKING_FOR_A_TRANSPORT, 238);
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case 3:
 
				delete this;
 
				break;
 

	
 
			case 4:
 
				DoCommandP(0, this->window_number, 0, NULL, CMD_BUY_COMPANY | CMD_MSG(STR_7060_CAN_T_BUY_COMPANY));
 
				break;
 
		}
 
	}
 
};
 

	
 
static const Widget _buy_company_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_LIGHT_BLUE,     0,    10,     0,    13, STR_00C5,              STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_LIGHT_BLUE,    11,   333,     0,    13, STR_00B3_MESSAGE_FROM, STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_LIGHT_BLUE,     0,   333,    14,   136, 0x0,                   STR_NULL},
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_LIGHT_BLUE,   148,   207,   117,   128, STR_00C9_NO,           STR_NULL},
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_LIGHT_BLUE,   218,   277,   117,   128, STR_00C8_YES,          STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _buy_company_desc = {
 
	153, 171, 334, 137, 334, 137,
 
	WC_BUY_COMPANY, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 
	_buy_company_widgets,
 
};
 

	
 

	
 
void ShowBuyCompanyDialog(CompanyID company)
 
{
 
	AllocateWindowDescFront<BuyCompanyWindow>(&_buy_company_desc, company);
 
}
 

	
 
/********** HIGHSCORE and ENDGAME windows */
 

	
 
extern StringID EndGameGetPerformanceTitleFromValue(uint value);
 

	
 

	
 
struct EndGameHighScoreBaseWindow : Window
 
{
 
	uint32 background_img;
 
	int8 rank;
 

	
 
	EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window(desc)
 
	{
 
	}
 

	
 
	/* Always draw a maximized window and within there the centered background */
 
	void SetupHighScoreEndWindow(uint *x, uint *y)
 
	{
 
		/* resize window to "full-screen" */
 
		this->width = _screen.width;
 
		this->height = _screen.height;
 
		this->widget[0].right = this->width - 1;
 
		this->widget[0].bottom = this->height - 1;
 

	
 
		this->DrawWidgets();
 

	
 
		/* Center Highscore/Endscreen background */
 
		*x = max(0, (_screen.width  / 2) - (640 / 2));
 
		*y = max(0, (_screen.height / 2) - (480 / 2));
 
		for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
 
			DrawSprite(this->background_img + i, PAL_NONE, *x, *y + (i * 50));
 
		}
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		delete this;
 
	}
 
};
 

	
 
/** End game window shown at the end of the game */
 
struct EndGameWindow : EndGameHighScoreBaseWindow {
 
	EndGameWindow(const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
 
	{
 
		/* Pause in single-player to have a look at the highscore at your own leisure */
 
		if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
 

	
 
		this->background_img = SPR_TYCOON_IMG1_BEGIN;
 

	
 
		if (_local_company != COMPANY_SPECTATOR) {
 
			const Company *c = GetCompany(_local_company);
 
			if (c->old_economy[0].performance_history == SCORE_MAX) {
 
				this->background_img = SPR_TYCOON_IMG2_BEGIN;
 
			}
 
		}
 

	
 
		/* In a network game show the endscores of the custom difficulty 'network' which is the last one
 
		 * as well as generate a TOP5 of that game, and not an all-time top5. */
 
		if (_networking) {
 
			this->window_number = lengthof(_highscore_table) - 1;
 
			this->rank = SaveHighScoreValueNetwork();
 
		} else {
 
			/* in single player _local company is always valid */
 
			const Company *c = GetCompany(_local_company);
 
			this->window_number = _settings_game.difficulty.diff_level;
 
			this->rank = SaveHighScoreValue(c);
 
		}
 

	
 
		MarkWholeScreenDirty();
 
	}
 

	
 
	~EndGameWindow()
 
	{
 
		if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
 
		ShowHighscoreTable(this->window_number, this->rank);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const Company *c;
 
		uint x, y;
 

	
 
		this->SetupHighScoreEndWindow(&x, &y);
 

	
 
		if (!IsValidCompanyID(_local_company)) return;
 

	
 
		c = GetCompany(_local_company);
 
		/* We need to get performance from last year because the image is shown
 
		 * at the start of the new year when these things have already been copied */
 
		if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
 
			SetDParam(0, c->index);
 
			SetDParam(1, c->index);
 
			SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
 
			DrawStringMultiCenter(x + (640 / 2), y + 107, STR_021C_OF_ACHIEVES_STATUS, 640);
 
		} else {
 
			SetDParam(0, c->index);
 
			SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
 
			DrawStringMultiCenter(x + (640 / 2), y + 157, STR_021B_ACHIEVES_STATUS, 640);
 
		}
 
	}
 
};
 

	
 
struct HighScoreWindow : EndGameHighScoreBaseWindow
 
{
 
	HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
 
	{
 
		/* pause game to show the chart */
 
		if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
 

	
 
		/* Close all always on-top windows to get a clean screen */
 
		if (_game_mode != GM_MENU) HideVitalWindows();
 

	
 
		MarkWholeScreenDirty();
 
		this->window_number = difficulty; // show highscore chart for difficulty...
 
		this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
 
		this->rank = ranking;
 
	}
 

	
 
	~HighScoreWindow()
 
	{
 
		if (_game_mode != GM_MENU) ShowVitalWindows();
 

	
 
		if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const HighScore *hs = _highscore_table[this->window_number];
 
		uint x, y;
 

	
 
		this->SetupHighScoreEndWindow(&x, &y);
 

	
 
		SetDParam(0, _settings_client.gui.ending_year);
 
		SetDParam(1, this->window_number + STR_6801_EASY);
 
		DrawStringMultiCenter(x + (640 / 2), y + 62, !_networking ? STR_0211_TOP_COMPANIES_WHO_REACHED : STR_TOP_COMPANIES_NETWORK_GAME, 500);
 

	
 
		/* Draw Highscore peepz */
 
		for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
 
			SetDParam(0, i + 1);
 
			DrawString(x + 40, y + 140 + (i * 55), STR_0212, TC_BLACK);
 

	
 
			if (hs[i].company[0] != '\0') {
 
				TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
 

	
 
				DoDrawString(hs[i].company, x + 71, y + 140 + (i * 55), colour);
 
				SetDParam(0, hs[i].title);
 
				SetDParam(1, hs[i].score);
 
				DrawString(x + 71, y + 160 + (i * 55), STR_HIGHSCORE_STATS, colour);
 
			}
 
		}
 
	}
 
};
 

	
 
static const Widget _highscore_widgets[] = {
 
{      WWT_PANEL, RESIZE_NONE,  COLOUR_END, 0, 640, 0, 480, 0x0, STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _highscore_desc = {
 
	0, 0, 641, 481, 641, 481,
 
	WC_HIGHSCORE, WC_NONE,
 
	0,
 
	_highscore_widgets,
 
};
 

	
 
static const WindowDesc _endgame_desc = {
 
	0, 0, 641, 481, 641, 481,
 
	WC_ENDSCREEN, WC_NONE,
 
	0,
 
	_highscore_widgets,
 
};
 

	
 
/** Show the highscore table for a given difficulty. When called from
 
 * endgame ranking is set to the top5 element that was newly added
 
 * and is thus highlighted */
 
void ShowHighscoreTable(int difficulty, int8 ranking)
 
{
 
	DeleteWindowByClass(WC_HIGHSCORE);
 
	new HighScoreWindow(&_highscore_desc, difficulty, ranking);
 
}
 

	
 
/** Show the endgame victory screen in 2050. Update the new highscore
 
 * if it was high enough */
 
void ShowEndGameChart()
 
{
 
	/* Dedicated server doesn't need the highscore window */
 
	if (_network_dedicated) return;
 

	
 
	HideVitalWindows();
 
	DeleteWindowByClass(WC_ENDSCREEN);
 
	new EndGameWindow(&_endgame_desc);
 
}
src/company_gui.h
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_gui.h GUI Functions related to companies. */
 

	
 
#ifndef COMPANY_GUI_H
 
#define COMPANY_GUI_H
 

	
 
#include "company_type.h"
 

	
 
uint16 GetDrawStringCompanyColor(CompanyID company);
 
void DrawCompanyIcon(CompanyID p, int x, int y);
 

	
 
void ShowCompanyStations(CompanyID company);
 
void ShowCompanyFinances(CompanyID company);
 
void ShowCompany(CompanyID company);
 

	
 
void InvalidateCompanyWindows(const Company *c);
 
void DeleteCompanyWindows(CompanyID company);
 

	
 
#endif /* COMPANY_GUI_H */
src/company_manager_face.h
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_manager_face.h Functionality related to the company manager's face */
 

	
 
#ifndef COMPANY_MANAGER_FACE_H
 
#define COMPANY_MANAGER_FACE_H
 

	
 
#include "core/random_func.hpp"
 
#include "core/bitmath_func.hpp"
 

	
 
/** The gender/race combinations that we have faces for */
 
enum GenderEthnicity {
 
	GENDER_FEMALE    = 0, ///< This bit set means a female, otherwise male
 
	ETHNICITY_BLACK  = 1, ///< This bit set means black, otherwise white
 

	
 
	GE_WM = 0,                                         ///< A male of Caucasian origin (white)
 
	GE_WF = 1 << GENDER_FEMALE,                        ///< A female of Caucasian origin (white)
 
	GE_BM = 1 << ETHNICITY_BLACK,                      ///< A male of African origin (black)
 
	GE_BF = 1 << ETHNICITY_BLACK | 1 << GENDER_FEMALE, ///< A female of African origin (black)
 
	GE_END,
 
};
 
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity); ///< See GenderRace as a bitset
 

	
 
/** Bitgroups of the CompanyManagerFace variable */
 
enum CompanyManagerFaceVariable {
 
	CMFV_GENDER,
 
	CMFV_ETHNICITY,
 
	CMFV_GEN_ETHN,
 
	CMFV_HAS_MOUSTACHE,
 
	CMFV_HAS_TIE_EARRING,
 
	CMFV_HAS_GLASSES,
 
	CMFV_EYE_COLOUR,
 
	CMFV_CHEEKS,
 
	CMFV_CHIN,
 
	CMFV_EYEBROWS,
 
	CMFV_MOUSTACHE,
 
	CMFV_LIPS,
 
	CMFV_NOSE,
 
	CMFV_HAIR,
 
	CMFV_JACKET,
 
	CMFV_COLLAR,
 
	CMFV_TIE_EARRING,
 
	CMFV_GLASSES,
 
	CMFV_END
 
};
 
DECLARE_POSTFIX_INCREMENT(CompanyManagerFaceVariable);
 

	
 
/** Information about the valid values of CompanyManagerFace bitgroups as well as the sprites to draw */
 
struct CompanyManagerFaceBitsInfo {
 
	byte     offset;               ///< Offset in bits into the CompanyManagerFace
 
	byte     length;               ///< Number of bits used in the CompanyManagerFace
 
	byte     valid_values[GE_END]; ///< The number of valid values per gender/ethnicity
 
	SpriteID first_sprite[GE_END]; ///< The first sprite per gender/ethnicity
 
};
 

	
 
/** Lookup table for indices into the CompanyManagerFace, valid ranges and sprites */
 
static const CompanyManagerFaceBitsInfo _cmf_info[] = {
 
	/* Index                   off len   WM  WF  BM  BF         WM     WF     BM     BF */
 
	/* CMFV_GENDER          */ {  0, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< 0 = male, 1 = female
 
	/* CMFV_ETHNICITY       */ {  1, 2, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< 0 = (Western-)Caucasian, 1 = African(-American)/Black
 
	/* CMFV_GEN_ETHN        */ {  0, 3, {  4,  4,  4,  4 }, {     0,     0,     0,     0 } }, ///< Shortcut to get/set gender _and_ ethnicity
 
	/* CMFV_HAS_MOUSTACHE   */ {  3, 1, {  2,  0,  2,  0 }, {     0,     0,     0,     0 } }, ///< Females do not have a moustache
 
	/* CMFV_HAS_TIE_EARRING */ {  3, 1, {  0,  2,  0,  2 }, {     0,     0,     0,     0 } }, ///< Draw the earring for females or not. For males the tie is always drawn.
 
	/* CMFV_HAS_GLASSES     */ {  4, 1, {  2,  2,  2,  2 }, {     0,     0,     0,     0 } }, ///< Whether to draw glasses or not
 
	/* CMFV_EYE_COLOUR      */ {  5, 2, {  3,  3,  1,  1 }, {     0,     0,     0,     0 } }, ///< Palette modification
 
	/* CMFV_CHEEKS          */ {  0, 0, {  1,  1,  1,  1 }, { 0x325, 0x326, 0x390, 0x3B0 } }, ///< Cheeks are only indexed by their gender/ethnicity
 
	/* CMFV_CHIN            */ {  7, 2, {  4,  1,  2,  2 }, { 0x327, 0x327, 0x391, 0x3B1 } },
 
	/* CMFV_EYEBROWS        */ {  9, 4, { 12, 16, 11, 16 }, { 0x32B, 0x337, 0x39A, 0x3B8 } },
 
	/* CMFV_MOUSTACHE       */ { 13, 2, {  3,  0,  3,  0 }, { 0x367,     0, 0x397,     0 } }, ///< Depends on CMFV_HAS_MOUSTACHE
 
	/* CMFV_LIPS            */ { 13, 4, { 12, 10,  9,  9 }, { 0x35B, 0x351, 0x3A5, 0x3C8 } }, ///< Depends on !CMFV_HAS_MOUSTACHE
 
	/* CMFV_NOSE            */ { 17, 3, {  8,  4,  4,  5 }, { 0x349, 0x34C, 0x393, 0x3B3 } }, ///< Depends on !CMFV_HAS_MOUSTACHE
 
	/* CMFV_HAIR            */ { 20, 4, {  9,  5,  5,  4 }, { 0x382, 0x38B, 0x3D4, 0x3D9 } },
 
	/* CMFV_JACKET          */ { 24, 2, {  3,  3,  3,  3 }, { 0x36B, 0x378, 0x36B, 0x378 } },
 
	/* CMFV_COLLAR          */ { 26, 2, {  4,  4,  4,  4 }, { 0x36E, 0x37B, 0x36E, 0x37B } },
 
	/* CMFV_TIE_EARRING     */ { 28, 3, {  6,  3,  6,  3 }, { 0x372, 0x37F, 0x372, 0x3D1 } }, ///< Depends on CMFV_HAS_TIE_EARRING
 
	/* CMFV_GLASSES         */ { 31, 1, {  2,  2,  2,  2 }, { 0x347, 0x347, 0x3AE, 0x3AE } }  ///< Depends on CMFV_HAS_GLASSES
 
};
 
assert_compile(lengthof(_cmf_info) == CMFV_END);
 

	
 
/**
 
 * Gets the company manager's face bits for the given company manager's face variable
 
 * @param cmf  the face to extract the bits from
 
 * @param cmfv the face variable to get the data of
 
 * @param ge   the gender and ethnicity of the face
 
 * @pre _cmf_info[cmfv].valid_values[ge] != 0
 
 * @return the requested bits
 
 */
 
static inline uint GetCompanyManagerFaceBits(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
 
{
 
	assert(_cmf_info[cmfv].valid_values[ge] != 0);
 

	
 
	return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
 
}
 

	
 
/**
 
 * Sets the company manager's face bits for the given company manager's face variable
 
 * @param cmf  the face to write the bits to
 
 * @param cmfv the face variable to write the data of
 
 * @param ge   the gender and ethnicity of the face
 
 * @param val  the new value
 
 * @pre val < _cmf_info[cmfv].valid_values[ge]
 
 */
 
static inline void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
 
{
 
	assert(val < _cmf_info[cmfv].valid_values[ge]);
 

	
 
	SB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length, val);
 
}
 

	
 
/**
 
 * Increase/Decrease the company manager's face variable by the given amount.
 
 * If the new value greater than the max value for this variable it will be set to 0.
 
 * Or is it negativ (< 0) it will be set to max value.
 
 *
 
 * @param cmf    the company manager face to write the bits to
 
 * @param cmfv   the company manager face variable to write the data of
 
 * @param ge     the gender and ethnicity of the company manager's face
 
 * @param amount the amount which change the value
 
 *
 
 * @pre 0 <= val < _cmf_info[cmfv].valid_values[ge]
 
 */
 
static inline void IncreaseCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, int8 amount)
 
{
 
	int8 val = GetCompanyManagerFaceBits(cmf, cmfv, ge) + amount; // the new value for the cmfv
 

	
 
	/* scales the new value to the correct scope */
 
	if (val >= _cmf_info[cmfv].valid_values[ge]) {
 
		val = 0;
 
	} else if (val < 0) {
 
		val = _cmf_info[cmfv].valid_values[ge] - 1;
 
	}
 

	
 
	SetCompanyManagerFaceBits(cmf, cmfv, ge, val); // save the new value
 
}
 

	
 
/**
 
 * Checks whether the company manager's face bits have a valid range
 
 * @param cmf  the face to extract the bits from
 
 * @param cmfv the face variable to get the data of
 
 * @param ge   the gender and ethnicity of the face
 
 * @return true if and only if the bits are valid
 
 */
 
static inline bool AreCompanyManagerFaceBitsValid(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
 
{
 
	return GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length) < _cmf_info[cmfv].valid_values[ge];
 
}
 

	
 
/**
 
 * Scales a company manager's face bits variable to the correct scope
 
 * @param cmfv the face variable to write the data of
 
 * @param ge  the gender and ethnicity of the face
 
 * @param val the to value to scale
 
 * @pre val < (1U << _cmf_info[cmfv].length), i.e. val has a value of 0..2^(bits used for this variable)-1
 
 * @return the scaled value
 
 */
 
static inline uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
 
{
 
	assert(val < (1U << _cmf_info[cmfv].length));
 

	
 
	return (val * _cmf_info[cmfv].valid_values[ge]) >> _cmf_info[cmfv].length;
 
}
 

	
 
/**
 
 * Scales all company manager's face bits to the correct scope
 
 *
 
 * @param cmf the company manager's face to write the bits to
 
 */
 
static inline void ScaleAllCompanyManagerFaceBits(CompanyManagerFace &cmf)
 
{
 
	IncreaseCompanyManagerFaceBits(cmf, CMFV_ETHNICITY, GE_WM, 0); // scales the ethnicity
 

	
 
	GenderEthnicity ge = (GenderEthnicity)GB(cmf, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // gender & ethnicity of the face
 

	
 
	/* Is a male face with moustache. Need to reduce CPU load in the loop. */
 
	bool is_moust_male = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
 

	
 
	for (CompanyManagerFaceVariable cmfv = CMFV_EYE_COLOUR; cmfv < CMFV_END; cmfv++) { // scales all other variables
 

	
 
		/* The moustache variable will be scaled only if it is a male face with has a moustache */
 
		if (cmfv != CMFV_MOUSTACHE || is_moust_male) {
 
			IncreaseCompanyManagerFaceBits(cmf, cmfv, ge, 0);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Make a random new face.
 
 * If it is for the advanced company manager's face window then the new face have the same gender
 
 * and ethnicity as the old one, else the gender is equal and the ethnicity is random.
 
 *
 
 * @param cmf the company manager's face to write the bits to
 
 * @param ge  the gender and ethnicity of the old company manager's face
 
 * @param adv if it for the advanced company manager's face window
 
 *
 
 * @pre scale 'ge' to a valid gender/ethnicity combination
 
 */
 
static inline void RandomCompanyManagerFaceBits(CompanyManagerFace &cmf, GenderEthnicity ge, bool adv)
 
{
 
	cmf = InteractiveRandom(); // random all company manager's face bits
 

	
 
	/* scale ge: 0 == GE_WM, 1 == GE_WF, 2 == GE_BM, 3 == GE_BF (and maybe in future: ...) */
 
	ge = (GenderEthnicity)((uint)ge % GE_END);
 

	
 
	/* set the gender (and ethnicity) for the new company manager's face */
 
	if (adv) {
 
		SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
 
	} else {
 
		SetCompanyManagerFaceBits(cmf, CMFV_GENDER, ge, HasBit(ge, GENDER_FEMALE));
 
	}
 

	
 
	/* scales all company manager's face bits to the correct scope */
 
	ScaleAllCompanyManagerFaceBits(cmf);
 
}
 

	
 
/**
 
 * Gets the sprite to draw for the given company manager's face variable
 
 * @param cmf  the face to extract the data from
 
 * @param cmfv the face variable to get the sprite of
 
 * @param ge   the gender and ethnicity of the face
 
 * @pre _cmf_info[cmfv].valid_values[ge] != 0
 
 * @return sprite to draw
 
 */
 
static inline SpriteID GetCompanyManagerFaceSprite(CompanyManagerFace cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge)
 
{
 
	assert(_cmf_info[cmfv].valid_values[ge] != 0);
 

	
 
	return _cmf_info[cmfv].first_sprite[ge] + GB(cmf, _cmf_info[cmfv].offset, _cmf_info[cmfv].length);
 
}
 

	
 
void DrawCompanyManagerFace(CompanyManagerFace face, int color, int x, int y);
 
CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face);
 
bool IsValidCompanyManagerFace(CompanyManagerFace cmf);
 

	
 
#endif /* COMPANY_MANAGER_FACE_H */
src/company_type.h
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file company_type.h Types related to companies. */
 

	
 
#ifndef COMPANY_TYPE_H
 
#define COMPANY_TYPE_H
 

	
 
#include "core/enum_type.hpp"
 

	
 
/**
 
 * Enum for all companies/owners.
 
 */
 
enum Owner {
 
	/* All companies below MAX_COMPANIES are playable
 
	 * companies, above, they are special, computer controlled 'companies' */
 
	OWNER_BEGIN     = 0x00, ///< First owner
 
	COMPANY_FIRST   = 0x00, ///< First company, same as owner
 
	MAX_COMPANIES   = 0x08, ///< Maximum number of companies
 
	OWNER_TOWN      = 0x0F, ///< A town owns the tile, or a town is expanding
 
	OWNER_NONE      = 0x10, ///< The tile has no ownership
 
	OWNER_WATER     = 0x11, ///< The tile/execution is done by "water"
 
	OWNER_END,              ///< Last + 1 owner
 
	INVALID_OWNER   = 0xFF, ///< An invalid owner
 
	INVALID_COMPANY = 0xFF, ///< An invalid company
 

	
 
	/* 'Fake' companies used for networks */
 
	COMPANY_INACTIVE_CLIENT = 253, ///< The client is joining
 
	COMPANY_NEW_COMPANY     = 254, ///< The client wants a new company
 
	COMPANY_SPECTATOR       = 255, ///< The client is spectating
 
};
 
DECLARE_POSTFIX_INCREMENT(Owner);
 

	
 
enum {
 
	MAX_LENGTH_PRESIDENT_NAME_BYTES  =  31, ///< The maximum length of a president name in bytes including '\0'
 
	MAX_LENGTH_PRESIDENT_NAME_PIXELS =  94, ///< The maximum length of a president name in pixels
 
	MAX_LENGTH_COMPANY_NAME_BYTES    =  31, ///< The maximum length of a company name in bytes including '\0'
 
	MAX_LENGTH_COMPANY_NAME_PIXELS   = 150, ///< The maximum length of a company name in pixels
 
};
 

	
 
/** Define basic enum properties */
 
template <> struct EnumPropsT<Owner> : MakeEnumPropsT<Owner, byte, OWNER_BEGIN, OWNER_END, INVALID_OWNER> {};
 
typedef TinyEnumT<Owner> OwnerByte;
 

	
 
typedef Owner CompanyID;
 
typedef OwnerByte CompanyByte;
 

	
 
typedef uint8 CompanyMask;
 

	
 
struct Company;
 
typedef uint32 CompanyManagerFace; ///< Company manager face bits, info see in company_manager_face.h
 

	
 
#endif /* COMPANY_TYPE_H */
src/console_cmds.cpp
Show inline comments
 
@@ -26,8 +26,8 @@
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "string_func.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "settings_type.h"
 
#include "gamelog.h"
 

	
src/core/random_func.cpp
Show inline comments
 
@@ -37,7 +37,7 @@ void SetRandomSeed(uint32 seed)
 
#ifdef RANDOM_DEBUG
 
#include "../network/network_data.h"
 
#include "../variables.h" /* _frame_counter */
 
#include "../player_func.h"
 
#include "../company_func.h"
 

	
 
uint32 DoRandom(int line, const char *file)
 
{
src/depot_gui.cpp
Show inline comments
 
@@ -21,7 +21,7 @@
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "order_func.h"
 
#include "depot_base.h"
 
#include "tilehighlight_func.h"
src/disaster_cmd.cpp
Show inline comments
 
@@ -29,7 +29,7 @@
 
#include "waypoint.h"
 
#include "town.h"
 
#include "industry.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "airport.h"
 
#include "variables.h"
 
#include "settings_type.h"
src/dock_gui.cpp
Show inline comments
 
@@ -18,10 +18,10 @@
 
#include "sound_func.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "slope_func.h"
 
#include "tilehighlight_func.h"
 
#include "player_base.h"
 
#include "company_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
src/economy.cpp
Show inline comments
 
@@ -6,8 +6,8 @@
 
#include "openttd.h"
 
#include "currency.h"
 
#include "landscape.h"
 
#include "player_base.h"
 
#include "player_func.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "command_func.h"
 
#include "saveload.h"
 
#include "industry.h"
 
@@ -44,7 +44,7 @@
 
#include "signal_func.h"
 
#include "gfx_func.h"
 
#include "autoreplace_func.h"
 
#include "player_gui.h"
 
#include "company_gui.h"
 
#include "signs_base.h"
 

	
 
#include "table/strings.h"
src/economy_func.h
Show inline comments
 
@@ -12,7 +12,7 @@
 
#include "tile_type.h"
 
#include "town_type.h"
 
#include "industry_type.h"
 
#include "player_type.h"
 
#include "company_type.h"
 
#include "station_type.h"
 

	
 
void ResetPriceBaseMultipliers();
src/effectvehicle.cpp
Show inline comments
 
@@ -16,7 +16,7 @@
 
#include "news_func.h"
 
#include "command_func.h"
 
#include "saveload.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "debug.h"
 
#include "vehicle_gui.h"
 
#include "rail_type.h"
src/elrail.cpp
Show inline comments
 
@@ -64,7 +64,7 @@
 
#include "transparency.h"
 
#include "tunnelbridge_map.h"
 
#include "vehicle_func.h"
 
#include "player_base.h"
 
#include "company_base.h"
 
#include "tunnelbridge.h"
 
#include "engine_func.h"
 
#include "elrail_func.h"
src/engine.cpp
Show inline comments
 
@@ -5,8 +5,8 @@
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "debug.h"
 
#include "player_base.h"
 
#include "player_func.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "command_func.h"
 
#include "news_func.h"
 
#include "saveload.h"
src/engine_type.h
Show inline comments
 
@@ -11,7 +11,7 @@
 
#include "gfx_type.h"
 
#include "date_type.h"
 
#include "sound_type.h"
 
#include "player_type.h"
 
#include "company_type.h"
 
#include "strings_type.h"
 

	
 
typedef uint16 EngineID;
src/genworld.cpp
Show inline comments
 
@@ -5,7 +5,7 @@
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "landscape.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "variables.h"
 
#include "thread.h"
 
#include "command_func.h"
src/genworld.h
Show inline comments
 
@@ -5,7 +5,7 @@
 
#ifndef GENWORLD_H
 
#define GENWORLD_H
 

	
 
#include "player_type.h"
 
#include "company_type.h"
 

	
 
/*
 
 * Order of these enums has to be the same as in lang/english.txt
src/graph_gui.cpp
Show inline comments
 
@@ -6,8 +6,8 @@
 
#include "openttd.h"
 
#include "gui.h"
 
#include "window_gui.h"
 
#include "player_base.h"
 
#include "player_gui.h"
 
#include "company_base.h"
 
#include "company_gui.h"
 
#include "economy_func.h"
 
#include "variables.h"
 
#include "cargotype.h"
src/group.h
Show inline comments
 
@@ -7,7 +7,7 @@
 

	
 
#include "group_type.h"
 
#include "oldpool.h"
 
#include "player_type.h"
 
#include "company_type.h"
 
#include "vehicle_type.h"
 
#include "engine_type.h"
 

	
src/group_cmd.cpp
Show inline comments
 
@@ -19,7 +19,7 @@
 
#include "autoreplace_base.h"
 
#include "autoreplace_func.h"
 
#include "string_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "order_func.h"
 
#include "oldpool_func.h"
 
#include "core/alloc_func.hpp"
src/group_gui.cpp
Show inline comments
 
@@ -21,7 +21,7 @@
 
#include "string_func.h"
 
#include "viewport_func.h"
 
#include "gfx_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "tilehighlight_func.h"
src/industry_gui.cpp
Show inline comments
 
@@ -22,13 +22,13 @@
 
#include "newgrf_text.h"
 
#include "strings_func.h"
 
#include "map_func.h"
 
#include "player_func.h"
 
#include "company_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 "company_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
src/livery.h
Show inline comments
 
@@ -5,7 +5,7 @@
 
#ifndef LIVERY_H
 
#define LIVERY_H
 

	
 
#include "player_type.h"
 
#include "company_type.h"
 

	
 
/* List of different livery schemes. */
 
enum LiveryScheme {
src/main_gui.cpp
Show inline comments
 
@@ -24,9 +24,9 @@
 
#include "strings_func.h"
 
#include "zoom_func.h"
 
#include "string_func.h"
 
#include "player_base.h"
 
#include "player_func.h"
 
#include "player_gui.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "company_gui.h"
 
#include "settings_type.h"
 
#include "toolbar_gui.h"
 
#include "statusbar_gui.h"
src/misc_cmd.cpp
Show inline comments
 
@@ -12,15 +12,15 @@
 
#include "network/network.h"
 
#include "variables.h"
 
#include "livery.h"
 
#include "player_face.h"
 
#include "company_manager_face.h"
 
#include "strings_func.h"
 
#include "gfx_func.h"
 
#include "functions.h"
 
#include "vehicle_func.h"
 
#include "string_func.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "player_gui.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "company_gui.h"
 
#include "settings_type.h"
 

	
 
#include "table/strings.h"
src/misc_gui.cpp
Show inline comments
 
@@ -19,8 +19,8 @@
 
#include "gfx_func.h"
 
#include "station_func.h"
 
#include "command_func.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "town.h"
 
#include "network/network.h"
 
#include "variables.h"
 
@@ -28,7 +28,7 @@
 
#include "train.h"
 
#include "tgp.h"
 
#include "cargotype.h"
 
#include "player_face.h"
 
#include "company_manager_face.h"
 
#include "strings_func.h"
 
#include "fileio_func.h"
 
#include "fios.h"
 
@@ -39,13 +39,13 @@
 
#include "date_func.h"
 
#include "sound_func.h"
 
#include "string_func.h"
 
#include "player_gui.h"
 
#include "company_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 "company_base.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
src/network/network.cpp
Show inline comments
 
@@ -3,7 +3,7 @@
 
/** @file network.cpp Base functions for networking support. */
 

	
 
#include "../stdafx.h"
 
#include "../player_type.h"
 
#include "../company_type.h"
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
@@ -32,7 +32,7 @@
 
#include "../core/random_func.hpp"
 
#include "../window_func.h"
 
#include "../string_func.h"
 
#include "../player_func.h"
 
#include "../company_func.h"
 
#include "../settings_type.h"
 
#include "../landscape_type.h"
 
#include "../rev.h"
 
@@ -40,7 +40,7 @@
 
	#include "../core/alloc_func.hpp"
 
#endif /* DEBUG_DUMP_COMMANDS */
 
#include "table/strings.h"
 
#include "../player_base.h"
 
#include "../company_base.h"
 

	
 
bool _network_server;     ///< network-server is active
 
bool _network_available;  ///< is network mode available?
src/network/network.h
Show inline comments
 
@@ -5,7 +5,7 @@
 
#ifndef NETWORK_H
 
#define NETWORK_H
 

	
 
#include "../player_type.h"
 
#include "../company_type.h"
 

	
 
#ifdef ENABLE_NETWORK
 

	
src/network/network_client.cpp
Show inline comments
 
@@ -23,9 +23,9 @@
 
#include "../strings_func.h"
 
#include "../window_func.h"
 
#include "../string_func.h"
 
#include "../player_func.h"
 
#include "../player_base.h"
 
#include "../player_gui.h"
 
#include "../company_func.h"
 
#include "../company_base.h"
 
#include "../company_gui.h"
 
#include "../rev.h"
 

	
 
#include "table/strings.h"
src/network/network_data.cpp
Show inline comments
 
@@ -13,7 +13,7 @@
 
#include "../core/alloc_func.hpp"
 
#include "../string_func.h"
 
#include "../date_func.h"
 
#include "../player_func.h"
 
#include "../company_func.h"
 

	
 
// Add a command to the local command queue
 
void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp)
src/network/network_gui.cpp
Show inline comments
 
@@ -24,12 +24,12 @@
 
#include "../core/alloc_func.hpp"
 
#include "../string_func.h"
 
#include "../gfx_func.h"
 
#include "../player_func.h"
 
#include "../company_func.h"
 
#include "../settings_type.h"
 
#include "../widgets/dropdown_func.h"
 
#include "../querystring_gui.h"
 
#include "../sortlist_type.h"
 
#include "../player_base.h"
 
#include "../company_base.h"
 

	
 
#include "table/strings.h"
 
#include "../table/sprites.h"
src/network/network_server.cpp
Show inline comments
 
@@ -24,9 +24,9 @@
 
#include "../core/alloc_func.hpp"
 
#include "../fileio_func.h"
 
#include "../string_func.h"
 
#include "../player_base.h"
 
#include "../player_func.h"
 
#include "../player_gui.h"
 
#include "../company_base.h"
 
#include "../company_func.h"
 
#include "../company_gui.h"
 
#include "../settings_type.h"
 

	
 
#include "table/strings.h"
src/network/network_type.h
Show inline comments
 
@@ -7,7 +7,7 @@
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
#include "../player_type.h"
 
#include "../company_type.h"
 
#include "../economy_type.h"
 
#include "core/config.h"
 
#include "core/game.h"
src/network/network_udp.cpp
Show inline comments
 
@@ -21,8 +21,8 @@
 
#include "../newgrf_config.h"
 
#include "../core/endian_func.hpp"
 
#include "../string_func.h"
 
#include "../player_base.h"
 
#include "../player_func.h"
 
#include "../company_base.h"
 
#include "../company_func.h"
 
#include "../settings_type.h"
 
#include "../rev.h"
 

	
src/newgrf.cpp
Show inline comments
 
@@ -44,7 +44,7 @@
 
#include "sound_func.h"
 
#include "string_func.h"
 
#include "road_func.h"
 
#include "player_base.h"
 
#include "company_base.h"
 
#include "settings_type.h"
 
#include "network/network.h"
 
#include "map_func.h"
src/newgrf_engine.cpp
Show inline comments
 
@@ -9,8 +9,8 @@
 
#include "engine_func.h"
 
#include "engine_base.h"
 
#include "train.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "station_base.h"
 
#include "airport.h"
 
#include "newgrf.h"
src/newgrf_house.cpp
Show inline comments
 
@@ -20,10 +20,10 @@
 
#include "newgrf_commons.h"
 
#include "transparency.h"
 
#include "functions.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "animated_tile_func.h"
 
#include "date_func.h"
 
#include "player_base.h"
 
#include "company_base.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
src/newgrf_industries.cpp
Show inline comments
 
@@ -19,8 +19,8 @@
 
#include "newgrf_town.h"
 
#include "window_func.h"
 
#include "town.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "command_func.h"
 

	
 
#include "table/strings.h"
src/newgrf_station.cpp
Show inline comments
 
@@ -22,7 +22,7 @@
 
#include "newgrf_town.h"
 
#include "gfx_func.h"
 
#include "date_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "animated_tile_func.h"
 
#include "functions.h"
 
#include "tunnelbridge_map.h"
src/news_gui.cpp
Show inline comments
 
@@ -19,7 +19,7 @@
 
#include "widgets/dropdown_func.h"
 
#include "map_func.h"
 
#include "statusbar_gui.h"
 
#include "player_face.h"
 
#include "company_manager_face.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
src/npf.h
Show inline comments
 
@@ -8,7 +8,7 @@
 
#include "aystar.h"
 
#include "station_type.h"
 
#include "rail_type.h"
 
#include "player_type.h"
 
#include "company_type.h"
 
#include "vehicle_type.h"
 
#include "tile_type.h"
 
#include "track_type.h"
src/oldloader.cpp
Show inline comments
 
@@ -7,8 +7,8 @@
 
#include "station_map.h"
 
#include "town.h"
 
#include "industry.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "aircraft.h"
 
#include "roadveh.h"
 
#include "ship.h"
src/openttd.cpp
Show inline comments
 
@@ -27,8 +27,8 @@
 
#include "debug.h"
 
#include "saveload.h"
 
#include "landscape.h"
 
#include "player_func.h"
 
#include "player_base.h"
 
#include "company_func.h"
 
#include "company_base.h"
 
#include "command_func.h"
 
#include "town.h"
 
#include "industry.h"
 
@@ -49,7 +49,7 @@
 
#include "yapf/yapf.h"
 
#include "settings_func.h"
 
#include "genworld.h"
 
#include "player_face.h"
 
#include "company_manager_face.h"
 
#include "group.h"
 
#include "strings_func.h"
 
#include "date_func.h"
src/order_cmd.cpp
Show inline comments
 
@@ -10,7 +10,7 @@
 
#include "order_base.h"
 
#include "waypoint.h"
 
#include "command_func.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "news_func.h"
 
#include "saveload.h"
 
#include "vehicle_gui.h"
src/order_gui.cpp
Show inline comments
 
@@ -24,7 +24,7 @@
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "settings_type.h"
 
#include "player_func.h"
 
#include "company_func.h"
 
#include "newgrf_cargo.h"
 
#include "widgets/dropdown_func.h"
 
#include "textbuf_gui.h"
src/player_base.h
Show inline comments
 
deleted file
src/player_face.h
Show inline comments
 
deleted file
src/player_func.h
Show inline comments
 
deleted file
src/player_gui.cpp
Show inline comments
 
deleted file
src/player_gui.h
Show inline comments
 
deleted file
src/player_type.h
Show inline comments
 
deleted file

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)