Author: cazfi
Date: Thu Apr 28 15:50:51 2016
New Revision: 32519

URL: http://svn.gna.org/viewcvs/freeciv?rev=32519&view=rev
Log:
Refactored city_unit_unhappiness() to check EFT_MAKE_CONTENT_MIL_PER only when 
result is needed.

See patch #7149

Modified:
    trunk/common/city.c

Modified: trunk/common/city.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/city.c?rev=32519&r1=32518&r2=32519&view=diff
==============================================================================
--- trunk/common/city.c (original)
+++ trunk/common/city.c Thu Apr 28 15:50:51 2016
@@ -2835,24 +2835,39 @@
 **************************************************************************/
 int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
 {
-  struct city *pcity = game_city_by_number(punit->homecity);
-  struct unit_type *ut = unit_type_get(punit);
-  struct player *plr = unit_owner(punit);
-  int happy_cost = utype_happy_cost(ut, plr);
-
-  if (!punit || !pcity || !free_unhappy || happy_cost <= 0) {
+  struct city *pcity;
+  struct unit_type *ut;
+  struct player *plr;
+  int happy_cost;
+
+  if (!punit || !free_unhappy) {
     return 0;
   }
+
+  pcity = game_city_by_number(punit->homecity);
+  if (pcity == NULL) {
+    return 0;
+  }
+
+  ut = unit_type_get(punit);
+  plr = unit_owner(punit);
+  happy_cost = utype_happy_cost(ut, plr);
+
+  if (happy_cost <= 0) {
+    return 0;
+  }
+
   fc_assert_ret_val(0 <= *free_unhappy, 0);
-
-  happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
 
   if (!unit_being_aggressive(punit) && !is_field_unit(punit)) {
     return 0;
   }
+
+  happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
   if (happy_cost <= 0) {
     return 0;
   }
+
   if (*free_unhappy >= happy_cost) {
     *free_unhappy -= happy_cost;
     return 0;
@@ -2860,6 +2875,7 @@
     happy_cost -= *free_unhappy;
     *free_unhappy = 0;
   }
+
   return happy_cost;
 }
 


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to