From 2e798b488d4750c99025a6718c4501812247aa2d Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 17 Nov 2024 02:47:40 +0200
Subject: [PATCH 31/31] Unhardcode wld.map from city_unit_unhappiness()

See RM #1130

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/aitools.c          | 5 +++--
 ai/default/aitools.h          | 3 ++-
 ai/default/aiunit.c           | 2 +-
 ai/default/daimilitary.c      | 4 +++-
 client/gui-gtk-3.0/citydlg.c  | 3 ++-
 client/gui-gtk-3.22/citydlg.c | 3 ++-
 client/gui-gtk-4.0/citydlg.c  | 3 ++-
 client/gui-qt/citydlg.cpp     | 4 +++-
 client/gui-sdl2/citydlg.c     | 3 ++-
 common/city.c                 | 7 ++++---
 common/city.h                 | 9 +++++----
 11 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/ai/default/aitools.c b/ai/default/aitools.c
index 7fb4dd7276..b9fcf7769a 100644
--- a/ai/default/aitools.c
+++ b/ai/default/aitools.c
@@ -1396,7 +1396,8 @@ void dai_build_adv_override(struct ai_type *ait, struct city *pcity,
   sure whether it is fully general for all possible parameters/
   combinations." --dwp
 **************************************************************************/
-bool dai_assess_military_unhappiness(struct city *pcity)
+bool dai_assess_military_unhappiness(const struct civ_map *nmap,
+                                     struct city *pcity)
 {
   int free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
   int unhap = 0;
@@ -1407,7 +1408,7 @@ bool dai_assess_military_unhappiness(struct city *pcity)
   }
 
   unit_list_iterate(pcity->units_supported, punit) {
-    int happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    int happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
 
     if (happy_cost > 0) {
       unhap += happy_cost;
diff --git a/ai/default/aitools.h b/ai/default/aitools.h
index 204d0b8eff..ca002ca921 100644
--- a/ai/default/aitools.h
+++ b/ai/default/aitools.h
@@ -78,7 +78,8 @@ bool dai_choose_role_unit(struct ai_type *ait, struct player *pplayer,
                           bool need_boat);
 void dai_build_adv_override(struct ai_type *ait, struct city *pcity,
                             struct adv_choice *choice);
-bool dai_assess_military_unhappiness(struct city *pcity);
+bool dai_assess_military_unhappiness(const struct civ_map *nmap,
+                                     struct city *pcity);
 
 void dai_consider_plr_dangerous(struct ai_type *ait, struct player *plr1,
                                 struct player *plr2,
diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c
index 652e97b008..5e49ddeb3e 100644
--- a/ai/default/aiunit.c
+++ b/ai/default/aiunit.c
@@ -1275,7 +1275,7 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
   if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
     /* I would have thought unhappiness should be taken into account
      * irrespectfully the city in which it will surface... -- GB */
-    unhap = dai_assess_military_unhappiness(pcity);
+    unhap = dai_assess_military_unhappiness(nmap, pcity);
   }
 
   bcost = unit_build_shield_cost_base(punit);
diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c
index 6238b7633d..72701ea52c 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -1230,13 +1230,15 @@ static void process_attacker_want(struct ai_type *ait,
   const struct unit_type *orig_utype = best_choice->value.utype;
   int victim_count = 1;
   int needferry = 0;
-  bool unhap = dai_assess_military_unhappiness(pcity);
+  bool unhap;
   struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
   const struct civ_map *nmap = &(wld.map);
 
   /* Has to be initialized to make gcc happy */
   struct ai_city *acity_data = NULL;
 
+  unhap = dai_assess_military_unhappiness(nmap, pcity);
+
   if (acity != NULL) {
     acity_data = def_ai_city_data(acity, ait);
   }
diff --git a/client/gui-gtk-3.0/citydlg.c b/client/gui-gtk-3.0/citydlg.c
index 19ebaab08b..73baae3d71 100644
--- a/client/gui-gtk-3.0/citydlg.c
+++ b/client/gui-gtk-3.0/citydlg.c
@@ -2109,6 +2109,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   int n, m, i;
   gchar *buf;
   int free_unhappy = get_city_bonus(pdialog->pcity, EFT_MAKE_CONTENT_MIL);
+  const struct civ_map *nmap = &(wld.map);
 
   if (NULL != client.conn.playing
       && city_owner(pdialog->pcity) != client.conn.playing) {
@@ -2158,7 +2159,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   i = 0;
   unit_list_iterate(units, punit) {
     struct unit_node *pnode;
-    int happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    int happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
 
     pnode = unit_node_vector_get(nodes, i);
     if (pnode) {
diff --git a/client/gui-gtk-3.22/citydlg.c b/client/gui-gtk-3.22/citydlg.c
index 726fdd1b01..f8a5be0107 100644
--- a/client/gui-gtk-3.22/citydlg.c
+++ b/client/gui-gtk-3.22/citydlg.c
@@ -2151,6 +2151,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   int n, m, i;
   gchar *buf;
   int free_unhappy = get_city_bonus(pdialog->pcity, EFT_MAKE_CONTENT_MIL);
+  const struct civ_map *nmap = &(wld.map);
 
   if (NULL != client.conn.playing
       && city_owner(pdialog->pcity) != client.conn.playing) {
@@ -2200,7 +2201,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   i = 0;
   unit_list_iterate(units, punit) {
     struct unit_node *pnode;
-    int happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    int happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
 
     pnode = unit_node_vector_get(nodes, i);
     if (pnode) {
diff --git a/client/gui-gtk-4.0/citydlg.c b/client/gui-gtk-4.0/citydlg.c
index 2c8aabe84d..ad34dd16dd 100644
--- a/client/gui-gtk-4.0/citydlg.c
+++ b/client/gui-gtk-4.0/citydlg.c
@@ -2156,6 +2156,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   int n, m, i;
   gchar *buf;
   int free_unhappy = get_city_bonus(pdialog->pcity, EFT_MAKE_CONTENT_MIL);
+  const struct civ_map *nmap = &(wld.map);
 
   if (NULL != client.conn.playing
       && city_owner(pdialog->pcity) != client.conn.playing) {
@@ -2209,7 +2210,7 @@ static void city_dialog_update_supported_units(struct city_dialog *pdialog)
   i = 0;
   unit_list_iterate(units, punit) {
     struct unit_node *pnode;
-    int happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    int happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
 
     pnode = unit_node_vector_get(nodes, i);
     if (pnode) {
diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp
index 8879fbbb04..7a7a01dc9a 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -3202,6 +3202,8 @@ void city_dialog::update_units()
   int n;
   int happy_cost;
   int free_unhappy = get_city_bonus(dlgcity, EFT_MAKE_CONTENT_MIL);
+  const struct civ_map *nmap = &(wld.map);
+
   supported_units->setUpdatesEnabled(false);
   supported_units->clear_layout();
 
@@ -3213,7 +3215,7 @@ void city_dialog::update_units()
   }
 
   unit_list_iterate(units, punit) {
-    happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
     ui = new unit_item(this, punit, true, happy_cost);
     ui->init_pix();
     supported_units->add_item(ui);
diff --git a/client/gui-sdl2/citydlg.c b/client/gui-sdl2/citydlg.c
index d7940fc9ca..83d47b3979 100644
--- a/client/gui-sdl2/citydlg.c
+++ b/client/gui-sdl2/citydlg.c
@@ -622,6 +622,7 @@ static SDL_Surface *create_unit_surface(struct unit *punit, bool support,
   SDL_Rect src_rect;
   SDL_Surface *psurf;
   struct canvas *destcanvas;
+  const struct civ_map *nmap = &(wld.map);
 
   destcanvas = canvas_create(tileset_full_tile_width(tileset),
                              tileset_unit_with_small_upkeep_height(tileset));
@@ -645,7 +646,7 @@ static SDL_Surface *create_unit_surface(struct unit *punit, bool support,
     src_rect.h = destcanvas->surf->h - src_rect.y;
 
     free_unhappy = get_city_bonus(pcity_dlg->pcity, EFT_MAKE_CONTENT_MIL);
-    happy_cost = city_unit_unhappiness(punit, &free_unhappy);
+    happy_cost = city_unit_unhappiness(nmap, punit, &free_unhappy);
 
     i = punit->upkeep[O_SHIELD] + punit->upkeep[O_FOOD] +
         punit->upkeep[O_GOLD] + happy_cost;
diff --git a/common/city.c b/common/city.c
index 36245d20e0..f0f71d9f0a 100644
--- a/common/city.c
+++ b/common/city.c
@@ -2940,13 +2940,13 @@ inline void set_city_production(struct city *pcity)
 /**********************************************************************//**
   Query unhappiness caused by a given unit.
 **************************************************************************/
-int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
+int city_unit_unhappiness(const struct civ_map *nmap,
+                          struct unit *punit, int *free_unhappy)
 {
   struct city *pcity;
   const struct unit_type *ut;
   struct player *plr;
   int happy_cost;
-  const struct civ_map *nmap = &(wld.map);
 
   if (punit == NULL || free_unhappy == NULL) {
     return 0;
@@ -2994,6 +2994,7 @@ int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
 static inline void city_support(struct city *pcity)
 {
   int free_unhappy, martial_law_each;
+  const struct civ_map *nmap = &(wld.map);
 
   /* Clear all usage values. */
   memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
@@ -3044,7 +3045,7 @@ static inline void city_support(struct city *pcity)
 
   free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
   unit_list_iterate(pcity->units_supported, punit) {
-    pcity->unit_happy_upkeep += city_unit_unhappiness(punit, &free_unhappy);
+    pcity->unit_happy_upkeep += city_unit_unhappiness(nmap, punit, &free_unhappy);
     output_type_iterate(o) {
       if (O_GOLD != o) {
         /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
diff --git a/common/city.h b/common/city.h
index 460d1e6085..2e4faa1e96 100644
--- a/common/city.h
+++ b/common/city.h
@@ -564,11 +564,12 @@ citizens player_angry_citizens(const struct player *pplayer);
 int city_population(const struct city *pcity);
 int city_total_impr_gold_upkeep(const struct city *pcity);
 int city_total_unit_gold_upkeep(const struct city *pcity);
-int city_unit_unhappiness(struct unit *punit, int *free_unhappy);
-bool city_happy(const struct city *pcity);  /* generally use celebrating instead */
-bool city_unhappy(const struct city *pcity);                /* anarchy??? */
+int city_unit_unhappiness(const struct civ_map *nmap,
+                          struct unit *punit, int *free_unhappy);
+bool city_happy(const struct city *pcity);  /* Generally use celebrating instead */
+bool city_unhappy(const struct city *pcity);                /* Anarchy??? */
 bool base_city_celebrating(const struct city *pcity);
-bool city_celebrating(const struct city *pcity);            /* love the king ??? */
+bool city_celebrating(const struct city *pcity);            /* Love the king ??? */
 bool city_rapture_grow(const struct city *pcity);
 bool city_is_occupied(const struct city *pcity);
 
-- 
2.45.2

