Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#11483) remove city_gold_surplus
Home

[Freeciv-Dev] (PR#11483) remove city_gold_surplus

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11483) remove city_gold_surplus
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 11 Dec 2004 15:31:13 -0800
Reply-to: bugs@xxxxxxxxxxx

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

All of city_gold_surplus's users just want to get the current surplus. 
This is held in pcity->surplus[O_GOLD] so there's no need for this function.

For internal use (and future AI use) I add two simpler functions in its 
place: city_unit_upkeep() and city_building_upkeep().  These return the 
upkeep needed to maintain all units or buildings in the city.

-jason

Index: client/cityrepdata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/cityrepdata.c,v
retrieving revision 1.44
diff -u -r1.44 cityrepdata.c
--- client/cityrepdata.c        11 Dec 2004 10:34:46 -0000      1.44
+++ client/cityrepdata.c        11 Dec 2004 23:28:31 -0000
@@ -271,9 +271,8 @@
                                   const void *data)
 {
   static char buf[32];
-  int goldie;
+  int goldie = pcity->surplus[O_GOLD];
 
-  goldie = city_gold_surplus(pcity, pcity->prod[O_GOLD]);
   my_snprintf(buf, sizeof(buf), "%s%d/%d/%d",
              (goldie < 0) ? "-" : (goldie > 0) ? "+" : "",
              (goldie < 0) ? (-goldie) : goldie,
@@ -286,11 +285,11 @@
                                 const void *data)
 {
   static char buf[8];
-  int income = city_gold_surplus(pcity, pcity->prod[O_GOLD]);
-  if (income > 0) {
-    my_snprintf(buf, sizeof(buf), "+%d", income);
+
+  if (pcity->surplus[O_GOLD] > 0) {
+    my_snprintf(buf, sizeof(buf), "+%d", pcity->surplus[O_GOLD]);
   } else {
-    my_snprintf(buf, sizeof(buf), "%3d", city_gold_surplus(pcity, 
pcity->prod[O_GOLD]));
+    my_snprintf(buf, sizeof(buf), "%3d", pcity->surplus[O_GOLD]);
   }
   return buf;
 }
Index: client/gui-gtk/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
retrieving revision 1.190
diff -u -r1.190 citydlg.c
--- client/gui-gtk/citydlg.c    11 Dec 2004 10:34:46 -0000      1.190
+++ client/gui-gtk/citydlg.c    11 Dec 2004 23:28:32 -0000
@@ -1697,7 +1697,7 @@
              pcity->surplus[O_TRADE] + pcity->waste[O_TRADE],
              pcity->surplus[O_TRADE]);
   my_snprintf(buf[GOLD], sizeof(buf[GOLD]), "%2d (%+2d)",
-             pcity->prod[O_GOLD], city_gold_surplus(pcity, 
pcity->prod[O_GOLD]));
+             pcity->prod[O_GOLD], pcity->surplus[O_GOLD]);
   my_snprintf(buf[LUXURY], sizeof(buf[LUXURY]), "%2d      ",
              pcity->prod[O_LUXURY]);
 
Index: client/gui-gtk-2.0/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/citydlg.c,v
retrieving revision 1.105
diff -u -r1.105 citydlg.c
--- client/gui-gtk-2.0/citydlg.c        11 Dec 2004 10:34:46 -0000      1.105
+++ client/gui-gtk-2.0/citydlg.c        11 Dec 2004 23:28:32 -0000
@@ -1328,7 +1328,7 @@
              pcity->surplus[O_TRADE] + pcity->waste[O_TRADE],
              pcity->surplus[O_TRADE]);
   my_snprintf(buf[GOLD], sizeof(buf[GOLD]), "%2d (%+2d)",
-             pcity->prod[O_GOLD], city_gold_surplus(pcity, 
pcity->prod[O_GOLD]));
+             pcity->prod[O_GOLD], pcity->surplus[O_GOLD]);
   my_snprintf(buf[LUXURY], sizeof(buf[LUXURY]), "%2d      ",
              pcity->prod[O_LUXURY]);
 
Index: client/gui-mui/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/citydlg.c,v
retrieving revision 1.88
diff -u -r1.88 citydlg.c
--- client/gui-mui/citydlg.c    11 Dec 2004 10:34:46 -0000      1.88
+++ client/gui-mui/citydlg.c    11 Dec 2004 23:28:32 -0000
@@ -1811,7 +1811,8 @@
   settextf(info->trade_text, "%2d (%+2d)",
           pcity->trade_prod + pcity->waste[O_TRADE],
           pcity->trade_prod);
-  settextf(info->gold_text, "%2d (%+2d)", pcity->prod[O_GOLD], 
city_gold_surplus(pcity, pcity->prod[O_GOLD]));
+  settextf(info->gold_text, "%2d (%+2d)", pcity->prod[O_GOLD],
+          pcity->surplus[O_GOLD]);
   settextf(info->luxury_text, "%2d", pcity->prod[O_LUXURY]);
   settextf(info->science_text, "%2d", pcity->prod[O_SCIENCE]);
 
