From 3664987f06033e898e7e358344c1d586b52a17a4 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 7 Nov 2026 16:30:56 -0800
Subject: [PATCH] ai/default: Raise diplomat_threat only for diplomat units

city_data->diplomat_threat is documented as "Enemy diplomat or spy is
near the city" (daicity.h) and is the sole trigger for
dai_choose_diplomat_defensive()'s 16000 want, which outranks every
military build. 

But assess_danger() set diplomat_threat for any unit in two moves'
distnace with utype_acts_hostile(). ACTION_HOSTILE is true of attackers,
pillagers, bombarders and city conquerors as well as of the spy types,
so an approaching Warriors made the city buy a Diplomat before a
defender. 

Measured on 40 ai games: of the cities with such a unit within two
tiles, only 21-29% had an enemy diplomat in the same radius. 

Test the unit the way the consumers do: dai_choose_diplomat_defensive()
answers with best_role_unit(pcity, UTYF_DIPLOMAT) and
count_diplomats_on_tile() counts UTYF_DIPLOMAT, so the threat is raised
by UTYF_DIPLOMAT units only.

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

diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c
index fd456a347b..b2e1ab2387 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -902,7 +902,11 @@ static unsigned int assess_danger(struct ai_type *ait,
       (void) dai_wants_defender_against(ait, nmap, pplayer, pcity, utype,
                                         vulnerability / MAX(move_time, 1));
 
-      if (utype_acts_hostile(unit_type_get(punit)) && 2 >= move_time) {
+      /* "Enemy diplomat or spy is near the city" (daicity.h): the same
+       * test dai_choose_diplomat_defensive() and count_diplomats_on_tile()
+       * use for the unit they answer it with. utype_acts_hostile() is
+       * true of every attacker, pillager and bombarder as well. */
+      if (unit_has_type_flag(punit, UTYF_DIPLOMAT) && 2 >= move_time) {
         city_data->diplomat_threat = TRUE;
       }
 
-- 
2.55.0

