Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#11519) put city support values in an array
Home

[Freeciv-Dev] (PR#11519) put city support values in an array

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11519) put city support values in an array
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 14 Dec 2004 14:14:08 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This patch creates a new array pcity->usage[] that holds the support 
values for the city.

pcity->usage[] is initialized and set completely in city_support().  In 
fact city_support() now doesn't affect any of the other array values. 
Someday soon generic_city_refresh should be given a parameter telling it 
not to call city_support(); I think this will make it much faster.  For 
the same reason the usage[] values are sent to the client.  However 
city_support() still calculates some happiness values and so is needed 
for now.

This patch also removes set_tax_surplus (which is merged into 
city_support) and adds set_surpluses() which is called at the very end 
and sets pcity->surplus[] from pcity->prod[] and pcity->usage[].  Thus 
pcity->surplus[] is not used at all before this point.  Instead all 
pcity->surplus[] from earlier on is changed to pcity->prod[].  This is a 
significant cleanup I think.

The usage[] array will also be useful for the CM.  Currently the CM 
tries to guess at the amount of support needed by the city, which is 
used to find the minimums in each category which is in turn used in pruning.

-jason



Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.445
diff -u -r1.445 packhand.c
--- client/packhand.c   13 Dec 2004 16:20:52 -0000      1.445
+++ client/packhand.c   14 Dec 2004 22:04:40 -0000
@@ -461,6 +461,7 @@
     pcity->waste[o] = packet->waste[o];
     pcity->prod[o] = packet->prod[o];
     pcity->citizen_base[o] = packet->citizen_base[o];
+    pcity->usage[o] = packet->usage[o];
   } output_type_iterate_end;
 
   pcity->food_stock=packet->food_stock;
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.284
diff -u -r1.284 city.c
--- common/city.c       13 Dec 2004 16:23:30 -0000      1.284
+++ common/city.c       14 Dec 2004 22:04:40 -0000
@@ -1633,7 +1633,7 @@
 
 /**************************************************************************
   Add the incomes of a city according to the taxrates (ignore # of 
-  specialists). trade should usually be pcity->surplus[O_TRADE].
+  specialists). trade should be in output[O_TRADE].
 **************************************************************************/
 void add_tax_income(const struct player *pplayer, int *output)
 {
@@ -1735,7 +1735,7 @@
   pcity->prod[O_LUXURY] = 0;
   pcity->prod[O_SCIENCE] = 0;
   pcity->prod[O_GOLD] = 0;
-  output[O_TRADE] = pcity->surplus[O_TRADE];
+  output[O_TRADE] = pcity->prod[O_TRADE];
   add_tax_income(city_owner(pcity), output);
   add_specialist_output(pcity, output);
   pcity->prod[O_LUXURY] += output[O_LUXURY];
@@ -1766,21 +1766,16 @@
   pcity->prod[O_GOLD] = (pcity->prod[O_GOLD] * pcity->bonus[O_GOLD]) / 100;
   pcity->prod[O_SCIENCE] = (pcity->prod[O_SCIENCE]
                          * pcity->bonus[O_SCIENCE]) / 100;
-  pcity->surplus[O_SHIELD] = pcity->prod[O_SHIELD];
 }
 
 /**************************************************************************
-  Set tax surpluses from the base productions.
+  Set the final surplus[] array from the prod[] and usage[] values.
 **************************************************************************/
