Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#11710) city output effect values
Home

[Freeciv-Dev] (PR#11710) city output effect values

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11710) city output effect values
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 28 Dec 2004 11:19:16 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=11710 >

> [jdorje - Mon Dec 27 20:51:18 2004]:
> 
> This patch takes the city output effect values - currently in an array - 
> and puts them into a switch in a separate function.
> 
> The advantage of putting them in a switch is that if we change the 
> output types (O_POLLUTION) we'll get a compiler warning/error.  It 
> encapsulates the ugliness of this conversion.
> 
> The same should be done with the ADD_TILE, INC_TILE, and PER_TILE effects.

Here's a patch that handles the ADD_TILE, INC_TILE, and PER_TILE effects
also.

-jason

? gmon.out
? new
? orig
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.296
diff -u -r1.296 city.c
--- common/city.c       28 Dec 2004 19:09:27 -0000      1.296
+++ common/city.c       28 Dec 2004 19:18:37 -0000
@@ -262,6 +262,128 @@
 }
 
 /**************************************************************************
+  Return the effect for the production bonus for this output type.
+**************************************************************************/
+static inline enum effect_type get_output_bonus_effect(Output_type_id otype)
+{
+  switch (otype) {
+  case O_SHIELD:
+    return EFT_PROD_BONUS;
+  case O_GOLD:
+    return EFT_TAX_BONUS;
+  case O_LUXURY:
+    return EFT_LUXURY_BONUS;
+  case O_SCIENCE:
+    return EFT_SCIENCE_BONUS;
+  case O_FOOD:
+  case O_TRADE:
+    return EFT_LAST;
+  case O_LAST:
+    break;
+  }
+
+  assert(0);
+  return EFT_LAST;
+}
+
+/**************************************************************************
+  Return the effect for waste reduction for this output type.
+**************************************************************************/
+static inline enum effect_type get_output_waste_effect(Output_type_id otype)
+{
+  switch (otype) {
+  case O_SHIELD:
+    return EFT_WASTE_PCT;
+  case O_TRADE:
+    return EFT_CORRUPT_PCT;
+  case O_FOOD:
+  case O_GOLD:
+  case O_LUXURY:
+  case O_SCIENCE:
+    return EFT_LAST;
+  case O_LAST:
+    break;
+  }
+
+  assert(0);
+  return EFT_LAST;
+}
+
+/**************************************************************************
+  Return the effect for add-tile city bonuses for this output type.
+**************************************************************************/
+static inline enum effect_type get_output_add_tile_effect(Output_type_id o)
+{
+  switch (o) {
+  case O_FOOD:
+    return EFT_FOOD_ADD_TILE;
+  case O_SHIELD:
+    return EFT_PROD_ADD_TILE;
+  case O_TRADE:
+    return EFT_TRADE_ADD_TILE;
+  case O_GOLD:
+  case O_LUXURY:
+  case O_SCIENCE:
+    return EFT_LAST;
+  case O_LAST:
+    break;
+  }
+
+  assert(0);
+  return EFT_LAST;
+}
+
+/**************************************************************************
+  Return the effect for inc-tile city bonuses for this output type.
+**************************************************************************/
+static inline enum effect_type get_output_inc_tile_effect(Output_type_id o)
+{
+  switch (o) {
+  case O_FOOD:
+    return EFT_FOOD_INC_TILE;
+  case O_SHIELD:
+    return EFT_PROD_INC_TILE;
+  case O_TRADE:
+    return EFT_TRADE_INC_TILE;
+  case O_GOLD:
+  case O_LUXURY:
+  case O_SCIENCE:
+    return EFT_LAST;
+  case O_LAST:
+    break;
+  }
+
+  assert(0);
+  return EFT_LAST;
+}
+
+/**************************************************************************
+  Return the effect for per-tile city bonuses for this output type.
+**************************************************************************/
+static inline enum effect_type get_output_per_tile_effect(Output_type_id o)
+{
+  switch (o) {
+  case O_FOOD:
+    return EFT_FOOD_PER_TILE;
+  case O_SHIELD:
+    return EFT_PROD_PER_TILE;
+  case O_TRADE:
+    return EFT_TRADE_PER_TILE;
+  case O_GOLD:
+  case O_LUXURY:
+  case O_SCIENCE:
+    return EFT_LAST;
+  case O_LAST:
+    break;
+  }
+
+  assert(0);
+  return EFT_LAST;
+}
+
+
+
+/**************************************************************************
   Set the worker on the citymap.  Also sets the worked field in the map.
 **************************************************************************/
 void set_worker_city(struct city *pcity, int city_x, int city_y,
@@ -643,18 +765,12 @@
     int before_penalty = (is_celebrating
                          ? g->celeb_output_before_penalty[otype]
                          : g->output_before_penalty[otype]);
-    enum effect_type add[O_MAX] = {EFT_FOOD_ADD_TILE, EFT_PROD_ADD_TILE,
-                                  EFT_TRADE_ADD_TILE, EFT_LAST, EFT_LAST,
-                                  EFT_LAST};
-    enum effect_type inc[O_MAX] = {EFT_FOOD_INC_TILE, EFT_PROD_INC_TILE,
-                                  EFT_TRADE_INC_TILE, EFT_LAST, EFT_LAST,
-                                  EFT_LAST};
-    enum effect_type per[O_MAX] = {EFT_FOOD_PER_TILE, EFT_PROD_PER_TILE,
-                                  EFT_TRADE_PER_TILE, EFT_LAST, EFT_LAST,
-                                  EFT_LAST};
+    enum effect_type add = get_output_add_tile_effect(otype);
+    enum effect_type inc = get_output_inc_tile_effect(otype);
+    enum effect_type per = get_output_per_tile_effect(otype);
 
-    if (add[otype] != EFT_LAST) {
-      prod += get_city_tile_bonus(pcity, ptile, add[otype]);
+    if (add != EFT_LAST) {
+      prod += get_city_tile_bonus(pcity, ptile, add);
     }
 
     /* Government & effect bonus/penalty. */
@@ -662,13 +778,13 @@
       prod += (is_celebrating
            ? g->celeb_output_inc_tile[otype]
            : g->output_inc_tile[otype]);
-      if (inc[otype] != EFT_LAST) {
-       prod += get_city_tile_bonus(pcity, ptile, inc[otype]);
+      if (inc != EFT_LAST) {
+       prod += get_city_tile_bonus(pcity, ptile, inc);
       }
     }
 
-    if (per[otype] != EFT_LAST) {
-      prod += (prod * get_city_tile_bonus(pcity, ptile, per[otype])) / 100;
+    if (per != EFT_LAST) {
+      prod += (prod * get_city_tile_bonus(pcity, ptile, per)) / 100;
     }
 
     if (before_penalty > 0 && prod > before_penalty) {
@@ -1517,13 +1633,11 @@
 **************************************************************************/
 int get_city_output_bonus(const struct city *pcity, Output_type_id otype)
 {
-  enum effect_type eft[] = {EFT_LAST, EFT_PROD_BONUS, EFT_LAST,
-                           EFT_TAX_BONUS, EFT_LUXURY_BONUS,
-                           EFT_SCIENCE_BONUS};
+  enum effect_type eft = get_output_bonus_effect(otype);
   int bonus = 100;
 
-  if (eft[otype] != EFT_LAST) {
-    bonus += get_city_bonus(pcity, eft[otype]);
+  if (eft != EFT_LAST) {
+    bonus += get_city_bonus(pcity, eft);
   }
 
   if (otype == O_SCIENCE
@@ -2165,8 +2279,7 @@
   unsigned int val;
   int penalty = 0;
   struct gov_waste *waste = &g->waste[otype];
-  enum effect_type eft[] = {EFT_LAST, EFT_WASTE_PCT, EFT_CORRUPT_PCT,
-                           EFT_LAST, EFT_LAST, EFT_LAST};
+  enum effect_type eft = get_output_waste_effect(otype);
 
   if (otype == O_TRADE) {
     /* FIXME: special case for trade: it is affected by notradesize and
@@ -2203,8 +2316,8 @@
   /* Now calculate the final waste.  Ordered to reduce integer
    * roundoff errors. */
   val = total * MAX(dist, 1) * waste->level;
-  if (eft[otype] != EFT_LAST) {
-    val -= (val * get_city_bonus(pcity, eft[otype])) / 100;
+  if (eft != EFT_LAST) {
+    val -= (val * get_city_bonus(pcity, eft)) / 100;
   }
   val /= 100 * 100; /* Level is a % multiplied by 100 */
   val = CLIP(penalty, val, total);

[Prev in Thread] Current Thread [Next in Thread]