Author: sveinung
Date: Tue Nov  1 19:02:31 2016
New Revision: 34310

URL: http://svn.gna.org/viewcvs/freeciv?rev=34310&view=rev
Log:
Action fall back act_prob utility function.

In some cases it is useful to get the probability of doing action A, or if
action A can't be done, action B. Introduce action_prob_fall_back() for
those cases.

See patch #7889

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

Modified: trunk/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.c?rev=34310&r1=34309&r2=34310&view=diff
==============================================================================
--- trunk/common/actions.c      (original)
+++ trunk/common/actions.c      Tue Nov  1 19:02:31 2016
@@ -3334,6 +3334,74 @@
 }
 
 /**************************************************************************
+  Returns ap1 with ap2 as fall back in cases where ap1 doesn't happen.
+  Said in math that is: P(A) + P(A') * P(B)
+
+  This is useful to calculate the probability of doing action A or, when A
+  is impossible, falling back to doing action B.
+**************************************************************************/
+struct act_prob action_prob_fall_back(const struct act_prob *ap1,
+                                      const struct act_prob *ap2)
+{
+  struct act_prob my_ap1;
+  struct act_prob my_ap2;
+  struct act_prob out;
+
+  /* The action probabilities are real. */
+  fc_assert(ap1 && !action_prob_not_relevant(*ap1));
+  fc_assert(ap2 && !action_prob_not_relevant(*ap2));
+
+  if (action_prob_is_signal(*ap1)
+      && are_action_probabilitys_equal(ap1, ap2)) {
+    /* Keep the information rather than converting the signal to
+     * ACTPROB_NOT_KNOWN. */
+
+    /* Assert that it is OK to convert the signal. */
+    fc_assert(action_prob_not_impl(*ap1));
+
+    out.min = ap1->min;
+    out.max = ap2->max;
+
+    return out;
+  }
+
+  /* Convert any signals to ACTPROB_NOT_KNOWN. */
+  if (action_prob_is_signal(*ap1)) {
+    /* Assert that it is OK to convert the signal. */
+    fc_assert(action_prob_not_impl(*ap1));
+
+    my_ap1.min = ACTPROB_VAL_MIN;
+    my_ap1.max = ACTPROB_VAL_MAX;
+  } else {
+    my_ap1.min = ap1->min;
+    my_ap1.max = ap1->max;
+  }
+
+  if (action_prob_is_signal(*ap2)) {
+    /* Assert that it is OK to convert the signal. */
+    fc_assert(action_prob_not_impl(*ap2));
+
+    my_ap2.min = ACTPROB_VAL_MIN;
+    my_ap2.max = ACTPROB_VAL_MAX;
+  } else {
+    my_ap2.min = ap2->min;
+    my_ap2.max = ap2->max;
+  }
+
+  /* The action probabilities now have a math friendly form. */
+  fc_assert(!action_prob_is_signal(my_ap1));
+  fc_assert(!action_prob_is_signal(my_ap2));
+
+  /* Do the math. */
+  out.min = my_ap1.min + (((ACTPROB_VAL_MAX - my_ap1.min) * my_ap2.min)
+                          / ACTPROB_VAL_MAX);
+  out.max = my_ap1.max + (((ACTPROB_VAL_MAX - my_ap1.max) * my_ap2.max)
+                          / ACTPROB_VAL_MAX);
+
+  return out;
+}
+
+/**************************************************************************
   Will a player with the government gov be immune to the action act?
 **************************************************************************/
 bool action_immune_government(struct government *gov, int act)

Modified: trunk/common/actions.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.h?rev=34310&r1=34309&r2=34310&view=diff
==============================================================================
--- trunk/common/actions.h      (original)
+++ trunk/common/actions.h      Tue Nov  1 19:02:31 2016
@@ -447,6 +447,9 @@
 bool are_action_probabilitys_equal(const struct act_prob *ap1,
                                    const struct act_prob *ap2);
 
+struct act_prob action_prob_fall_back(const struct act_prob *ap1,
+                                      const struct act_prob *ap2);
+
 struct act_prob action_prob_new_impossible(void);
 struct act_prob action_prob_new_not_relevant(void);
 struct act_prob action_prob_new_not_impl(void);


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

Reply via email to