# players.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "engine.h"`
`#include "player_func.h"`
`#include "player_gui.h"`
`#include "town.h"`
`#include "station.h"`
`#include "news.h"`
`#include "saveload.h"`
`#include "command_func.h"`
`#include "network/network.h"`
`#include "network/network_internal.h"`
`#include "variables.h"`
`#include "ai/ai.h"`
`#include "player_face.h"`
`#include "group.h"`
`#include "window_func.h"`
`#include "tile_map.h"`
`#include "strings_func.h"`
`#include "gfx_func.h"`
`#include "functions.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "autoreplace_func.h"`
`#include "autoreplace_gui.h"`
`#include "string_func.h"`
`#include "ai/default/default.h"`
`#include "ai/trolly/trolly.h"`
`#include "road_func.h"`
`#include "rail.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## Defines

#define | COLOR_SWAP(i, j) do { byte t=colors[i];colors[i]=colors[j];colors[j]=t; } while(0)
#define | LAST_HS_ITEM lengthof(_highscore_table) - 1

## Functions

void | SetLocalPlayer (PlayerID new_player)
| Sets the local player and updates the patch settings that are set on a per-company (player) basis to reflect the core's state in the GUI.
bool | IsHumanPlayer (PlayerID pi)
uint16 | GetDrawStringPlayerColor (PlayerID player)
void | DrawPlayerIcon (PlayerID p, int x, int y)
PlayerFace | ConvertFromOldPlayerFace (uint32 face)
| Converts an old player face format to the new player face format.
bool | IsValidPlayerFace (PlayerFace pf)
| Checks whether a player's face is a valid encoding.
void | InvalidatePlayerWindows (const Player *p)
bool | CheckPlayerHasMoney (CommandCost cost)
static void | SubtractMoneyFromAnyPlayer (Player *p, CommandCost cost)
void | SubtractMoneyFromPlayer (CommandCost cost)
void | SubtractMoneyFromPlayerFract (PlayerID player, CommandCost cst)
void | GetNameOfOwner (Owner owner, TileIndex tile)
bool | CheckOwnership (PlayerID owner)
bool | CheckTileOwnership (TileIndex tile)
static void | GenerateCompanyName (Player *p)
static byte | GeneratePlayerColor ()
static void | GeneratePresidentName (Player *p)
static Player * | AllocatePlayer ()
void | ResetPlayerLivery (Player *p)
| Reset the livery schemes to the player's primary colour.
Player * | DoStartupNewPlayer (bool is_ai)
| Create a new player and sets all player variables default values.
void | StartupPlayers ()
static void | MaybeStartNewPlayer ()
void | InitializePlayers ()
void | OnTick_Players ()
void | ShowPlayerFinances (PlayerID player)
void | PlayersYearlyLoop ()
static void | DeletePlayerStuff (PlayerID pi)
CommandCost | CmdSetAutoReplace (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Change engine renewal parameters.
CommandCost | CmdPlayerCtrl (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Control the players: add, delete, etc.
StringID | EndGameGetPerformanceTitleFromValue (uint value)
static bool | CheatHasBeenUsed ()
| Return true if any cheat has been used, false otherwise.
int8 | SaveHighScoreValue (const Player *p)
| Save the highscore for the player.
static int CDECL | HighScoreSorter (const void *a, const void *b)
| Sort all players given their performance.
int8 | SaveHighScoreValueNetwork ()
void | SaveToHighScore ()
| Save HighScore table to file.
void | LoadFromHighScore ()
| Initialize the highscore table to 0 and if any file exists, load in values.
static void | SaveLoad_PLYR (Player *p)
static void | Save_PLYR ()
static void | Load_PLYR ()

## Variables

Player | _players [MAX_PLAYERS]
PlayerByte | _local_player
PlayerByte | _current_player
byte | _player_colors [MAX_PLAYERS]
PlayerFace | _player_face
| for player face storage in openttd.cfg
HighScore | _highscore_table [5][5]
static const byte | _color_sort [16] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}
static const byte | _color_similar_1 [16] = {8, 6, 255, 12, 255, 0, 1, 1, 0, 13, 11, 10, 3, 9, 15, 14}
static const byte | _color_similar_2 [16] = {5, 7, 255, 255, 255, 8, 7, 6, 5, 12, 255, 255, 9, 255, 255, 255}
static const StringID | _endgame_perf_titles []
static const SaveLoad | _player_desc []
static const SaveLoad | _player_economy_desc []
static const SaveLoad | _player_livery_desc []
const ChunkHandler | _player_chunk_handlers []

## Detailed Description

Definition in file players.cpp.

## Function Documentation

CommandCost CmdPlayerCtrl | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Control the players: add, delete, etc.

Parameters:

  | tile | unused
  | flags | operation to perform

  p1

  various functionality
  - p1 = 0 - create a new player, Which player (network) it will be is in p2
  - p1 = 1 - create a new AI player
  - p1 = 2 - delete a player. Player is identified by p2
  - p1 = 3 - merge two companies together. Player to merge #1 with player #2. Identified by p2

  p2

  various functionality, dictated by p1
  - p1 = 0 - ClientID of the newly created player
  - p1 = 2 - PlayerID of the that is getting deleted
  - p1 = 3 - #1 p2 = (bit 0-15) - player to merge (p2 & 0xFFFF)
    - #2 p2 = (bit 16-31) - player to be merged into ((p2>>16)&0xFFFF)

Todo:
  In the case of p1=0, create new player, the clientID of the new player is in parameter p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) on the server itself. First of all this is unbelievably ugly; second of all, well, it IS ugly! Someone fix this up :) So where to fix?

- - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
- - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received

Definition at line 788 of file players.cpp.

References _cmd_text, _networking, _patches_newgame, AddNewsItem(), CMD_CHANGE_PRESIDENT_NAME, CMD_ERROR, CMD_SET_AUTOREPLACE, DC_EXEC, DeletePlayerWindows(), DNC_BANKRUPCY, DoStartupNewPlayer(), GB(), MarkWholeScreenDirty(), NB_BBANKRUPT, NEWS_FLAGS, NM_CALLBACK, NT_COMPANY_INFO, OWNER_NONE, PLAYER_SPECTATOR, and SetLocalPlayer().

CommandCost CmdSetAutoReplace | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Change engine renewal parameters.

Parameters:

  | tile | unused
  | flags | operation to perform

  p1

  bits 0-3 command
  - p1 = 0 - change auto renew bool
  - p1 = 1 - change auto renew months
  - p1 = 2 - change auto renew money
  - p1 = 3 - change auto renew array
  - p1 = 4 - change bool, months & money all together
  - p1 = 5 - change renew_keep_length

  p2

  value to set if p1 = 0, then:
  - p2 = enable engine renewal if p1 = 1, then:
  - p2 = months left before engine expires to replace it if p1 = 2, then
  - p2 = minimum amount of money available if p1 = 3, then:
  - p1 bits 8-15 = engine group
  - p2 bits 0-15 = old engine type
  - p2 bits 16-31 = new engine type if p1 = 4, then:
  - p1 bit 15 = enable engine renewal
  - p1 bits 16-31 = months left before engine expires to replace it
  - p2 bits 0-31 = minimum amount of money available if p1 = 5, then
  - p2 = enable renew_keep_length

Definition at line 655 of file players.cpp.

References AddEngineReplacementForPlayer(), AIR_CTOL, Clamp(), ClampU(), CMD_ERROR, DC_EXEC, GB(), HasBit(), InvalidateAutoreplaceWindow(), IsAllGroupID(), and RemoveEngineReplacementForPlayer().

PlayerFace ConvertFromOldPlayerFace | ( | uint32 | face | ) |

Converts an old player face format to the new player face format.

Meaning of the bits in the old face (some bits are used in several times):

- 4 and 5: chin
- 6 to 9: eyebrows
- 10 to 13: nose
- 13 to 15: lips (also moustache for males)
- 16 to 19: hair
- 20 to 22: eye color
- 20 to 27: tie, ear rings etc.
- 28 to 30: glasses
- 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white)
- 31: gender (0 = male, 1 = female)

Parameters:

  | face | the face in the old format

Returns:
  the face in the new format

Definition at line 109 of file players.cpp.

References ClampU(), ETHNICITY_BLACK, GB(), GE_WF, GE_WM, GENDER_FEMALE, HasBit(), max(), ScalePlayerFaceValue(), SetBit(), and SetPlayerFaceBits().

Player* DoStartupNewPlayer | ( | bool | is_ai | ) |

Create a new player and sets all player variables default values.

Parameters:

  | is_ai | is a ai player?

Returns:
  the player struct

Definition at line 484 of file players.cpp.

References _networking, _patches_newgame, GetPlayerRailtypes(), PLAYER_SPECTATOR, RandomPlayerFaceBits(), and ResetPlayerLivery().

Referenced by CmdPlayerCtrl().

bool IsValidPlayerFace | ( | PlayerFace | pf | ) |

Checks whether a player's face is a valid encoding.

Unused bits are not enforced to be 0.

Parameters:

  | pf | the fact to check

Returns:
  true if and only if the face is valid

Definition at line 165 of file players.cpp.

References ArePlayerFaceBitsValid(), GE_WM, GENDER_FEMALE, GetPlayerFaceBits(), and HasBit().

Referenced by CmdSetPlayerFace().

void ResetPlayerLivery | ( | Player * | p | ) |

Reset the livery schemes to the player's primary colour.

This is used on loading games without livery information and on new player start up.

Parameters:

  | p | Player to reset.

Definition at line 469 of file players.cpp.

Referenced by DoStartupNewPlayer().

void SetLocalPlayer | ( | PlayerID | new_player | ) |

Sets the local player and updates the patch settings that are set on a per-company (player) basis to reflect the core's state in the GUI.

Parameters:

  | new_player | the new player

Precondition:
  IsValidPlayer(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE

Definition at line 55 of file players.cpp.

References OWNER_NONE, and PLAYER_SPECTATOR.

Referenced by _GenerateWorld(), ClickChangePlayerCheat(), CmdPlayerCtrl(), GenerateWorld(), and StartScenario().

## Variable Documentation

const StringID _endgame_perf_titles[]`[static]`

Initial value:

     {
      STR_0213_BUSINESSMAN,
      STR_0213_BUSINESSMAN,
      STR_0213_BUSINESSMAN,
      STR_0213_BUSINESSMAN,
      STR_0213_BUSINESSMAN,
      STR_0214_ENTREPRENEUR,
      STR_0214_ENTREPRENEUR,
      STR_0215_INDUSTRIALIST,
      STR_0215_INDUSTRIALIST,
      STR_0216_CAPITALIST,
      STR_0216_CAPITALIST,
      STR_0217_MAGNATE,
      STR_0217_MAGNATE,
      STR_0218_MOGUL,
      STR_0218_MOGUL,
      STR_0219_TYCOON_OF_THE_CENTURY
    }

Definition at line 940 of file players.cpp.

const ChunkHandler _player_chunk_handlers[]

Initial value:

     {
      { 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
    }

const SaveLoad _player_economy_desc[]`[static]`

Initial value:

     {

      SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
      SLE_CONDVAR(PlayerEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
      SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
      SLE_CONDVAR(PlayerEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
      SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
      SLE_CONDVAR(PlayerEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),

          SLE_VAR(PlayerEconomyEntry, delivered_cargo,     SLE_INT32),
          SLE_VAR(PlayerEconomyEntry, performance_history, SLE_INT32),

    }

Definition at line 1168 of file players.cpp.

const SaveLoad _player_livery_desc[]`[static]`

Initial value:

     {
      SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
      SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
      SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),

    }

Definition at line 1183 of file players.cpp.