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

[Freeciv-Dev] (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] (PR#9127) city.c improvements
From: "Per Inge Mathisen" <per@xxxxxxxxxxx>
Date: Sun, 27 Jun 2004 06:25:24 -0700
Reply-to: rt@xxxxxxxxxxx

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

CHANGES:
 - added some inline to static functions called only one other place (or
two places in one case)
 - added the following functions:
   * void get_tax_income(const struct city *pcity, int *sci, int *lux,
                         int *tax)
     get science, luxuries and gold income for a city
   * int happy_citizens_from_luxuries(const struct city *pcity, int
                                      luxuries)
     how many happy citizens does this city get given these luxuries
   * void get_food_trade_shields(const struct city *pcity, int *food,
                                 int *trade, int *shields)
     get number of food, trade and shields produced by a city
 - made city_unhappy() return an int instead of a bool, the int telling
how many citizens have to be made happy to compensate for the unhappy
people
 - made get_city_tithes_bonus() a public function

These changes are used in a patch that I will post in a moment.

  - Per

Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.111
diff -u -r1.111 advdomestic.c
--- ai/advdomestic.c    25 Jun 2004 23:43:00 -0000      1.111
+++ ai/advdomestic.c    27 Jun 2004 13:22:31 -0000
@@ -238,9 +238,10 @@
   while (happy > 0 && sad > 0) { happy--; sad--; value += SADVAL; }
   
   /* Desperately seeking Colosseum - we need happy improvements urgently. */
-  if (city_unhappy(pcity))
+  if (city_unhappy(pcity) > 0) {
     value += SADVAL * (sad + content);
-  
+  }
+ 
   /* Usage of (happy && content) led to a lack of foresight, especially
    * re: Chapel -- Syela */
   while (happy > 0) { happy--; value += SADVAL; }
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.156
diff -u -r1.156 aicity.c
--- ai/aicity.c 25 Jun 2004 23:43:00 -0000      1.156
+++ ai/aicity.c 27 Jun 2004 13:22:32 -0000
@@ -52,7 +52,7 @@
 #include "aicity.h"
 
 #define CITY_EMERGENCY(pcity)                        \
- (pcity->shield_surplus < 0 || city_unhappy(pcity)   \
+ (pcity->shield_surplus < 0 || city_unhappy(pcity) > 0 \
   || pcity->food_stock + pcity->food_surplus < 0)
 #define LOG_BUY LOG_DEBUG
 
@@ -678,7 +678,7 @@
 
   freelog(LOG_EMERGENCY,
           "Emergency in %s (%s, angry%d, unhap%d food%d, prod%d)",
-          pcity->name, city_unhappy(pcity) ? "unhappy" : "content",
+          pcity->name, city_unhappy(pcity) > 0 ? "unhappy" : "content",
           pcity->ppl_angry[4], pcity->ppl_unhappy[4],
           pcity->food_surplus, pcity->shield_surplus);
 
@@ -712,7 +712,7 @@
   }
 
   unit_list_iterate_safe(pcity->units_supported, punit) {
-    if (city_unhappy(pcity)
+    if (city_unhappy(pcity) > 0
         && punit->unhappiness != 0
         && punit->ai.passenger == 0) {
       UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
Index: client/cityrepdata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/cityrepdata.c,v
retrieving revision 1.33
diff -u -r1.33 cityrepdata.c
--- client/cityrepdata.c        27 May 2004 22:14:18 -0000      1.33
+++ client/cityrepdata.c        27 Jun 2004 13:22:32 -0000
@@ -61,7 +61,7 @@
 {
   static char buf[4];
   my_snprintf(buf, sizeof(buf), "%s", (city_celebrating(pcity) ? "*" :
-                                      (city_unhappy(pcity) ? "X" : " ")));
+                                      (city_unhappy(pcity) > 0 ? "X" : " ")));
   return buf;
 }
 
@@ -70,7 +70,7 @@
   static char buf[16];
   my_snprintf(buf, sizeof(buf), "%s",
              (city_celebrating(pcity) ? Q_("?city_state:Rapture") :
-              (city_unhappy(pcity) ? Q_("?city_state:Disorder") :
+              (city_unhappy(pcity) > 0 ? Q_("?city_state:Disorder") :
                Q_("?city_state:Peace"))));
   return buf;
 }
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.382
diff -u -r1.382 packhand.c
--- client/packhand.c   25 Jun 2004 23:35:55 -0000      1.382
+++ client/packhand.c   27 Jun 2004 13:22:33 -0000
@@ -516,7 +516,7 @@
   }
 
   pcity->client.happy = city_happy(pcity);
-  pcity->client.unhappy = city_unhappy(pcity);
+  pcity->client.unhappy = city_unhappy(pcity) > 0;
 
   popup = (city_is_new && can_client_change_view()
            && pcity->owner == game.player_idx && popup_new_cities)
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       27 Jun 2004 13:22:34 -0000
@@ -1252,7 +1252,7 @@
 bool city_unhappy(const struct city *pcity)
 {
   return (pcity->ppl_happy[4] <
-         pcity->ppl_unhappy[4] + 2 * pcity->ppl_angry[4]);
+          pcity->ppl_unhappy[4] + 2 * pcity->ppl_angry[4]);
 }
 
 /**************************************************************************
@@ -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,70 @@
 }
 
 /**************************************************************************
+  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;
+  }
+}
+
+/**************************************************************************
+  For a given amount of luxuries, how many happy or content do we get?
+**************************************************************************/
+int happy_citizens_from_luxuries(const struct city *pcity, int luxuries)
+{
+  int happy = pcity->ppl_happy[1];
+  int unhappy = pcity->ppl_unhappy[1];
+  int content = pcity->ppl_content[1];
+  int angry = pcity->ppl_angry[1];
+  int lux = luxuries;
+
+  citizen_luxury_happy(pcity, &lux, &angry, &unhappy, &happy, &content);
+
+  return happy;
+}
+
+/**************************************************************************
   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 +2076,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 +2110,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,9 +2160,9 @@
   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)) {
+  if (city_unhappy(pcity) > 0) {
     pcity->food_surplus = MIN(0, pcity->food_surplus);
     pcity->tax_total = 0;
     pcity->science_total = 0;
@@ -2144,7 +2173,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 +2196,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 +2247,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       27 Jun 2004 13:22:34 -0000
@@ -349,7 +349,7 @@
 int city_gold_surplus(const struct city *pcity);
 int city_buy_cost(const struct city *pcity);
 bool city_happy(const struct city *pcity);  /* generally use celebrating 
instead */
-bool city_unhappy(const struct city *pcity);                /* anarchy??? */
+bool city_unhappy(const struct city *pcity);
 bool base_city_celebrating(const struct city *pcity);
 bool city_celebrating(const struct city *pcity);            /* love the king 
??? */
 bool city_rapture_grow(const struct city *pcity);
