Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#11375) merge city_corruption and city_waste
Home

[Freeciv-Dev] (PR#11375) merge city_corruption and city_waste

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11375) merge city_corruption and city_waste
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 6 Dec 2004 10:44:12 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=11375 >

This patch merges city_corruption() and city_waste().  The new function 
looks like

   corruption = city_waste(pcity, O_TRADE, pcity->trade_prod);
   waste = city_waste(pcity, O_SHIELD, pcity->shield_prod);

the changes are actually pretty simple.  I chose the name "waste" 
because "corruption" is too trade-centric.  This changes gets rid of 
some *ugly* duplicated code and makes it easier to someday add food waste.

-jason

Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.179
diff -u -r1.179 aicity.c
--- ai/aicity.c 5 Dec 2004 09:01:00 -0000       1.179
+++ ai/aicity.c 6 Dec 2004 18:41:29 -0000
@@ -109,8 +109,8 @@
   int want = 0, food, trade, shields, lux, sci, tax;
 
   get_food_trade_shields(acity, &food, &trade, &shields);
-  trade -= city_corruption(acity, trade);
-  shields -= city_waste(acity, shields);
+  trade -= city_waste(acity, O_TRADE, trade);
+  shields -= city_waste(acity, O_SHIELD, shields);
   get_tax_income(pplayer, trade, &sci, &lux, &tax);
 
   built_impr_iterate(acity, i) {
Index: ai/aisettler.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aisettler.c,v
retrieving revision 1.11
diff -u -r1.11 aisettler.c
--- ai/aisettler.c      22 Nov 2004 19:14:41 -0000      1.11
+++ ai/aisettler.c      6 Dec 2004 18:41:29 -0000
@@ -227,14 +227,14 @@
      * never make cities. */
     if (game.fulltradesize == 1) {
       result->corruption = ai->science_priority
-       * city_corruption(pcity, 
-                         result->citymap[result->o_x][result->o_y].trade
-                         + result->citymap[2][2].trade);
+       * city_waste(pcity, O_TRADE,
+                    result->citymap[result->o_x][result->o_y].trade
+                    + result->citymap[2][2].trade);
     } else {
       result->corruption = 0;
     }
     result->waste = ai->shield_priority
-      * city_waste(pcity,
+      * city_waste(pcity, O_SHIELD,
                   result->citymap[result->o_x][result->o_y].shield
                   + result->citymap[2][2].shield);
   } else {
@@ -242,11 +242,11 @@
      * is possible (with notradesize) that we _gain_ value here. */
     pcity->size++;
     result->corruption = ai->science_priority
-      * (city_corruption(pcity,
-                        result->citymap[result->o_x][result->o_y].trade)
+      * (city_waste(pcity, O_TRADE,
+                   result->citymap[result->o_x][result->o_y].trade)
         - pcity->corruption);
     result->waste = ai->shield_priority
-      * (city_waste(pcity,
+      * (city_waste(pcity, O_SHIELD,
                    result->citymap[result->o_x][result->o_y].shield)
         - pcity->shield_waste);
     pcity->size--;
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.272
diff -u -r1.272 city.c
--- common/city.c       6 Dec 2004 18:01:14 -0000       1.272
+++ common/city.c       6 Dec 2004 18:41:29 -0000
@@ -2016,10 +2016,10 @@
        trade_between_cities(pcity, find_city_by_id(pcity->trade[i]));
     pcity->surplus[O_TRADE] += pcity->trade_value[i];
   }
-  pcity->corruption = city_corruption(pcity, pcity->surplus[O_TRADE]);
+  pcity->corruption = city_waste(pcity, O_TRADE, pcity->surplus[O_TRADE]);
   pcity->surplus[O_TRADE] -= pcity->corruption;
 
-  pcity->shield_waste = city_waste(pcity, pcity->shield_prod);
+  pcity->shield_waste = city_waste(pcity, O_SHIELD, pcity->shield_prod);
   pcity->shield_prod -= pcity->shield_waste;
 }
 
@@ -2214,87 +2214,60 @@
 }
 
 /**************************************************************************
-  Give corruption generated by city. Corruption is halved during love 
-  the XXX days.
+  Give corruption/waste generated by city.  otype gives the output type
+  (O_SHIELD/O_TRADE).  'total' gives the total output of this type in the
+  city.
 **************************************************************************/
-int city_corruption(const struct city *pcity, int trade)
+int city_waste(const struct city *pcity, Output_type_id otype, int total)
 {
   struct government *g = get_gov_pcity(pcity);
-  struct city *capital;
   int dist;
   unsigned int val;
-  int trade_penalty;
-  struct gov_waste *corruption = &g->waste[O_TRADE];
-
-  assert(game.notradesize < game.fulltradesize);
-  if (pcity->size <= game.notradesize) {
-    trade_penalty = trade;
-  } else if (pcity->size >= game.fulltradesize) {
-    trade_penalty = 0;
-  } else {
-    trade_penalty = trade * (game.fulltradesize - pcity->size) /
+  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};
+
+  if (otype == O_TRADE) {
+    /* FIXME: special case for trade: it is affected by notradesize and
+     * fulltradesize server settings. */
+    assert(game.notradesize < game.fulltradesize);
+    if (pcity->size <= game.notradesize) {
+      penalty = total;
+    } else if (pcity->size >= game.fulltradesize) {
+      penalty = 0;
+    } else {
+      penalty = total * (game.fulltradesize - pcity->size) /
        (game.fulltradesize - game.notradesize);
-  }
-
-  if (corruption->level == 0) {
-    return trade_penalty;
-  }
-  if (corruption->fixed_distance != 0) {
-    dist = corruption->fixed_distance;
-  } else {
-    capital = find_palace(city_owner(pcity));
-    if (!capital)
-      dist = corruption->max_distance_cap;
-    else {
-      int tmp = real_map_distance(capital->tile, pcity->tile);
-      dist = MIN(corruption->max_distance_cap, tmp);
     }
   }
-  dist = dist * corruption->distance_factor + corruption->extra_distance;
-
-  /* Now calculate the final corruption.  Ordered to reduce integer
-   * roundoff errors. */
-  val = trade * MAX(dist, 1) * corruption->level;
-  val -= (val * get_city_bonus(pcity, EFT_CORRUPT_PCT)) / 100;
-  val /= 100 * 100; /* Level is a % multiplied by 100 */
-  val = CLIP(trade_penalty, val, trade);
-  return val;
-}
-
-/************************************************************************** 
-  Give amount of waste generated by city. Waste is corruption for shields.
-**************************************************************************/
-int city_waste(const struct city *pcity, int shields)
-{
-  struct government *g = get_gov_pcity(pcity);
-  struct city *capital;
-  int dist;
-  int shield_penalty = 0;
-  unsigned int val;
-  struct gov_waste *waste = &g->waste[O_SHIELD];
 
   if (waste->level == 0) {
-    return shield_penalty;
+    return penalty;
   }
   if (waste->fixed_distance != 0) {
     dist = waste->fixed_distance;
   } else {
-    capital = find_palace(city_owner(pcity));
+    const struct city *capital = find_palace(city_owner(pcity));
+
     if (!capital) {
       dist = waste->max_distance_cap;
     } else {
       int tmp = real_map_distance(capital->tile, pcity->tile);
+
       dist = MIN(waste->max_distance_cap, tmp);
     }
   }
   dist = dist * waste->distance_factor + waste->extra_distance;
-  /* Ordered to reduce integer roundoff errors */
-  val = shields * MAX(dist, 1) * waste->level;
-  val /= 100 * 100; /* Level is a % multiplied by 100 */
-
-  val -= (val * get_city_bonus(pcity, EFT_WASTE_PCT)) / 100;
 
-  val = CLIP(shield_penalty, val, shields);
+  /* 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;
+  }
+  val /= 100 * 100; /* Level is a % multiplied by 100 */
+  val = CLIP(penalty, val, total);
   return val;
 }
 
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.176
diff -u -r1.176 city.h
--- common/city.h       5 Dec 2004 09:01:00 -0000       1.176
+++ common/city.h       6 Dec 2004 18:41:29 -0000
@@ -503,8 +503,7 @@
                          void (*send_unit_info) (struct player * pplayer,
                                                  struct unit * punit));
 void adjust_city_free_cost(int *num_free, int *this_cost);
-int city_corruption(const struct city *pcity, int trade);
-int city_waste(const struct city *pcity, int shields);
+int city_waste(const struct city *pcity, Output_type_id otype, int total);
 int city_specialists(const struct city *pcity);                 /* 
elv+tax+scie */
 Specialist_type_id best_specialist(Output_type_id otype,
                                   const struct city *pcity);
Index: server/cityturn.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v
retrieving revision 1.279
diff -u -r1.279 cityturn.c
--- server/cityturn.c   5 Dec 2004 09:08:19 -0000       1.279
+++ server/cityturn.c   6 Dec 2004 18:41:30 -0000
@@ -87,8 +87,8 @@
    generic_city_refresh(pcity, TRUE, send_unit_info);
    /* AI would calculate this 1000 times otherwise; better to do it
       once -- Syela */
-   pcity->ai.trade_want =
-       TRADE_WEIGHTING - city_corruption(pcity, TRADE_WEIGHTING);
+   pcity->ai.trade_want
+     = TRADE_WEIGHTING - city_waste(pcity, O_TRADE, TRADE_WEIGHTING);
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#11375) merge city_corruption and city_waste, Jason Short <=