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

calculate the upkeep for all units of all cities at the start of the turn and 
save it in the unit struct

depends on ticket 40619

compile tested and also with a short game

diff -ur freeciv-2.1.99svn15584.gold//common/unit.c freeciv-2.1.99svn15584.gold1//common/unit.c
--- freeciv-2.1.99svn15584.gold//common/unit.c	2009-04-11 12:41:20.514129345 +0200
+++ freeciv-2.1.99svn15584.gold1//common/unit.c	2009-04-11 12:39:06.000000000 +0200
@@ -1373,6 +1373,8 @@
     punit->tile = NULL;
     punit->homecity = IDENTITY_NUMBER_ZERO;
   }
+  /* upkeep will be set each turn within the cityturn loop */
+  memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
   punit->goto_tile = NULL;
   punit->veteran = veteran_level;
   /* A unit new and fresh ... */
diff -ur freeciv-2.1.99svn15584.gold//common/unit.h freeciv-2.1.99svn15584.gold1//common/unit.h
--- freeciv-2.1.99svn15584.gold//common/unit.h	2009-04-11 12:41:19.382132035 +0200
+++ freeciv-2.1.99svn15584.gold1//common/unit.h	2009-04-11 12:39:05.000000000 +0200
@@ -142,6 +142,7 @@
   struct player *owner; /* Cannot be NULL. */
   int id;
   int homecity;
+  int upkeep[O_LAST]; /* unit upkeep with regards to the homecity */
 
   int moves_left;
   int hp;
diff -ur freeciv-2.1.99svn15584.gold//server/cityturn.c freeciv-2.1.99svn15584.gold1//server/cityturn.c
--- freeciv-2.1.99svn15584.gold//server/cityturn.c	2009-04-11 18:04:28.550131515 +0200
+++ freeciv-2.1.99svn15584.gold1//server/cityturn.c	2009-04-12 00:48:34.806125540 +0200
@@ -93,10 +93,10 @@
 #define SPECVEC_TYPE struct cityimpr
 #include "specvec.h"
 
-/* Helper struct for storing a unit with its gold upkeep. */
+/* Helper struct for storing all units with gold upkeep.
+ * Replace this by unit_list? - MaPfa */
 struct unitgold {
   struct unit *punit;
-  int gold_upkeep;
 };
 
 #define SPECVEC_TAG unitgold
@@ -117,6 +117,7 @@
 static void define_orig_production_values(struct city *pcity);
 static void update_city_activity(struct city *pcity);
 static void nullify_caravan_and_disband_plus(struct city *pcity);
+static void calc_city_units_upkeep(const struct city *pcity);
 
 static float city_migration_score(const struct city *pcity);
 static bool do_city_migration(struct city *pcity_from,
@@ -501,6 +502,33 @@
 }
 
 /**************************************************************************
+  Update upkeep needed for all units supported by the city
+**************************************************************************/
+static void calc_city_units_upkeep(const struct city *pcity)
+{
+  int free[O_LAST], upkeep[O_LAST];
+
+  if (!pcity || !pcity->units_supported
+      || unit_list_size(pcity->units_supported) < 1) {
+    return;
+  }
+
+  memset(free, 0, O_COUNT * sizeof(*free));
+  output_type_iterate(o) {
+    free[o] = get_city_output_bonus(pcity, get_output_type(o),
+                                    EFT_UNIT_UPKEEP_FREE_PER_CITY);
+  } output_type_iterate_end;
+
+  /* save the upkeep for all units in the corresponding punit struct */
+  unit_list_iterate(pcity->units_supported, punit) {
+    city_unit_upkeep(punit, upkeep, free);
+    output_type_iterate(o) {
+      punit->upkeep[o] = upkeep[o];
+    } output_type_iterate_end;
+  } unit_list_iterate_end;
+}
+
+/**************************************************************************
   Reduce the city specialists by some (positive) value.
   Return the amount of reduction.
 **************************************************************************/
@@ -1649,20 +1677,14 @@
 static int city_total_unit_gold_upkeep(const struct city *pcity)
 {
   int gold_needed = 0;
-  int free[O_COUNT], upkeep[O_COUNT];
 
   if (!pcity || !pcity->units_supported
       || unit_list_size(pcity->units_supported) < 1) {
     return 0;
   }
 
-  memset(free, 0, O_COUNT * sizeof(*free));
-  free[O_GOLD] = get_city_output_bonus(pcity, get_output_type(O_GOLD),
-                                       EFT_UNIT_UPKEEP_FREE_PER_CITY);
-
   unit_list_iterate(pcity->units_supported, punit) {
-    city_unit_upkeep(punit, upkeep, free);
-    gold_needed += upkeep[O_GOLD];
+    gold_needed += punit->upkeep[O_GOLD];
   } unit_list_iterate_end;
 
   return gold_needed;
@@ -1734,7 +1756,7 @@
   while (pplayer->economic.gold < 0 && n > 0) {
     r = myrand(n);
     punit = units->p[r].punit;
-    gold_upkeep = units->p[r].gold_upkeep;
+    gold_upkeep = punit->upkeep[O_GOLD];
 
     notify_player(pplayer, unit_tile(punit), E_UNIT_LOST_MISC,
                   _("Not enough gold. %s disbanded"),
@@ -1760,7 +1782,6 @@
   struct cityimpr ci;
   struct unitgold_vector units;
   struct unitgold ug;
-  int free[O_COUNT], upkeep[O_COUNT];
 
   if (!pplayer) {
     return;
@@ -1783,16 +1804,10 @@
     goto CLEANUP;
   }
 
-  memset(free, 0, O_COUNT * sizeof(*free));
-
   city_list_iterate(pplayer->cities, pcity) {
-    free[O_GOLD] = get_city_output_bonus(pcity, get_output_type(O_GOLD),
-                                         EFT_UNIT_UPKEEP_FREE_PER_CITY);
     unit_list_iterate(pcity->units_supported, punit) {
-      city_unit_upkeep(punit, upkeep, free);
-      if (upkeep[O_GOLD] > 0) {
+      if (punit->upkeep[O_GOLD] > 0) {
         ug.punit = punit;
-        ug.gold_upkeep = upkeep[O_GOLD];
         unitgold_vector_append(&units, &ug);
       }
     } unit_list_iterate_end;
@@ -1826,7 +1841,6 @@
   struct cityimpr ci;
   struct unitgold_vector units;
   struct unitgold ug;
-  int free[O_COUNT], upkeep[O_COUNT];
 
   if (!pcity) {
     return;
@@ -1850,16 +1864,10 @@
     goto CLEANUP;
   }
 
-  memset(free, 0, O_COUNT * sizeof(*free));
-  free[O_GOLD] = get_city_output_bonus(pcity, get_output_type(O_GOLD),
-                                       EFT_UNIT_UPKEEP_FREE_PER_CITY);
-
   /* Create a vector of all supported units with gold upkeep. */
   unit_list_iterate(pcity->units_supported, punit) {
-    city_unit_upkeep(punit, upkeep, free);
-    if (upkeep[O_GOLD] > 0) {
+    if (punit->upkeep[O_GOLD] > 0) {
       ug.punit = punit;
-      ug.gold_upkeep = upkeep[O_GOLD];
       unitgold_vector_append(&units, &ug);
     }
   } unit_list_iterate_end;
@@ -2043,6 +2051,9 @@
   pplayer = city_owner(pcity);
   gov = government_of_city(pcity);
 
+  /* update upkeep of suported units */
+  calc_city_units_upkeep(pcity);
+
   city_refresh(pcity);
 
   /* Reporting of celebrations rewritten, copying the treatment of disorder below,
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to