From f42053da2c606e2c0b148337ced7a9afbf28e7e0 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 7 Jul 2025 13:41:40 +0300
Subject: [PATCH 85/85] Refactor does_req_contradicts_reqs(),
 req_vec_first_contradiction_in_vec()

...for code re-use.

- Make req_vec_first_contradiction_in_vec() non-static
- Move req_vec_first_contradiction_in_vec() to requirements.c
- Improve req_vec_first_contradiction_in_vec() style
- Make does_req_contradicts_reqs() to use req_vec_first_contradiction_in_vec()

See RM #1584

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actions.c      | 25 -------------------------
 common/requirements.c | 29 ++++++++++++++++++++++-------
 common/requirements.h |  3 +++
 3 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/common/actions.c b/common/actions.c
index 15791ecfea..2942f9bac0 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -1763,31 +1763,6 @@ req_vec_first_local_diplrel(const struct requirement_vector *vec)
   return NULL;
 }
 
-/**********************************************************************//**
-  Returns the first requirement in the specified requirement vector that
-  contradicts the specified requirement or NULL if no contradiction was
-  detected.
-  @param req the requirement that may contradict the vector
-  @param vec the requirement vector to look in
-  @return the first local DiplRel requirement.
-**************************************************************************/
-static struct requirement *
-req_vec_first_contradiction_in_vec(const struct requirement *req,
-                                   const struct requirement_vector *vec)
-{
-  /* If the requirement is contradicted by any requirement in the vector it
-   * contradicts the entire requirement vector. */
-  requirement_vector_iterate(vec, preq) {
-    if (are_requirements_contradictions(req, preq)) {
-      return preq;
-    }
-  } requirement_vector_iterate_end;
-
-  /* Not a singe requirement in the requirement vector is contradicted be
-   * the specified requirement. */
-  return NULL;
-}
-
 /**********************************************************************//**
   Detects a local DiplRel requirement in a tile targeted action without
   an explicit claimed requirement in the target reqs.
diff --git a/common/requirements.c b/common/requirements.c
index 5ca4dd30ac..681c435abf 100644
--- a/common/requirements.c
+++ b/common/requirements.c
@@ -2071,23 +2071,38 @@ bool are_requirements_contradictions(const struct requirement *req1,
 }
 
 /**********************************************************************//**
-  Returns TRUE if the given requirement contradicts the given requirement
-  vector.
+  Returns the first requirement in the specified requirement vector that
+  contradicts the specified requirement or NULL if no contradiction was
+  detected.
+  @param req the requirement that may contradict the vector
+  @param vec the requirement vector to look in
+  @return the first local DiplRel requirement.
 **************************************************************************/
-bool does_req_contradicts_reqs(const struct requirement *req,
-                               const struct requirement_vector *vec)
+struct requirement *
+req_vec_first_contradiction_in_vec(const struct requirement *req,
+                                   const struct requirement_vector *vec)
 {
   /* If the requirement is contradicted by any requirement in the vector it
    * contradicts the entire requirement vector. */
   requirement_vector_iterate(vec, preq) {
     if (are_requirements_contradictions(req, preq)) {
-      return TRUE;
+      return preq;
     }
   } requirement_vector_iterate_end;
 
-  /* Not a singe requirement in the requirement vector is contradicted be
+  /* Not a single requirement in the requirement vector is contradicted to be
    * the specified requirement. */
-  return FALSE;
+  return nullptr;
+}
+
+/**********************************************************************//**
+  Returns TRUE if the given requirement contradicts the given requirement
+  vector.
+**************************************************************************/
+bool does_req_contradicts_reqs(const struct requirement *req,
+                               const struct requirement_vector *vec)
+{
+  return req_vec_first_contradiction_in_vec(req, vec) != nullptr;
 }
 
 /**********************************************************************//**
diff --git a/common/requirements.h b/common/requirements.h
index f3341344ab..209f543bc2 100644
--- a/common/requirements.h
+++ b/common/requirements.h
@@ -161,6 +161,9 @@ bool are_requirements_contradictions(const struct requirement *req1,
 bool req_implies_req(const struct requirement *req1,
                      const struct requirement *req2);
 
+struct requirement *
+req_vec_first_contradiction_in_vec(const struct requirement *req,
+                                   const struct requirement_vector *vec);
 bool does_req_contradicts_reqs(const struct requirement *req,
                                const struct requirement_vector *vec);
 
-- 
2.47.2

