Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] Re: (PR#9127) city.c improvements
Home

[Freeciv-Dev] Re: (PR#9127) city.c improvements

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#9127) city.c improvements
From: "Per Inge Mathisen" <per@xxxxxxxxxxx>
Date: Tue, 6 Jul 2004 13:12:48 -0700
Reply-to: rt@xxxxxxxxxxx

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

New patch attached. Intend commit very soon along with the new AI
celebration code.

  - Per

Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.221
diff -u -r1.221 city.c
--- common/city.c       25 Jun 2004 23:29:59 -0000      1.221
+++ common/city.c       6 Jul 2004 20:09:45 -0000
@@ -1752,7 +1752,7 @@
   Return the amount of gold generated by buildings under "tithe" attribute
   governments.
 **************************************************************************/
-static int get_city_tithes_bonus(const struct city *pcity)
+int get_city_tithes_bonus(const struct city *pcity)
 {
   int tithes_bonus = 0;
 
@@ -1805,14 +1805,13 @@
 }
 
 /**************************************************************************
-  Modify the incomes according to the taxrates and # of specialists.
+  Get the incomes of a city according to the taxrates (ignore # of 
+  specialists). trade should usually be pcity->trade_prod.
 **************************************************************************/
-static void set_tax_income(struct city *pcity)
+void get_tax_income(struct player *pplayer, int trade, int *sci, 
+                    int *lux, int *tax)
 {
-  int sci, tax, lux, rate = pcity->trade_prod;
-  int sci_rest, tax_rest, lux_rest;
-  struct player *pplayer = city_owner(pcity);
-  int sci_rate, lux_rate, tax_rate;
+  int sci_rest, tax_rest, lux_rest, sci_rate, lux_rate, tax_rate, rate = trade;
 
   if (game.rgame.changable_tax) {
     sci_rate = pplayer->economic.science;
@@ -1825,7 +1824,7 @@
   }
   
   /* ANARCHY */
-  if (get_gov_pcity(pcity)->index == game.government_when_anarchy) {
+  if (get_gov_pplayer(pplayer)->index == game.government_when_anarchy) {
     sci_rate = 0;
     lux_rate = 100;
     tax_rate = 100 - sci_rate - lux_rate;
@@ -1843,81 +1842,85 @@
    * target with the smaller whole-numbered value is chosen.
    */
 
-  sci = (rate * sci_rate) / 100;
-  tax = (rate * tax_rate) / 100;
-  lux = (rate * lux_rate) / 100;
-  
+  *sci = (rate * sci_rate) / 100;
+  *tax = (rate * tax_rate) / 100;
+  *lux = (rate * lux_rate) / 100;
+
   /* these are the fractions multiplied by 100 */
-  sci_rest = rate * sci_rate - sci * 100;
-  tax_rest = rate * tax_rate - tax * 100;
-  lux_rest = rate * lux_rate - lux * 100;
+  sci_rest = rate * sci_rate - (*sci) * 100;
+  tax_rest = rate * tax_rate - (*tax) * 100;
+  lux_rest = rate * lux_rate - (*lux) * 100;
 
-  rate -= (sci + tax + lux);  
+  rate -= ((*sci) + (*tax) + (*lux));
 
   while (rate > 0) {
     if (sci_rest > lux_rest && sci_rest > tax_rest) {
-      sci++;
+      (*sci)++;
       sci_rest = 0;
       rate--;
     }
     if (tax_rest > sci_rest && tax_rest > lux_rest && rate > 0) {
-      tax++;
+      (*tax)++;
       tax_rest = 0;
       rate--;
     }
     if (lux_rest > tax_rest && lux_rest > sci_rest && rate > 0) {
-      lux++;
+      (*lux)++;
       lux_rest = 0;
       rate--;
     }
     if (sci_rest == tax_rest && sci_rest > lux_rest && rate > 0) {
-      if (sci < tax) {
-       sci++;
+      if (*sci < *tax) {
+       (*sci)++;
        sci_rest = 0;
        rate--;
       } else {
-       tax++;
+       (*tax)++;
        tax_rest = 0;
        rate--;
       }
     }
     if (sci_rest == lux_rest && sci_rest > tax_rest && rate > 0) {
-      if (sci < lux) {
-       sci++;
+      if (*sci < *lux) {
+       (*sci)++;
        sci_rest = 0;
        rate--;
       } else {
-       lux++;
+       (*lux)++;
        lux_rest = 0;
        rate--;
       }
     }
     if (tax_rest == lux_rest && tax_rest > sci_rest && rate > 0) {
-      if (tax < lux) {
-       tax++;
+      if (*tax < *lux) {
+       (*tax)++;
        tax_rest = 0;
        rate--;
       } else {
-       lux++;
+       (*lux)++;
        lux_rest = 0;
        rate--;
       }
     }
   }
+  assert(*sci + *tax + *lux == trade);
+}
 
-  assert(sci + tax + lux == pcity->trade_prod);
-
-  pcity->science_total = sci;
-  pcity->tax_total = tax;
-  pcity->luxury_total = lux;
+/**************************************************************************
+  Modify the incomes according to the taxrates and # of specialists.
+**************************************************************************/
+static inline void set_tax_income(struct city *pcity)
+{
+  get_tax_income(city_owner(pcity), pcity->trade_prod, &pcity->science_total, 
+                 &pcity->luxury_total, &pcity->tax_total);
 
   pcity->luxury_total += (pcity->specialists[SP_ELVIS]
                          * game.rgame.specialists[SP_ELVIS].bonus);
   pcity->science_total += (pcity->specialists[SP_SCIENTIST]
                           * game.rgame.specialists[SP_SCIENTIST].bonus);
-  pcity->tax_total += ((pcity->specialists[SP_TAXMAN]
-                       * game.rgame.specialists[SP_TAXMAN].bonus) 
-                      + get_city_tithes_bonus(pcity));
+  pcity->tax_total += (pcity->specialists[SP_TAXMAN]
+                       * game.rgame.specialists[SP_TAXMAN].bonus);
+  pcity->tax_total += get_city_tithes_bonus(pcity);
 }
 
 /**************************************************************************
@@ -1985,44 +1988,54 @@
 }
 
 /**************************************************************************
+  Make people happy: 
+   * angry citizen are eliminated first
+   * then content are made happy, then unhappy content, etc.
+   * each conversions costs 2 or 4 luxuries.
+**************************************************************************/
+static inline void citizen_luxury_happy(const struct city *pcity, int 
*luxuries,
+                                        int *angry, int *unhappy, int *happy, 
+                                        int *content)
+{
+  while (*luxuries >= 2 && *angry > 0) {
+    (*angry)--;
+    (*unhappy)++;
+    *luxuries -= 2;
+  }
+  while (*luxuries >= 2 && *content > 0) {
+    (*content)--;
+    (*happy)++;
+    *luxuries -= 2;
+  }
+  while (*luxuries >= 4 && *unhappy > 0) {
+    (*unhappy)--;
+    (*happy)++;
+    *luxuries -= 4;
+  }
+  if (*luxuries >= 2 && *unhappy > 0) {
+    (*unhappy)--;
+    (*content)++;
+    *luxuries -= 2;
+  }
+}
+
+/**************************************************************************
   Make citizens happy due to luxury.
 **************************************************************************/
-static void citizen_happy_luxury(struct city *pcity)
+static inline void citizen_happy_luxury(struct city *pcity)
 {
   int x = pcity->luxury_total;
 
   happy_copy(pcity, 0);
 
-  /* make people happy: 
-     angry citizen are eliminated first,
-     then content are made happy, then unhappy content, etc.  
-     each conversions costs 2 luxuries. */
-  while (x >= 2 && pcity->ppl_angry[1] > 0) {
-    pcity->ppl_angry[1]--;
-    pcity->ppl_unhappy[1]++;
-    x -= 2;
-  }
-  while (x >= 2 && pcity->ppl_content[1] > 0) {
-    pcity->ppl_content[1]--;
-    pcity->ppl_happy[1]++;
-    x -= 2;
-  }
-  while (x >= 4 && pcity->ppl_unhappy[1] > 0) {
-    pcity->ppl_unhappy[1]--;
-    pcity->ppl_happy[1]++;
-    x -= 4;
-  }
-  if (x >= 2 && pcity->ppl_unhappy[1] > 0) {
-    pcity->ppl_unhappy[1]--;
-    pcity->ppl_content[1]++;
-    x -= 2;
-  }
+  citizen_luxury_happy(pcity, &x, &pcity->ppl_angry[1], 
&pcity->ppl_unhappy[1], 
+                       &pcity->ppl_happy[1], &pcity->ppl_content[1]);
 }
 
 /**************************************************************************
   Make given number of citizens unhappy due to units in the field.
 **************************************************************************/
-static void citizen_unhappy_units(struct city *pcity, int unhap)
+static inline void citizen_unhappy_units(struct city *pcity, int unhap)
 {
   while (unhap > 0 && pcity->ppl_content[3] > 0) {
     pcity->ppl_content[3]--;
@@ -2047,7 +2060,7 @@
 /**************************************************************************
   Make citizens content due to city improvements.
 **************************************************************************/
-static void citizen_content_buildings(struct city *pcity)
+static inline void citizen_content_buildings(struct city *pcity)
 {
   struct government *g = get_gov_pcity(pcity);
   int faces = 0;
@@ -2081,7 +2094,7 @@
 /**************************************************************************
   Make citizens happy due to wonders.
 **************************************************************************/
-static void citizen_happy_wonders(struct city *pcity)
+static inline void citizen_happy_wonders(struct city *pcity)
 {
   int bonus = 0;
 
@@ -2131,7 +2144,7 @@
   Set food, tax, science and shields production to zero if city is in
   revolt.
 **************************************************************************/
-static void unhappy_city_check(struct city *pcity)
+static inline void unhappy_city_check(struct city *pcity)
 {
   if (city_unhappy(pcity)) {
     pcity->food_surplus = MIN(0, pcity->food_surplus);
@@ -2144,7 +2157,7 @@
 /**************************************************************************
   Calculate pollution.
 **************************************************************************/
-static void set_pollution(struct city *pcity)
+static inline void set_pollution(struct city *pcity)
 {
   struct player *pplayer = city_owner(pcity);
 
@@ -2167,36 +2180,40 @@
 }
 
 /**************************************************************************
-  Calculate food, trade and shields generated by a city, and set 
-  associated variables.
+  Calculate food, trade and shields generated by a city, and set
+  associated variables given to us.
 **************************************************************************/
-static void set_food_trade_shields(struct city *pcity)
+void get_food_trade_shields(const struct city *pcity, int *food, int *trade,
+                            int *shields)
 {
-  int i;
   bool is_celebrating = base_city_celebrating(pcity);
 
-  pcity->food_prod = 0;
-  pcity->shield_prod = 0;
-  pcity->trade_prod = 0;
-
-  pcity->food_surplus = 0;
-  pcity->shield_surplus = 0;
-  pcity->corruption = 0;
-  pcity->shield_waste = 0;
+  *food = 0;
+  *trade = 0;
+  *shields = 0;
   
   city_map_iterate(x, y) {
     if (get_worker_city(pcity, x, y) == C_TILE_WORKER) {
-      pcity->food_prod +=
-         base_city_get_food_tile(x, y, pcity, is_celebrating);
-      pcity->shield_prod +=
-         base_city_get_shields_tile(x, y, pcity, is_celebrating);
-      pcity->trade_prod +=
-         base_city_get_trade_tile(x, y, pcity, is_celebrating);
+      *food += base_city_get_food_tile(x, y, pcity, is_celebrating);
+      *shields += base_city_get_shields_tile(x, y, pcity, is_celebrating);
+      *trade += base_city_get_trade_tile(x, y, pcity, is_celebrating);
     }
-  }
-  city_map_iterate_end;
-  pcity->tile_trade = pcity->trade_prod;
+  } city_map_iterate_end;
+}
 
+/**************************************************************************
+   Set food, trade and shields production in a city.
+**************************************************************************/
+static inline void set_food_trade_shields(struct city *pcity)
+{
+  int i;
+  pcity->food_surplus = 0;
+  pcity->shield_surplus = 0;
+
+  get_food_trade_shields(pcity, &pcity->food_prod, &pcity->trade_prod,
+                         &pcity->shield_prod);
+  
+  pcity->tile_trade = pcity->trade_prod;
   pcity->food_surplus = pcity->food_prod - pcity->size * 2;
 
   for (i = 0; i < NUM_TRADEROUTES; i++) {
@@ -2214,9 +2231,9 @@
 /**************************************************************************
   Calculate upkeep costs.
 **************************************************************************/
-static void city_support(struct city *pcity, 
-                        void (*send_unit_info) (struct player *pplayer,
-                                                struct unit *punit))
+static inline void city_support(struct city *pcity, 
+                               void (*send_unit_info) (struct player *pplayer,
+                                                       struct unit *punit))
 {
   struct government *g = get_gov_pcity(pcity);
 
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.149
diff -u -r1.149 city.h
--- common/city.h       14 Jun 2004 23:43:08 -0000      1.149
+++ common/city.h       6 Jul 2004 20:09:45 -0000
@@ -190,6 +190,7 @@
                                    wonders wisely */
   int distance_to_wonder_city;  /* wondercity will set this for us, 
                                    avoiding paradox */
+  bool celebrate;               /* try to celebrate in this city */
 
   /* Used for caching when settlers evalueate which tile to improve,
      and when we place workers. */
@@ -496,6 +497,12 @@
 void city_styles_alloc(int num);
 void city_styles_free(void);
 
+void get_food_trade_shields(const struct city *pcity, int *food, int *trade,
+                            int *shields);
+void get_tax_income(struct player *pplayer, int trade, int *sci,
+                    int *lux, int *tax);
+int get_city_tithes_bonus(const struct city *pcity);
+
 /*
  * Iterates over all improvements which are built in the given city.
  */

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