Index: client/gui-sdl/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/citydlg.c,v
retrieving revision 1.48
diff -u -r1.48 citydlg.c
--- client/gui-sdl/citydlg.c    11 Dec 2004 10:34:46 -0000      1.48
+++ client/gui-sdl/citydlg.c    11 Dec 2004 23:28:33 -0000
@@ -2966,7 +2966,7 @@
   /* ================================================================= */
   /* gold label */
   my_snprintf(cBuf, sizeof(cBuf), _("Gold: %d (%d) per turn"),
-             city_gold_surplus(pCity, pcity->prod[O_GOLD]), 
pCity->prod[O_GOLD]);
+             pCity->surplus[O_GOLD], pCity->prod[O_GOLD]);
 
   copy_chars_to_string16(pStr, cBuf);
   pStr->fgcol = *get_game_colorRGB(COLOR_STD_CITY_GOLD);
@@ -2981,7 +2981,7 @@
   FREESURFACE(pBuf);
 
   /* draw coins */
-  count = city_gold_surplus(pCity, pcity->prod[O_GOLD]);
+  count = pcity->surplus[O_GOLD];
   if (count) {
 
     if (count > 0) {
@@ -3012,8 +3012,8 @@
   }
 
   /* upkeep label */
-  my_snprintf(cBuf, sizeof(cBuf), _("Upkeep : %d"), pCity->prod[O_GOLD] -
-             city_gold_surplus(pCity, pcity->prod[O_GOLD]));
+  my_snprintf(cBuf, sizeof(cBuf), _("Upkeep : %d"),
+             pCity->prod[O_GOLD] - pcity->surplus[O_GOLD]);
 
   copy_chars_to_string16(pStr, cBuf);
   pStr->fgcol = *get_game_colorRGB(COLOR_STD_CITY_UNKEEP);
@@ -3028,7 +3028,7 @@
   FREESURFACE(pBuf);
 
   /* draw upkeep */
-  count = city_gold_surplus(pCity, pcity->prod[O_GOLD]);
+  count = pcity->surplus[O_GOLD];
   if (pCity->prod[O_GOLD] - count) {
 
     dest.x = pWindow->size.x + 423;
Index: client/gui-sdl/cityrep.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/cityrep.c,v
retrieving revision 1.17
diff -u -r1.17 cityrep.c
--- client/gui-sdl/cityrep.c    11 Dec 2004 10:34:46 -0000      1.17
+++ client/gui-sdl/cityrep.c    11 Dec 2004 23:28:33 -0000
@@ -386,7 +386,7 @@
     add_to_gui_list(MAX_ID - pCity->id, pBuf);
             
     /* ----------- */
-    my_snprintf(cBuf, sizeof(cBuf), "%d", city_gold_surplus(pCity, 
pcity->prod[O_GOLD]));
+    my_snprintf(cBuf, sizeof(cBuf), "%d", pcity->surplus[O_GOLD]);
     pStr = create_str16_from_char(cBuf, 10);
     pStr->style |= SF_CENTER;
     pStr->fgcol = *get_game_colorRGB(COLOR_STD_CITY_GOLD);
@@ -974,7 +974,7 @@
             
   /* gold surplus */
   pWidget = pWidget->prev;
-  my_snprintf(cBuf, sizeof(cBuf), "%d", city_gold_surplus(pCity, 
pcity->prod[O_GOLD]));
+  my_snprintf(cBuf, sizeof(cBuf), "%d", pcity->surplus[O_GOLD]);
   copy_chars_to_string16(pWidget->string16, cBuf);
     
   /* science income */
Index: client/gui-win32/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/citydlg.c,v
retrieving revision 1.89
diff -u -r1.89 citydlg.c
--- client/gui-win32/citydlg.c  11 Dec 2004 10:34:47 -0000      1.89
+++ client/gui-win32/citydlg.c  11 Dec 2004 23:28:33 -0000
@@ -449,8 +449,7 @@
              pcity->surplus[O_TRADE] + pcity->waste[O_TRADE],
              pcity->surplus[O_TRADE]);
   my_snprintf(buf[GOLD], sizeof(buf[GOLD]), "%2d (%+2d)",
-             pcity->prod[O_GOLD],
-             city_gold_surplus(pcity, pcity->prod[O_GOLD]));
+             pcity->prod[O_GOLD], pcity->surplus[O_GOLD]);
   my_snprintf(buf[LUXURY], sizeof(buf[LUXURY]), "%2d      ",
              pcity->prod[O_LUXURY]);
 
