Skip to content

Commit

Permalink
Remove function player_slot_count()
Browse files Browse the repository at this point in the history
It always returns a constant
  • Loading branch information
psampathkumar authored and lmoureaux committed Jun 29, 2022
1 parent 2081c13 commit 224ab12
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 64 deletions.
2 changes: 1 addition & 1 deletion ai/default/aidata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void dai_data_init(struct ai_type *ait, struct player *pplayer)
ai->last_num_oceans = -1;

ai->diplomacy.player_intel_slots =
new const ai_dip_intel *[player_slot_count()]();
new const ai_dip_intel *[MAX_NUM_PLAYER_SLOTS]();
player_slots_iterate(pslot)
{
const struct ai_dip_intel **player_intel_slot =
Expand Down
2 changes: 1 addition & 1 deletion ai/default/daidiplomacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ void dai_diplomacy_begin_new_phase(struct ai_type *ait,
{
struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
struct adv_data *adv = adv_data_get(pplayer, nullptr);
int war_desire[player_slot_count()];
int war_desire[MAX_NUM_PLAYER_SLOTS];
int best_desire = 0;
struct player *best_target = nullptr;

Expand Down
21 changes: 7 additions & 14 deletions common/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ void player_slots_init()
int i;

// Init player slots.
player_slots.pslots = new player_slot[player_slot_count()]();
player_slots.pslots = new player_slot[MAX_NUM_PLAYER_SLOTS]();
/* Can't use the defined functions as the needed data will be
* defined here. */
for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
player_slots.pslots[i].player = nullptr;
}
player_slots.used_slots = 0;
Expand Down Expand Up @@ -363,17 +363,10 @@ struct player_slot *player_slot_first() { return player_slots.pslots; }
struct player_slot *player_slot_next(struct player_slot *pslot)
{
pslot++;
return (pslot < player_slots.pslots + player_slot_count() ? pslot
: nullptr);
return (pslot < player_slots.pslots + MAX_NUM_PLAYER_SLOTS ? pslot
: nullptr);
}

/**
Returns the total number of player slots, i.e. the maximum
number of players (including barbarians, etc.) that could ever
exist at once.
*/
int player_slot_count() { return (MAX_NUM_PLAYER_SLOTS); }

/**
Returns the index of the player slot.
*/
Expand Down Expand Up @@ -417,7 +410,7 @@ bool player_slot_is_used(const struct player_slot *pslot)
struct player_slot *player_slot_by_number(int player_id)
{
if (!player_slots_initialised()
|| !(0 <= player_id && player_id < player_slot_count())) {
|| !(0 <= player_id && player_id < MAX_NUM_PLAYER_SLOTS)) {
return nullptr;
}

Expand Down Expand Up @@ -472,7 +465,7 @@ struct player *player_new(struct player_slot *pslot)
pplayer = new player();
pplayer->slot = pslot;
pslot->player = pplayer;
pplayer->diplstates = new const player_diplstate *[player_slot_count()]();
pplayer->diplstates = new const player_diplstate *[MAX_NUM_PLAYER_SLOTS]();

player_slots_iterate(dslot)
{
Expand Down Expand Up @@ -858,7 +851,7 @@ struct player *player_by_name_prefix(const char *name,
{
int ind;

*result = match_prefix(player_name_by_number, player_slot_count(),
*result = match_prefix(player_name_by_number, MAX_NUM_PLAYER_SLOTS,
MAX_LEN_NAME - 1, fc_strncasequotecmp,
effectivestrlenquote, name, &ind);

Expand Down
2 changes: 0 additions & 2 deletions common/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,6 @@ void player_slots_free();
struct player_slot *player_slot_first();
struct player_slot *player_slot_next(struct player_slot *pslot);

// A player slot contains a possibly uninitialized player.
int player_slot_count();
int player_slot_index(const struct player_slot *pslot);
struct player *player_slot_get_player(const struct player_slot *pslot);
bool player_slot_is_used(const struct player_slot *pslot);
Expand Down
2 changes: 1 addition & 1 deletion common/research.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void researches_init()

// Ensure we have enough space for players or teams.
fc_assert(ARRAY_SIZE(research_array) >= team_slot_count());
fc_assert(ARRAY_SIZE(research_array) >= player_slot_count());
fc_assert(ARRAY_SIZE(research_array) >= MAX_NUM_PLAYER_SLOTS);

memset(research_array, 0, sizeof(research_array));
for (i = 0; i < ARRAY_SIZE(research_array); i++) {
Expand Down
2 changes: 1 addition & 1 deletion server/advisors/advdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void adv_data_init(struct player *pplayer)

adv->government_want = nullptr;

adv->dipl.adv_dipl_slots = new adv_dipl *[player_slot_count()]();
adv->dipl.adv_dipl_slots = new adv_dipl *[MAX_NUM_PLAYER_SLOTS]();
player_slots_iterate(pslot)
{
struct adv_dipl **dip_slot =
Expand Down
2 changes: 1 addition & 1 deletion server/connecthand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ static void package_conn_info(struct connection *pconn,
packet->established = pconn->established;
packet->player_num = (nullptr != pconn->playing)
? player_number(pconn->playing)
: player_slot_count();
: MAX_NUM_PLAYER_SLOTS;
packet->observer = pconn->observer;
packet->access_level = pconn->access_level;

Expand Down
8 changes: 4 additions & 4 deletions server/edithand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void edithand_init()
need_continents_reassigned = false;

delete[] unfogged_players;
unfogged_players = new bool[player_slot_count()]();
unfogged_players = new bool[MAX_NUM_PLAYER_SLOTS]();
}

/**
Expand Down Expand Up @@ -163,7 +163,7 @@ static void check_leaving_edit_mode()
players_iterate_end;

// Clear the whole array.
memset(unfogged_players, 0, player_slot_count() * sizeof(bool));
memset(unfogged_players, 0, MAX_NUM_PLAYER_SLOTS * sizeof(bool));

check_edited_tile_terrains();
conn_list_do_unbuffer(game.est_connections);
Expand Down Expand Up @@ -882,11 +882,11 @@ void handle_edit_player_create(struct connection *pc, int tag)
struct nation_type *pnation;
struct research *presearch;

if (player_count() >= player_slot_count()) {
if (player_count() >= MAX_NUM_PLAYER_SLOTS) {
notify_conn(pc->self, nullptr, E_BAD_COMMAND, ftc_editor,
_("No more players can be added because the maximum "
"number of players (%d) has been reached."),
player_slot_count());
MAX_NUM_PLAYER_SLOTS);
return;
}

Expand Down
5 changes: 3 additions & 2 deletions server/gamehand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
\_____/ / If not, see https://www.gnu.org/licenses/.
\____/ ********************************************************/

#include <QtWidgets/QtWidgets>
#include <cstdio> // for remove()

// utility
Expand Down Expand Up @@ -415,8 +416,8 @@ static void do_team_placement(const struct team_placement_config *pconfig,
void init_new_game()
{
struct startpos_list *impossible_list, *targeted_list, *flexible_list;
struct tile *player_startpos[player_slot_count()];
int placed_units[player_slot_count()];
struct tile *player_startpos[MAX_NUM_PLAYER_SLOTS];
int placed_units[MAX_NUM_PLAYER_SLOTS];
int players_to_place = player_count();
int sulen;

Expand Down
4 changes: 2 additions & 2 deletions server/maphand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ static void create_vision_dependencies()
*/
void give_shared_vision(struct player *pfrom, struct player *pto)
{
bv_player save_vision[player_slot_count()];
bv_player save_vision[MAX_NUM_PLAYER_SLOTS];
if (pfrom == pto) {
return;
}
Expand Down Expand Up @@ -1678,7 +1678,7 @@ void give_shared_vision(struct player *pfrom, struct player *pto)
*/
void remove_shared_vision(struct player *pfrom, struct player *pto)
{
bv_player save_vision[player_slot_count()];
bv_player save_vision[MAX_NUM_PLAYER_SLOTS];

fc_assert_ret(pfrom != pto);
if (!gives_shared_vision(pfrom, pto)) {
Expand Down
10 changes: 5 additions & 5 deletions server/plrhand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ static void package_player_common(struct player *plr,
packet->is_connected = plr->is_connected;
packet->flags = plr->flags;
packet->ai_skill_level = is_ai(plr) ? plr->ai_common.skill_level : 0;
for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
packet->love[i] = plr->ai_common.love[i];
}
packet->barbarian_type = plr->ai_common.barbarian_type;
Expand Down Expand Up @@ -2020,7 +2020,7 @@ bool server_player_set_name_full(const struct connection *caller,
// Try to append a number to 'real_name'.
char test[MAX_LEN_NAME];

for (i = 2; i <= player_slot_count(); i++) {
for (i = 2; i <= MAX_NUM_PLAYER_SLOTS; i++) {
fc_snprintf(test, sizeof(test), "%s%d", real_name, i);
if (server_player_name_is_allowed(caller, pplayer, pnation, test,
error_buf, error_buf_len)) {
Expand Down Expand Up @@ -2050,7 +2050,7 @@ bool server_player_set_name_full(const struct connection *caller,
}

// Try a very default name...
for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
fc_snprintf(real_name, sizeof(real_name), _("Player no. %d"), i);
if (server_player_name_is_allowed(caller, pplayer, pnation, real_name,
error_buf, error_buf_len)) {
Expand Down Expand Up @@ -2188,7 +2188,7 @@ void maybe_make_contact(struct tile *ptile, struct player *pplayer)
void shuffle_players()
{
// shuffled_order is defined global
int n = player_slot_count();
int n = MAX_NUM_PLAYER_SLOTS;
int i;

log_debug("shuffle_players: creating shuffled order");
Expand Down Expand Up @@ -2217,7 +2217,7 @@ void set_shuffled_players(int *shuffled_players)
log_debug("set_shuffled_players: loading shuffled array %p",
shuffled_players);

for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
shuffled_order[i] = shuffled_players[i];
log_debug("shuffled_order[%d] = %d", i, shuffled_order[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion server/plrhand.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void reset_all_start_commands(bool plrchange);
struct player *NAME_pplayer; \
log_debug("shuffled_players_iterate @ %s line %d", __FILE__, \
__FC_LINE__); \
for (MY_i = 0; MY_i < player_slot_count(); MY_i++) { \
for (MY_i = 0; MY_i < MAX_NUM_PLAYER_SLOTS; MY_i++) { \
NAME_pplayer = shuffled_player(MY_i); \
if (NAME_pplayer != nullptr) {

Expand Down
6 changes: 3 additions & 3 deletions server/report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ static bool scan_score_log(char *id)
log_debug("add player '%s' (from line %d: '%s')", plr_name, line_nr,
line);

if (0 > plr_no || plr_no >= player_slot_count()) {
if (0 > plr_no || plr_no >= MAX_NUM_PLAYER_SLOTS) {
qCritical("[%s:%d] Invalid player number: %d!",
game.server.scorefile, line_nr, plr_no);
return false;
Expand All @@ -1243,7 +1243,7 @@ static bool scan_score_log(char *id)
return false;
}

if (!(plr_no >= 0 && plr_no < player_slot_count())) {
if (!(plr_no >= 0 && plr_no < MAX_NUM_PLAYER_SLOTS)) {
qCritical("[%s:%d] Invalid player number: %d!",
game.server.scorefile, line_nr, plr_no);
return false;
Expand Down Expand Up @@ -1291,7 +1291,7 @@ void log_civ_score_init()
score_log = new logging_civ_score[1]();
score_log->fp = nullptr;
score_log->last_turn = -1;
score_log->plrdata = new plrdata_slot[player_slot_count()]();
score_log->plrdata = new plrdata_slot[MAX_NUM_PLAYER_SLOTS]();
player_slots_iterate(pslot)
{
struct plrdata_slot *plrdata =
Expand Down
2 changes: 1 addition & 1 deletion server/sanitycheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static void check_misc(const char *file, const char *function, int line)
SANITY_CHECK(nplayers == player_count());
SANITY_CHECK(nbarbs == server.nbarbarians);

SANITY_CHECK(player_count() <= player_slot_count());
SANITY_CHECK(player_count() <= MAX_NUM_PLAYER_SLOTS);
SANITY_CHECK(team_count() <= MAX_NUM_TEAM_SLOTS);
SANITY_CHECK(normal_player_count() <= game.server.max_players);
}
Expand Down
2 changes: 1 addition & 1 deletion server/savegame/savecompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ static void compat_load_020600(struct loaddata *loading,
{"now_name", ENTRY_STR}, {"got_tech", ENTRY_BOOL},
{"done", ENTRY_STR}};

int researches[MAX(player_slot_count(), team_slot_count())];
int researches[MAX(MAX_NUM_PLAYER_SLOTS, team_slot_count())];
int count = 0;
int i;

Expand Down
8 changes: 4 additions & 4 deletions server/savegame/savegame2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2489,8 +2489,8 @@ static void sg_load_players_basic(struct loaddata *loading)
if (secfile_lookup_int_default(loading->file, -1,
"players.shuffled_player_%d", 0)
>= 0) {
int shuffled_players[player_slot_count()];
bool shuffled_player_set[player_slot_count()];
int shuffled_players[MAX_NUM_PLAYER_SLOTS];
bool shuffled_player_set[MAX_NUM_PLAYER_SLOTS];

player_slots_iterate(pslot)
{
Expand Down Expand Up @@ -2533,14 +2533,14 @@ static void sg_load_players_basic(struct loaddata *loading)
// Insert missing numbers.
int shuffle_index = player_count();

for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
if (!shuffled_player_set[i]) {
shuffled_players[shuffle_index] = i;
shuffle_index++;
}
/* shuffle_index must not grow behind the size of shuffled_players.
*/
sg_failure_ret(shuffle_index <= player_slot_count(),
sg_failure_ret(shuffle_index <= MAX_NUM_PLAYER_SLOTS,
"Invalid player shuffle data!");
}

Expand Down
8 changes: 4 additions & 4 deletions server/savegame/savegame3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3499,8 +3499,8 @@ static void sg_load_players_basic(struct loaddata *loading)
if (secfile_lookup_int_default(loading->file, -1,
"players.shuffled_player_%d", 0)
>= 0) {
int shuffled_players[player_slot_count()];
bool shuffled_player_set[player_slot_count()];
int shuffled_players[MAX_NUM_PLAYER_SLOTS];
bool shuffled_player_set[MAX_NUM_PLAYER_SLOTS];

player_slots_iterate(pslot)
{
Expand Down Expand Up @@ -3543,14 +3543,14 @@ static void sg_load_players_basic(struct loaddata *loading)
// Insert missing numbers.
int shuffle_index = player_count();

for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
if (!shuffled_player_set[i]) {
shuffled_players[shuffle_index] = i;
shuffle_index++;
}
/* shuffle_index must not grow behind the size of shuffled_players.
*/
sg_failure_ret(shuffle_index <= player_slot_count(),
sg_failure_ret(shuffle_index <= MAX_NUM_PLAYER_SLOTS,
"Invalid player shuffle data!");
}

Expand Down
4 changes: 2 additions & 2 deletions server/score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void rank_users(bool interrupt)
FILE *fp;
int i, t_winner_score = 0;
enum victory_state { VS_NONE, VS_LOSER, VS_WINNER };
enum victory_state plr_state[player_slot_count()];
enum victory_state plr_state[MAX_NUM_PLAYER_SLOTS];
struct player *spacerace_winner = nullptr;
struct team *t_winner = nullptr;

Expand All @@ -443,7 +443,7 @@ void rank_users(bool interrupt)
}

// initialize plr_state
for (i = 0; i < player_slot_count(); i++) {
for (i = 0; i < MAX_NUM_PLAYER_SLOTS; i++) {
plr_state[i] = VS_NONE;
}

Expand Down
2 changes: 1 addition & 1 deletion server/srv_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@ const char *aifill(int amount)
}

if (limit < player_count()) {
int removal = player_slot_count() - 1;
int removal = MAX_NUM_PLAYER_SLOTS - 1;

while (limit < player_count() && 0 <= removal) {
struct player *pplayer = player_by_number(removal);
Expand Down
Loading

0 comments on commit 224ab12

Please sign in to comment.