@@ -427,193 +427,193 @@ static byte GeneratePlayerColor(void)
do COLOR_SWAP(i, i + 1); while (++i != 15);
break;
}
// Return the first available color
for (i = 0;; i++) {
if (colors[i] != 0xFF) return colors[i];
static void GeneratePresidentName(Player *p)
{
Player *pp;
char buffer[100], buffer2[40];
for (;;) {
restart:;
p->president_name_2 = Random();
p->president_name_1 = SPECSTR_PRESIDENT_NAME;
SetDParam(0, p->president_name_2);
GetString(buffer, p->president_name_1);
if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 94)
continue;
FOR_ALL_PLAYERS(pp) {
if (pp->is_active && p != pp) {
SetDParam(0, pp->president_name_2);
GetString(buffer2, pp->president_name_1);
if (strcmp(buffer2, buffer) == 0)
goto restart;
return;
static Player *AllocatePlayer(void)
Player *p;
// Find a free slot
FOR_ALL_PLAYERS(p) {
if (!p->is_active) {
int i = p->index;
memset(p, 0, sizeof(Player));
p->index = i;
return p;
return NULL;
Player *DoStartupNewPlayer(bool is_ai)
p = AllocatePlayer();
if (p == NULL) return NULL;
// Make a color
p->player_color = GeneratePlayerColor();
_player_colors[p->index] = p->player_color;
p->name_1 = STR_SV_UNNAMED;
p->is_active = true;
p->money64 = p->player_money = p->current_loan = 100000;
p->is_ai = is_ai;
p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */
p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = OWNER_SPECTATOR;
p->avail_railtypes = GetPlayerRailtypes(p->index);
p->inaugurated_year = _cur_year;
p->face = Random();
/* Engine renewal settings */
p->engine_renew_list = NULL;
p->renew_keep_length = false;
p->engine_renew = false;
p->engine_renew_months = -6;
p->engine_renew_money = 100000;
GeneratePresidentName(p);
InvalidateWindow(WC_GRAPH_LEGEND, 0);
InvalidateWindow(WC_TOOLBAR_MENU, 0);
InvalidateWindow(WC_CLIENT_LIST, 0);
if (is_ai && (!_networking || _network_server) && _ai.enabled)
AI_StartNewAI(p->index);
memset(p->num_engines, 0, TOTAL_NUM_ENGINES);
memset(p->num_engines, 0, sizeof(p->num_engines));
void StartupPlayers(void)
// The AI starts like in the setting with +2 month max
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
static void MaybeStartNewPlayer(void)
uint n;
// count number of competitors
n = 0;
if (p->is_active && p->is_ai) n++;
// when there's a lot of computers in game, the probability that a new one starts is lower
if (n < (uint)_opt.diff.max_no_competitors &&
n < (_network_server ?
InteractiveRandomRange(_opt.diff.max_no_competitors + 2) :
RandomRange(_opt.diff.max_no_competitors + 2)
)) {
/* Send a command to all clients to start up a new AI.
* Works fine for Multiplayer and Singleplayer */
DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
// The next AI starts like the difficulty setting said, with +2 month max
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
void InitializePlayers(void)
uint i;
memset(_players, 0, sizeof(_players));
for (i = 0; i != MAX_PLAYERS; i++) _players[i].index = i;
_cur_player_tick_index = 0;
void OnTick_Players(void)
if (_game_mode == GM_EDITOR) return;
p = GetPlayer(_cur_player_tick_index);
_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
if (p->name_1 != 0) GenerateCompanyName(p);
if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start)
MaybeStartNewPlayer();
// index is the next parameter in _decode_parameters to set up
StringID GetPlayerNameString(PlayerID player, uint index)
if (IS_HUMAN_PLAYER(player) && player < MAX_PLAYERS) {
SetDParam(index, player+1);
return STR_7002_PLAYER;
return STR_EMPTY;
extern void ShowPlayerFinances(int player);
void PlayersYearlyLoop(void)
// Copy statistics
if (p->is_active) {
memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
InvalidateWindow(WC_FINANCES, p->index);
if (_patches.show_finances && _local_player != OWNER_SPECTATOR) {
ShowPlayerFinances(_local_player);
p = GetPlayer(_local_player);
if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
SndPlayFx(SND_01_BAD_YEAR);
} else {
SndPlayFx(SND_00_GOOD_YEAR);
Status change: