From cf39b8e37c4f28edd5460a50cb0ad92a27d37afc Mon Sep 17 00:00:00 2001
From: Aleksandar Krauthaker <zebal@protonmail.ch>
Date: Sun, 29 Nov 2026 14:47:43 +0100
Subject: [PATCH] AI: Shelter auto worker stuck in non allied territory

When an AI settler/worker decides to do something different than previously planned,
and finds itself in the middle of 'enemy' territory, it gets stuck.
Every tile in non-allied territory is considered impassible for the purpose of pathing to somewhere to improve or settle.

Use the new adv_seek_shelter() helper in such cases to send the unit to the nearest city, where it will be able to make a new decision.

Suggested-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Aleksandar Krauthaker <zebal@protonmail.ch>
Reviewed-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 ai/default/daisettler.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 4caca72142..833a2d64df 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -1194,6 +1194,8 @@ BUILD_CITY:
       fc_assert(result == nullptr);
 
       dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
+
+      adv_seek_shelter(nmap, punit, AUT_AUTO_WORKER);
       goto CLEANUP;
     }
   } else {
@@ -1201,8 +1203,9 @@ BUILD_CITY:
     adv_unit_new_task(punit, AUT_AUTO_WORKER, best_tile);
   }
 
-  if (best_tile != nullptr
-      && auto_worker_setup_work(nmap, pplayer, punit, state, 0, &path,
+  if (best_tile == nullptr) {
+    adv_seek_shelter(nmap, punit, AUT_AUTO_WORKER);
+  } else if (auto_worker_setup_work(nmap, pplayer, punit, state, 0, &path,
                                 best_tile, best_act, &best_target,
                                 completion_time)) {
     if (pcity != nullptr) {
-- 
2.47.3

