From 42fe81e5a22282742affbcc208cd570cb8b3aa98 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 21 Nov 2026 16:28:04 -0800
Subject: [PATCH] resolve_city_emergency(): Disband an idle boat too

The emergency meant to spare a boat carrying units, and tested the
ferry's passenger field against 0. That field's values are private to
aiferry.c: dai_ferry_init_ferry() sets it to FERRY_AVAILABLE (-1) on
every boat, loaded or empty, so no boat was ever disbanded. One
Trireme idle outside the borders can then keep a small city in
disorder until the government falls.

Add dai_ferry_in_use(), true for a boat that carries units or that a
passenger has booked, and spare only those.

See RM #2496

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 ai/default/aiferry.c | 11 +++++++++++
 ai/default/aiferry.h |  1 +
 ai/default/daicity.c |  3 ++-
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/ai/default/aiferry.c b/ai/default/aiferry.c
index 020a559e1c..8c2bc6ec8e 100644
--- a/ai/default/aiferry.c
+++ b/ai/default/aiferry.c
@@ -467,6 +467,17 @@ bool is_boat_free(struct ai_type *ait, struct unit *boat,
           && (ferry_type->fuel == 0 || utype_has_flag(ferry_type, UTYF_COAST)));
 }
 
+/**********************************************************************//**
+  Does the boat carry units, or has a passenger booked it? The passenger
+  field alone cannot tell: it is negative, never 0, for a boat nobody
+  has booked.
+**************************************************************************/
+bool dai_ferry_in_use(struct ai_type *ait, struct unit *pferry)
+{
+  return get_transporter_occupancy(pferry) > 0
+    || def_ai_unit_data(pferry, ait)->passenger > 0;
+}
+
 /**********************************************************************//**
   Check if unit is boss in ferry
 **************************************************************************/
diff --git a/ai/default/aiferry.h b/ai/default/aiferry.h
index f8cc79f6b5..cb38ee9aa0 100644
--- a/ai/default/aiferry.h
+++ b/ai/default/aiferry.h
@@ -76,6 +76,7 @@ bool dai_amphibious_goto_constrained(struct ai_type *ait,
 bool is_boat_free(struct ai_type *ait, struct unit *boat,
                   struct unit *punit, int cap);
 bool is_boss_of_boat(struct ai_type *ait, struct unit *punit);
+bool dai_ferry_in_use(struct ai_type *ait, struct unit *pferry);
 
 /*
  * Main boat managing function.  Gets units on board to where they want to
diff --git a/ai/default/daicity.c b/ai/default/daicity.c
index 660ea70165..ee24cd6fb6 100644
--- a/ai/default/daicity.c
+++ b/ai/default/daicity.c
@@ -53,6 +53,7 @@
 #include "handicaps.h"
 
 /* ai/default */
+#include "aiferry.h"
 #include "aihand.h"
 #include "daidata.h"
 #include "daidiplomacy.h"
@@ -1078,7 +1079,7 @@ static void resolve_city_emergency(struct ai_type *ait, struct player *pplayer,
     if (city_unhappy(pcity)
         && (utype_happy_cost(unit_type_get(punit), pplayer) > 0
             && (unit_being_aggressive(nmap, punit) || is_field_unit(punit)))
-        && def_ai_unit_data(punit, ait)->passenger == 0) {
+        && !dai_ferry_in_use(ait, punit)) {
       UNIT_LOG(LOG_EMERGENCY, punit, "is causing unrest, disbanded");
       /* TODO: If Help Wonder stops blocking Disband Unit there may be
        * cases where Disband Unit should be selected. Example: Field unit
-- 
2.55.0