Index: client/gui-xaw/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/citydlg.c,v
retrieving revision 1.126
diff -u -r1.126 citydlg.c
--- client/gui-xaw/citydlg.c    11 Dec 2004 10:34:47 -0000      1.126
+++ client/gui-xaw/citydlg.c    11 Dec 2004 23:28:34 -0000
@@ -271,7 +271,7 @@
   if (pdialog) {
     pcity=pdialog->pcity;
     goldtotal=pcity->prod[O_GOLD];
-    goldsurplus=city_gold_surplus(pcity, pcity->prod[O_GOLD]);
+    goldsurplus = pcity->surplus[O_GOLD];
     luxtotal=pcity->prod[O_LUXURY];
     scitotal=pcity->prod[O_SCIENCE];
   }
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.281
diff -u -r1.281 city.c
--- common/city.c       11 Dec 2004 10:41:26 -0000      1.281
+++ common/city.c       11 Dec 2004 23:28:34 -0000
@@ -1082,24 +1082,33 @@
 }
 
 /*************************************************************************
-  Calculate amount of gold remaining in city after paying for buildings 
-  and units.  Does not count capitalization.
+  Calculate how much is needed to pay for buildings in this city.
 *************************************************************************/
-int city_gold_surplus(const struct city *pcity, int tax_total)
+int city_building_upkeep(const struct city *pcity, Output_type_id otype)
 {
   int cost = 0;
 
-  built_impr_iterate(pcity, i) {
-    cost += improvement_upkeep(pcity, i);
-  } built_impr_iterate_end;
+  if (otype == O_GOLD) {
+    built_impr_iterate(pcity, i) {
+      cost += improvement_upkeep(pcity, i);
+    } built_impr_iterate_end;
+  }
+
+  return cost;
+}
+
+/*************************************************************************
+  Calculate how much is needed to pay for units in this city.
+*************************************************************************/
+int city_unit_upkeep(const struct city *pcity, Output_type_id otype)
+{
+  int cost = 0;
 
   unit_list_iterate(pcity->units_supported, punit) {
-    /* FIXME: unlike food and shields, gold upkeep isn't subtracted off of
-     * the surplus directly when calculating unit support. */
-    cost += punit->upkeep[O_GOLD];
+    cost += punit->upkeep[otype];
   } unit_list_iterate_end;
 
-  return tax_total - cost;
+  return cost;
 }
 
 /**************************************************************************
@@ -1762,7 +1771,11 @@
 {
   pcity->surplus[O_SCIENCE] = pcity->prod[O_SCIENCE];
   pcity->surplus[O_LUXURY] = pcity->prod[O_LUXURY];
-  pcity->surplus[O_GOLD] = city_gold_surplus(pcity, pcity->prod[O_GOLD]);
+
+  /* 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);
 }
 
 /**************************************************************************
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.182
diff -u -r1.182 city.h
--- common/city.h       11 Dec 2004 10:34:47 -0000      1.182
+++ common/city.h       11 Dec 2004 23:28:34 -0000
@@ -365,7 +365,8 @@
 
 struct player *city_owner(const struct city *pcity);
 int city_population(const struct city *pcity);
-int city_gold_surplus(const struct city *pcity, int tax_total);
+int city_building_upkeep(const struct city *pcity, Output_type_id otype);
+int city_unit_upkeep(const struct city *pcity, Output_type_id otype);
 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??? */
Index: common/player.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/player.c,v
retrieving revision 1.163
diff -u -r1.163 player.c
--- common/player.c     11 Dec 2004 10:34:47 -0000      1.163
+++ common/player.c     11 Dec 2004 23:28:34 -0000
@@ -400,7 +400,7 @@
   /* City income/expenses. */
   city_list_iterate(pplayer->cities, pcity) {
     /* Gold suplus accounts for imcome plus building and unit upkeep. */
-    income += city_gold_surplus(pcity, pcity->prod[O_GOLD]);
+    income += pcity->surplus[O_GOLD];
 
     /* Capitalization income. */
     if (get_current_construction_bonus(pcity, EFT_PROD_TO_GOLD) > 0) {
Index: common/aicore/cm.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/aicore/cm.c,v
retrieving revision 1.52
diff -u -r1.52 cm.c
--- common/aicore/cm.c  11 Dec 2004 10:34:47 -0000      1.52
+++ common/aicore/cm.c  11 Dec 2004 23:28:35 -0000
@@ -2069,7 +2069,7 @@
   freelog(LOG_NORMAL, "  trade   = %3d", pcity->surplus[O_TRADE]);
 
   freelog(LOG_NORMAL, "  gold    = %3d (%+3d)", pcity->prod[O_GOLD],
-          city_gold_surplus(pcity, pcity->prod[O_GOLD]));
+         pcity->surplus[O_GOLD]);
   freelog(LOG_NORMAL, "  luxury  = %3d", pcity->prod[O_LUXURY]);
   freelog(LOG_NORMAL, "  science = %3d", pcity->prod[O_SCIENCE]);
 }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#11483) remove city_gold_surplus, Jason Short <=