@@ -496,6 +496,13 @@
 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);
+int happy_citizens_from_luxuries(const struct city *pcity, int luxuries);
+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.
  */
Index: server/citytools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/citytools.c,v
retrieving revision 1.261
diff -u -r1.261 citytools.c
--- server/citytools.c  14 Jun 2004 23:43:08 -0000      1.261
+++ server/citytools.c  27 Jun 2004 13:22:35 -0000
@@ -1768,7 +1768,7 @@
    * unit list to check the occupied status. */
   bool occupied =
     (unit_list_size(&(map_get_tile(pcity->x, pcity->y)->units)) > 0);
-  bool happy = city_happy(pcity), unhappy = city_unhappy(pcity);
+  bool happy = city_happy(pcity), unhappy = city_unhappy(pcity) > 0;
  
   if (pdcity
       && pdcity->id == pcity->id
Index: server/cityturn.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v
retrieving revision 1.249
diff -u -r1.249 cityturn.c
--- server/cityturn.c   29 May 2004 20:34:31 -0000      1.249
+++ server/cityturn.c   27 Jun 2004 13:22:35 -0000
@@ -1216,7 +1216,7 @@
 
   /* Stability bonuses */
   if (g->index != game.government_when_anarchy) {
-    if (!city_unhappy(pcity)) {
+    if (!city_unhappy(pcity) > 0) {
       cost *= 2;
     }
     if (city_celebrating(pcity)) {
@@ -1364,7 +1364,7 @@
     pay_for_units(pplayer, pcity);
     pay_for_buildings(pplayer, pcity);
 
-    if(city_unhappy(pcity)) { 
+    if (city_unhappy(pcity) > 0) { 
       pcity->anarchy++;
       if (pcity->anarchy == 1) 
         notify_player_ex(pplayer, pcity->x, pcity->y, E_CITY_DISORDER,

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9127) city.c improvements, Per Inge Mathisen <=