Author: sveinung
Date: Fri Jun 24 12:31:09 2016
New Revision: 33012

URL: http://svn.gna.org/viewcvs/freeciv?rev=33012&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:
    branches/S2_6/common/actions.c

Modified: branches/S2_6/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/common/actions.c?rev=33012&r1=33011&r2=33012&view=diff
==============================================================================
--- branches/S2_6/common/actions.c      (original)
+++ branches/S2_6/common/actions.c      Fri Jun 24 12:31:09 2016
@@ -54,10 +54,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.
@@ -313,37 +313,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. */
@@ -378,25 +365,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);
@@ -1317,7 +1302,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.
@@ -1328,6 +1312,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.
 **************************************************************************/
@@ -1335,7 +1341,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