Skip to content

Commit

Permalink
Factor out unit_attack_civilian_casualties().
Browse files Browse the repository at this point in the history
Factor out the civilian casualties from attacking or bombarding units in a
city to a new function.

Ported from: freeciv/freeciv@cac8000
  • Loading branch information
psampathkumar authored and lmoureaux committed Aug 10, 2022
1 parent 29c2967 commit eff7751
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions server/unithand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,25 @@ static void send_combat(struct unit *pattacker, struct unit *pdefender,
conn_list_iterate_end;
}

/**
Reduce the city's population after an attack action.
*/
static void unit_attack_civilian_casualties(const struct unit *punit,
struct city *pcity,
const struct action *paction,
const char *reason)
{
struct player *pplayer = unit_owner(punit);

if (pcity && city_size_get(pcity) > 1
&& get_city_bonus(pcity, EFT_UNIT_NO_LOSE_POP) <= 0
&& kills_citizen_after_attack(punit)) {
city_reduce_size(pcity, 1, pplayer, reason);
city_refresh(pcity);
send_city_info(nullptr, pcity);
}
}

/**
This function assumes the bombard is legal. The calling function should
have already made all necessary checks.
Expand Down Expand Up @@ -3678,14 +3697,7 @@ static bool unit_bombard(struct unit *punit, struct tile *ptile,

unit_did_action(punit);
unit_forget_last_activity(punit);

if (pcity && city_size_get(pcity) > 1
&& get_city_bonus(pcity, EFT_UNIT_NO_LOSE_POP) <= 0
&& kills_citizen_after_attack(punit)) {
city_reduce_size(pcity, 1, pplayer, "bombard");
city_refresh(pcity);
send_city_info(nullptr, pcity);
}
unit_attack_civilian_casualties(punit, pcity, paction, "bombard");

send_unit_info(nullptr, punit);

Expand Down Expand Up @@ -3965,13 +3977,8 @@ static bool do_attack(struct unit *punit, struct tile *def_tile,
action_consequence_success(paction, pplayer, unit_owner(pdefender),
def_tile, unit_link(pdefender));

if (pdefender->hp <= 0 && (pcity = tile_city(def_tile))
&& city_size_get(pcity) > 1
&& get_city_bonus(pcity, EFT_UNIT_NO_LOSE_POP) <= 0
&& kills_citizen_after_attack(punit)) {
city_reduce_size(pcity, 1, pplayer, "attack");
city_refresh(pcity);
send_city_info(nullptr, pcity);
if (pdefender->hp <= 0 && (pcity = tile_city(def_tile))) {
unit_attack_civilian_casualties(punit, pcity, paction, "attack");
}
if (punit->hp > 0 && pdefender->hp > 0) {
// Neither died
Expand Down

0 comments on commit eff7751

Please sign in to comment.