From 645167d52c898420aac12957f6dd70126d66e83f Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 17 Nov 2024 02:44:50 +0200
Subject: [PATCH 41/41] 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/daimilitary.c      | 4 +++-
 ai/default/daiunit.c          | 2 +-
 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 ++-
 client/gui-sdl3/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 fd6fb96121..7734786ae8 100644
--- a/ai/default/aitools.c
+++ b/ai/default/aitools.c
@@ -1414,7 +1414,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;
@@ -1425,7 +1426,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 0ea286d507..eb79bf6ff9 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/daimilitary.c b/ai/default/daimilitary.c
index 47996f2a84..8e0d31b0de 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -1255,13 +1255,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/ai/default/daiunit.c b/ai/default/daiunit.c
index 07b309279f..6bb2d43313 100644
--- a/ai/default/daiunit.c
+++ b/ai/default/daiunit.c
@@ -1276,7 +1276,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/client/gui-gtk-3.22/citydlg.c b/client/gui-gtk-3.22/citydlg.c
index 873d595afc..04af9b6f28 100644
--- a/client/gui-gtk-3.22/citydlg.c
+++ b/client/gui-gtk-3.22/citydlg.c
@@ -2253,6 +2253,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) {
@@ -2302,7 +2303,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 457b3e5502..197bb53d7e 100644
--- a/client/gui-gtk-4.0/citydlg.c
+++ b/client/gui-gtk-4.0/citydlg.c
@@ -2243,6 +2243,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) {
@@ -2296,7 +2297,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 d6027f2c2e..ea3f8cdebc 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -3300,6 +3300,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();
 
@@ -3311,7 +3313,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 78bdb10766..ca84365088 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/client/gui-sdl3/citydlg.c b/client/gui-sdl3/citydlg.c
index 55c0ce36ea..48f3f4fb60 100644
--- a/client/gui-sdl3/citydlg.c
+++ b/client/gui-sdl3/citydlg.c
@@ -618,6 +618,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));
@@ -641,7 +642,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 579a35a7a5..d0fc48fb40 100644
--- a/common/city.c
+++ b/common/city.c
@@ -3046,13 +3046,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;
@@ -3100,6 +3100,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));
@@ -3150,7 +3151,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 70486bba5e..fda52379b8 100644
--- a/common/city.h
+++ b/common/city.h
@@ -584,11 +584,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