-static void set_tax_surplus(struct city *pcity)
+static void set_surpluses(struct city *pcity)
 {
-  pcity->surplus[O_SCIENCE] = pcity->prod[O_SCIENCE];
-  pcity->surplus[O_LUXURY] = pcity->prod[O_LUXURY];
-
-  /* Does not count capitalization. */
-  pcity->surplus[O_GOLD] = pcity->prod[O_GOLD];
-  pcity->surplus[O_GOLD] -= city_building_upkeep(pcity, O_GOLD);
-  pcity->surplus[O_GOLD] -= city_unit_upkeep(pcity, O_GOLD);
+  output_type_iterate(o) {
+    pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
+  } output_type_iterate_end;
 }
 
 /**************************************************************************
@@ -1983,15 +1978,11 @@
 static inline void unhappy_city_check(struct city *pcity)
 {
   if (city_unhappy(pcity)) {
-    pcity->surplus[O_FOOD] = MIN(0, pcity->surplus[O_FOOD]);
-    pcity->surplus[O_SHIELD] = MIN(0, pcity->surplus[O_SHIELD]);
-    pcity->surplus[O_GOLD] -= pcity->prod[O_GOLD];
-    pcity->surplus[O_SCIENCE] -= pcity->prod[O_SCIENCE];
-
-    /* FIXME: These are special cases because many parts of the code still
-     * check tax_total and science_total instead of the surplus[] array. */
+    pcity->prod[O_FOOD] = MIN(pcity->usage[O_FOOD], pcity->prod[O_FOOD]);
+    pcity->prod[O_SHIELD] = MIN(pcity->usage[O_SHIELD], pcity->prod[O_SHIELD]);
     pcity->prod[O_GOLD] = 0;
     pcity->prod[O_SCIENCE] = 0;
+    /* Trade and luxury are unaffected. */
   }
 }
 
@@ -2030,35 +2021,30 @@
   int i;
   int output[O_COUNT];
 
-  pcity->surplus[O_FOOD] = 0;
-  pcity->surplus[O_SHIELD] = 0;
-
   get_worked_tile_output(pcity, output);
   pcity->prod[O_FOOD] = output[O_FOOD];
   pcity->prod[O_SHIELD] = output[O_SHIELD];
-  pcity->surplus[O_TRADE] = output[O_TRADE];
+  pcity->prod[O_TRADE] = output[O_TRADE];
 
   pcity->citizen_base[O_FOOD] = pcity->prod[O_FOOD];
   pcity->citizen_base[O_SHIELD] = pcity->prod[O_SHIELD];
-  pcity->citizen_base[O_TRADE] = pcity->surplus[O_TRADE];
-
-  pcity->surplus[O_FOOD] = pcity->prod[O_FOOD] - pcity->size * 2;
+  pcity->citizen_base[O_TRADE] = pcity->prod[O_TRADE];
 
   for (i = 0; i < NUM_TRADEROUTES; i++) {
     pcity->trade_value[i] =
        trade_between_cities(pcity, find_city_by_id(pcity->trade[i]));
-    pcity->surplus[O_TRADE] += pcity->trade_value[i];
+    pcity->prod[O_TRADE] += pcity->trade_value[i];
   }
-  pcity->waste[O_TRADE] = city_waste(pcity, O_TRADE, pcity->surplus[O_TRADE]);
-  pcity->surplus[O_TRADE] -= pcity->waste[O_TRADE];
-  pcity->prod[O_TRADE] = pcity->surplus[O_TRADE]; /* Set it here. */
+  pcity->waste[O_TRADE] = city_waste(pcity, O_TRADE, pcity->prod[O_TRADE]);
+  pcity->prod[O_TRADE] -= pcity->waste[O_TRADE];
 
   pcity->waste[O_SHIELD] = city_waste(pcity, O_SHIELD, pcity->prod[O_SHIELD]);
   pcity->prod[O_SHIELD] -= pcity->waste[O_SHIELD];
 }
 
 /**************************************************************************
-  Calculate upkeep costs.
+  Calculate upkeep costs.  This builds the pcity->usage[] array as well
+  as setting some happiness values.
 **************************************************************************/
 static inline void city_support(struct city *pcity, 
                                void (*send_unit_info) (struct player *pplayer,
@@ -2076,6 +2062,10 @@
 
   happy_copy(pcity, 2);
 
+  memset(pcity->usage, 0, O_COUNT * sizeof(*pcity->usage));
+  pcity->usage[O_GOLD] += city_building_upkeep(pcity, O_GOLD);
+  pcity->usage[O_FOOD] += 2 * pcity->size;
+
   /*
    * If you modify anything here these places might also need updating:
    * - ai/aitools.c : ai_assess_military_unhappiness
@@ -2158,21 +2148,21 @@
     if (shield_cost > 0) {
       adjust_city_free_cost(&free_shield, &shield_cost);
       if (shield_cost > 0) {
-       pcity->surplus[O_SHIELD] -= shield_cost;
+       pcity->usage[O_SHIELD] += shield_cost;
        this_unit->upkeep[O_SHIELD] = shield_cost;
       }
     }
     if (food_cost > 0) {
       adjust_city_free_cost(&free_food, &food_cost);
       if (food_cost > 0) {
-       pcity->surplus[O_FOOD] -= food_cost;
+       pcity->usage[O_FOOD] += food_cost;
        this_unit->upkeep[O_FOOD] = food_cost;
       }
     }
     if (gold_cost > 0) {
       adjust_city_free_cost(&free_gold, &gold_cost);
       if (gold_cost > 0) {
-       /* FIXME: gold upkeep is subtracted off of the tax_total later. */
+       pcity->usage[O_GOLD] += gold_cost;
        this_unit->upkeep[O_GOLD] = gold_cost;
       }
     }
@@ -2203,13 +2193,13 @@
   citizen_happy_size(pcity);
   set_tax_income(pcity);       /* calc base luxury, tax & bulbs */
   add_buildings_effect(pcity); /* marketplace, library wonders.. */
-  set_tax_surplus(pcity);
   pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
   citizen_happy_luxury(pcity); /* with our new found luxuries */
   citizen_content_buildings(pcity);    /* temple cathedral colosseum */
   city_support(pcity, send_unit_info); /* manage settlers, and units */
   citizen_happy_wonders(pcity);        /* happy wonders & fundamentalism */
   unhappy_city_check(pcity);
+  set_surpluses(pcity);
 
   if (refresh_trade_route_cities
       && pcity->citizen_base[O_TRADE] != prev_tile_trade) {
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.184
diff -u -r1.184 city.h
--- common/city.h       13 Dec 2004 16:23:30 -0000      1.184
+++ common/city.h       14 Dec 2004 22:04:40 -0000
@@ -241,6 +241,7 @@
   int waste[O_MAX]; /* Waste/corruption in each category. */
   int prod[O_MAX]; /* Production is total minus waste. */
   int citizen_base[O_MAX]; /* Base production from citizens. */
+  int usage[O_MAX]; /* Amount of each resource being used. */
 
   /* Cached values for CPU savings. */
   int bonus[O_MAX];
Index: common/packets.def
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.def,v
retrieving revision 1.75
diff -u -r1.75 packets.def
--- common/packets.def  13 Dec 2004 16:20:53 -0000      1.75
+++ common/packets.def  14 Dec 2004 22:04:41 -0000
@@ -404,6 +404,7 @@
   UINT16 waste[O_MAX];
   UINT16 prod[O_MAX];
   SINT16 citizen_base[O_MAX];
+  SINT16 usage[O_MAX];
   UINT16 food_stock, shield_stock;
 
   UINT16 trade[NUM_TRADEROUTES];
Index: server/citytools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/citytools.c,v
retrieving revision 1.293
diff -u -r1.293 citytools.c
--- server/citytools.c  13 Dec 2004 16:20:55 -0000      1.293
+++ server/citytools.c  14 Dec 2004 22:04:42 -0000
@@ -1549,6 +1549,7 @@
     packet->waste[o] = pcity->waste[o];
     packet->prod[o] = pcity->prod[o];
     packet->citizen_base[o] = pcity->citizen_base[o];
+    packet->usage[o] = pcity->usage[o];
   } output_type_iterate_end;
 
   packet->food_stock=pcity->food_stock;

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#11519) put city support values in an array, Jason Short <=