/***************************************************************************** The number of citizens for the player in the city. *****************************************************************************/ static const char *col_citizens(const struct city *pcity, const struct player_slot *pslot) { citizens nationality = citizens_nation_get(pcity, pslot); if (nationality == 0) { return "-"; } else { static char buf[8]; fc_snprintf(buf, sizeof(buf), "%d", nationality); return buf; } }
void citizens_update(struct city *pcity) { int delta; fc_assert_ret(pcity); if (pcity->server.debug) { /* before */ citizens_print(pcity); } if (game.info.citizen_nationality != TRUE) { return; } if (pcity->nationality == NULL) { /* If nationalities are not set (virtual cities) do nothing. */ return; } delta = city_size_get(pcity) - citizens_count(pcity); if (delta == 0) { /* No change of the city size */ return; } if (delta > 0) { /* Add new citizens with the nationality of the current owner. */ citizens_nation_add(pcity, city_owner(pcity)->slot, delta); log_citizens_add(pcity, delta, city_owner(pcity)); } else { /* Removed citizens. */ struct player_slot *city_nations[MAX_NUM_PLAYER_SLOTS]; int count = 0; /* Create a list of foreign nationalities. */ citizens_foreign_iterate(pcity, pslot, nationality) { city_nations[count] = pslot; count++; } citizens_foreign_iterate_end; /* First remove from foreign nationalities. */ while (count > 0 && delta < 0) { int select = fc_rand(count); struct player_slot *pslot = city_nations[select]; struct player *pplayer = player_slot_get_player(pslot); citizens nationality = citizens_nation_get(pcity, pslot); fc_assert_ret(nationality != 0); fc_assert_ret(pplayer != NULL); if (nationality == 1) { /* Remove one citizen. */ delta++; citizens_nation_set(pcity, pslot, 0); /* Remove this nation from the list of nationalities. */ if (select != count) { city_nations[select] = city_nations[count - 1]; } count--; log_citizens_add(pcity, -1, pplayer); } else { /* Get the minimal reduction = the maximum value of two negative * numbers. */ int diff = MAX(delta, - nationality / 2); delta -= diff; citizens_nation_add(pcity, pslot, diff); log_citizens_add(pcity, diff, pplayer); } } if (delta < 0) { /* Now take the remaining citizens loss from the nation of the owner. */ citizens_nation_add(pcity, city_owner(pcity)->slot, delta); log_citizens_add(pcity, delta, city_owner(pcity)); } }