From 3d296d1c790e0acf19d48ad62a4b3aa768498ada Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Fri, 19 Feb 2021 17:16:21 +0100 Subject: [PATCH] Introduce action_is_in_use() Introduce the new function action_is_in_use() and make use of it. See osdn #41611 --- client/helpdata.c | 6 +++-- common/actions.c | 57 ++++++++++++++++++++++++++++++++++++++- common/actions.h | 2 ++ tools/ruleutil/rulesave.c | 29 +++++++++++--------- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/client/helpdata.c b/client/helpdata.c index 2879fa9511..15628b968f 100644 --- a/client/helpdata.c +++ b/client/helpdata.c @@ -1490,6 +1490,7 @@ char *helptext_building(char *buf, size_t bufsz, struct player *pplayer, /* Building protects against action. */ action_iterate(act) { + struct action *paction = action_by_number(act); /* Nothing is found yet. */ bool vulnerable = FALSE; enum req_range min_range = REQ_RANGE_COUNT; @@ -1499,7 +1500,7 @@ char *helptext_building(char *buf, size_t bufsz, struct player *pplayer, continue; } - if (action_enabler_list_size(action_enablers_for_action(act)) == 0) { + if (!action_is_in_use(paction)) { /* This action isn't enabled at all. */ continue; } @@ -2905,6 +2906,7 @@ char *helptext_unit(char *buf, size_t bufsz, struct player *pplayer, } } action_iterate_end; action_iterate(act) { + struct action *paction = action_by_number(act); bool vulnerable; if (action_by_number(act)->quiet) { @@ -2920,7 +2922,7 @@ char *helptext_unit(char *buf, size_t bufsz, struct player *pplayer, } /* All units are immune to this since its not enabled */ - if (action_enabler_list_size(action_enablers_for_action(act)) == 0) { + if (!action_is_in_use(paction)) { continue; } diff --git a/common/actions.c b/common/actions.c index 75fbf57b97..9ed9f6a034 100644 --- a/common/actions.c +++ b/common/actions.c @@ -6526,8 +6526,10 @@ int action_dice_roll_odds(const struct player *act_player, **************************************************************************/ bool action_immune_government(struct government *gov, action_id act) { + struct action *paction = action_by_number(act); + /* Always immune since its not enabled. Doesn't count. */ - if (action_enabler_list_size(action_enablers_for_action(act)) == 0) { + if (!action_is_in_use(paction)) { return FALSE; } @@ -6661,6 +6663,59 @@ bool action_mp_full_makes_legal(const struct unit *actor, unit_move_rate(actor)); } +/**********************************************************************//** + Returns TRUE iff the specified action has an actor that fulfills its + hard requirements in the current ruleset. + @param paction the action to check + @returns TRUE if the action's hard requirement may be fulfilled in + the current ruleset. +**************************************************************************/ +static bool action_has_possible_actor_hard_reqs(struct action *paction) +{ + switch (action_get_actor_kind(paction)) { + case AAK_UNIT: + unit_type_iterate(putype) { + if (action_actor_utype_hard_reqs_ok(paction, putype)) { + return TRUE; + } + } unit_type_iterate_end; + break; + case AAK_COUNT: + fc_assert(action_get_actor_kind(paction) != AAK_COUNT); + break; + } + + /* No actor detected. */ + return FALSE; +} + +/**********************************************************************//** + Returns TRUE if the specified action may be enabled in the current + ruleset. + @param paction the action to check if is in use. + @returns TRUE if the action could be enabled in the current ruleset. +**************************************************************************/ +bool action_is_in_use(struct action *paction) +{ + struct action_enabler_list *enablers; + + if (!action_has_possible_actor_hard_reqs(paction)) { + /* Hard requirements not fulfilled. */ + return FALSE; + } + + enablers = action_enablers_for_action(paction->id); + + action_enabler_list_iterate(enablers, ae) { + if (!ae->disabled) { + return TRUE; + } + } action_enabler_list_iterate_end; + + /* No non deleted action enabler. */ + return FALSE; +} + /**********************************************************************//** Returns action auto performer rule slot number num so it can be filled. **************************************************************************/ diff --git a/common/actions.h b/common/actions.h index 6bf738b69e..6dee92f033 100644 --- a/common/actions.h +++ b/common/actions.h @@ -900,6 +900,8 @@ bool action_maybe_possible_actor_unit(const action_id wanted_action, bool action_mp_full_makes_legal(const struct unit *actor, const action_id act_id); +bool action_is_in_use(struct action *paction); + /* Action lists */ void action_list_end(action_id *act_list, int size); void action_list_add_all_by_result(action_id *act_list, diff --git a/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c index 14fb632432..1a70765c78 100644 --- a/tools/ruleutil/rulesave.c +++ b/tools/ruleutil/rulesave.c @@ -574,8 +574,9 @@ static bool save_action_auto_actions(struct section_file *sfile, for (i = 0; i < NUM_ACTIONS && auto_perf->alternatives[i] != ACTION_NONE; i++) { - if (action_enabler_list_size(action_enablers_for_action( - auto_perf->alternatives[i])) == 0) { + struct action *paction = action_by_number(auto_perf->alternatives[i]); + + if (!action_is_in_use(paction)) { /* Don't mention non enabled actions. */ continue; } @@ -861,8 +862,10 @@ static bool save_action_range(struct section_file *sfile, action_id act) static bool save_action_kind(struct section_file *sfile, action_id act) { if (action_target_kind_ruleset_var_name(act) != NULL) { + struct action *paction = action_by_number(act); + /* Target kind can be loaded from the ruleset. */ - if (action_enabler_list_size(action_enablers_for_action(act)) == 0) { + if (!action_is_in_use(paction)) { /* Don't save the default for actions that aren't enabled. */ return TRUE; } @@ -884,8 +887,10 @@ static bool save_action_actor_consuming_always(struct section_file *sfile, action_id act) { if (action_actor_consuming_always_ruleset_var_name(act) != NULL) { + struct action *paction = action_by_number(act); + /* Actor consumption can be loaded from the ruleset. */ - if (action_enabler_list_size(action_enablers_for_action(act)) == 0) { + if (!action_is_in_use(paction)) { /* Don't save value for actions that aren't enabled. */ return TRUE; } @@ -916,8 +921,7 @@ static bool save_action_blocked_by(struct section_file *sfile, return TRUE; } - if (action_enabler_list_size(action_enablers_for_action(paction->id)) - == 0) { + if (!action_is_in_use(paction)) { /* Don't save value for actions that aren't enabled. */ return TRUE; } @@ -927,8 +931,9 @@ static bool save_action_blocked_by(struct section_file *sfile, action_rule_name(paction)); action_iterate(blocker_id) { - if (action_enabler_list_size(action_enablers_for_action(blocker_id)) - == 0) { + struct action *pblocker = action_by_number(blocker_id); + + if (!action_is_in_use(pblocker)) { /* Don't save value for actions that aren't enabled. */ continue; } @@ -967,8 +972,7 @@ static bool save_action_post_success_force(struct section_file *sfile, return TRUE; } - if (action_enabler_list_size(action_enablers_for_action(paction->id)) - == 0) { + if (!action_is_in_use(paction)) { /* Don't save value for actions that aren't enabled. */ return TRUE; } @@ -994,8 +998,9 @@ static bool save_bv_actions(struct section_file *sfile, int i = 0; action_iterate(act_id) { - if (action_enabler_list_size(action_enablers_for_action(act_id)) - == 0) { + struct action *paction = action_by_number(act_id); + + if (!action_is_in_use(paction)) { /* Don't save value for actions that aren't enabled. */ continue; } -- 2.20.1