From 1d98e12a114136d9c76c6438df8b2b2e2ad91f0a Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 8 Nov 2026 05:17:53 +0200
Subject: [PATCH 61/61] AI: Fix declaring war on multiple targets after senate
 dissolved

See RM #2400

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daidata.h      |  1 -
 ai/default/daidiplomacy.c | 22 ++++++++++------------
 ai/default/daisanity.c    | 14 ++++++++------
 3 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/ai/default/daidata.h b/ai/default/daidata.h
index 74791ed326..c87376b708 100644
--- a/ai/default/daidata.h
+++ b/ai/default/daidata.h
@@ -94,7 +94,6 @@ struct ai_plr
     char love_incr;           /* Modify love with this fixed amount */
     int req_love_for_peace;
     int req_love_for_alliance;
-    struct player *war_target;
   } diplomacy;
 
   /* Cache map for AI settlers; defined in daisettler.c. */
diff --git a/ai/default/daidiplomacy.c b/ai/default/daidiplomacy.c
index 2bde380eee..a1289704a0 100644
--- a/ai/default/daidiplomacy.c
+++ b/ai/default/daidiplomacy.c
@@ -1414,7 +1414,6 @@ static void dai_go_to_war(struct ai_type *ait, struct player *pplayer,
       pplayer->government = real_gov;
       handle_player_change_government(pplayer,
                                       game.info.government_during_revolution_id);
-      def_ai_player_data(pplayer, ait)->diplomacy.war_target = target;
       return;
     } else {
       /* There would be Senate even during revolution. Better not to revolt for nothing */
@@ -1439,20 +1438,19 @@ static void dai_go_to_war(struct ai_type *ait, struct player *pplayer,
 **********************************************************************/
 void dai_revolution_start(struct ai_type *ait, struct player *pplayer)
 {
-  struct ai_plr *data = def_ai_player_data(pplayer, ait);
-
-  if (data->diplomacy.war_target != nullptr) {
-    /* Target might have died since it was set. */
-    if (data->diplomacy.war_target->is_alive) {
-      if (gives_shared_vision(pplayer, data->diplomacy.war_target)) {
-        remove_shared_vision(pplayer, data->diplomacy.war_target);
+  players_iterate_alive(opponent) {
+    struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, opponent);
+
+    if (adip->countdown < -1 && !pplayers_at_war(pplayer, opponent)) {
+      /* We were waiting for opportunity to declare war without
+       * the senate intervening. */
+      if (gives_shared_vision(pplayer, opponent)) {
+        remove_shared_vision(pplayer, opponent);
       }
 
-      dai_declare_war(ait, pplayer, data->diplomacy.war_target);
+      dai_declare_war(ait, pplayer, opponent);
     }
-
-    data->diplomacy.war_target = nullptr;
-  }
+  } players_iterate_alive_end;
 }
 
 /******************************************************************//**
diff --git a/ai/default/daisanity.c b/ai/default/daisanity.c
index ef6c3f0a30..1bccc69055 100644
--- a/ai/default/daisanity.c
+++ b/ai/default/daisanity.c
@@ -34,22 +34,24 @@ void dai_sanity_check(struct ai_type *ait, struct player *pplayer)
 #define SANITY_CHECK(check) \
   fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, check, , NOLOGMSG, NOLOGMSG)
 
-  struct player *wt;
-
+#if 0
   if (!pplayer->is_alive) {
     return;
   }
 
-  wt = def_ai_player_data(pplayer, ait)->diplomacy.war_target;
-
   players_iterate_alive(opponent) {
     struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, opponent);
     bool at_war = pplayers_at_war(pplayer, opponent);
-    bool war_target = (wt == opponent);
 
     /* SANITY_CHECK(adip->countdown < 0 || !at_war); */
-    SANITY_CHECK(adip->countdown >= -1 || at_war || war_target);
+
+    /* It's ok for countdown to show war, but player being only in the process
+     * of dissolving the senate and declaring the war after that.
+     * Countdown is actually the trigger that causes the war declaration
+     * in that case. */
+    /* SANITY_CHECK(adip->countdown >= -1 || at_war); */
   } players_iterate_alive_end;
+#endif
 
 #endif /* SANITY_CHECKING */
 }
-- 
2.53.0

