From 9d8aec6f9273e579bfb0c4c7cfa4fe515dd0c3f6 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 7 Nov 2026 17:39:24 -0800
Subject: [PATCH] ai/default: Spend gold before the city choices are reset

dai_manage_cities() reset every city's choice.want to -1 and then
called dai_spend_gold(), which looks for the city with the highest
want. Nothing ever beat the zero it starts from, so the buy loop
returned on its first iteration: no emergency buys, no selling a
building to afford a defender, treasuries idling at several times the
reserve while cities under siege waited for production. The reset has
been in that position since c84ce356f5 (2017); the buy code has been
maintained all along, so this was never the intent. Spend first, then
reset. dai_spend_gold() already marks the cities it handles with -1,
so nothing is processed twice.

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 ai/default/daicity.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/ai/default/daicity.c b/ai/default/daicity.c
index ddc5d549e2..2352751804 100644
--- a/ai/default/daicity.c
+++ b/ai/default/daicity.c
@@ -969,12 +969,14 @@ void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
 
   city_list_iterate(pplayer->cities, pcity) {
     dai_city_choose_build(ait, pplayer, pcity);
-
-    /* Initialize for next turn */
-    def_ai_city_data(pcity, ait)->choice.want = -1;
   } city_list_iterate_end;
 
   dai_spend_gold(ait, pplayer);
+
+  /* Initialize for next turn */
+  city_list_iterate(pplayer->cities, pcity) {
+    def_ai_city_data(pcity, ait)->choice.want = -1;
+  } city_list_iterate_end;
 }
 
 /**********************************************************************//**
-- 
2.55.0

