From 71ba1084ca89267e61335e4a78d5e2e7c00a6359 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
daiferry.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/daicity.c  |  3 ++-
 ai/default/daiferry.c | 11 +++++++++++
 ai/default/daiferry.h |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/ai/default/daicity.c b/ai/default/daicity.c
index ddc5d549e2..0665e188cc 100644
--- a/ai/default/daicity.c
+++ b/ai/default/daicity.c
@@ -56,6 +56,7 @@
 #include "daidata.h"
 #include "daidiplomacy.h"
 #include "daidomestic.h"
+#include "daiferry.h"
 #include "daihand.h"
 #include "daimilitary.h"
 #include "daieffects.h"
@@ -1101,7 +1102,7 @@ static void resolve_city_emergency(struct ai_type *ait,
     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
diff --git a/ai/default/daiferry.c b/ai/default/daiferry.c
index 2d4fbf83ba..49557fd1a9 100644
--- a/ai/default/daiferry.c
+++ b/ai/default/daiferry.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/daiferry.h b/ai/default/daiferry.h
index 6407b1ddeb..5697efcdbe 100644
--- a/ai/default/daiferry.h
+++ b/ai/default/daiferry.h
@@ -78,6 +78,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
-- 
2.55.0

