truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" truelight@0: #include "player.h" truelight@0: #include "town.h" truelight@0: #include "vehicle.h" truelight@0: #include "station.h" truelight@0: #include "gfx.h" truelight@0: #include "news.h" truelight@0: #include "saveload.h" truelight@0: #include "command.h" truelight@0: truelight@0: extern void StartupEconomy(); truelight@0: truelight@0: static const SpriteID cheeks_table[4] = { truelight@0: 0x325, 0x326, truelight@0: 0x390, 0x3B0, truelight@0: }; truelight@0: truelight@0: static const SpriteID mouth_table[3] = { truelight@0: 0x34C, 0x34D, 0x34F truelight@0: }; truelight@0: truelight@0: void DrawPlayerFace(uint32 face, int color, int x, int y) truelight@0: { truelight@0: byte flag = 0; truelight@0: truelight@0: if ( (int32)face < 0) truelight@0: flag |= 1; truelight@0: if ((((((face >> 7) ^ face) >> 7) ^ face) & 0x8080000) == 0x8000000) truelight@0: flag |= 2; truelight@0: truelight@0: /* draw the gradient */ truelight@0: DrawSprite( (color<<16) + 0x0307836A, x, y); truelight@0: truelight@0: /* draw the cheeks */ truelight@0: DrawSprite(cheeks_table[flag&3], x, y); truelight@0: truelight@0: /* draw the chin */ truelight@0: /* FIXME: real code uses -2 in zoomlevel 1 */ truelight@0: { truelight@0: uint val = (face >> 4) & 3; truelight@0: if (!(flag & 2)) { truelight@0: DrawSprite(0x327 + (flag&1?0:val), x, y); truelight@0: } else { truelight@0: DrawSprite((flag&1?0x3B1:0x391) + (val>>1), x, y); truelight@0: } truelight@0: } truelight@0: /* draw the eyes */ truelight@0: { truelight@0: uint val1 = (face >> 6)&15; truelight@0: uint val2 = (face >> 20)&7; truelight@0: uint32 high = 0x314<<16; truelight@0: truelight@0: if (val2 >= 6) { truelight@0: high = 0x30F<<16; truelight@0: if (val2 != 6) truelight@0: high = 0x30D<<16; truelight@0: } truelight@0: truelight@0: if (!(flag & 2)) { truelight@0: if (!(flag & 1)) { truelight@0: DrawSprite(high+((val1 * 12 >> 4) + 0x832B), x, y); truelight@0: } else { truelight@0: DrawSprite(high+(val1 + 0x8337), x, y); truelight@0: } truelight@0: } else { truelight@0: if (!(flag & 1)) { truelight@0: DrawSprite(high+((val1 * 11 >> 4) + 0x839A), x, y); truelight@0: } else { truelight@0: DrawSprite(high+(val1 + 0x83B8), x, y); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: /* draw the mouth */ truelight@0: { truelight@0: uint val = (face >> 10) & 63; truelight@0: uint val2; truelight@0: truelight@0: if (!(flag&1)) { truelight@0: val2 = ((val&0xF) * 15 >> 4); truelight@0: truelight@0: if (val2 < 3) { truelight@0: DrawSprite((flag&2 ? 0x397 : 0x367) + val2, x, y); truelight@0: /* skip the rest */ truelight@0: goto skip_mouth; truelight@0: } truelight@0: truelight@0: val2 -= 3; truelight@0: if (flag & 2) { truelight@0: if (val2 > 8) val2 = 0; truelight@0: val2 += 0x3A5 - 0x35B; truelight@0: } truelight@0: DrawSprite(val2 + 0x35B, x, y); truelight@0: } else if (!(flag&2)) { truelight@0: DrawSprite(((val&0xF) * 10 >> 4) + 0x351, x, y); truelight@0: } else { truelight@0: DrawSprite(((val&0xF) * 9 >> 4) + 0x3C8, x, y); truelight@0: } truelight@0: truelight@0: val >>= 3; truelight@0: truelight@0: if (!(flag&2)) { truelight@0: if (!(flag&1)) { truelight@0: DrawSprite(0x349 + val, x, y); truelight@0: } else { truelight@0: DrawSprite( mouth_table[(val*3>>3)], x, y); truelight@0: } truelight@0: } else { truelight@0: if (!(flag&1)) { truelight@0: DrawSprite(0x393 + (val&3), x, y); truelight@0: } else { truelight@0: DrawSprite(0x3B3 + (val*5>>3), x, y); truelight@0: } truelight@0: } truelight@0: truelight@0: skip_mouth:; truelight@0: } truelight@0: truelight@0: truelight@0: /* draw the hair */ truelight@0: { truelight@0: uint val = (face >> 16) & 15; truelight@0: if (!(flag&2)) { truelight@0: if (!(flag&1)) { truelight@0: DrawSprite(0x382 + (val*9>>4), x, y); truelight@0: } else { truelight@0: DrawSprite(0x38B + (val*5>>4), x, y); truelight@0: } truelight@0: } else { truelight@0: if (!(flag&1)) { truelight@0: DrawSprite(0x3D4 + (val*5>>4), x, y); truelight@0: } else { truelight@0: DrawSprite(0x3D9 + (val*5>>4), x, y); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: /* draw the tie */ truelight@0: { truelight@0: uint val = (face >> 20) & 0xFF; truelight@0: truelight@0: if (!(flag&1)) { truelight@0: DrawSprite(0x36B + ((val&3)*3>>2), x, y); truelight@0: DrawSprite(0x36E + ((val>>2)&3), x, y); truelight@0: DrawSprite(0x372 + ((val>>4)*6>>4), x, y); truelight@0: } else { truelight@0: DrawSprite(0x378 + ((val&3)*3>>2), x, y); truelight@0: DrawSprite(0x37B + ((val>>2)&3), x, y); truelight@0: truelight@0: val >>= 4; truelight@0: if (val < 3) { truelight@0: DrawSprite((flag&2 ? 0x3D1 : 0x37F) + val, x, y); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: /* draw the glasses */ truelight@0: { truelight@0: uint val = (face >> 28) & 7; truelight@0: truelight@0: if (!(flag&2)) { truelight@0: if (val<=1) { truelight@0: DrawSprite(0x347 + val, x, y); truelight@0: } truelight@0: } else { truelight@0: if (val<=1) { truelight@0: DrawSprite(0x3AE + val, x, y); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void InvalidatePlayerWindows(Player *p) truelight@0: { truelight@0: uint pid = p->index; truelight@0: if ( (byte)pid == _local_player) truelight@0: InvalidateWindow(WC_STATUS_BAR, 0); truelight@0: truelight@0: InvalidateWindow(WC_FINANCES, pid); truelight@0: } truelight@0: truelight@0: bool CheckPlayerHasMoney(int32 cost) truelight@0: { truelight@0: if (cost > 0) { truelight@0: uint pid = _current_player; truelight@0: if (pid < MAX_PLAYERS && cost > DEREF_PLAYER(pid)->player_money) { truelight@0: SET_DPARAM32(0, cost); truelight@0: _error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES; truelight@0: return false; truelight@0: } truelight@0: } truelight@0: return true; truelight@0: } truelight@0: truelight@0: static void SubtractMoneyFromAnyPlayer(Player *p, int32 cost) truelight@0: { truelight@0: p->money64 -= cost; truelight@0: UpdatePlayerMoney32(p); truelight@0: truelight@0: p->yearly_expenses[0][_yearly_expenses_type] += cost; truelight@0: truelight@0: if ( ( 1 << _yearly_expenses_type ) & (1<<7|1<<8|1<<9|1<<10)) truelight@0: p->cur_economy.income -= cost; truelight@0: else if (( 1 << _yearly_expenses_type ) & (1<<2|1<<3|1<<4|1<<5|1<<6|1<<11)) truelight@0: p->cur_economy.expenses -= cost; truelight@0: truelight@0: InvalidatePlayerWindows(p); truelight@0: } truelight@0: truelight@0: void SubtractMoneyFromPlayer(int32 cost) truelight@0: { truelight@0: uint pid = _current_player; truelight@0: if (pid < MAX_PLAYERS) truelight@0: SubtractMoneyFromAnyPlayer(DEREF_PLAYER(pid), cost); truelight@0: } truelight@0: truelight@0: void SubtractMoneyFromPlayerFract(byte player, int32 cost) truelight@0: { truelight@0: Player *p = DEREF_PLAYER(player); truelight@0: byte m = p->player_money_fraction; truelight@0: p->player_money_fraction = m - (byte)cost; truelight@0: cost >>= 8; truelight@0: if (p->player_money_fraction > m) truelight@0: cost++; truelight@0: if (cost != 0) truelight@0: SubtractMoneyFromAnyPlayer(p, cost); truelight@0: } truelight@0: truelight@0: // the player_money field is kept as it is, but money64 contains the actual amount of money. truelight@0: void UpdatePlayerMoney32(Player *p) truelight@0: { truelight@0: if (p->money64 < -2000000000) truelight@0: p->player_money = -2000000000; truelight@0: else if (p->money64 > 2000000000) truelight@0: p->player_money = 2000000000; truelight@0: else truelight@0: p->player_money = (int32)p->money64; truelight@0: } truelight@0: truelight@0: void GetNameOfOwner(byte owner, uint tile) truelight@0: { truelight@0: SET_DPARAM8(2, owner); truelight@0: truelight@0: if (owner != OWNER_TOWN) { truelight@0: if (owner >= 8) truelight@0: SET_DPARAM16(0, STR_0150_SOMEONE); truelight@0: else { truelight@0: Player *p = DEREF_PLAYER(owner); truelight@0: SET_DPARAM16(0, p->name_1); truelight@0: SET_DPARAM32(1, p->name_2); truelight@0: } truelight@0: } else { truelight@0: Town *t = ClosestTownFromTile(tile, (uint)-1); truelight@0: SET_DPARAM16(0, t->townnametype); truelight@0: SET_DPARAM32(1, t->townnameparts); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: bool CheckOwnership(byte owner) truelight@0: { truelight@0: assert(owner <= OWNER_WATER); truelight@0: truelight@0: if (owner == _current_player) truelight@0: return true; truelight@0: _error_message = STR_013B_OWNED_BY; truelight@0: GetNameOfOwner(owner, 0); truelight@0: return false; truelight@0: } truelight@0: truelight@0: bool CheckTileOwnership(uint tile) truelight@0: { truelight@0: byte owner = _map_owner[tile]; truelight@0: assert(owner <= OWNER_WATER); truelight@0: if (owner == _current_player) truelight@0: return true; truelight@0: _error_message = STR_013B_OWNED_BY; truelight@0: truelight@0: // no need to get the name of the owner unless we're the local player (saves some time) truelight@0: if (_current_player == _local_player) truelight@0: GetNameOfOwner(owner, tile); truelight@0: return false; truelight@0: } truelight@0: truelight@0: static void GenerateCompanyName(Player *p) truelight@0: { truelight@0: uint tile; truelight@0: Town *t; truelight@0: StringID str; truelight@0: Player *pp; truelight@0: uint32 strp; truelight@0: char buffer[100]; truelight@0: truelight@0: if (p->name_1 != STR_SV_UNNAMED) truelight@0: return; truelight@0: truelight@0: tile = p->last_build_coordinate; truelight@0: if (tile == 0) truelight@0: return; truelight@0: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); truelight@0: truelight@0: if (IS_INT_INSIDE(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST+1)) { truelight@0: str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START; truelight@0: strp = t->townnameparts; truelight@0: truelight@0: verify_name:; truelight@0: // No player must have this name already truelight@0: FOR_ALL_PLAYERS(pp) { truelight@0: if (pp->name_1 == str && pp->name_2 == strp) truelight@0: goto bad_town_name; truelight@0: } truelight@0: truelight@0: GetString(buffer, str); truelight@0: if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 150) truelight@0: goto bad_town_name; truelight@0: truelight@0: set_name:; truelight@0: p->name_1 = str; truelight@0: p->name_2 = strp; truelight@0: truelight@0: MarkWholeScreenDirty(); truelight@0: truelight@0: if (!IS_HUMAN_PLAYER(p->index)) { truelight@0: SET_DPARAM16(0, t->index); truelight@0: AddNewsItem(p->index + (4 << 4), NEWS_FLAGS(NM_CALLBACK, NF_TILE, NT_COMPANY_INFO, DNC_BANKRUPCY), p->last_build_coordinate, 0); truelight@0: } truelight@0: return; truelight@0: } truelight@0: bad_town_name:; truelight@0: truelight@0: if (p->president_name_1 == SPECSTR_PRESIDENT_NAME) { truelight@0: str = SPECSTR_ANDCO_NAME; truelight@0: strp = p->president_name_2; truelight@0: goto set_name; truelight@0: } else { truelight@0: str = SPECSTR_ANDCO_NAME; truelight@0: strp = Random(); truelight@0: goto verify_name; truelight@0: } truelight@0: } truelight@0: truelight@0: #define COLOR_SWAP(i,j) do { byte t=colors[i];colors[i]=colors[j];colors[j]=t; } while(0) truelight@0: truelight@0: static const byte _color_sort[16] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}; truelight@0: static const byte _color_similar_1[16] = {8, 6, 255, 12, 255, 0, 1, 1, 0, 13, 11, 10, 3, 9, 15, 14}; truelight@0: static const byte _color_similar_2[16] = {5, 7, 255, 255, 255, 8, 7, 6, 5, 12, 255, 255, 9, 255, 255, 255}; truelight@0: truelight@0: static byte GeneratePlayerColor() truelight@0: { truelight@0: byte colors[16], pcolor, t2; truelight@0: int i,j,n; truelight@0: uint32 r; truelight@0: Player *p; truelight@0: truelight@0: // Initialize array truelight@0: for(i=0; i!=16; i++) truelight@0: colors[i] = i; truelight@0: truelight@0: // And randomize it truelight@0: n = 100; truelight@0: do { truelight@0: r = Random(); truelight@0: COLOR_SWAP(r & 0xF, (r >> 4) & 0xF); truelight@0: } while (--n); truelight@0: truelight@0: // Bubble sort it according to the values in table 1 truelight@0: i = 16; truelight@0: do { truelight@0: for(j=0; j!=15; j++) { truelight@0: if (_color_sort[colors[j]] < _color_sort[colors[j+1]]) { truelight@0: COLOR_SWAP(j,j+1); truelight@0: } truelight@0: } truelight@0: } while (--i); truelight@0: truelight@0: // Move the colors that look similar to each player's color to the side truelight@0: FOR_ALL_PLAYERS(p) if (p->is_active) { truelight@0: pcolor = p->player_color; truelight@0: for(i=0; i!=16; i++) if (colors[i] == pcolor) { truelight@0: colors[i] = 0xFF; truelight@0: truelight@0: t2 = _color_similar_1[pcolor]; truelight@0: if (t2 == 0xFF) break; truelight@0: for(i=0; i!=15; i++) { truelight@0: if (colors[i] == t2) { truelight@0: do COLOR_SWAP(i,i+1); while (++i != 15); truelight@0: break; truelight@0: } truelight@0: } truelight@0: truelight@0: t2 = _color_similar_2[pcolor]; truelight@0: if (t2 == 0xFF) break; truelight@0: for(i=0; i!=15; i++) { truelight@0: if (colors[i] == t2) { truelight@0: do COLOR_SWAP(i,i+1); while (++i != 15); truelight@0: break; truelight@0: } truelight@0: } truelight@0: break; truelight@0: } truelight@0: } truelight@0: truelight@0: // Return the first available color truelight@0: i = 0; truelight@0: for(;;) { truelight@0: if (colors[i] != 0xFF) truelight@0: return colors[i]; truelight@0: i++; truelight@0: } truelight@0: } truelight@0: truelight@0: static void GeneratePresidentName(Player *p) truelight@0: { truelight@0: Player *pp; truelight@0: char buffer[100], buffer2[40]; truelight@0: truelight@0: for(;;) { truelight@0: restart:; truelight@0: truelight@0: p->president_name_2 = Random(); truelight@0: p->president_name_1 = SPECSTR_PRESIDENT_NAME; truelight@0: truelight@0: SET_DPARAM32(0, p->president_name_2); truelight@0: GetString(buffer, p->president_name_1); truelight@0: if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 94) truelight@0: continue; truelight@0: truelight@0: FOR_ALL_PLAYERS(pp) { truelight@0: if (pp->is_active && p != pp) { truelight@0: SET_DPARAM32(0, pp->president_name_2); truelight@0: GetString(buffer2, pp->president_name_1); truelight@0: if (str_eq(buffer2, buffer)) truelight@0: goto restart; truelight@0: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: extern int GetPlayerMaxRailtype(int p); truelight@0: truelight@0: static Player *AllocatePlayer() truelight@0: { truelight@0: Player *p; truelight@0: // Find a free slot truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (!p->is_active) { truelight@0: int i = p->index; truelight@0: memset(p, 0, sizeof(Player)); truelight@0: p->index = i; truelight@0: return p; truelight@0: } truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: Player *DoStartupNewPlayer(bool is_ai) truelight@0: { truelight@0: Player *p; truelight@0: int index; truelight@0: truelight@0: p = AllocatePlayer(); truelight@0: if (p == NULL) return NULL; truelight@0: truelight@0: index = p->index; truelight@0: truelight@0: // Make a color truelight@0: p->player_color = GeneratePlayerColor(); truelight@0: _player_colors[index] = p->player_color; truelight@0: p->name_1 = STR_SV_UNNAMED; truelight@0: p->is_active = true; truelight@0: truelight@0: p->money64 = p->player_money = p->current_loan = 100000; truelight@0: truelight@0: p->is_ai = is_ai; truelight@0: p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */ truelight@0: p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = 0xFF; truelight@0: truelight@0: p->max_railtype = GetPlayerMaxRailtype(index); truelight@0: p->inaugurated_year = _cur_year; truelight@0: p->face = Random(); truelight@0: truelight@0: GeneratePresidentName(p); truelight@0: truelight@0: InvalidateWindow(WC_GRAPH_LEGEND, 0); truelight@0: InvalidateWindow(WC_TOOLBAR_MENU, 0); truelight@0: truelight@0: return p; truelight@0: } truelight@0: truelight@0: void StartupPlayers() truelight@0: { truelight@0: uint32 r; truelight@0: truelight@0: r = Random(); truelight@0: _next_competitor_start = (byte)r + _opt.diff.competitor_start_time * 6882 + 1; truelight@0: } truelight@0: truelight@0: static void MaybeStartNewPlayer() truelight@0: { truelight@0: uint n; truelight@0: Player *p; truelight@0: truelight@0: // count number of competitors truelight@0: n = 0; truelight@0: for(p=_players; p!=endof(_players); p++) truelight@0: if (p->is_active && p->is_ai) truelight@0: n++; truelight@0: truelight@0: // when there's a lot of computers in game, the probability that a new one starts is lower truelight@0: if (n < (uint)_opt.diff.max_no_competitors && n < RandomRange(_opt.diff.max_no_competitors)) truelight@0: DoStartupNewPlayer(true); truelight@0: truelight@0: _next_competitor_start = RandomRange(19980) + 11100; truelight@0: } truelight@0: truelight@0: void InitializePlayers() truelight@0: { truelight@0: int i; truelight@0: memset(_players, 0, sizeof(_players)); truelight@0: for(i = 0; i != MAX_PLAYERS; i++) truelight@0: _players[i].index=i; truelight@0: _cur_player_tick_index = 0; truelight@0: } truelight@0: truelight@0: void OnTick_Players() truelight@0: { truelight@0: Player *p; truelight@0: truelight@0: if (_game_mode == GM_EDITOR) truelight@0: return; truelight@0: truelight@0: p = DEREF_PLAYER(_cur_player_tick_index); truelight@0: _cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS; truelight@0: if (p->name_1 != 0) GenerateCompanyName(p); truelight@0: truelight@0: if (_game_mode != GM_MENU && !--_next_competitor_start) { truelight@0: MaybeStartNewPlayer(); truelight@0: } truelight@0: } truelight@0: truelight@0: void RunOtherPlayersLoop() truelight@0: { truelight@0: Player *p; truelight@0: truelight@0: _is_ai_player = true; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active) { truelight@0: _current_player = p->index; truelight@0: AiDoGameLoop(p); truelight@0: } truelight@0: } truelight@0: truelight@0: _is_ai_player = false; truelight@0: // XXX: is this needed? truelight@0: _current_player = 0; truelight@0: } truelight@0: truelight@0: StringID GetPlayerNameString(byte player) truelight@0: { truelight@0: if (IS_HUMAN_PLAYER(player) && player < 2) // temporarily fixes the names in the list. truelight@0: return STR_7002_PLAYER_1+player; truelight@0: return STR_EMPTY; truelight@0: } truelight@0: truelight@0: extern void ShowPlayerFinances(int player); truelight@0: truelight@0: void PlayersYearlyLoop() truelight@0: { truelight@0: Player *p; truelight@0: truelight@0: // Copy statistics truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active) { truelight@0: memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0])); truelight@0: memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0])); truelight@0: InvalidateWindow(WC_FINANCES, p->index); truelight@0: } truelight@0: } truelight@0: truelight@0: if (_patches.show_finances && _local_player != 0xff) { truelight@0: ShowPlayerFinances(_local_player); truelight@0: p = DEREF_PLAYER(_local_player); truelight@0: if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) { truelight@0: SndPlayFx(0x28); truelight@0: } else { truelight@0: SndPlayFx(0x27); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void DeletePlayerWindows(int pi) truelight@0: { truelight@0: DeleteWindowById(WC_COMPANY, pi); truelight@0: DeleteWindowById(WC_FINANCES, pi); truelight@0: DeleteWindowById(WC_STATION_LIST, pi); truelight@0: DeleteWindowById(WC_TRAINS_LIST, pi); truelight@0: DeleteWindowById(WC_ROADVEH_LIST, pi); truelight@0: DeleteWindowById(WC_SHIPS_LIST, pi); truelight@0: DeleteWindowById(WC_AIRCRAFT_LIST, pi); truelight@0: DeleteWindowById(WC_BUY_COMPANY, pi); truelight@0: } truelight@0: truelight@0: static void DeletePlayerStuff(int pi) truelight@0: { truelight@0: Player *p; truelight@0: truelight@0: DeletePlayerWindows(pi); truelight@0: p = DEREF_PLAYER(pi); truelight@0: DeleteName(p->name_1); truelight@0: DeleteName(p->president_name_1); truelight@0: p->name_1 = 0; truelight@0: p->president_name_1 = 0; truelight@0: } truelight@0: truelight@0: // functionality. truelight@0: // 0 - make new player truelight@0: // 1 - make new AI player truelight@0: // 2 - delete player (p1 >> 8) & 0xFF truelight@0: // 3 - join player (p1 >> 8) & 0xFF with (p1 >> 16) & 0xFF truelight@0: int32 CmdPlayerCtrl(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int pi; truelight@0: Player *p; truelight@0: truelight@0: if (!(flags & DC_EXEC)) truelight@0: return 0; truelight@0: truelight@0: switch(p1 & 0xff) { truelight@0: case 0: // make new player truelight@0: p = DoStartupNewPlayer(false); truelight@0: if (_local_player == 0xff && p != NULL) { truelight@0: _local_player = p->index; truelight@0: MarkWholeScreenDirty(); truelight@0: } truelight@0: break; truelight@0: case 1: // make new ai player truelight@0: DoStartupNewPlayer(true); truelight@0: break; truelight@0: case 2: // delete player truelight@0: pi = (byte)(p1 >> 8); truelight@0: ChangeOwnershipOfPlayerItems(pi, 255); truelight@0: DeletePlayerStuff(pi); truelight@0: break; truelight@0: truelight@0: case 3: // join player truelight@0: pi = (byte)(p1 >> 8); truelight@0: ChangeOwnershipOfPlayerItems(pi, (byte)(p1 >> 16)); truelight@0: DeletePlayerStuff(pi); truelight@0: break; truelight@0: } truelight@0: truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // Save/load of players truelight@0: static const byte _player_desc[] = { truelight@0: SLE_VAR(Player,name_2, SLE_UINT32), truelight@0: SLE_VAR(Player,name_1, SLE_STRINGID), truelight@0: truelight@0: SLE_VAR(Player,president_name_1,SLE_UINT16), truelight@0: SLE_VAR(Player,president_name_2,SLE_UINT32), truelight@0: truelight@0: SLE_VAR(Player,face, SLE_UINT32), truelight@0: truelight@0: // money was changed to a 64 bit field in savegame version 1. truelight@0: SLE_CONDVAR(Player,money64, SLE_VAR_I64 | SLE_FILE_I32, 0, 0), truelight@0: SLE_CONDVAR(Player,money64, SLE_INT64, 1, 255), truelight@0: truelight@0: SLE_VAR(Player,current_loan, SLE_INT32), truelight@0: truelight@0: SLE_VAR(Player,player_color, SLE_UINT8), truelight@0: SLE_VAR(Player,player_money_fraction,SLE_UINT8), truelight@0: SLE_VAR(Player,max_railtype, SLE_UINT8), truelight@0: SLE_VAR(Player,block_preview, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Player,cargo_types, SLE_UINT16), truelight@0: SLE_VAR(Player,location_of_house,SLE_UINT16), truelight@0: SLE_VAR(Player,last_build_coordinate,SLE_UINT16), truelight@0: SLE_VAR(Player,inaugurated_year,SLE_UINT8), truelight@0: truelight@0: SLE_ARR(Player,share_owners, SLE_UINT8, 4), truelight@0: truelight@0: SLE_VAR(Player,num_valid_stat_ent,SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Player,quarters_of_bankrupcy,SLE_UINT8), truelight@0: SLE_VAR(Player,bankrupt_asked, SLE_UINT8), truelight@0: SLE_VAR(Player,bankrupt_timeout,SLE_INT16), truelight@0: SLE_VAR(Player,bankrupt_value, SLE_INT32), truelight@0: truelight@0: // yearly expenses was changed to 64-bit in savegame version 2. truelight@0: SLE_CONDARR(Player,yearly_expenses, SLE_FILE_I32|SLE_VAR_I64, 3*13, 0, 1), truelight@0: SLE_CONDARR(Player,yearly_expenses, SLE_INT64, 3*13, 2, 255), truelight@0: truelight@0: SLE_CONDVAR(Player,is_ai, SLE_UINT8, 2, 255), truelight@0: SLE_CONDVAR(Player,is_active, SLE_UINT8, 4, 255), truelight@0: truelight@0: // reserve extra space in savegame here. (currently 64 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _player_economy_desc[] = { truelight@0: // these were changed to 64-bit in savegame format 2 truelight@0: SLE_CONDVAR(PlayerEconomyEntry,income, SLE_INT32, 0, 1), truelight@0: SLE_CONDVAR(PlayerEconomyEntry,expenses, SLE_INT32, 0, 1), truelight@0: SLE_CONDVAR(PlayerEconomyEntry,company_value, SLE_INT32, 0, 1), truelight@0: SLE_CONDVAR(PlayerEconomyEntry,income, SLE_FILE_I64 | SLE_VAR_I32, 2, 255), truelight@0: SLE_CONDVAR(PlayerEconomyEntry,expenses,SLE_FILE_I64 | SLE_VAR_I32, 2, 255), truelight@0: SLE_CONDVAR(PlayerEconomyEntry,company_value,SLE_FILE_I64 | SLE_VAR_I32, 2, 255), truelight@0: truelight@0: SLE_VAR(PlayerEconomyEntry,delivered_cargo, SLE_INT32), truelight@0: SLE_VAR(PlayerEconomyEntry,performance_history, SLE_INT32), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _player_ai_desc[] = { truelight@0: SLE_VAR(PlayerAI,state, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,tick, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,state_counter, SLE_UINT16), truelight@0: SLE_VAR(PlayerAI,timeout_counter, SLE_UINT16), truelight@0: truelight@0: SLE_VAR(PlayerAI,state_mode, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,banned_tile_count, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,railtype_to_use, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(PlayerAI,cargo_type, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,num_wagons, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,build_kind, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,num_build_rec, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,num_loco_to_build, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,num_want_fullload, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(PlayerAI,route_type_mask, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(PlayerAI,start_tile_a, SLE_UINT16), truelight@0: SLE_VAR(PlayerAI,cur_tile_a, SLE_UINT16), truelight@0: SLE_VAR(PlayerAI,start_dir_a, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,cur_dir_a, SLE_UINT8), truelight@0: truelight@0: SLE_VAR(PlayerAI,start_tile_b, SLE_UINT16), truelight@0: SLE_VAR(PlayerAI,cur_tile_b, SLE_UINT16), truelight@0: SLE_VAR(PlayerAI,start_dir_b, SLE_UINT8), truelight@0: SLE_VAR(PlayerAI,cur_dir_b, SLE_UINT8), truelight@0: truelight@0: SLE_REF(PlayerAI,cur_veh, REF_VEHICLE), truelight@0: truelight@0: SLE_ARR(PlayerAI,wagon_list, SLE_UINT16, 9), truelight@0: SLE_ARR(PlayerAI,order_list_blocks, SLE_UINT8, 20), truelight@0: SLE_ARR(PlayerAI,banned_tiles, SLE_UINT16, 16), truelight@0: truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255), truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _player_ai_build_rec_desc[] = { truelight@0: SLE_VAR(AiBuildRec,spec_tile, SLE_UINT16), truelight@0: SLE_VAR(AiBuildRec,use_tile, SLE_UINT16), truelight@0: SLE_VAR(AiBuildRec,rand_rng, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,cur_building_rule,SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,unk6, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,unk7, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,buildcmd_a, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,buildcmd_b, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,direction, SLE_UINT8), truelight@0: SLE_VAR(AiBuildRec,cargo, SLE_UINT8), truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static void SaveLoad_PLYR(Player *p) { truelight@0: int i; truelight@0: truelight@0: SlObject(p, _player_desc); truelight@0: truelight@0: // Write AI? truelight@0: if (!IS_HUMAN_PLAYER(p->index)) { truelight@0: SlObject(&p->ai, _player_ai_desc); truelight@0: for(i=0; i!=p->ai.num_build_rec; i++) truelight@0: SlObject(&p->ai.src + i, _player_ai_build_rec_desc); truelight@0: } truelight@0: truelight@0: // Write economy truelight@0: SlObject(&p->cur_economy, _player_economy_desc); truelight@0: truelight@0: // Write old economy entries. truelight@0: { truelight@0: PlayerEconomyEntry *pe; truelight@0: for(i=p->num_valid_stat_ent,pe=p->old_economy; i!=0; i--,pe++) truelight@0: SlObject(pe, _player_economy_desc); truelight@0: } truelight@0: } truelight@0: truelight@0: static void Save_PLYR() truelight@0: { truelight@0: Player *p; truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active) { truelight@0: SlSetArrayIndex(p->index); truelight@0: SlAutolength((AutolengthProc*)SaveLoad_PLYR, p); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void Load_PLYR() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { truelight@0: Player *p = DEREF_PLAYER(index); truelight@0: p->is_ai = (index != 0); truelight@0: SaveLoad_PLYR(p); truelight@0: _player_colors[index] = p->player_color; truelight@0: UpdatePlayerMoney32(p); truelight@0: } truelight@0: } truelight@0: truelight@0: const ChunkHandler _player_chunk_handlers[] = { truelight@0: { 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST}, truelight@0: }; truelight@0: