From 943979493c91764bf577db812bcbf8a3693635eb Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 20 Feb 2023 23:10:00 +0200 Subject: [PATCH 5/5] Use enabler_get_action[_id]() where applicable See osdn #47464 Signed-off-by: Marko Lindqvist --- common/actions.c | 27 +++++++++-------- common/actions.h | 3 ++ common/improvement.c | 21 +++++++------ common/unittype.c | 54 ++++++++++++++++++++++------------ server/rscompat.c | 7 +++-- server/rssanity.c | 2 +- server/ruleset.c | 4 +-- tools/ruledit/tab_enablers.cpp | 12 ++++++-- tools/ruledit/validity.c | 2 +- tools/ruleutil/rulesave.c | 2 +- 10 files changed, 82 insertions(+), 52 deletions(-) diff --git a/common/actions.c b/common/actions.c index 713f3f7298..5266745f5e 100644 --- a/common/actions.c +++ b/common/actions.c @@ -2428,10 +2428,10 @@ void action_enabler_add(struct action_enabler *enabler) /* Sanity check: a non existing action enabler can't be added. */ fc_assert_ret(enabler); /* Sanity check: a non existing action doesn't have enablers. */ - fc_assert_ret(action_id_exists(enabler->action)); + fc_assert_ret(action_id_exists(enabler_get_action_id(enabler))); action_enabler_list_append( - action_enablers_for_action(enabler->action), + action_enablers_for_action(enabler_get_action_id(enabler)), enabler); } @@ -2445,10 +2445,10 @@ bool action_enabler_remove(struct action_enabler *enabler) /* Sanity check: a non existing action enabler can't be removed. */ fc_assert_ret_val(enabler, FALSE); /* Sanity check: a non existing action doesn't have enablers. */ - fc_assert_ret_val(action_id_exists(enabler->action), FALSE); + fc_assert_ret_val(action_id_exists(enabler_get_action_id(enabler)), FALSE); return action_enabler_list_remove( - action_enablers_for_action(enabler->action), + action_enablers_for_action(enabler_get_action_id(enabler)), enabler); } @@ -2469,6 +2469,7 @@ action_enablers_for_action(action_id action) enabler or NULL if no hard obligatory reqs were missing. It is the responsibility of the caller to free the suggestion when it is done with it. + @param enabler the action enabler to suggest a fix for. @param oblig hard obligatory requirements to check @return a problem with fix suggestions or NULL if no obligatory hard @@ -2486,8 +2487,8 @@ ae_suggest_repair_if_no_oblig(const struct action_enabler *enabler, /* Sanity check: a non existing action doesn't have any obligatory hard * requirements. */ - fc_assert_ret_val(action_id_exists(enabler->action), NULL); - paction = action_by_number(enabler->action); + fc_assert_ret_val(action_id_exists(enabler_get_action_id(enabler)), NULL); + paction = enabler_get_action(enabler); /* No obligatory hard requirements. */ fc_assert_ret_val(oblig, NULL); @@ -2591,8 +2592,8 @@ action_enabler_suggest_repair_oblig(const struct action_enabler *enabler) /* Sanity check: a non existing action doesn't have any obligatory hard * requirements. */ - fc_assert_ret_val(action_id_exists(enabler->action), NULL); - paction = action_by_number(enabler->action); + fc_assert_ret_val(action_id_exists(enabler_get_action_id(enabler)), NULL); + paction = enabler_get_action(enabler); if (paction->result != ACTRES_NONE) { /* A hard coded action result may mean obiligatory requirements. */ @@ -2682,7 +2683,7 @@ enabler_tile_tgt_local_diplrel_implies_claimed( struct requirement *claimed_req; struct requirement tile_is_claimed; struct requirement tile_is_unclaimed; - struct action *paction = action_by_number(enabler->action); + struct action *paction = enabler_get_action(enabler); struct astring astr; if (action_get_target_kind(paction) != ATK_TILE) { @@ -2753,7 +2754,7 @@ enabler_first_self_contradiction(const struct action_enabler *enabler) struct requirement *local_diplrel; struct requirement *unclaimed_req; struct requirement tile_is_claimed; - struct action *paction = action_by_number(enabler->action); + struct action *paction = enabler_get_action(enabler); struct astring astr1; struct astring astr2; @@ -2896,7 +2897,7 @@ action_enabler_suggest_improvement(const struct action_enabler *enabler) return out; } - paction = action_by_number(enabler->action); + paction = enabler_get_action(enabler); /* Look for improvement suggestions to the requirement vectors. */ out = req_vec_suggest_improvement(&enabler->actor_reqs, @@ -7332,7 +7333,7 @@ bool action_mp_full_makes_legal(const struct unit *actor, bool action_enabler_utype_possible_actor(const struct action_enabler *ae, const struct unit_type *act_utype) { - const struct action *paction = action_by_number(ae->action); + const struct action *paction = enabler_get_action(ae); struct universal actor_univ = { .kind = VUT_UTYPE, .value.utype = act_utype }; @@ -7358,7 +7359,7 @@ bool action_enabler_utype_possible_actor(const struct action_enabler *ae, **************************************************************************/ bool action_enabler_possible_actor(const struct action_enabler *ae) { - const struct action *paction = action_by_number(ae->action); + const struct action *paction = enabler_get_action(ae); switch (action_get_actor_kind(paction)) { case AAK_UNIT: diff --git a/common/actions.h b/common/actions.h index e98039f4aa..5fa7c80bb4 100644 --- a/common/actions.h +++ b/common/actions.h @@ -398,6 +398,7 @@ struct action_enabler #define action_has_result(_act_, _res_) ((_act_)->result == (_res_)) #define enabler_get_action(_enabler_) action_by_number(_enabler_->action) +#define enabler_get_action_id(_enabler_) (_enabler_->action) #define SPECLIST_TAG action_enabler #define SPECLIST_TYPE struct action_enabler @@ -608,6 +609,8 @@ enum action_sub_target_kind action_get_sub_target_kind( int action_number(const struct action *action); +#define action_id(_act_) (_act_->id) + #define action_has_result_safe(paction, result) \ (paction && action_has_result(paction, result)) #define action_id_has_result_safe(act_id, result) \ diff --git a/common/improvement.c b/common/improvement.c index 1810d72c01..a8e04e4ff5 100644 --- a/common/improvement.c +++ b/common/improvement.c @@ -510,9 +510,10 @@ static bool impr_protects_vs_actions(const struct city *pcity, return FALSE; } - action_enablers_iterate(act) { - if (!requirement_fulfilled_by_improvement(pimprove, &act->target_reqs) - && !is_action_possible_on_city(act->action, NULL, pcity)) { + action_enablers_iterate(enabler) { + if (!requirement_fulfilled_by_improvement(pimprove, &enabler->target_reqs) + && !is_action_possible_on_city(enabler_get_action_id(enabler), + NULL, pcity)) { return TRUE; } } action_enablers_iterate_end; @@ -521,7 +522,7 @@ static bool impr_protects_vs_actions(const struct city *pcity, } /**********************************************************************//** - Returns TRUE iff improvement allows its owner to perform an action + Returns TRUE iff improvement allows its owner to perform an action. **************************************************************************/ static bool impr_allows_actions(const struct city *pcity, const struct impr_type *pimprove) @@ -531,12 +532,14 @@ static bool impr_allows_actions(const struct city *pcity, return FALSE; } - action_enablers_iterate(act) { - if (requirement_needs_improvement(pimprove, &act->actor_reqs)) { - switch (action_id_get_actor_kind(act->action)) { + action_enablers_iterate(enabler) { + if (requirement_needs_improvement(pimprove, &enabler->actor_reqs)) { + action_id act = enabler_get_action_id(enabler); + + switch (action_id_get_actor_kind(act)) { case AAK_UNIT: unit_type_iterate(ut) { - if (!utype_can_do_action(ut, act->action)) { + if (!utype_can_do_action(ut, act)) { /* Not relevant */ continue; } @@ -553,7 +556,7 @@ static bool impr_allows_actions(const struct city *pcity, } unit_type_iterate_end; break; case AAK_COUNT: - fc_assert(action_id_get_actor_kind(act->action) != AAK_COUNT); + fc_assert(action_id_get_actor_kind(act) != AAK_COUNT); break; } } diff --git a/common/unittype.c b/common/unittype.c index bad8d580aa..e3a96320b0 100644 --- a/common/unittype.c +++ b/common/unittype.c @@ -417,17 +417,19 @@ static void unit_can_act_cache_set(struct unit_type *putype) /* See if the unit type can do an action controlled by generalized action * enablers */ action_enablers_iterate(enabler) { - const struct action *paction = action_by_number(enabler->action); - if (action_id_get_actor_kind(enabler->action) == AAK_UNIT + const struct action *paction = enabler_get_action(enabler); + action_id act_id = action_id(paction); + + if (action_get_actor_kind(paction) == AAK_UNIT && action_actor_utype_hard_reqs_ok(paction, putype) && requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs))) { log_debug("act_cache: %s can %s", utype_rule_name(putype), - action_id_rule_name(enabler->action)); - BV_SET(unit_can_act_cache[enabler->action], utype_index(putype)); + action_rule_name(paction)); + BV_SET(unit_can_act_cache[act_id], utype_index(putype)); BV_SET(unit_can_act_cache[ACTION_ANY], utype_index(putype)); - if (action_is_hostile(enabler->action)) { + if (action_is_hostile(act_id)) { BV_SET(unit_can_act_cache[ACTION_HOSTILE], utype_index(putype)); } } @@ -690,16 +692,19 @@ static void unit_state_action_cache_set(struct unit_type *putype) action_enablers_iterate(enabler) { if (requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs)) - && action_id_get_actor_kind(enabler->action) == AAK_UNIT) { + && action_get_actor_kind(enabler_get_action(enabler)) == AAK_UNIT) { bool present = TRUE; + do { + action_id act_id = enabler_get_action_id(enabler); + /* OK if not mentioned */ req.present = present; if (!is_req_in_vec(&req, &(enabler->actor_reqs))) { - BV_SET(ustate_act_cache[utype_index(putype)][enabler->action], + BV_SET(ustate_act_cache[utype_index(putype)][act_id], requirement_unit_state_ereq(req.source.value.unit_state, !req.present)); - if (action_is_hostile(enabler->action)) { + if (action_is_hostile(act_id)) { BV_SET(ustate_act_cache[utype_index(putype)][ACTION_HOSTILE], requirement_unit_state_ereq(req.source.value.unit_state, !req.present)); @@ -765,22 +770,27 @@ static void local_dipl_rel_action_cache_set(struct unit_type *putype) * the cache when units can do an action given a certain diplomatic * relationship property value. */ action_enablers_iterate(enabler) { + struct action *paction = enabler_get_action(enabler); + if (requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs)) - && action_id_get_actor_kind(enabler->action) == AAK_UNIT - && ((action_id_get_target_kind(enabler->action) != ATK_TILE - && action_id_get_target_kind(enabler->action) != ATK_EXTRAS) + && action_get_actor_kind(paction) == AAK_UNIT + && ((action_get_target_kind(paction) != ATK_TILE + && action_get_target_kind(paction) != ATK_EXTRAS) /* No diplomatic relation to Nature */ || !does_req_contradicts_reqs(&tile_is_claimed, &enabler->target_reqs))) { bool present = TRUE; + do { + action_id act_id = enabler_get_action_id(enabler); + req.present = present; if (!does_req_contradicts_reqs(&req, &(enabler->actor_reqs))) { - BV_SET(dipl_rel_action_cache[uidx][enabler->action], + BV_SET(dipl_rel_action_cache[uidx][act_id], requirement_diplrel_ereq(req.source.value.diplrel, REQ_RANGE_LOCAL, req.present)); - if (action_is_hostile(enabler->action)) { + if (action_is_hostile(act_id)) { BV_SET(dipl_rel_action_cache[uidx][ACTION_HOSTILE], requirement_diplrel_ereq(req.source.value.diplrel, REQ_RANGE_LOCAL, @@ -842,18 +852,21 @@ local_dipl_rel_tile_other_tgt_action_cache_set(struct unit_type *putype) action_enablers_iterate(enabler) { if (requirement_fulfilled_by_unit_type(putype, &(enabler->actor_reqs)) - && action_id_get_actor_kind(enabler->action) == AAK_UNIT + && action_get_actor_kind(enabler_get_action(enabler)) == AAK_UNIT /* No diplomatic relation to Nature */ && !does_req_contradicts_reqs(&tile_is_claimed, &enabler->target_reqs)) { bool present = TRUE; + do { + action_id act_id = enabler_get_action_id(enabler); + req.present = present; if (!does_req_contradicts_reqs(&req, &(enabler->target_reqs))) { - BV_SET(dipl_rel_tile_other_tgt_a_cache[uidx][enabler->action], + BV_SET(dipl_rel_tile_other_tgt_a_cache[uidx][act_id], requirement_diplrel_ereq(req.source.value.diplrel, REQ_RANGE_LOCAL, req.present)); - if (action_is_hostile(enabler->action)) { + if (action_is_hostile(act_id)) { BV_SET(dipl_rel_tile_other_tgt_a_cache[uidx][ACTION_HOSTILE], requirement_diplrel_ereq(req.source.value.diplrel, REQ_RANGE_LOCAL, @@ -911,17 +924,20 @@ static void tgt_citytile_act_cache_set(struct unit_type *putype) action_enablers_iterate(enabler) { if (requirement_fulfilled_by_unit_type(putype, &(enabler->target_reqs)) - && action_id_get_actor_kind(enabler->action) == AAK_UNIT) { + && action_get_actor_kind(enabler_get_action(enabler)) == AAK_UNIT) { bool present = TRUE; + do { + action_id act_id = enabler_get_action_id(enabler); + /* OK if not mentioned */ req.present = present; if (!is_req_in_vec(&req, &(enabler->target_reqs))) { BV_SET( - ctile_tgt_act_cache[utype_index(putype)][enabler->action], + ctile_tgt_act_cache[utype_index(putype)][act_id], requirement_citytile_ereq(req.source.value.citytile, !req.present)); - if (action_is_hostile(enabler->action)) { + if (action_is_hostile(act_id)) { BV_SET( ctile_tgt_act_cache[utype_index(putype)][ACTION_HOSTILE], requirement_citytile_ereq(req.source.value.citytile, diff --git a/server/rscompat.c b/server/rscompat.c index 06896b6cd7..9f1517ccf2 100644 --- a/server/rscompat.c +++ b/server/rscompat.c @@ -157,6 +157,7 @@ bool rscompat_check_cap_and_version(struct section_file *file, /**********************************************************************//** Add all hard obligatory requirements to an action enabler or disable it. + @param ae the action enabler to add requirements to. @return TRUE iff adding obligatory hard reqs for the enabler's action needs to restart - say if an enabler was added or removed. @@ -166,7 +167,7 @@ rscompat_enabler_add_obligatory_hard_reqs(struct action_enabler *ae) { struct req_vec_problem *problem; - struct action *paction = action_by_number(ae->action); + struct action *paction = enabler_get_action(ae); /* Some changes requires starting to process an action's enablers from * the beginning. */ bool needs_restart = FALSE; @@ -386,7 +387,7 @@ void rscompat_postprocess(struct rscompat_info *info) } action_iterate_end; action_enablers_iterate(ae) { - if (ae->action == ACTION_CLEAN_POLLUTION) { + if (enabler_get_action_id(ae) == ACTION_CLEAN_POLLUTION) { /* TODO: Stop making the copy to preserve enabler for * the original action. */ struct action_enabler *copy = action_enabler_copy(ae); @@ -399,7 +400,7 @@ void rscompat_postprocess(struct rscompat_info *info) action_enabler_add(copy); } - if (ae->action == ACTION_CLEAN_FALLOUT) { + if (enabler_get_action_id(ae) == ACTION_CLEAN_FALLOUT) { /* TODO: Stop making the copy to preserve enabler for * the original action. */ struct action_enabler *copy = action_enabler_copy(ae); diff --git a/server/rssanity.c b/server/rssanity.c index 6063bab2b5..d5360025e0 100644 --- a/server/rssanity.c +++ b/server/rssanity.c @@ -1255,7 +1255,7 @@ bool sanity_check_ruleset_data(struct rscompat_info *compat) ok = FALSE; } - if (action_id_get_target_kind(enabler->action) == ATK_SELF) { + if (action_get_target_kind(enabler_get_action(enabler)) == ATK_SELF) { /* Special test for self targeted actions. */ if (requirement_vector_size(&(enabler->target_reqs)) > 0) { diff --git a/server/ruleset.c b/server/ruleset.c index 74dff2763a..1f71b16c20 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -7964,7 +7964,7 @@ static bool load_ruleset_actions(struct section_file *file, break; } - enabler->action = paction->id; + enabler->action = action_id(paction); actor_reqs = lookup_req_list(file, compat, sec_name, "actor_reqs", action_text); if (actor_reqs == NULL) { @@ -8824,7 +8824,7 @@ static void send_ruleset_action_enablers(struct conn_list *dest) struct packet_ruleset_action_enabler packet; action_enablers_iterate(enabler) { - packet.enabled_action = enabler->action; + packet.enabled_action = enabler_get_action_id(enabler); counter = 0; requirement_vector_iterate(&enabler->actor_reqs, req) { diff --git a/tools/ruledit/tab_enablers.cpp b/tools/ruledit/tab_enablers.cpp index f5d7b99bbc..81dfcbe234 100644 --- a/tools/ruledit/tab_enablers.cpp +++ b/tools/ruledit/tab_enablers.cpp @@ -371,7 +371,7 @@ void tab_enabler::edit_type(QAction *action) if (selected != nullptr && paction != nullptr) { // Must remove and add back because enablers are stored by action. action_enabler_remove(selected); - selected->action = paction->id; + selected->action = action_id(paction); action_enabler_add(selected); // Show the changes. @@ -409,9 +409,9 @@ void tab_enabler::edit_actor_reqs() fix_enabler_item::fix_enabler_item(struct action_enabler *enabler) { char buf[MAX_LEN_NAME * 2]; - struct action *paction = action_by_number(enabler->action); + struct action *paction = enabler_get_action(enabler); - fc_assert_ret(paction); + fc_assert_ret(paction != NULL); /* Can't use QString::asprintf() as msys libintl.h defines asprintf() * as a macro */ @@ -498,6 +498,7 @@ void fix_enabler_item::apply_accepted_changes() { // The user has approved the solution current_enabler->action = local_copy->action; + requirement_vector_copy(¤t_enabler->actor_reqs, &local_copy->actor_reqs); requirement_vector_copy(¤t_enabler->target_reqs, @@ -513,6 +514,7 @@ void fix_enabler_item::undo_accepted_changes() { // The user has rejected all solutions local_copy->action = current_enabler->action; + requirement_vector_copy(&local_copy->actor_reqs, ¤t_enabler->actor_reqs); requirement_vector_copy(&local_copy->target_reqs, @@ -521,6 +523,7 @@ void fix_enabler_item::undo_accepted_changes() /********************************************************************//** Returns the number of requirement vectors in this item. + @return the number of requirement vectors the item has. ************************************************************************/ int fix_enabler_item::num_vectors() @@ -532,6 +535,7 @@ int fix_enabler_item::num_vectors() Returns a function pointer to a function that names this item kind's requirement vector number number. Useful when there is more than one requirement vector. + @return the requirement vector namer for ruleset items of this kind. ************************************************************************/ requirement_vector_namer fix_enabler_item::vector_namer() @@ -543,6 +547,7 @@ requirement_vector_namer fix_enabler_item::vector_namer() Returns a function pointer to a function that returns a writable pointer to the specified requirement vector in the specified parent item. + @return a writable pointer to the requirement vector getter function. ************************************************************************/ requirement_vector_by_number fix_enabler_item::vector_getter() @@ -552,6 +557,7 @@ requirement_vector_by_number fix_enabler_item::vector_getter() /**********************************************************************//** Check if the specified vector belongs to this item + @param vec the requirement vector that may belong to this item. @return true iff the vector belongs to this item. **************************************************************************/ diff --git a/tools/ruledit/validity.c b/tools/ruledit/validity.c index 015dd239ce..747f7a5a6a 100644 --- a/tools/ruledit/validity.c +++ b/tools/ruledit/validity.c @@ -130,7 +130,7 @@ static bool is_universal_needed(struct universal *uni, requirers_cb cb, char buf[1024]; fc_snprintf(buf, sizeof(buf), R__("%s action enabler"), - action_id_rule_name(enabler->action)); + action_rule_name(enabler_get_action(enabler))); cb(buf, data); needed = TRUE; } diff --git a/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c index 1d1fe45a36..0bc295c28e 100644 --- a/tools/ruleutil/rulesave.c +++ b/tools/ruleutil/rulesave.c @@ -1216,7 +1216,7 @@ static bool save_actions_ruleset(const char *filename, const char *name) fc_snprintf(path, sizeof(path), "actionenabler_%d", sect_idx++); - secfile_insert_str(sfile, action_id_rule_name(pae->action), + secfile_insert_str(sfile, action_rule_name(enabler_get_action(pae)), "%s.action", path); save_reqs_vector(sfile, &(pae->actor_reqs), path, "actor_reqs"); -- 2.39.1