src/players.cpp
author rubidium
Fri, 25 Apr 2008 15:22:32 +0000
changeset 9055 4dc6a0c0ef47
parent 9044 d01d22d12fce
child 9058 25cfd71b68e6
permissions -rw-r--r--
(svn r12897) -Codechange: some coding style in station_cmd.cpp.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2170
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2170
diff changeset
     2
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 5944
diff changeset
     3
/** @file players.cpp
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4444
diff changeset
     4
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
8786
1823ff88a054 (svn r12490) -Codechange: rename engine.h to engine_func.h and remove unneeded inclusions of engine.h and/or replace them with engine_type.h.
rubidium
parents: 8785
diff changeset
     7
#include "engine_func.h"
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
     8
#include "player_func.h"
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
     9
#include "player_gui.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "town.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8309
diff changeset
    11
#include "news_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "saveload.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    13
#include "command_func.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5441
diff changeset
    14
#include "network/network.h"
8276
245f1b131d64 (svn r11840) -Codechange: split network.h so not everything in there needs to be included when wanting to know whether we are a server.
rubidium
parents: 8264
diff changeset
    15
#include "network/network_internal.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2154
diff changeset
    16
#include "variables.h"
8965
29a591456a2f (svn r12757) -Codechange: move all cheat related stuff from all over the place to a single location.
rubidium
parents: 8786
diff changeset
    17
#include "cheat_func.h"
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
    18
#include "ai/ai.h"
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
    19
#include "player_face.h"
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6558
diff changeset
    20
#include "group.h"
8106
e6790dd9e750 (svn r11667) -Codechange: split window.h into a header that defines some 'global' window related types, on that defined 'global' window functions and one that defines functions and types only used by *_gui.cpps.
rubidium
parents: 7998
diff changeset
    21
#include "window_func.h"
8108
b42a0e5c67ef (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 8106
diff changeset
    22
#include "tile_map.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    23
#include "strings_func.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8121
diff changeset
    24
#include "gfx_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8123
diff changeset
    25
#include "functions.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    26
#include "date_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    27
#include "vehicle_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8155
diff changeset
    28
#include "sound_func.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8208
diff changeset
    29
#include "autoreplace_func.h"
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8208
diff changeset
    30
#include "autoreplace_gui.h"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8212
diff changeset
    31
#include "string_func.h"
8229
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
    32
#include "ai/default/default.h"
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
    33
#include "ai/trolly/trolly.h"
8236
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8230
diff changeset
    34
#include "road_func.h"
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8230
diff changeset
    35
#include "rail.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8763
diff changeset
    36
#include "sprite.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    38
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    39
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    40
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    41
Player _players[MAX_PLAYERS];
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    42
PlayerByte _local_player;
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    43
PlayerByte _current_player;
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    44
/* NOSAVE: can be determined from player structs */
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    45
byte _player_colors[MAX_PLAYERS];
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    46
PlayerFace _player_face; ///< for player face storage in openttd.cfg
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    47
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    48
5376
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    49
/**
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    50
 * Sets the local player and updates the patch settings that are set on a
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    51
 * per-company (player) basis to reflect the core's state in the GUI.
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    52
 * @param new_player the new player
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    53
 * @pre IsValidPlayer(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    54
 */
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    55
void SetLocalPlayer(PlayerID new_player)
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    56
{
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    57
	/* Player could also be PLAYER_SPECTATOR or OWNER_NONE */
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    58
	assert(IsValidPlayer(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE);
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    59
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    60
	_local_player = new_player;
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    61
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    62
	/* Do not update the patches if we are in the intro GUI */
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    63
	if (IsValidPlayer(new_player) && _game_mode != GM_MENU) {
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    64
		const Player *p = GetPlayer(new_player);
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    65
		_patches.autorenew        = p->engine_renew;
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    66
		_patches.autorenew_months = p->engine_renew_months;
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    67
		_patches.autorenew_money  = p->engine_renew_money;
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    68
		InvalidateWindow(WC_GAME_OPTIONS, 0);
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    69
	}
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    70
}
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
    71
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    72
bool IsHumanPlayer(PlayerID pi)
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    73
{
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    74
	return !GetPlayer(pi)->is_ai;
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    75
}
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8247
diff changeset
    76
4428
6d9fab57f2af (svn r6183) Move GetDrawStringPlayerColor() out of gfx.[ch]
tron
parents: 4344
diff changeset
    77
6d9fab57f2af (svn r6183) Move GetDrawStringPlayerColor() out of gfx.[ch]
tron
parents: 4344
diff changeset
    78
uint16 GetDrawStringPlayerColor(PlayerID player)
6d9fab57f2af (svn r6183) Move GetDrawStringPlayerColor() out of gfx.[ch]
tron
parents: 4344
diff changeset
    79
{
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4921
diff changeset
    80
	/* Get the color for DrawString-subroutines which matches the color
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4921
diff changeset
    81
	 * of the player */
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4921
diff changeset
    82
	if (!IsValidPlayer(player)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOR;
4444
8d40844e6755 (svn r6222) Remove struct ColorList, because the names of its attributes are plain confusing
tron
parents: 4428
diff changeset
    83
	return (_colour_gradient[_player_colors[player]][4]) | IS_PALETTE_COLOR;
4428
6d9fab57f2af (svn r6183) Move GetDrawStringPlayerColor() out of gfx.[ch]
tron
parents: 4344
diff changeset
    84
}
6d9fab57f2af (svn r6183) Move GetDrawStringPlayerColor() out of gfx.[ch]
tron
parents: 4344
diff changeset
    85
5750
5824ab1f36e2 (svn r8300) -Codechange: Add a prototype for DrawPlayerIcon to players.h and include that
maedhros
parents: 5692
diff changeset
    86
void DrawPlayerIcon(PlayerID p, int x, int y)
5824ab1f36e2 (svn r8300) -Codechange: Add a prototype for DrawPlayerIcon to players.h and include that
maedhros
parents: 5692
diff changeset
    87
{
5824ab1f36e2 (svn r8300) -Codechange: Add a prototype for DrawPlayerIcon to players.h and include that
maedhros
parents: 5692
diff changeset
    88
	DrawSprite(SPR_PLAYER_ICON, PLAYER_SPRITE_COLOR(p), x, y);
5824ab1f36e2 (svn r8300) -Codechange: Add a prototype for DrawPlayerIcon to players.h and include that
maedhros
parents: 5692
diff changeset
    89
}
5824ab1f36e2 (svn r8300) -Codechange: Add a prototype for DrawPlayerIcon to players.h and include that
maedhros
parents: 5692
diff changeset
    90
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    91
/**
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
    92
 * Converts an old player face format to the new player face format
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    93
 *
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
    94
 * Meaning of the bits in the old face (some bits are used in several times):
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    95
 * - 4 and 5: chin
6149
2d662e0fd6fb (svn r8892) -Fix (r8879): swapped the eye colour and eyebrow bits.
rubidium
parents: 6144
diff changeset
    96
 * - 6 to 9: eyebrows
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    97
 * - 10 to 13: nose
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    98
 * - 13 to 15: lips (also moustache for males)
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
    99
 * - 16 to 19: hair
6149
2d662e0fd6fb (svn r8892) -Fix (r8879): swapped the eye colour and eyebrow bits.
rubidium
parents: 6144
diff changeset
   100
 * - 20 to 22: eye color
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   101
 * - 20 to 27: tie, ear rings etc.
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   102
 * - 28 to 30: glasses
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   103
 * - 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white)
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   104
 * - 31: gender (0 = male, 1 = female)
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   105
 *
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   106
 * @param face the face in the old format
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   107
 * @return the face in the new format
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   108
 */
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   109
PlayerFace ConvertFromOldPlayerFace(uint32 face)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
{
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   111
	PlayerFace pf = 0;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   112
	GenderEthnicity ge = GE_WM;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   114
	if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   115
	if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   116
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   117
	SetPlayerFaceBits(pf, PFV_GEN_ETHN,    ge, ge);
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   118
	SetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   119
	SetPlayerFaceBits(pf, PFV_EYE_COLOUR,  ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   120
	SetPlayerFaceBits(pf, PFV_CHIN,        ge, ScalePlayerFaceValue(PFV_CHIN,     ge, GB(face,  4, 2)));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   121
	SetPlayerFaceBits(pf, PFV_EYEBROWS,    ge, ScalePlayerFaceValue(PFV_EYEBROWS, ge, GB(face,  6, 4)));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   122
	SetPlayerFaceBits(pf, PFV_HAIR,        ge, ScalePlayerFaceValue(PFV_HAIR,     ge, GB(face, 16, 4)));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   123
	SetPlayerFaceBits(pf, PFV_JACKET,      ge, ScalePlayerFaceValue(PFV_JACKET,   ge, GB(face, 20, 2)));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   124
	SetPlayerFaceBits(pf, PFV_COLLAR,      ge, ScalePlayerFaceValue(PFV_COLLAR,   ge, GB(face, 22, 2)));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   125
	SetPlayerFaceBits(pf, PFV_GLASSES,     ge, GB(face, 28, 1));
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   126
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   127
	uint lips = GB(face, 10, 4);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   128
	if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   129
		SetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE, ge, true);
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   130
		SetPlayerFaceBits(pf, PFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   131
	} else {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   132
		if (!HasBit(ge, GENDER_FEMALE)) {
6346
9b250d7d2bff (svn r9377) -Fix (r9376): the off-by-one error was also present at another location :(
rubidium
parents: 6259
diff changeset
   133
			lips = lips * 15 / 16;
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   134
			lips -= 3;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   135
			if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   136
		} else {
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   137
			lips = ScalePlayerFaceValue(PFV_LIPS, ge, lips);
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   138
		}
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   139
		SetPlayerFaceBits(pf, PFV_LIPS, ge, lips);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   141
		uint nose = GB(face, 13, 3);
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   142
		if (ge == GE_WF) {
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   143
			nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   144
		} else {
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   145
			nose = ScalePlayerFaceValue(PFV_NOSE, ge, nose);
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   146
		}
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   147
		SetPlayerFaceBits(pf, PFV_NOSE, ge, nose);
6137
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   148
	}
f015946be07b (svn r8879) -Codechange: make DrawPlayerFace easier to read.
rubidium
parents: 6117
diff changeset
   149
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   150
	uint tie_earring = GB(face, 24, 4);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   151
	if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   152
		if (HasBit(ge, GENDER_FEMALE)) SetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge, true);
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   153
		SetPlayerFaceBits(pf, PFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScalePlayerFaceValue(PFV_TIE_EARRING, ge, tie_earring / 2));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   156
	return pf;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   157
}
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   158
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   159
/**
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   160
 * Checks whether a player's face is a valid encoding.
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   161
 * Unused bits are not enforced to be 0.
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   162
 * @param pf the fact to check
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   163
 * @return true if and only if the face is valid
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   164
 */
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   165
bool IsValidPlayerFace(PlayerFace pf)
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   166
{
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   167
	if (!ArePlayerFaceBitsValid(pf, PFV_GEN_ETHN, GE_WM)) return false;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   168
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   169
	GenderEthnicity ge   = (GenderEthnicity)GetPlayerFaceBits(pf, PFV_GEN_ETHN, GE_WM);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   170
	bool has_moustache   = !HasBit(ge, GENDER_FEMALE) && GetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE,   ge) != 0;
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   171
	bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge) != 0;
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   172
	bool has_glasses     = GetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge) != 0;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   173
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   174
	if (!ArePlayerFaceBitsValid(pf, PFV_EYE_COLOUR, ge)) return false;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   175
	for (PlayerFaceVariable pfv = PFV_CHEEKS; pfv < PFV_END; pfv++) {
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   176
		switch (pfv) {
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   177
			case PFV_MOUSTACHE:   if (!has_moustache)   continue; break;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   178
			case PFV_LIPS:        /* FALL THROUGH */
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   179
			case PFV_NOSE:        if (has_moustache)    continue; break;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   180
			case PFV_TIE_EARRING: if (!has_tie_earring) continue; break;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   181
			case PFV_GLASSES:     if (!has_glasses)     continue; break;
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   182
			default: break;
6144
aaa63fc00f1d (svn r8887) -Fix (r8879)[MorphOS]: initialization of mouth_table[] skipped by goto
KUDr
parents: 6137
diff changeset
   183
		}
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   184
		if (!ArePlayerFaceBitsValid(pf, pfv, ge)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6149
diff changeset
   187
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
   190
void InvalidatePlayerWindows(const Player *p)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
{
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2147
diff changeset
   192
	PlayerID pid = p->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2147
diff changeset
   194
	if (pid == _local_player) InvalidateWindow(WC_STATUS_BAR, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
	InvalidateWindow(WC_FINANCES, pid);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   198
bool CheckPlayerHasMoney(CommandCost cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
{
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   200
	if (cost.GetCost() > 0) {
2498
3ed05caa4449 (svn r3024) -Codechange: Another batch of replacements of int/uint/int16/byte/-1 with proper types and constants
tron
parents: 2484
diff changeset
   201
		PlayerID pid = _current_player;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   202
		if (IsValidPlayer(pid) && cost.GetCost() > GetPlayer(pid)->player_money) {
7002
1bf6a62b0fcb (svn r10258) -Codechange: as we are now using int64 all over the place, it's better to use int64 variables in the string generating too instead of packing them into two int32s.
rubidium
parents: 6991
diff changeset
   203
			SetDParam(0, cost.GetCost());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
			_error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   211
static void SubtractMoneyFromAnyPlayer(Player *p, CommandCost cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
{
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   213
	if (cost.GetCost() == 0) return;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   214
	assert(cost.GetExpensesType() != INVALID_EXPENSES);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   215
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   216
	p->player_money -= cost.GetCost();
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   217
	p->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   219
	if (HasBit(1 << EXPENSES_TRAIN_INC    |
6841
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   220
	           1 << EXPENSES_ROADVEH_INC  |
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   221
	           1 << EXPENSES_AIRCRAFT_INC |
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   222
	           1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
8242
719060189b7e (svn r11806) -Fix (r11793) [FS#1638]: sign inversion when updating income and expenses
glx
parents: 8237
diff changeset
   223
		p->cur_economy.income -= cost.GetCost();
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   224
	} else if (HasBit(1 << EXPENSES_TRAIN_RUN    |
6841
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   225
	                  1 << EXPENSES_ROADVEH_RUN  |
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   226
	                  1 << EXPENSES_AIRCRAFT_RUN |
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   227
	                  1 << EXPENSES_SHIP_RUN     |
46b2303390c6 (svn r10080) -Cleanup: replaced magic numbers in SubtractMoneyFromAnyPlayer() with enum values
bjarni
parents: 6724
diff changeset
   228
	                  1 << EXPENSES_PROPERTY     |
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   229
	                  1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
8242
719060189b7e (svn r11806) -Fix (r11793) [FS#1638]: sign inversion when updating income and expenses
glx
parents: 8237
diff changeset
   230
		p->cur_economy.expenses -= cost.GetCost();
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   231
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   232
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	InvalidatePlayerWindows(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   236
void SubtractMoneyFromPlayer(CommandCost cost)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
{
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   238
	PlayerID pid = _current_player;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   239
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4849
diff changeset
   240
	if (IsValidPlayer(pid)) SubtractMoneyFromAnyPlayer(GetPlayer(pid), cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   243
void SubtractMoneyFromPlayerFract(PlayerID player, CommandCost cst)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
{
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1901
diff changeset
   245
	Player *p = GetPlayer(player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
	byte m = p->player_money_fraction;
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   247
	Money cost = cst.GetCost();
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   248
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
	p->player_money_fraction = m - (byte)cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
	cost >>= 8;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   251
	if (p->player_money_fraction > m) cost++;
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8229
diff changeset
   252
	if (cost != 0) SubtractMoneyFromAnyPlayer(p, CommandCost(cst.GetExpensesType(), cost));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
4849
1c6f21eb97f2 (svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
Darkvater
parents: 4848
diff changeset
   255
void GetNameOfOwner(Owner owner, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
{
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   257
	SetDParam(2, owner);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   258
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
	if (owner != OWNER_TOWN) {
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4849
diff changeset
   260
		if (!IsValidPlayer(owner)) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   261
			SetDParam(0, STR_0150_SOMEONE);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   262
		} else {
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   263
			const Player* p = GetPlayer(owner);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   264
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   265
			SetDParam(0, STR_COMPANY_NAME);
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   266
			SetDParam(1, p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
	} else {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   269
		const Town* t = ClosestTownFromTile(tile, (uint)-1);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   270
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 1977
diff changeset
   271
		SetDParam(0, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 1977
diff changeset
   272
		SetDParam(1, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
   277
bool CheckOwnership(PlayerID owner)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
{
4849
1c6f21eb97f2 (svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
Darkvater
parents: 4848
diff changeset
   279
	assert(owner < OWNER_END);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   281
	if (owner == _current_player) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	_error_message = STR_013B_OWNED_BY;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
	GetNameOfOwner(owner, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   287
bool CheckTileOwnership(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
{
4849
1c6f21eb97f2 (svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
Darkvater
parents: 4848
diff changeset
   289
	Owner owner = GetTileOwner(tile);
1901
03bf9bf99319 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
   290
4849
1c6f21eb97f2 (svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
Darkvater
parents: 4848
diff changeset
   291
	assert(owner < OWNER_END);
1901
03bf9bf99319 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
   292
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   293
	if (owner == _current_player) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	_error_message = STR_013B_OWNED_BY;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   295
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   296
	/* no need to get the name of the owner unless we're the local player (saves some time) */
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2395
diff changeset
   297
	if (IsLocalPlayer()) GetNameOfOwner(owner, tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
static void GenerateCompanyName(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
{
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   303
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
	StringID str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
	Player *pp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
	uint32 strp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
	char buffer[100];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   310
	if (p->name_1 != STR_SV_UNNAMED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
	tile = p->last_build_coordinate;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   313
	if (tile == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	t = ClosestTownFromTile(tile, (uint)-1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
   317
	if (IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
		str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
		strp = t->townnameparts;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
verify_name:;
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   322
		/* No player must have this name already */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
		FOR_ALL_PLAYERS(pp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   324
			if (pp->name_1 == str && pp->name_2 == strp) goto bad_town_name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4878
diff changeset
   327
		GetString(buffer, str, lastof(buffer));
4609
954fe701062f (svn r6462) -Codechange: Have GetStringWidth() return width as well as the height bounding
Darkvater
parents: 4606
diff changeset
   328
		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 150)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
			goto bad_town_name;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
set_name:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
		p->name_1 = str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
		p->name_2 = strp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4824
diff changeset
   337
		if (!IsHumanPlayer(p->index)) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   338
			SetDParam(0, t->index);
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8309
diff changeset
   339
			AddNewsItem((StringID)(p->index | NB_BNEWCOMPANY), NM_CALLBACK, NF_TILE, NT_COMPANY_INFO, DNC_BANKRUPCY, p->last_build_coordinate, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
bad_town_name:;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   344
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
	if (p->president_name_1 == SPECSTR_PRESIDENT_NAME) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
		str = SPECSTR_ANDCO_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
		strp = p->president_name_2;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   348
		goto set_name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
		str = SPECSTR_ANDCO_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
		strp = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
		goto verify_name;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   356
static const byte _colour_sort[COLOUR_END] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   357
static const Colours _similar_colour[COLOUR_END][2] = {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   358
	{ COLOUR_BLUE,       COLOUR_LIGHT_BLUE }, // COLOUR_DARK_BLUE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   359
	{ COLOUR_GREEN,      COLOUR_DARK_GREEN }, // COLOUR_PALE_GREEN
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   360
	{ INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_PINK
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   361
	{ COLOUR_ORANGE,     INVALID_COLOUR    }, // COLOUR_YELLOW
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   362
	{ INVALID_COLOUR,    INVALID_COLOUR    }, // COLOUR_RED
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   363
	{ COLOUR_DARK_BLUE,  COLOUR_BLUE       }, // COLOUR_LIGHT_BLUE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   364
	{ COLOUR_PALE_GREEN, COLOUR_DARK_GREEN }, // COLOUR_GREEN
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   365
	{ COLOUR_PALE_GREEN, COLOUR_GREEN      }, // COLOUR_DARK_GREEN
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   366
	{ COLOUR_BLUE,       COLOUR_LIGHT_BLUE }, // COLOUR_BLUE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   367
	{ COLOUR_BROWN,      COLOUR_ORANGE     }, // COLOUR_CREAM
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   368
	{ COLOUR_PURPLE,     INVALID_COLOUR    }, // COLOUR_MAUVE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   369
	{ COLOUR_MAUVE,      INVALID_COLOUR    }, // COLOUR_PURPLE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   370
	{ COLOUR_YELLOW,     COLOUR_CREAM      }, // COLOUR_ORANGE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   371
	{ COLOUR_CREAM,      INVALID_COLOUR    }, // COLOUR_BROWN
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   372
	{ COLOUR_WHITE,      INVALID_COLOUR    }, // COLOUR_GREY
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   373
	{ COLOUR_GREY,       INVALID_COLOUR    }, // COLOUR_WHITE
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   374
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   376
static byte GeneratePlayerColour()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
{
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   378
	Colours colours[COLOUR_END];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   380
	/* Initialize array */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   381
	for (uint i = 0; i < COLOUR_END; i++) colours[i] = (Colours)i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   383
	/* And randomize it */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   384
	for (uint i = 0; i < 100; i++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   385
		uint r = Random();
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   386
		Swap(colours[GB(r, 0, 4)], colours[GB(r, 4, 4)]);
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   387
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   389
	/* Bubble sort it according to the values in table 1 */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   390
	for (uint i = 0; i < COLOUR_END; i++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   391
		for (uint j = 1; j < COLOUR_END; j++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   392
			if (_colour_sort[colours[j - 1]] < _colour_sort[colours[j]]) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   393
				Swap(colours[j - 1], colours[j]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
		}
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   396
	};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   398
	/* Move the colors that look similar to each player's color to the side */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   399
	Player *p;
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   400
	FOR_ALL_PLAYERS(p) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   401
		if (!p->is_active) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   403
		Colours pcolour = (Colours)p->player_color;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   405
		for (uint i = 0; i < COLOUR_END; i++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   406
			if (colours[i] == pcolour) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   407
				colours[i] = INVALID_COLOUR;
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   408
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
			}
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   410
		}
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   411
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   412
		for (uint j = 0; j < 2; j++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   413
			Colours similar = _similar_colour[pcolour][j];
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   414
			if (similar == INVALID_COLOUR) break;
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   415
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   416
			for (uint i = 1; i < COLOUR_END; i++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   417
				if (colours[i - 1] == similar) Swap(colours[i - 1], colours[i]);
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   418
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   422
	/* Return the first available color */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   423
	for (uint i = 0; i < COLOUR_END; i++) {
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   424
		if (colours[i] != INVALID_COLOUR) return colours[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
	}
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   426
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   427
	NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
static void GeneratePresidentName(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	Player *pp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
	char buffer[100], buffer2[40];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2944
diff changeset
   435
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
restart:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
		p->president_name_2 = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
		p->president_name_1 = SPECSTR_PRESIDENT_NAME;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   440
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   441
		SetDParam(0, p->index);
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   442
		GetString(buffer, STR_PLAYER_NAME, lastof(buffer));
4609
954fe701062f (svn r6462) -Codechange: Have GetStringWidth() return width as well as the height bounding
Darkvater
parents: 4606
diff changeset
   443
		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
		FOR_ALL_PLAYERS(pp) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
			if (pp->is_active && p != pp) {
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   448
				SetDParam(0, pp->index);
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   449
				GetString(buffer2, STR_PLAYER_NAME, lastof(buffer2));
1304
6460f4c8600e (svn r1808) Use strcmp() instead of home brewed function str_eq()
tron
parents: 1174
diff changeset
   450
				if (strcmp(buffer2, buffer) == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
					goto restart;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   458
static Player *AllocatePlayer()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
	Player *p;
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   461
	/* Find a free slot */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
		if (!p->is_active) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   464
			PlayerID i = p->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
			memset(p, 0, sizeof(Player));
8229
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
   466
			memset(&_players_ai[i], 0, sizeof(PlayerAI));
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
   467
			memset(&_players_ainew[i], 0, sizeof(PlayerAiNew));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
			p->index = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
			return p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   475
void ResetPlayerLivery(Player *p)
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   476
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   477
	for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   478
		p->livery[scheme].in_use  = false;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   479
		p->livery[scheme].colour1 = p->player_color;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   480
		p->livery[scheme].colour2 = p->player_color;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   481
	}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   482
}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   483
7734
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   484
/**
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   485
 * Create a new player and sets all player variables default values
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   486
 *
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   487
 * @param is_ai is a ai player?
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   488
 * @return the player struct
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   489
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
Player *DoStartupNewPlayer(bool is_ai)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
	p = AllocatePlayer();
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   495
	if (p == NULL) return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   497
	/* Make a color */
9044
d01d22d12fce (svn r12863) -Codechange: rewrite GeneratePlayerColour to be better understandable.
rubidium
parents: 9017
diff changeset
   498
	p->player_color = GeneratePlayerColour();
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
   499
	ResetPlayerLivery(p);
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   500
	_player_colors[p->index] = p->player_color;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	p->name_1 = STR_SV_UNNAMED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
	p->is_active = true;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   503
6952
b19643469024 (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 6950
diff changeset
   504
	p->player_money = p->current_loan = 100000;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
	p->is_ai = is_ai;
8229
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
   507
	_players_ai[p->index].state = 5; // AIS_WANT_NEW_ROUTE
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   508
	p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = PLAYER_SPECTATOR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   510
	p->avail_railtypes = GetPlayerRailtypes(p->index);
6685
00490513d7b1 (svn r9917) -Codechange: prepare some more areas for more road types.
rubidium
parents: 6648
diff changeset
   511
	p->avail_roadtypes = GetPlayerRoadtypes(p->index);
4326
2e2c9d21ed96 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4321
diff changeset
   512
	p->inaugurated_year = _cur_year;
7734
627817106768 (svn r11269) -Feature: user customisable faces. Patch by BigBB.
rubidium
parents: 7258
diff changeset
   513
	RandomPlayerFaceBits(p->face, (GenderEthnicity)Random(), false); // create a random player face
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   515
	/* Engine renewal settings */
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2817
diff changeset
   516
	p->engine_renew_list = NULL;
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   517
	p->renew_keep_length = false;
7865
ecb46243c772 (svn r11415) -Fix [FS#1428]: do not ignore the autorenew settings for new games when creating a new game.
rubidium
parents: 7842
diff changeset
   518
	p->engine_renew = _patches_newgame.autorenew;
ecb46243c772 (svn r11415) -Fix [FS#1428]: do not ignore the autorenew settings for new games when creating a new game.
rubidium
parents: 7842
diff changeset
   519
	p->engine_renew_months = _patches_newgame.autorenew_months;
ecb46243c772 (svn r11415) -Fix [FS#1428]: do not ignore the autorenew settings for new games when creating a new game.
rubidium
parents: 7842
diff changeset
   520
	p->engine_renew_money = _patches_newgame.autorenew_money;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   521
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
	GeneratePresidentName(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
	InvalidateWindow(WC_GRAPH_LEGEND, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	InvalidateWindow(WC_TOOLBAR_MENU, 0);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   526
	InvalidateWindow(WC_CLIENT_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   528
	if (is_ai && (!_networking || _network_server) && _ai.enabled)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   529
		AI_StartNewAI(p->index);
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   530
4580
9e0e39d077a5 (svn r6430) -Fix r6424: fixed failure to init the whole array of engines when starting a new player
bjarni
parents: 4574
diff changeset
   531
	memset(p->num_engines, 0, sizeof(p->num_engines));
4574
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4549
diff changeset
   532
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
	return p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   536
void StartupPlayers()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
{
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   538
	/* The AI starts like in the setting with +2 month max */
165
f81fa8c27236 (svn r166) -Codechange: change 74 for constant DAY_TICKS
darkvater
parents: 84
diff changeset
   539
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   542
static void MaybeStartNewPlayer()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	Player *p;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   546
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   547
	/* count number of competitors */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
	n = 0;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   549
	FOR_ALL_PLAYERS(p) {
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   550
		if (p->is_active && p->is_ai) n++;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
   551
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   553
	/* when there's a lot of computers in game, the probability that a new one starts is lower */
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   554
	if (n < (uint)_opt.diff.max_no_competitors &&
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   555
			n < (_network_server ?
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   556
				InteractiveRandomRange(_opt.diff.max_no_competitors + 2) :
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   557
				RandomRange(_opt.diff.max_no_competitors + 2)
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   558
			)) {
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   559
		/* Send a command to all clients to start up a new AI.
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   560
		 * Works fine for Multiplayer and Singleplayer */
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   561
		DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   562
	}
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   563
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   564
	/* The next AI starts like the difficulty setting said, with +2 month max */
2092
6b0d340ecc42 (svn r2602) -Codechange: removed unused variable (disable_computer)
truelight
parents: 2070
diff changeset
   565
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   566
	_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   569
void InitializePlayers()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	memset(_players, 0, sizeof(_players));
8155
ab1e6353db79 (svn r11717) -Fix [FS#1590]: make sure invalid players have all shares owned by PLAYER_SPECTATOR
smatz
parents: 8144
diff changeset
   572
	for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
ab1e6353db79 (svn r11717) -Fix [FS#1590]: make sure invalid players have all shares owned by PLAYER_SPECTATOR
smatz
parents: 8144
diff changeset
   573
		_players[i].index = i;
ab1e6353db79 (svn r11717) -Fix [FS#1590]: make sure invalid players have all shares owned by PLAYER_SPECTATOR
smatz
parents: 8144
diff changeset
   574
		for (uint j = 0; j < 4; j++) _players[i].share_owners[j] = PLAYER_SPECTATOR;
ab1e6353db79 (svn r11717) -Fix [FS#1590]: make sure invalid players have all shares owned by PLAYER_SPECTATOR
smatz
parents: 8144
diff changeset
   575
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
	_cur_player_tick_index = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   579
void OnTick_Players()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   583
	if (_game_mode == GM_EDITOR) return;
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   584
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   585
	p = GetPlayer((PlayerID)_cur_player_tick_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
	if (p->name_1 != 0) GenerateCompanyName(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   589
	if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start)
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   590
		MaybeStartNewPlayer();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   593
extern void ShowPlayerFinances(PlayerID player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
   595
void PlayersYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   599
	/* Copy statistics */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
		if (p->is_active) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
			memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
			memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
			InvalidateWindow(WC_FINANCES, p->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   608
	if (_patches.show_finances && _local_player != PLAYER_SPECTATOR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
		ShowPlayerFinances(_local_player);
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1901
diff changeset
   610
		p = GetPlayer(_local_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
		if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 534
diff changeset
   612
			SndPlayFx(SND_01_BAD_YEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
		} else {
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 534
diff changeset
   614
			SndPlayFx(SND_00_GOOD_YEAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
2544
2c6a25db1bd0 (svn r3073) int/byte -> EngineID/PlayerID/VehicleID
tron
parents: 2535
diff changeset
   619
static void DeletePlayerStuff(PlayerID pi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
	DeletePlayerWindows(pi);
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1901
diff changeset
   624
	p = GetPlayer(pi);
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   625
	p->name_1 = STR_NULL;
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   626
	p->president_name_1 = STR_NULL;
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   627
	free(p->name);
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   628
	free(p->president_name);
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   629
	p->name = NULL;
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   630
	p->president_name = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   633
/** Change engine renewal parameters
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3393
diff changeset
   634
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6399
diff changeset
   635
 * @param flags operation to perform
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   636
 * @param p1 bits 0-3 command
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   637
 * - p1 = 0 - change auto renew bool
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   638
 * - p1 = 1 - change auto renew months
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   639
 * - p1 = 2 - change auto renew money
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   640
 * - p1 = 3 - change auto renew array
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   641
 * - p1 = 4 - change bool, months & money all together
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   642
 * - p1 = 5 - change renew_keep_length
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   643
 * @param p2 value to set
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   644
 * if p1 = 0, then:
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   645
 * - p2 = enable engine renewal
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   646
 * if p1 = 1, then:
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   647
 * - p2 = months left before engine expires to replace it
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   648
 * if p1 = 2, then
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   649
 * - p2 = minimum amount of money available
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   650
 * if p1 = 3, then:
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6558
diff changeset
   651
 * - p1 bits  8-15 = engine group
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   652
 * - p2 bits  0-15 = old engine type
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   653
 * - p2 bits 16-31 = new engine type
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   654
 * if p1 = 4, then:
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   655
 * - p1 bit     15 = enable engine renewal
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   656
 * - p1 bits 16-31 = months left before engine expires to replace it
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   657
 * - p2 bits  0-31 = minimum amount of money available
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   658
 * if p1 = 5, then
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   659
 * - p2 = enable renew_keep_length
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   660
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   661
CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   662
{
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4849
diff changeset
   663
	if (!IsValidPlayer(_current_player)) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   664
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   665
	Player *p = GetPlayer(_current_player);
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   666
	switch (GB(p1, 0, 3)) {
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   667
		case 0:
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   668
			if (p->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   669
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   670
			if (flags & DC_EXEC) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   671
				p->engine_renew = HasBit(p2, 0);
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2395
diff changeset
   672
				if (IsLocalPlayer()) {
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   673
					_patches.autorenew = p->engine_renew;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   674
					InvalidateWindow(WC_GAME_OPTIONS, 0);
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   675
				}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   676
			}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   677
			break;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   678
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   679
		case 1:
9011
151f5c9430be (svn r12808) -Fix: enforce autorenew values range in command
glx
parents: 8969
diff changeset
   680
			if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   681
			if (p->engine_renew_months == (int16)p2) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   682
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   683
			if (flags & DC_EXEC) {
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   684
				p->engine_renew_months = (int16)p2;
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2395
diff changeset
   685
				if (IsLocalPlayer()) {
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   686
					_patches.autorenew_months = p->engine_renew_months;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   687
					InvalidateWindow(WC_GAME_OPTIONS, 0);
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   688
				}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   689
			}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   690
			break;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   691
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   692
		case 2:
9011
151f5c9430be (svn r12808) -Fix: enforce autorenew values range in command
glx
parents: 8969
diff changeset
   693
			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   694
			if (p->engine_renew_money == p2) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   695
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   696
			if (flags & DC_EXEC) {
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   697
				p->engine_renew_money = p2;
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2395
diff changeset
   698
				if (IsLocalPlayer()) {
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   699
					_patches.autorenew_money = p->engine_renew_money;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   700
					InvalidateWindow(WC_GAME_OPTIONS, 0);
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   701
				}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   702
			}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   703
			break;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   704
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   705
		case 3: {
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   706
			EngineID old_engine_type = GB(p2, 0, 16);
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   707
			EngineID new_engine_type = GB(p2, 16, 16);
6648
d0ab85b643a8 (svn r9879) -Fix (r9874): global autoreplace was little broken ;)
rubidium
parents: 6643
diff changeset
   708
			GroupID id_g = GB(p1, 16, 16);
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   709
			CommandCost cost;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   710
7258
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7096
diff changeset
   711
			if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   712
			if (new_engine_type != INVALID_ENGINE) {
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   713
				/* First we make sure that it's a valid type the user requested
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   714
				 * check that it's an engine that is in the engine array */
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   715
				if (!IsEngineIndex(new_engine_type)) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   716
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   717
				/* check that the new vehicle type is the same as the original one */
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   718
				if (GetEngine(old_engine_type)->type != GetEngine(new_engine_type)->type) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   719
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   720
				/* make sure that we do not replace a plane with a helicopter or vise versa */
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   721
				if (GetEngine(new_engine_type)->type == VEH_AIRCRAFT &&
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   722
						(AircraftVehInfo(old_engine_type)->subtype & AIR_CTOL) != (AircraftVehInfo(new_engine_type)->subtype & AIR_CTOL)) {
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   723
					return CMD_ERROR;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   724
				}
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   725
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   726
				/* make sure that the player can actually buy the new engine */
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   727
				if (!HasBit(GetEngine(new_engine_type)->player_avail, _current_player)) return CMD_ERROR;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   728
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6558
diff changeset
   729
				cost = AddEngineReplacementForPlayer(p, old_engine_type, new_engine_type, id_g, flags);
2697
97fc22baf3da (svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
peter1138
parents: 2684
diff changeset
   730
			} else {
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
   731
				cost = RemoveEngineReplacementForPlayer(p, old_engine_type, id_g, flags);
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   732
			}
2811
30a1aff946db (svn r3359) - Autoreplace GUI: When starting or stopping a replacement, redraw the replace window after the command has completed, rather than after calling the command.
peter1138
parents: 2795
diff changeset
   733
7096
9b231813f232 (svn r10363) -Fix [FS#911]: invalidating autoreplace windows didn't take account of vehicle group (Matthias Wolf)
peter1138
parents: 7058
diff changeset
   734
			if (IsLocalPlayer()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
2811
30a1aff946db (svn r3359) - Autoreplace GUI: When starting or stopping a replacement, redraw the replace window after the command has completed, rather than after calling the command.
peter1138
parents: 2795
diff changeset
   735
30a1aff946db (svn r3359) - Autoreplace GUI: When starting or stopping a replacement, redraw the replace window after the command has completed, rather than after calling the command.
peter1138
parents: 2795
diff changeset
   736
			return cost;
2755
ba133949851b (svn r3300) Remove unreachable code - in this case some duplicate breaks
tron
parents: 2697
diff changeset
   737
		}
ba133949851b (svn r3300) Remove unreachable code - in this case some duplicate breaks
tron
parents: 2697
diff changeset
   738
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   739
		case 4:
9011
151f5c9430be (svn r12808) -Fix: enforce autorenew values range in command
glx
parents: 8969
diff changeset
   740
			if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
151f5c9430be (svn r12808) -Fix: enforce autorenew values range in command
glx
parents: 8969
diff changeset
   741
			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   742
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   743
			if (flags & DC_EXEC) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   744
				p->engine_renew = HasBit(p1, 15);
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   745
				p->engine_renew_months = (int16)GB(p1, 16, 16);
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   746
				p->engine_renew_money = p2;
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   747
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2395
diff changeset
   748
				if (IsLocalPlayer()) {
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   749
					_patches.autorenew = p->engine_renew;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   750
					_patches.autorenew_months = p->engine_renew_months;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   751
					_patches.autorenew_money = p->engine_renew_money;
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   752
					InvalidateWindow(WC_GAME_OPTIONS, 0);
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   753
				}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   754
			}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   755
			break;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   756
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   757
		case 5:
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   758
			if (p->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   759
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   760
			if (flags & DC_EXEC) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   761
				p->renew_keep_length = HasBit(p2, 0);
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   762
				if (IsLocalPlayer()) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   763
					InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   764
				}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   765
			}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   766
		break;
9013
f51d22e7c7b8 (svn r12810) -Codechange: style and useless casts in CmdSetAutoReplace()
glx
parents: 9011
diff changeset
   767
	}
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2616
diff changeset
   768
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   769
	return CommandCost();
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   770
}
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
   771
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   772
/** Control the players: add, delete, etc.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3393
diff changeset
   773
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6399
diff changeset
   774
 * @param flags operation to perform
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   775
 * @param p1 various functionality
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   776
 * - p1 = 0 - create a new player, Which player (network) it will be is in p2
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   777
 * - p1 = 1 - create a new AI player
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   778
 * - p1 = 2 - delete a player. Player is identified by p2
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   779
 * - p1 = 3 - merge two companies together. Player to merge #1 with player #2. Identified by p2
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   780
 * @param p2 various functionality, dictated by p1
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   781
 * - p1 = 0 - ClientID of the newly created player
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   782
 * - p1 = 2 - PlayerID of the that is getting deleted
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   783
 * - p1 = 3 - #1 p2 = (bit  0-15) - player to merge (p2 & 0xFFFF)
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   784
 *          - #2 p2 = (bit 16-31) - player to be merged into ((p2>>16)&0xFFFF)
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   785
 * @todo In the case of p1=0, create new player, the clientID of the new player is in parameter
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   786
 * p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   787
 * on the server itself. First of all this is unbelievably ugly; second of all, well,
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   788
 * it IS ugly! <b>Someone fix this up :)</b> So where to fix?@n
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   789
 * @arg - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)@n
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   790
 * @arg - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   791
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6841
diff changeset
   792
CommandCost CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
{
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   794
	if (flags & DC_EXEC) _current_player = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   796
	switch (p1) {
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   797
	case 0: { /* Create a new player */
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   798
		/* Joining Client:
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   799
		 * _local_player: PLAYER_SPECTATOR
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   800
		 * _network_playas/cid = requested company/player
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   801
		 *
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   802
		 * Other client(s)/server:
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   803
		 * _local_player/_network_playas: what they play as
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   804
		 * cid = requested company/player of joining client */
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   805
		Player *p;
5692
bce5374837b0 (svn r8155) -Fix: fixed compilation when --disable-network is in use
bjarni
parents: 5668
diff changeset
   806
#ifdef ENABLE_NETWORK
4858
1b63ed767b29 (svn r6784) -Codechange: Change invalid PlayerID type into ClientID which it is in CmdPlayerCtrl
Darkvater
parents: 4854
diff changeset
   807
		uint16 cid = p2; // ClientID
5692
bce5374837b0 (svn r8155) -Fix: fixed compilation when --disable-network is in use
bjarni
parents: 5668
diff changeset
   808
#endif /* ENABLE_NETWORK */
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
   809
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   810
		/* This command is only executed in a multiplayer game */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   811
		if (!_networking) return CMD_ERROR;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   812
5375
a0bf4e683027 (svn r7560) -Fix (r2300): p2 is not a PlayerID when creating a new company, but the ClientID (index in _network_client_info). Therefore it was not possible to create a new company if 8 or more clients were connected. Thanks to FlashFF for noticing this bug.
rubidium
parents: 5077
diff changeset
   813
		/* Has the network client a correct ClientID? */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   814
		if (!(flags & DC_EXEC)) return CommandCost();
5441
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5376
diff changeset
   815
#ifdef ENABLE_NETWORK
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   816
		if (cid >= MAX_CLIENT_INFO) return CommandCost();
5441
e8bac228a6db (svn r7692) -Fix: OpenTTD didn't compile without network enabled (newgrf sync code)
Darkvater
parents: 5376
diff changeset
   817
#endif /* ENABLE_NETWORK */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   818
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   819
		/* Delete multiplayer progress bar */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   820
		DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   821
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
		p = DoStartupNewPlayer(false);
670
d164965bb35a (svn r1108) -Fix: [Network] Fixed problem around slow clients:
truelight
parents: 662
diff changeset
   823
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   824
		/* A new player could not be created, revert to being a spectator */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   825
		if (p == NULL) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   826
#ifdef ENABLE_NETWORK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   827
			if (_network_server) {
4858
1b63ed767b29 (svn r6784) -Codechange: Change invalid PlayerID type into ClientID which it is in CmdPlayerCtrl
Darkvater
parents: 4854
diff changeset
   828
				NetworkClientInfo *ci = &_network_client_info[cid];
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   829
				ci->client_playas = PLAYER_SPECTATOR;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   830
				NetworkUpdateClientInfo(ci->client_index);
7022
b2a065911b24 (svn r10286) -Fix r10284: nice typo (tnx glx)
truelight
parents: 7021
diff changeset
   831
			} else if (_local_player == PLAYER_SPECTATOR) {
7021
52b83eb6cc88 (svn r10284) -Fix [FS#899]: in the unlucky event 2 clients join at the same time to start a new company while there is only one spot left, all clients became spectator. Now only make either one of the joining clients spectator (much more game-friendly ;))
truelight
parents: 7002
diff changeset
   832
				_network_playas = PLAYER_SPECTATOR;
52b83eb6cc88 (svn r10284) -Fix [FS#899]: in the unlucky event 2 clients join at the same time to start a new company while there is only one spot left, all clients became spectator. Now only make either one of the joining clients spectator (much more game-friendly ;))
truelight
parents: 7002
diff changeset
   833
			}
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   834
#endif /* ENABLE_NETWORK */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   835
			break;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   836
		}
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 688
diff changeset
   837
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   838
		/* This is the joining client who wants a new company */
6399
d9bda26ab475 (svn r9529) -Fix [FS#705]: when 2 clients joined together, the second asserted on the NewCompany command of the first
truelight
parents: 6352
diff changeset
   839
		if (_local_player != _network_playas && _network_playas == p->index) {
d9bda26ab475 (svn r9529) -Fix [FS#705]: when 2 clients joined together, the second asserted on the NewCompany command of the first
truelight
parents: 6352
diff changeset
   840
			assert(_local_player == PLAYER_SPECTATOR);
5376
204acb84473f (svn r7561) -Fix (FS#431): core and (patches) GUI were not in-sync with respect to autorenew settings. This is only a temporary fix, as the definite fix needs to move the autorenew settings to a per-company settings window.
rubidium
parents: 5375
diff changeset
   841
			SetLocalPlayer(p->index);
8247
16e1c8db6f55 (svn r11811) -Fix: make compilation without networking work again (and thus move the debugdumpcommand stuff out of the network 'area').
rubidium
parents: 8242
diff changeset
   842
#ifdef ENABLE_NETWORK
7998
9cf9f9153262 (svn r11556) -Feature: allow setting a default password for new companies in network games.
rubidium
parents: 7954
diff changeset
   843
			if (!StrEmpty(_network_default_company_pass)) {
9cf9f9153262 (svn r11556) -Feature: allow setting a default password for new companies in network games.
rubidium
parents: 7954
diff changeset
   844
				char *password = _network_default_company_pass;
9cf9f9153262 (svn r11556) -Feature: allow setting a default password for new companies in network games.
rubidium
parents: 7954
diff changeset
   845
				NetworkChangeCompanyPassword(1, &password);
9cf9f9153262 (svn r11556) -Feature: allow setting a default password for new companies in network games.
rubidium
parents: 7954
diff changeset
   846
			}
8247
16e1c8db6f55 (svn r11811) -Fix: make compilation without networking work again (and thus move the debugdumpcommand stuff out of the network 'area').
rubidium
parents: 8242
diff changeset
   847
#endif /* ENABLE_NETWORK */
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   848
			MarkWholeScreenDirty();
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   849
		}
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   850
5073
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   851
		/* Now that we have a new player, broadcast its autorenew settings to
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   852
		 * all clients so everything is in sync */
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   853
		DoCommand(0,
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   854
			(_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4,
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   855
			_patches.autorenew_money,
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   856
			DC_EXEC,
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   857
			CMD_SET_AUTOREPLACE
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   858
		);
99ff52c14896 (svn r7131) -Fix (r7126) which fixes (r2817) the activating of a function call that was
Darkvater
parents: 5068
diff changeset
   859
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   860
#ifdef ENABLE_NETWORK
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   861
		if (_network_server) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   862
			/* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   863
			 * server-side in network_server.c:838, function
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   864
			 * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
4858
1b63ed767b29 (svn r6784) -Codechange: Change invalid PlayerID type into ClientID which it is in CmdPlayerCtrl
Darkvater
parents: 4854
diff changeset
   865
			NetworkClientInfo *ci = &_network_client_info[cid];
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   866
			ci->client_playas = p->index;
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   867
			NetworkUpdateClientInfo(ci->client_index);
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   868
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   869
			if (IsValidPlayer(ci->client_playas)) {
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   870
				PlayerID player_backup = _local_player;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   871
				_network_player_info[p->index].months_empty = 0;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   872
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   873
				/* XXX - When a client joins, we automatically set its name to the
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   874
				 * player's name (for some reason). As it stands now only the server
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   875
				 * knows the client's name, so it needs to send out a "broadcast" to
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   876
				 * do this. To achieve this we send a network command. However, it
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   877
				 * uses _local_player to execute the command as.  To prevent abuse
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   878
				 * (eg. only yourself can change your name/company), we 'cheat' by
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   879
				 * impersonation _local_player as the server. Not the best solution;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   880
				 * but it works.
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   881
				 * TODO: Perhaps this could be improved by when the client is ready
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   882
				 * with joining to let it send itself the command, and not the server?
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   883
				 * For example in network_client.c:534? */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   884
				_cmd_text = ci->client_name;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   885
				_local_player = ci->client_playas;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   886
				NetworkSend_Command(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME, NULL);
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   887
				_local_player = player_backup;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   888
			}
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   889
		}
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   890
#endif /* ENABLE_NETWORK */
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   891
	} break;
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   892
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   893
	case 1: /* Make a new AI player */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   894
		if (!(flags & DC_EXEC)) return CommandCost();
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   895
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
		DoStartupNewPlayer(true);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
		break;
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   898
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   899
	case 2: { /* Delete a player */
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   900
		Player *p;
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   901
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   902
		if (!IsValidPlayer((PlayerID)p2)) return CMD_ERROR;
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   903
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   904
		if (!(flags & DC_EXEC)) return CommandCost();
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   905
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   906
		p = GetPlayer((PlayerID)p2);
688
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   907
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   908
		/* Only allow removal of HUMAN companies */
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4824
diff changeset
   909
		if (IsHumanPlayer(p->index)) {
688
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   910
			/* Delete any open window of the company */
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   911
			DeletePlayerWindows(p->index);
688
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   912
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   913
			/* Show the bankrupt news */
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   914
			SetDParam(0, p->index);
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8309
diff changeset
   915
			AddNewsItem((StringID)(p->index | NB_BBANKRUPT), NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
688
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   916
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   917
			/* Remove the company */
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   918
			ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
688
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   919
			p->is_active = false;
1bf7db6d29b7 (svn r1129) -Add: [Network] Added 'reset_company <company-id>'. If a company is
truelight
parents: 679
diff changeset
   920
		}
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2817
diff changeset
   921
		RemoveAllEngineReplacementForPlayer(p);
8237
6f925c9cf836 (svn r11801) -Codechange: remove some unneeded includes from some header files.
rubidium
parents: 8236
diff changeset
   922
		RemoveAllGroupsForPlayer(p->index);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2817
diff changeset
   923
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   924
	} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   926
	case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   927
		PlayerID pid_old = (PlayerID)GB(p2,  0, 16);
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   928
		PlayerID pid_new = (PlayerID)GB(p2, 16, 16);
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   929
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4849
diff changeset
   930
		if (!IsValidPlayer(pid_old) || !IsValidPlayer(pid_new)) return CMD_ERROR;
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   931
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   932
		if (!(flags & DC_EXEC)) return CMD_ERROR;
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   933
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   934
		ChangeOwnershipOfPlayerItems(pid_old, pid_new);
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   935
		DeletePlayerStuff(pid_old);
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   936
	} break;
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4944
diff changeset
   937
1796
614d996f6be7 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1787
diff changeset
   938
	default: return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   941
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   944
static const StringID _endgame_perf_titles[] = {
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   945
	STR_0213_BUSINESSMAN,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   946
	STR_0213_BUSINESSMAN,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   947
	STR_0213_BUSINESSMAN,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   948
	STR_0213_BUSINESSMAN,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   949
	STR_0213_BUSINESSMAN,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   950
	STR_0214_ENTREPRENEUR,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   951
	STR_0214_ENTREPRENEUR,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   952
	STR_0215_INDUSTRIALIST,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   953
	STR_0215_INDUSTRIALIST,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   954
	STR_0216_CAPITALIST,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   955
	STR_0216_CAPITALIST,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   956
	STR_0217_MAGNATE,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   957
	STR_0217_MAGNATE,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   958
	STR_0218_MOGUL,
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   959
	STR_0218_MOGUL,
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   960
	STR_0219_TYCOON_OF_THE_CENTURY
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   961
};
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   962
1016
75b12fe9c4e1 (svn r1516) -Cheating players do not get their names added to the highscore table/file
darkvater
parents: 998
diff changeset
   963
StringID EndGameGetPerformanceTitleFromValue(uint value)
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   964
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4023
diff changeset
   965
	value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
2613
c43a30171b62 (svn r3151) - Fix: showing the highscore might crash the game with an invalid string message in the case when a highscore file was used before certain strings were added.
Darkvater
parents: 2572
diff changeset
   966
c43a30171b62 (svn r3151) - Fix: showing the highscore might crash the game with an invalid string message in the case when a highscore file was used before certain strings were added.
Darkvater
parents: 2572
diff changeset
   967
	return _endgame_perf_titles[value];
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   968
}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   969
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   970
/** Save the highscore for the player */
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
   971
int8 SaveHighScoreValue(const Player *p)
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   972
{
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   973
	HighScore *hs = _highscore_table[_opt.diff_level];
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   974
	uint i;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   975
	uint16 score = p->old_economy[0].performance_history;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   976
1016
75b12fe9c4e1 (svn r1516) -Cheating players do not get their names added to the highscore table/file
darkvater
parents: 998
diff changeset
   977
	/* Exclude cheaters from the honour of being in the highscore table */
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3873
diff changeset
   978
	if (CheatHasBeenUsed()) return -1;
1016
75b12fe9c4e1 (svn r1516) -Cheating players do not get their names added to the highscore table/file
darkvater
parents: 998
diff changeset
   979
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   980
	for (i = 0; i < lengthof(_highscore_table[0]); i++) {
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   981
		/* You are in the TOP5. Move all values one down and save us there */
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   982
		if (hs[i].score <= score) {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   983
			/* move all elements one down starting from the replaced one */
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   984
			memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   985
			SetDParam(0, p->index);
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
   986
			SetDParam(1, p->index);
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4878
diff changeset
   987
			GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   988
			hs[i].score = score;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   989
			hs[i].title = EndGameGetPerformanceTitleFromValue(score);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   990
			return i;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   991
		}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   992
	}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   993
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   994
	return -1; // too bad; we did not make it into the top5
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   995
}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
   996
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
   997
/** Sort all players given their performance */
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
   998
static int CDECL HighScoreSorter(const void *a, const void *b)
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
   999
{
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1000
	const Player *pa = *(const Player* const*)a;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1001
	const Player *pb = *(const Player* const*)b;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1002
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1003
	return pb->old_economy[0].performance_history - pa->old_economy[0].performance_history;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1004
}
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1005
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1006
/* Save the highscores in a network game when it has ended */
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1007
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
  1008
int8 SaveHighScoreValueNetwork()
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1009
{
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1010
	const Player* p;
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1011
	const Player* pl[MAX_PLAYERS];
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1012
	size_t count = 0;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1013
	int8 player = -1;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1014
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1015
	/* Sort all active players with the highest score first */
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1016
	FOR_ALL_PLAYERS(p) if (p->is_active) pl[count++] = p;
2992
bb83a99307cb (svn r3568) - Fix: constantizing side-effect of qsort in r3563. Ugly? Yes. Makes MSVC happy? Yes.
Darkvater
parents: 2990
diff changeset
  1017
	qsort((Player*)pl, count, sizeof(pl[0]), HighScoreSorter);
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1018
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1019
	{
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1020
		uint i;
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1021
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1022
		memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1023
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1024
		/* Copy over Top5 companies */
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2475
diff changeset
  1025
		for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1026
			HighScore* hs = &_highscore_table[LAST_HS_ITEM][i];
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1027
7058
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
  1028
			SetDParam(0, pl[i]->index);
8105bb13ce3d (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7022
diff changeset
  1029
			SetDParam(1, pl[i]->index);
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4878
diff changeset
  1030
			GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1031
			hs->score = pl[i]->old_economy[0].performance_history;
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1032
			hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1033
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1034
			/* get the ranking of the local player */
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1035
			if (pl[i]->index == _local_player) player = i;
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1036
		}
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1037
	}
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1038
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1039
	/* Add top5 players to highscore table */
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1040
	return player;
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1041
}
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1042
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1043
/** Save HighScore table to file */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
  1044
void SaveToHighScore()
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1045
{
2613
c43a30171b62 (svn r3151) - Fix: showing the highscore might crash the game with an invalid string message in the case when a highscore file was used before certain strings were added.
Darkvater
parents: 2572
diff changeset
  1046
	FILE *fp = fopen(_highscore_file, "wb");
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1047
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1048
	if (fp != NULL) {
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1049
		uint i;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1050
		HighScore *hs;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1051
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1052
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1053
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1054
				/* First character is a command character, so strlen will fail on that */
4321
958a8e9c012b (svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents: 4293
diff changeset
  1055
				byte length = min(sizeof(hs->company), (hs->company[0] == '\0') ? 0 : (int)strlen(&hs->company[1]) + 1);
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1056
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1057
				fwrite(&length, sizeof(length), 1, fp); // write away string length
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1058
				fwrite(hs->company, length, 1, fp);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1059
				fwrite(&hs->score, sizeof(hs->score), 1, fp);
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1060
				fwrite("", 2, 1, fp); // XXX - placeholder for hs->title, not saved anymore; compatibility
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1061
			}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1062
		}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1063
		fclose(fp);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1064
	}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1065
}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1066
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1067
/** Initialize the highscore table to 0 and if any file exists, load in values */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
  1068
void LoadFromHighScore()
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1069
{
2613
c43a30171b62 (svn r3151) - Fix: showing the highscore might crash the game with an invalid string message in the case when a highscore file was used before certain strings were added.
Darkvater
parents: 2572
diff changeset
  1070
	FILE *fp = fopen(_highscore_file, "rb");
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1071
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1072
	memset(_highscore_table, 0, sizeof(_highscore_table));
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1073
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1074
	if (fp != NULL) {
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1075
		uint i;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1076
		HighScore *hs;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1077
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1078
		for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1079
			for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1080
				byte length;
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1081
				fread(&length, sizeof(length), 1, fp);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1082
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1083
				fread(hs->company, 1, length, fp);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1084
				fread(&hs->score, sizeof(hs->score), 1, fp);
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1085
				fseek(fp, 2, SEEK_CUR); // XXX - placeholder for hs->title, not saved anymore; compatibility
2613
c43a30171b62 (svn r3151) - Fix: showing the highscore might crash the game with an invalid string message in the case when a highscore file was used before certain strings were added.
Darkvater
parents: 2572
diff changeset
  1086
				hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1087
			}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1088
		}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1089
		fclose(fp);
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1090
	}
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1017
diff changeset
  1091
998
c90459c24842 (svn r1496) -Fix: highscore no longer crashes in network games with a dedicated server. At the end of the game (can only be set by the server) the highscore is shown for the top5 companies of that game
darkvater
parents: 983
diff changeset
  1092
	/* Initialize end of game variable (when to show highscore chart) */
4285
19cb745c694e (svn r5915) -Cleanup: some variables were named *_date while they were only holding years; rename these variables to match this.
rubidium
parents: 4261
diff changeset
  1093
	_patches.ending_year = 2051;
983
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1094
}
4765bf636f6b (svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents: 717
diff changeset
  1095
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1096
/* Save/load of players */
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1834
diff changeset
  1097
static const SaveLoad _player_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1098
	    SLE_VAR(Player, name_2,          SLE_UINT32),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1099
	    SLE_VAR(Player, name_1,          SLE_STRINGID),
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  1100
	SLE_CONDSTR(Player, name,            SLE_STR, 0,                       84, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
  1102
	    SLE_VAR(Player, president_name_1, SLE_UINT16),
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
  1103
	    SLE_VAR(Player, president_name_2, SLE_UINT32),
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  1104
	SLE_CONDSTR(Player, president_name,  SLE_STR, 0,                       84, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1106
	    SLE_VAR(Player, face,            SLE_UINT32),
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
  1107
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1108
	/* money was changed to a 64 bit field in savegame version 1. */
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1109
	SLE_CONDVAR(Player, player_money,          SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1110
	SLE_CONDVAR(Player, player_money,          SLE_INT64,                   1, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1112
	SLE_CONDVAR(Player, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1113
	SLE_CONDVAR(Player, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1115
	    SLE_VAR(Player, player_color,          SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1116
	    SLE_VAR(Player, player_money_fraction, SLE_UINT8),
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6483
diff changeset
  1117
	SLE_CONDVAR(Player, avail_railtypes,       SLE_UINT8,                   0, 57),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1118
	    SLE_VAR(Player, block_preview,         SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
9017
3d422fffe39c (svn r12816) -Fix: the cargo count in the performance rating window could be wrong.
rubidium
parents: 9013
diff changeset
  1120
	SLE_CONDVAR(Player, cargo_types,           SLE_FILE_U16 | SLE_VAR_U32,  0, 93),
3d422fffe39c (svn r12816) -Fix: the cargo count in the performance rating window could be wrong.
rubidium
parents: 9013
diff changeset
  1121
	SLE_CONDVAR(Player, cargo_types,           SLE_UINT32,                 94, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1122
	SLE_CONDVAR(Player, location_of_house,     SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1123
	SLE_CONDVAR(Player, location_of_house,     SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1124
	SLE_CONDVAR(Player, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1125
	SLE_CONDVAR(Player, last_build_coordinate, SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1126
	SLE_CONDVAR(Player, inaugurated_year,      SLE_FILE_U8  | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1127
	SLE_CONDVAR(Player, inaugurated_year,      SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1128
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1129
	    SLE_ARR(Player, share_owners,          SLE_UINT8, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1130
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1131
	    SLE_VAR(Player, num_valid_stat_ent,    SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1132
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1133
	    SLE_VAR(Player, quarters_of_bankrupcy, SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1134
	    SLE_VAR(Player, bankrupt_asked,        SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1135
	    SLE_VAR(Player, bankrupt_timeout,      SLE_INT16),
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1136
	SLE_CONDVAR(Player, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1137
	SLE_CONDVAR(Player, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1139
	/* yearly expenses was changed to 64-bit in savegame version 2. */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1140
	SLE_CONDARR(Player, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1141
	SLE_CONDARR(Player, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1143
	SLE_CONDVAR(Player, is_ai,                 SLE_BOOL, 2, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1144
	SLE_CONDVAR(Player, is_active,             SLE_BOOL, 4, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1146
	/* Engine renewal settings */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3047
diff changeset
  1147
	SLE_CONDNULL(512, 16, 18),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1148
	SLE_CONDREF(Player, engine_renew_list,     REF_ENGINE_RENEWS,          19, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1149
	SLE_CONDVAR(Player, engine_renew,          SLE_BOOL,                   16, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1150
	SLE_CONDVAR(Player, engine_renew_months,   SLE_INT16,                  16, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1151
	SLE_CONDVAR(Player, engine_renew_money,    SLE_UINT32,                 16, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1152
	SLE_CONDVAR(Player, renew_keep_length,     SLE_BOOL,                    2, SL_MAX_VERSION), // added with 16.1, but was blank since 2
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2204
diff changeset
  1153
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1154
	/* reserve extra space in savegame here. (currently 63 bytes) */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3047
diff changeset
  1155
	SLE_CONDNULL(63, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1834
diff changeset
  1160
static const SaveLoad _player_economy_desc[] = {
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1161
	/* these were changed to 64-bit in savegame format 2 */
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1162
	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1163
	SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1164
	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6953
diff changeset
  1165
	SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1166
	SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1167
	SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1169
	    SLE_VAR(PlayerEconomyEntry, delivered_cargo,     SLE_INT32),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1170
	    SLE_VAR(PlayerEconomyEntry, performance_history, SLE_INT32),
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 165
diff changeset
  1171
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1175
static const SaveLoad _player_livery_desc[] = {
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1176
	SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1177
	SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1178
	SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1179
	SLE_END()
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1180
};
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1181
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2944
diff changeset
  1182
static void SaveLoad_PLYR(Player* p)
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2944
diff changeset
  1183
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
	SlObject(p, _player_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1188
	/* Write AI? */
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4824
diff changeset
  1189
	if (!IsHumanPlayer(p->index)) {
8229
00e7467ceeee (svn r11792) -Codechange: move some (virtually) globally included AI related structs to a place where they are only included a few times.
rubidium
parents: 8214
diff changeset
  1190
		SaveLoad_AI(p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1193
	/* Write economy */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
	SlObject(&p->cur_economy, _player_economy_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1196
	/* Write old economy entries. */
2988
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1197
	for (i = 0; i < p->num_valid_stat_ent; i++) {
26ff5a921204 (svn r3563) Simplify the code for calculating the highscore table in network games and saving of old ecenomy data
tron
parents: 2987
diff changeset
  1198
		SlObject(&p->old_economy[i], _player_economy_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
	}
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1200
6352
938ab8f48e5d (svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents: 6346
diff changeset
  1201
	/* Write each livery entry. */
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1202
	int num_liveries = CheckSavegameVersion(63) ? LS_END - 4 : (CheckSavegameVersion(85) ? LS_END - 2: LS_END);
6724
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1203
	for (i = 0; i < num_liveries; i++) {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1204
		SlObject(&p->livery[i], _player_livery_desc);
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4580
diff changeset
  1205
	}
6724
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1206
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1207
	if (num_liveries < LS_END) {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1208
		/* We want to insert some liveries somewhere in between. This means some have to be moved. */
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1209
		memmove(&p->livery[LS_FREIGHT_WAGON], &p->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(p->livery[0]));
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1210
		p->livery[LS_PASSENGER_WAGON_MONORAIL] = p->livery[LS_MONORAIL];
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1211
		p->livery[LS_PASSENGER_WAGON_MAGLEV]   = p->livery[LS_MAGLEV];
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1212
	}
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1213
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8276
diff changeset
  1214
	if (num_liveries == LS_END - 4) {
6724
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1215
		/* Copy bus/truck liveries over to trams */
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1216
		p->livery[LS_PASSENGER_TRAM] = p->livery[LS_BUS];
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1217
		p->livery[LS_FREIGHT_TRAM]   = p->livery[LS_TRUCK];
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6685
diff changeset
  1218
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
  1221
static void Save_PLYR()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1224
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1225
		if (p->is_active) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
			SlSetArrayIndex(p->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
			SlAutolength((AutolengthProc*)SaveLoad_PLYR, p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1230
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6190
diff changeset
  1232
static void Load_PLYR()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1235
	while ((index = SlIterateArray()) != -1) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1236
		Player *p = GetPlayer((PlayerID)index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
		SaveLoad_PLYR(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
		_player_colors[index] = p->player_color;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
  1239
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
  1240
		/* This is needed so an AI is attached to a loaded AI */
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
  1241
		if (p->is_ai && (!_networking || _network_server) && _ai.enabled)
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2381
diff changeset
  1242
			AI_StartNewAI(p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1246
extern const ChunkHandler _player_chunk_handlers[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
	{ 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
};