From 5b50e8bc2e426c08ce6641249581b6da6c2e5b6f Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Fri, 19 Feb 2021 22:09:09 +0100 Subject: [PATCH 6/6] Find missing universals in enabler requirements. Make action_enabler_suggest_improvement() suggest removing references to missing universals. This gives ruledit and other users of action_enabler_suggest_improvement() access to the knowledge of missing universals. See osnd #41616 --- common/actions.c | 14 +++++++++++ common/requirements.c | 55 +++++++++++++++++++++++++++++++++++++++++++ common/requirements.h | 4 ++++ 3 files changed, 73 insertions(+) diff --git a/common/actions.c b/common/actions.c index 9ed9f6a034..944bf77f44 100644 --- a/common/actions.c +++ b/common/actions.c @@ -2584,6 +2584,20 @@ action_enabler_suggest_improvement(const struct action_enabler *enabler) paction = action_by_number(enabler->action); + /* Check if a universal that never will appear in the game is checked. */ + out = req_vec_get_first_missing_univ(&enabler->actor_reqs, + action_enabler_vector_number, + enabler); + if (out) { + return out; + } + out = req_vec_get_first_missing_univ(&enabler->target_reqs, + action_enabler_vector_number, + enabler); + if (out) { + return out; + } + /* Detect unused action enablers. */ if (action_get_actor_kind(paction) == AAK_UNIT) { bool has_user = FALSE; diff --git a/common/requirements.c b/common/requirements.c index d7528187e3..3231a7ab9a 100644 --- a/common/requirements.c +++ b/common/requirements.c @@ -3747,6 +3747,61 @@ req_vec_get_first_contradiction(const struct requirement_vector *vec, return NULL; } +/**********************************************************************//** + Returns the first universal known to always be absent in the specified + requirement vector with suggested solutions or NULL if no missing + universals were found. + It is the responsibility of the caller to free the suggestion when it is + done with it. + @param vec the requirement vector to look in. + @param get_num function that returns the requirement vector's number in + the parent item. + @param parent_item the item that owns the vector. + @return the first missing universal found. +**************************************************************************/ +struct req_vec_problem * +req_vec_get_first_missing_univ(const struct requirement_vector *vec, + requirement_vector_number get_num, + const void *parent_item) +{ + int i; + req_vec_num_in_item vec_num; + + if (vec == NULL || requirement_vector_size(vec) == 0) { + /* No vector. */ + return NULL; + } + + if (get_num == NULL || parent_item == NULL) { + vec_num = 0; + } else { + vec_num = get_num(parent_item, vec); + } + + /* Look for contradictions */ + for (i = 0; i < requirement_vector_size(vec); i++) { + struct requirement *preq = requirement_vector_get(vec, i); + if (universal_never_there(&preq->source)) { + struct req_vec_problem *problem; + + problem = req_vec_problem_new(1, + N_("Requirement {%s} mentions %s but it will never be there."), + req_to_fstring(preq), universal_rule_name(&preq->source)); + + /* The solution is to remove the reference to the missing + * universal. */ + problem->suggested_solutions[0].operation = RVCO_REMOVE; + problem->suggested_solutions[0].vector_number = vec_num; + problem->suggested_solutions[0].req = *preq; + + /* Only the first missing universal is reported. */ + return problem; + } + } + + return NULL; +} + /**********************************************************************//** Return TRUE iff the two sources are equivalent. Note this isn't the same as an == or memcmp check. diff --git a/common/requirements.h b/common/requirements.h index e9f5f832f6..5ffcdc3139 100644 --- a/common/requirements.h +++ b/common/requirements.h @@ -235,6 +235,10 @@ struct req_vec_problem * req_vec_get_first_contradiction(const struct requirement_vector *vec, requirement_vector_number get_num, const void *parent_item); +struct req_vec_problem * +req_vec_get_first_missing_univ(const struct requirement_vector *vec, + requirement_vector_number get_num, + const void *parent_item); /* General universal functions. */ -- 2.20.1