From 158a2d6c6c8183fccf6a6c5d94f8192480327260 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 28 Nov 2026 13:48:09 -0800
Subject: [PATCH] sdl: Keep diplomacy gold limit in step with players' gold

A diplomacy meeting's gold field took its limit, and its "Gold(max N)"
label, from each player's gold when the meeting panel was built, and
never again. Once the player's gold grew, say at the next turn, the
offer was still clamped to the old amount. A player who had no gold
when the meeting opened got no gold field at all.

Fill the update_diplomacy_gold() stub added in RM #2506: clamp the
offer to the giver's current gold, always create the gold field when
gold clauses are enabled, and redraw the label of that player's panels
when their gold changes.

See RM #2508

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 client/gui-sdl2/diplodlg.c | 55 +++++++++++++++++++++++++++++++++-----
 client/gui-sdl3/diplodlg.c | 55 +++++++++++++++++++++++++++++++++-----
 2 files changed, 98 insertions(+), 12 deletions(-)

diff --git a/client/gui-sdl2/diplodlg.c b/client/gui-sdl2/diplodlg.c
index 19efce913d..938a3ff14a 100644
--- a/client/gui-sdl2/diplodlg.c
+++ b/client/gui-sdl2/diplodlg.c
@@ -408,6 +408,7 @@ static int gold_callback(struct widget *pwidget)
 {
   int amount;
   struct diplomacy_dialog *pdialog;
+  struct player *giver = player_by_number(pwidget->data.cont->id0);
 
   if (!(pdialog = get_diplomacy_dialog(player_by_number(pwidget->data.cont->id1)))) {
     pdialog = get_diplomacy_dialog(player_by_number(pwidget->data.cont->id0));
@@ -416,9 +417,9 @@ static int gold_callback(struct widget *pwidget)
   if (pwidget->string_utf8->text != NULL) {
     sscanf(pwidget->string_utf8->text, "%d", &amount);
 
-    if (amount > pwidget->data.cont->value) {
+    if (amount > giver->economic.gold) {
       /* Max player gold */
-      amount = pwidget->data.cont->value;
+      amount = giver->economic.gold;
     }
 
   } else {
@@ -690,9 +691,7 @@ static struct advanced_dialog *popup_diplomatic_objects(struct player *pplayer0,
 
   /* ---------------------------- */
 
-  if (clause_enabled(CLAUSE_GOLD) && pplayer0->economic.gold > 0) {
-    cont->value = pplayer0->economic.gold;
-
+  if (clause_enabled(CLAUSE_GOLD)) {
     fc_snprintf(cbuf, sizeof(cbuf), _("Gold(max %d)"), pplayer0->economic.gold);
     buf = create_iconlabel_from_chars_fonto(NULL, pwindow->dst,
                                             cbuf, FONTO_ATTENTION,
@@ -1788,10 +1787,54 @@ void popup_diplomacy_dialog(struct player *pplayer)
   }
 }
 
+/**********************************************************************//**
+  Show the player's gold in the gold label of a panel of theirs.
+  Return whether the label changed.
+**************************************************************************/
+static bool update_gold_label(struct advanced_dialog *panel,
+                              struct player *pplayer)
+{
+  struct widget *buf = panel->end_widget_list;
+  char cbuf[128];
+
+  if (buf->data.cont->id0 != player_number(pplayer)) {
+    return FALSE;
+  }
+
+  /* The label is the widget before the gold edit. */
+  do {
+    buf = buf->prev;
+    if (buf->action == gold_callback) {
+      buf = buf->next;
+      fc_snprintf(cbuf, sizeof(cbuf), _("Gold(max %d)"),
+                  pplayer->economic.gold);
+      if (strcmp(buf->string_utf8->text, cbuf) == 0) {
+        return FALSE;
+      }
+      copy_chars_to_utf8_str(buf->string_utf8, cbuf);
+      widget_redraw(buf);
+      widget_mark_dirty(buf);
+
+      return TRUE;
+    }
+  } while (buf != panel->begin_widget_list);
+
+  return FALSE;
+}
+
 /**********************************************************************//**
   Player's gold may have changed.
 **************************************************************************/
 void update_diplomacy_gold(struct player *pplayer)
 {
-  /* PORTME */
+  bool changed = FALSE;
+
+  dialog_list_iterate(dialog_list, pdialog) {
+    changed |= update_gold_label(pdialog->poffers, pplayer);
+    changed |= update_gold_label(pdialog->pwants, pplayer);
+  } dialog_list_iterate_end;
+
+  if (changed) {
+    flush_dirty();
+  }
 }
diff --git a/client/gui-sdl3/diplodlg.c b/client/gui-sdl3/diplodlg.c
index ce1d744fc2..f581ddb49c 100644
--- a/client/gui-sdl3/diplodlg.c
+++ b/client/gui-sdl3/diplodlg.c
@@ -404,6 +404,7 @@ static int gold_callback(struct widget *pwidget)
 {
   int amount;
   struct diplomacy_dialog *pdialog;
+  struct player *giver = player_by_number(pwidget->data.cont->id0);
 
   if (!(pdialog = get_diplomacy_dialog(player_by_number(pwidget->data.cont->id1)))) {
     pdialog = get_diplomacy_dialog(player_by_number(pwidget->data.cont->id0));
@@ -412,9 +413,9 @@ static int gold_callback(struct widget *pwidget)
   if (pwidget->string_utf8->text != NULL) {
     sscanf(pwidget->string_utf8->text, "%d", &amount);
 
-    if (amount > pwidget->data.cont->value) {
+    if (amount > giver->economic.gold) {
       /* Max player gold */
-      amount = pwidget->data.cont->value;
+      amount = giver->economic.gold;
     }
 
   } else {
@@ -686,9 +687,7 @@ static struct advanced_dialog *popup_diplomatic_objects(struct player *pplayer0,
 
   /* ---------------------------- */
 
-  if (clause_enabled(CLAUSE_GOLD) && pplayer0->economic.gold > 0) {
-    cont->value = pplayer0->economic.gold;
-
+  if (clause_enabled(CLAUSE_GOLD)) {
     fc_snprintf(cbuf, sizeof(cbuf), _("Gold(max %d)"), pplayer0->economic.gold);
     buf = create_iconlabel_from_chars_fonto(NULL, pwindow->dst,
                                             cbuf, FONTO_ATTENTION,
@@ -1784,10 +1783,54 @@ void popup_diplomacy_dialog(struct player *pplayer)
   }
 }
 
+/**********************************************************************//**
+  Show the player's gold in the gold label of a panel of theirs.
+  Return whether the label changed.
+**************************************************************************/
+static bool update_gold_label(struct advanced_dialog *panel,
+                              struct player *pplayer)
+{
+  struct widget *buf = panel->end_widget_list;
+  char cbuf[128];
+
+  if (buf->data.cont->id0 != player_number(pplayer)) {
+    return FALSE;
+  }
+
+  /* The label is the widget before the gold edit. */
+  do {
+    buf = buf->prev;
+    if (buf->action == gold_callback) {
+      buf = buf->next;
+      fc_snprintf(cbuf, sizeof(cbuf), _("Gold(max %d)"),
+                  pplayer->economic.gold);
+      if (strcmp(buf->string_utf8->text, cbuf) == 0) {
+        return FALSE;
+      }
+      copy_chars_to_utf8_str(buf->string_utf8, cbuf);
+      widget_redraw(buf);
+      widget_mark_dirty(buf);
+
+      return TRUE;
+    }
+  } while (buf != panel->begin_widget_list);
+
+  return FALSE;
+}
+
 /**********************************************************************//**
   Player's gold may have changed.
 **************************************************************************/
 void update_diplomacy_gold(struct player *pplayer)
 {
-  /* PORTME */
+  bool changed = FALSE;
+
+  dialog_list_iterate(dialog_list, pdialog) {
+    changed |= update_gold_label(pdialog->poffers, pplayer);
+    changed |= update_gold_label(pdialog->pwants, pplayer);
+  } dialog_list_iterate_end;
+
+  if (changed) {
+    flush_dirty();
+  }
 }
-- 
2.55.0

