From 94005e94d193f28c0bc80af3198d46bd39c6035a Mon Sep 17 00:00:00 2001
From: Aleksandar Krauthaker <zebal@protonmail.ch>
Date: Wed, 2 Dec 2026 15:08:15 +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>
Reviewed-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daisettler.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 4caca72142..46a8e9d3cc 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_NONE);
       goto CLEANUP;
     }
   } else {
@@ -1201,10 +1203,11 @@ 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,
-                                best_tile, best_act, &best_target,
-                                completion_time)) {
+  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) {
       clear_worker_tasks(pcity);
     }
-- 
2.47.3

