Author: sveinung
Date: Tue Oct 18 14:11:49 2016
New Revision: 34167

URL: http://svn.gna.org/viewcvs/freeciv?rev=34167&view=rev
Log:
Add a req vec conflict removal utility.

Existing requirement adjustment code appends needed requirements to a vector
without checking if an existing requirement contradicts them. Add
requirement_vector_contradiction_clean(), an utility that detects and
removes conflicting requirements.

See patch #7837

Modified:
    trunk/common/requirements.c
    trunk/common/requirements.h

Modified: trunk/common/requirements.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/requirements.c?rev=34167&r1=34166&r2=34167&view=diff
==============================================================================
--- trunk/common/requirements.c (original)
+++ trunk/common/requirements.c Tue Oct 18 14:11:49 2016
@@ -1111,6 +1111,44 @@
   return FALSE;
 }
 
+/* No self contradictions in the requirement vector. */
+#define NO_CONTRADICTIONS (-1)
+
+/**************************************************************************
+  Returns the first requirement in a requirement vector that contradicts
+  with other requirements in the same requirement vector.
+**************************************************************************/
+static int first_contradiction(const struct requirement_vector *vec)
+{
+  int i;
+
+  for (i = 0; i < requirement_vector_size(vec); i++) {
+    struct requirement *preq = requirement_vector_get(vec, i);
+
+    if (does_req_contradicts_reqs(preq, vec)) {
+      return i;
+    }
+  }
+
+  return NO_CONTRADICTIONS;
+}
+
+/**************************************************************************
+  Clean up self contradictions from a requirement vector.
+
+  When two requirements conflicts the earliest requirement is removed.
+  This allows requirement adjustment code to append the new requirement(s)
+  and leave the contradiction clean up to this function.
+**************************************************************************/
+void requirement_vector_contradiction_clean(struct requirement_vector *vec)
+{
+  int conflict;
+
+  while (NO_CONTRADICTIONS != (conflict = first_contradiction(vec))) {
+    requirement_vector_remove(vec, conflict);
+  }
+}
+
 /****************************************************************************
   Returns TRUE if players are in the same requirements range.
 ****************************************************************************/

Modified: trunk/common/requirements.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/requirements.h?rev=34167&r1=34166&r2=34167&view=diff
==============================================================================
--- trunk/common/requirements.h (original)
+++ trunk/common/requirements.h Tue Oct 18 14:11:49 2016
@@ -108,6 +108,8 @@
 bool does_req_contradicts_reqs(const struct requirement *req,
                                const struct requirement_vector *vec);
 
+void requirement_vector_contradiction_clean(struct requirement_vector *vec);
+
 bool is_req_active(const struct player *target_player,
                   const struct player *other_player,
                   const struct city *target_city,


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to