Author: sveinung
Date: Fri Jun 24 12:29:15 2016
New Revision: 33008

URL: http://svn.gna.org/viewcvs/freeciv?rev=33008&view=rev
Log:
Don't assume that an act prob is an int.

Use functions to check properties of action probability data. This isolates
the handling of the format to those functions.

See patch #7302

Modified:
    trunk/common/actions.c

Modified: trunk/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.c?rev=33008&r1=33007&r2=33008&view=diff
==============================================================================
--- trunk/common/actions.c      (original)
+++ trunk/common/actions.c      Fri Jun 24 12:29:15 2016
@@ -60,10 +60,10 @@
                              const struct output_type *target_output,
                              const struct specialist *target_specialist);
 
-#ifndef FREECIV_NDEBUG
 static inline bool action_prob_is_signal(action_probability probability);
 static inline bool action_prob_not_relevant(action_probability probability);
-#endif /* FREECIV_NDEBUG */
+static inline bool action_prob_unknown(action_probability probability);
+static inline bool action_prob_not_impl(action_probability probability);
 
 /**************************************************************************
   Initialize the actions and the action enablers.
@@ -561,37 +561,24 @@
 
   /* How to interpret action probabilities like prob is documented in
    * fc_types.h */
-  switch (prob) {
-  case ACTPROB_NOT_KNOWN:
-    /* Unknown because the player don't have the required knowledge to
-     * determine the probability of success for this action. */
-
-    /* TRANS: the chance of a diplomat action succeeding is unknown. */
-    probtxt = _("?%");
-
-    break;
-  case ACTPROB_NOT_IMPLEMENTED:
-    /* Unknown because of missing server support. */
-    probtxt = NULL;
-
-    break;
-  case ACTPROB_NA:
-    /* Should not exist */
-    probtxt = NULL;
-
-    break;
-  case ACTPROB_IMPOSSIBLE:
-    /* ACTPROB_IMPOSSIBLE is a 0% probability of success */
-  default:
-    /* Should be in the range 0 (0%) to 200 (100%) */
-    fc_assert_msg(!action_prob_is_signal(prob),
-                  "Diplomat action probability out of range");
-
-    /* TRANS: the probability that a diplomat action will succeed. */
+  if (action_prob_is_signal(prob)) {
+    if (action_prob_unknown(prob)) {
+      /* Unknown because the player don't have the required knowledge to
+       * determine the probability of success for this action. */
+
+      /* TRANS: the chance of a diplomat action succeeding is unknown. */
+      probtxt = _("?%");
+    } else {
+      fc_assert(action_prob_not_impl(prob)
+                || action_prob_not_relevant(prob));
+
+      /* Unknown because of missing server support or should not exits. */
+      probtxt = NULL;
+    }
+  } else {
+    /* TRANS: the probability that an action will succeed. */
     astr_set(&chance, _("%.1f%%"), (double)prob / 2);
     probtxt = astr_str(&chance);
-
-    break;
   }
 
   /* Format the info part of the action's UI name. */
@@ -626,25 +613,23 @@
 {
   static struct astring tool_tip = ASTRING_INIT;
 
-  switch (act_prob) {
-  case ACTPROB_NOT_KNOWN:
-    /* Missing in game knowledge. An in game action can change this. */
-    astr_set(&tool_tip,
-             _("Starting to do this may currently be impossible."));
-    break;
-  case ACTPROB_NOT_IMPLEMENTED:
-    /* Missing server support. No in game action will change this. */
-    astr_clear(&tool_tip);
-    break;
-  default:
-    {
-      /* The unit is 0.5% chance of success. */
-      const double converted = (double)act_prob / 2.0;
-
-      astr_set(&tool_tip, _("The probability of success is %.1f%%."),
-               converted);
-    }
-    break;
+  if (action_prob_is_signal(act_prob)) {
+    if (action_prob_unknown(act_prob)) {
+      /* Missing in game knowledge. An in game action can change this. */
+      astr_set(&tool_tip,
+               _("Starting to do this may currently be impossible."));
+    } else {
+      fc_assert(action_prob_not_impl(act_prob));
+
+      /* Missing server support. No in game action will change this. */
+      astr_clear(&tool_tip);
+    }
+  } else {
+    /* The unit is 0.5% chance of success. */
+    const double converted = (double)act_prob / 2.0;
+
+    astr_set(&tool_tip, _("The probability of success is %.1f%%."),
+             converted);
   }
 
   return astr_str(&tool_tip);
@@ -2607,22 +2592,23 @@
                                 target_unit, NULL,
                                 NULL, NULL);
 
-    switch (prob_unit) {
-    case ACTPROB_IMPOSSIBLE:
+    if (!action_prob_possible(prob_unit)) {
       /* One unit makes it impossible for all units. */
       return ACTPROB_IMPOSSIBLE;
-    case ACTPROB_NOT_IMPLEMENTED:
-      /* Not implemented dominates all except impossible. */
-      prob_all = ACTPROB_NOT_IMPLEMENTED;
-      break;
-    case ACTPROB_NOT_KNOWN:
-      if (prob_all != ACTPROB_NOT_IMPLEMENTED) {
-        /* Not known dominates all except not implemented and
-         * impossible. */
-        prob_all = ACTPROB_NOT_KNOWN;
+    } else if (action_prob_is_signal(prob_unit)) {
+      if (action_prob_not_impl(prob_unit)) {
+        /* Not implemented dominates all except impossible. */
+        prob_all = ACTPROB_NOT_IMPLEMENTED;
+      } else {
+        fc_assert(action_prob_unknown(prob_unit));
+
+        if (!action_prob_not_impl(prob_all)) {
+          /* Not known dominates all except not implemented and
+           * impossible. */
+          prob_all = ACTPROB_NOT_KNOWN;
+        }
       }
-      break;
-    default:
+    } else {
       fc_assert_msg(prob_unit <= 200, "Invalid probability %d", prob_unit);
 
       if (200 < prob_all) {
@@ -2736,7 +2722,6 @@
   return ACTPROB_IMPOSSIBLE != probability && ACTPROB_NA != probability;
 }
 
-#ifndef FREECIV_NDEBUG
 /**************************************************************************
   Returns TRUE iff the given action probability represents the lack of
   an action probability.
@@ -2747,6 +2732,28 @@
 }
 
 /**************************************************************************
+  Returns TRUE iff the given action probability represents that support
+  for finding this action probability currently is missing from Freeciv.
+**************************************************************************/
+static inline bool action_prob_not_impl(action_probability probability)
+{
+  return ACTPROB_NOT_IMPLEMENTED == probability;
+}
+
+/**************************************************************************
+  Returns TRUE iff the given action probability represents that the player
+  currently doesn't have enough information to find the real value.
+
+ It is caused by the probability depending on a rule that depends on game
+ state the player don't have access to. It may be possible for the player
+ to later gain access to this game state.
+**************************************************************************/
+static inline bool action_prob_unknown(action_probability probability)
+{
+  return ACTPROB_NOT_KNOWN == probability;
+}
+
+/**************************************************************************
   Returns TRUE iff the given action probability represents a special
   signal value rather than a regular action probability value.
 **************************************************************************/
@@ -2754,7 +2761,6 @@
 {
   return probability < 0 || probability > 200;
 }
-#endif /* FREECIV_NDEBUG */
 
 /**************************************************************************
   Will a player with the government gov be immune to the action act?


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

Reply via email to