From 6b59804531bca17953645101ea7533a77aedcdea Mon Sep 17 00:00:00 2001
From: John Robertson <jro@advops.com>
Date: Thu, 16 Oct 2025 16:45:26 -0600
Subject: [PATCH] enforced API consistancy regarding the handling of unit paths

---
 ai/default/daisettler.c       |  6 ++++--
 server/advisors/autoworkers.c | 34 +++++++++++++++++++++-------------
 server/advisors/autoworkers.h |  6 +++---
 3 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 2380259e4f..0ec079a8b1 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -1161,7 +1161,9 @@ BUILD_CITY:
       cityresult_destroy(result);
 
       /*** Go back to and found a city ***/
-      pf_path_destroy(path);
+      if (path != NULL) {
+        pf_path_destroy(path);
+      }
       path = NULL;
       goto BUILD_CITY;
     } else if (best_impr > 0) {
@@ -1185,7 +1187,7 @@ BUILD_CITY:
   }
 
   if (best_tile != NULL
-      && auto_worker_setup_work(nmap, pplayer, punit, state, 0, path,
+      && auto_worker_setup_work(nmap, pplayer, punit, state, 0, &path,
                                 best_tile, best_act, &best_target,
                                 completion_time)) {
     if (pcity != NULL) {
diff --git a/server/advisors/autoworkers.c b/server/advisors/autoworkers.c
index 377ee1dea2..ebd6be8529 100644
--- a/server/advisors/autoworkers.c
+++ b/server/advisors/autoworkers.c
@@ -444,7 +444,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
                                       enum unit_activity *best_act,
                                       struct extra_type **best_target,
                                       struct tile **best_tile,
-                                      struct pf_path **path,
+                                      struct pf_path **ppath,
                                       struct workermap *state)
 {
   const struct player *pplayer = unit_owner(punit);
@@ -822,8 +822,11 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
     *best_tile = NULL;
   }
 
-  if (path) {
-    *path = *best_tile ? pf_map_path(pfm, *best_tile) : NULL;
+  if (ppath) {
+    if (*ppath != nullptr) {
+      pf_path_destroy(*ppath);
+    }
+    *ppath = *best_tile ? pf_map_path(pfm, *best_tile) : NULL;
   }
 
   pf_map_destroy(pfm);
@@ -836,7 +839,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
 **************************************************************************/
 struct city *worker_evaluate_city_requests(struct unit *punit,
                                            struct worker_task **best_task,
-                                           struct pf_path **path,
+                                           struct pf_path **ppath,
                                            struct workermap *state)
 {
   const struct player *pplayer = unit_owner(punit);
@@ -912,8 +915,11 @@ struct city *worker_evaluate_city_requests(struct unit *punit,
 
   *best_task = best;
 
-  if (path != NULL) {
-    *path = best ? pf_map_path(pfm, best->ptile) : NULL;
+  if (ppath != NULL) {
+    if (*ppath != NULL) {
+      pf_path_destroy(*ppath);
+    }
+    *ppath = best ? pf_map_path(pfm, best->ptile) : NULL;
   }
 
   pf_map_destroy(pfm);
@@ -975,7 +981,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
     best_target = best_task->tgt;
 
     if (auto_worker_setup_work(nmap, pplayer, punit, state, recursion,
-                               path, best_task->ptile, best_task->act,
+                               &path, best_task->ptile, best_task->act,
                                &best_target, completion_time)) {
       clear_worker_task(taskcity, best_task);
     }
@@ -1000,7 +1006,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
 
     adv_unit_new_task(punit, AUT_AUTO_WORKER, best_tile);
 
-    auto_worker_setup_work(nmap, pplayer, punit, state, recursion, path,
+    auto_worker_setup_work(nmap, pplayer, punit, state, recursion, &path,
                            best_tile, best_act, &best_target,
                            completion_time);
 
@@ -1017,7 +1023,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
 bool auto_worker_setup_work(const struct civ_map *nmap,
                             struct player *pplayer, struct unit *punit,
                             struct workermap *state, int recursion,
-                            struct pf_path *path,
+                            struct pf_path **ppath,
                             struct tile *best_tile,
                             enum unit_activity best_act,
                             struct extra_type **best_target,
@@ -1094,18 +1100,18 @@ bool auto_worker_setup_work(const struct civ_map *nmap,
              best_target && *best_target ? extra_rule_name(*best_target) : "-",
              TILE_XY(best_tile));
 
-    if (!path) {
+    if (ppath != nullptr && *ppath == nullptr) {
       pft_fill_unit_parameter(&parameter, nmap, punit);
       parameter.omniscience = !has_handicap(pplayer, H_MAP);
       parameter.get_TB = autoworker_tile_behavior;
       pfm = pf_map_new(&parameter);
-      path = pf_map_path(pfm, best_tile);
+      *ppath = pf_map_path(pfm, best_tile);
     }
 
-    if (path) {
+    if (ppath != nullptr && *ppath != nullptr) {
       bool alive;
 
-      alive = adv_follow_path(punit, path, best_tile);
+      alive = adv_follow_path(punit, *ppath, best_tile);
 
       if (alive && same_pos(unit_tile(punit), best_tile)
 	  && punit->moves_left > 0) {
@@ -1129,6 +1135,8 @@ bool auto_worker_setup_work(const struct civ_map *nmap,
                  "%d move frags left", TILE_XY(unit_tile(punit)),
                  punit->moves_left);
       }
+      pf_path_destroy(*ppath);
+      *ppath = NULL;
     } else {
       UNIT_LOG(LOG_DEBUG, punit,
                "does not find path (%d, %d) -> (%d, %d)",
diff --git a/server/advisors/autoworkers.h b/server/advisors/autoworkers.h
index 9b911954f2..969d1a82af 100644
--- a/server/advisors/autoworkers.h
+++ b/server/advisors/autoworkers.h
@@ -34,7 +34,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
 bool auto_worker_setup_work(const struct civ_map *nmap,
                             struct player *pplayer, struct unit *punit,
                             struct workermap *state, int recursion,
-                            struct pf_path *path,
+                            struct pf_path **ppath,
                             struct tile *best_tile,
                             enum unit_activity best_act,
                             struct extra_type **best_target,
@@ -45,12 +45,12 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
                                       enum unit_activity *best_act,
                                       struct extra_type **best_target,
                                       struct tile **best_tile,
-                                      struct pf_path **path,
+                                      struct pf_path **ppath,
                                       struct workermap *state);
 
 struct city *worker_evaluate_city_requests(struct unit *punit,
                                            struct worker_task **best_task,
-                                           struct pf_path **path,
+                                           struct pf_path **ppath,
                                            struct workermap *state);
 
 void adv_unit_new_task(struct unit *punit, enum adv_unit_task task,
-- 
2.43.0

