From e1fb94c398b1d981eb1caa5ff03e9a3810d5d253 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 21 Jan 2023 23:28:03 +0200 Subject: [PATCH 43/43] Split is_military_unit() by semantic purpose See osdn #46583 Signed-off-by: Marko Lindqvist --- ai/default/aiunit.c | 6 ++--- ai/default/daimilitary.c | 5 ++-- client/control.c | 4 ++-- common/city.c | 4 ++-- common/unit.c | 42 ++++++++++++++++++++++++++++++---- common/unit.h | 6 ++++- server/advisors/autosettlers.c | 2 +- server/report.c | 4 ++-- server/score.c | 4 ++-- server/srv_main.c | 8 ++++--- 10 files changed, 62 insertions(+), 23 deletions(-) diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c index 334461fef1..26b9d0ee0b 100644 --- a/ai/default/aiunit.c +++ b/ai/default/aiunit.c @@ -215,7 +215,7 @@ static void dai_airlift(struct ai_type *ait, struct player *pplayer) /**********************************************************************//** This is a much simplified form of assess_defense (see daimilitary.c), but which doesn't use pcity->server.ai.wallvalue and just returns a boolean - value. This is for use with "foreign" cities, especially non-ai + value. This is for use with "foreign" cities, especially non-ai cities, where ai.wallvalue may be out of date or uninitialized --dwp **************************************************************************/ static bool has_defense(struct city *pcity) @@ -223,7 +223,7 @@ static bool has_defense(struct city *pcity) struct tile *ptile = city_tile(pcity); unit_list_iterate(ptile->units, punit) { - if (is_military_unit(punit) && base_get_defense_power(punit) != 0 + if (is_guard_unit(punit) && base_get_defense_power(punit) != 0 && punit->hp != 0) { struct unit_class *pclass = unit_class_get(punit); @@ -2697,7 +2697,7 @@ void dai_manage_unit(struct ai_type *ait, struct player *pplayer, unit_data->done = TRUE; /* we did our best, which was ... nothing */ return; - } else if (is_military_unit(punit)) { + } else if (!is_special_unit(punit)) { TIMING_LOG(AIT_MILITARY, TIMER_START); UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military"); dai_manage_military(ait, pplayer, punit); diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c index 8c1c32ce33..d8aa4e39e9 100644 --- a/ai/default/daimilitary.c +++ b/ai/default/daimilitary.c @@ -229,7 +229,7 @@ static int base_assess_defense_unit(struct city *pcity, struct unit *punit, int defense; int fp; - if (!is_military_unit(punit)) { + if (!is_special_unit(punit)) { return 0; } @@ -1507,7 +1507,8 @@ static struct adv_choice *kill_something_with(struct ai_type *ait, best_choice->type = CT_ATTACKER; adv_choice_set_use(best_choice, "attacker"); - fc_assert_ret_val(is_military_unit(myunit) && !utype_fuel(unit_type_get(myunit)), choice); + fc_assert_ret_val(!is_special_unit(myunit) + && !utype_fuel(unit_type_get(myunit)), choice); if (city_data->danger != 0 && assess_defense(ait, pcity) == 0) { /* Defense comes first! */ diff --git a/client/control.c b/client/control.c index aa613a8ec2..a94da7ea55 100644 --- a/client/control.c +++ b/client/control.c @@ -2978,10 +2978,10 @@ static struct unit *quickselect(struct tile *ptile, } else if (qtype == SELECT_LAND) { if (utype_move_type(unit_type_get(punit)) == UMT_LAND) { if (punit->moves_left > 0) { - if (is_military_unit(punit)) { + if (!is_special_unit(punit)) { return punit; } else if (!panymoveland) { - panymoveland = punit; + panymoveland = punit; } } else if (!panyland) { panyland = punit; diff --git a/common/city.c b/common/city.c index a4fa280679..933f827ee8 100644 --- a/common/city.c +++ b/common/city.c @@ -3011,7 +3011,7 @@ static inline void city_support(struct city *pcity) /* Food consumption by citizens. */ pcity->usage[O_FOOD] += game.info.food_cost * city_size_get(pcity); - /* military units in this city (need _not_ be home city) can make + /* Military units in this city (need _not_ be home city) can make * unhappy citizens content */ martial_law_each = get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH); if (martial_law_each > 0) { @@ -3020,7 +3020,7 @@ static inline void city_support(struct city *pcity) unit_list_iterate(pcity->tile->units, punit) { if ((count < martial_law_max || martial_law_max == 0) - && is_military_unit(punit) + && is_martial_law_unit(punit) && unit_owner(punit) == city_owner(pcity)) { count++; } diff --git a/common/unit.c b/common/unit.c index 966c5cce05..c677802ff9 100644 --- a/common/unit.c +++ b/common/unit.c @@ -312,15 +312,47 @@ bool is_attack_unit(const struct unit *punit) } /**********************************************************************//** - Military units are capable of enforcing martial law. Military ground - and heli units can occupy empty cities -- see unit_can_take_over(punit). - Some military units, like the Galleon, have no attack strength. + Is unit capable of enforcing martial law? **************************************************************************/ -bool is_military_unit(const struct unit *punit) +bool is_martial_law_unit(const struct unit *punit) { return !unit_has_type_flag(punit, UTYF_CIVILIAN); } +/**********************************************************************//** + Does unit occupy the tile? +**************************************************************************/ +bool is_occupying_unit(const struct unit *punit) +{ + return !unit_has_type_flag(punit, UTYF_CIVILIAN); +} + +/**********************************************************************//** + Can this unit enter peaceful borders? +**************************************************************************/ +bool is_enter_borders_unit(const struct unit *punit) +{ + return unit_has_type_flag(punit, UTYF_CIVILIAN); +} + +/**********************************************************************//** + Does it make sense for this unit to protect others? +**************************************************************************/ +bool is_guard_unit(const struct unit *punit) +{ + return !unit_has_type_flag(punit, UTYF_CIVILIAN); +} + +/**********************************************************************//** + Does it make sense to use this unit for some special role? + + If yes, don't waste it as cannon fodder. +**************************************************************************/ +bool is_special_unit(const struct unit *punit) +{ + return unit_has_type_flag(punit, UTYF_CIVILIAN); +} + /**********************************************************************//** Return TRUE iff this unit can do the specified generalized (ruleset defined) action enabler controlled action. @@ -1345,7 +1377,7 @@ struct unit *unit_occupies_tile(const struct tile *ptile, const struct player *pplayer) { unit_list_iterate(ptile->units, punit) { - if (!is_military_unit(punit)) { + if (!is_occupying_unit(punit)) { continue; } diff --git a/common/unit.h b/common/unit.h index b87435706f..e5aa5beca4 100644 --- a/common/unit.h +++ b/common/unit.h @@ -355,7 +355,11 @@ bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile); bv_extras get_unit_tile_pillage_set(const struct tile *ptile); bool is_attack_unit(const struct unit *punit); -bool is_military_unit(const struct unit *punit); /* !set !dip !cara */ +bool is_martial_law_unit(const struct unit *punit); +bool is_occupying_unit(const struct unit *punit); +bool is_enter_borders_unit(const struct unit *punit); +bool is_guard_unit(const struct unit *punit); +bool is_special_unit(const struct unit *punit); bool unit_can_do_action(const struct unit *punit, const action_id act_id); bool unit_can_do_action_result(const struct unit *punit, diff --git a/server/advisors/autosettlers.c b/server/advisors/autosettlers.c index e67879b1b4..76cd219009 100644 --- a/server/advisors/autosettlers.c +++ b/server/advisors/autosettlers.c @@ -1129,7 +1129,7 @@ bool adv_settler_safe_tile(const struct player *pplayer, struct unit *punit, struct tile *ptile) { unit_list_iterate(ptile->units, defender) { - if (is_military_unit(defender)) { + if (is_guard_unit(defender)) { return TRUE; } } unit_list_iterate_end; diff --git a/server/report.c b/server/report.c index 5cdcd07e22..44f5bf6ea1 100644 --- a/server/report.c +++ b/server/report.c @@ -699,9 +699,9 @@ static int get_munits(const struct player *pplayer) { int result = 0; - /* count up military units */ + /* Count up military units */ unit_list_iterate(pplayer->units, punit) { - if (is_military_unit(punit)) { + if (!is_special_unit(punit)) { result++; } } unit_list_iterate_end; diff --git a/server/score.c b/server/score.c index bfb79a5853..f405337658 100644 --- a/server/score.c +++ b/server/score.c @@ -317,10 +317,10 @@ void calc_civ_score(struct player *pplayer) pplayer->score.techs += research_get(pplayer)->future_tech * 5 / 2; unit_list_iterate(pplayer->units, punit) { - if (is_military_unit(punit)) { + if (!is_special_unit(punit)) { /* TODO: Which units really should count? */ pplayer->score.units++; } - } unit_list_iterate_end + } unit_list_iterate_end; improvement_iterate(i) { if (is_great_wonder(i) diff --git a/server/srv_main.c b/server/srv_main.c index 8265393f1d..2352c3abf0 100644 --- a/server/srv_main.c +++ b/server/srv_main.c @@ -826,11 +826,13 @@ static void notify_illegal_armistice_units(struct player *phost, struct unit *a_unit = NULL; unit_list_iterate(pguest->units, punit) { - if (tile_owner(unit_tile(punit)) == phost && is_military_unit(punit)) { + if (tile_owner(unit_tile(punit)) == phost + && !is_enter_borders_unit(punit)) { nunits++; a_unit = punit; } } unit_list_iterate_end; + if (nunits > 0) { struct astring unitstr = ASTRING_INIT; @@ -863,7 +865,7 @@ static void remove_illegal_armistice_units(struct player *plr1, /* Remove illegal units */ unit_list_iterate_safe(plr1->units, punit) { if (tile_owner(unit_tile(punit)) == plr2 - && is_military_unit(punit)) { + && !is_enter_borders_unit(punit)) { notify_player(plr1, unit_tile(punit), E_DIPLOMACY, ftc_server, _("Your %s was disbanded in accordance with " "your peace treaty with the %s."), @@ -874,7 +876,7 @@ static void remove_illegal_armistice_units(struct player *plr1, } unit_list_iterate_safe_end; unit_list_iterate_safe(plr2->units, punit) { if (tile_owner(unit_tile(punit)) == plr1 - && is_military_unit(punit)) { + && !is_enter_borders_unit(punit)) { notify_player(plr2, unit_tile(punit), E_DIPLOMACY, ftc_server, _("Your %s was disbanded in accordance with " "your peace treaty with the %s."), -- 2.39.0