From dd8d29bdbd76410e44358265226542b7cd4daac3 Mon Sep 17 00:00:00 2001
From: John Robertson <jro@advops.com>
Date: Fri, 10 Oct 2025 14:57:38 -0600
Subject: [PATCH] consistancy in API treatment of 'path' parameter

---
 ai/default/daisettler.c       |  4 ++--
 server/advisors/autoworkers.c | 29 +++++++++++++++++------------
 server/advisors/autoworkers.h |  2 +-
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 6a0d26bff1..11628bff4a 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -1162,7 +1162,7 @@ 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) {
@@ -1186,7 +1186,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) != ACTIVITY_IDLE) {
     if (pcity != NULL) {
diff --git a/server/advisors/autoworkers.c b/server/advisors/autoworkers.c
index ad55d3d472..3adecf16f0 100644
--- a/server/advisors/autoworkers.c
+++ b/server/advisors/autoworkers.c
@@ -1092,6 +1092,7 @@ struct city *worker_evaluate_city_requests(struct unit *punit,
   }
 
   if (path != NULL) {
+    if (*path != NULL) pf_path_destroy(*path);
     *path = best ? pf_map_path(pfm, best->ptile) : NULL;
   }
 
@@ -1138,7 +1139,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
     best_target = best_task->tgt;
 
     switch (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)) {
       case ACTIVITY_IDLE:
         CITY_LOG(LOG_NORMAL, taskcity, "giving up on %s(%d, %d)", unit_activity_name(best_task->act), TILE_XY(best_task->ptile));
@@ -1162,7 +1163,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
 enum unit_activity 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 **path,
                             struct tile *best_tile,
                             enum unit_activity best_act,
                             struct extra_type **best_target,
@@ -1182,20 +1183,20 @@ enum unit_activity auto_worker_setup_work(const struct civ_map *nmap,
     }
 
     UNIT_LOG(LOG_NORMAL, punit,
-             "is heading to do %s(%s) at (%d, %d)",
+             "considering %s(%s) at (%d, %d)",
              unit_activity_name(best_act),
              best_target && *best_target ? extra_rule_name(*best_target) : "-",
              TILE_XY(best_tile));
 
-    if (!path) {
+    if (*path == NULL) {
       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);
+      *path = pf_map_path(pfm, best_tile);
     }
 
-    if (path) {
+    if (*path != NULL) {
       bool alive;
       /* Hack: make sure the unit has enough moves_left for the move to succeed,
       * and adjust moves_left to afterward (if successful). */
@@ -1205,7 +1206,7 @@ enum unit_activity auto_worker_setup_work(const struct civ_map *nmap,
       punit->goto_tile = best_tile;
       unit_activity_handling(punit, ACTIVITY_GOTO, ACTION_NONE);
 
-      alive = adv_follow_path(punit, path, best_tile);
+      alive = adv_follow_path(punit, *path, best_tile);
 
       log_normal("%s", (alive ? "ALIVE" : "\x1b[31munalive\x1b[0m"));
       if (alive && same_pos(unit_tile(punit), best_tile) && punit->moves_left > 0) {
@@ -1223,13 +1224,15 @@ enum unit_activity auto_worker_setup_work(const struct civ_map *nmap,
         ret = best_act;
       } else if (alive) {
         punit->moves_left = old_moves;
+        send_unit_info(NULL, punit);
         UNIT_LOG(LOG_NORMAL, punit,
                  "didn't start work yet; got to (%d, %d) with "
                  "%d move frags left", TILE_XY(unit_tile(punit)),
                  punit->moves_left);
-        send_unit_info(NULL, punit);
         ret = ACTIVITY_GOTO;
       }
+      pf_path_destroy(*path);
+      *path = NULL;
     } else {
       UNIT_LOG(LOG_NORMAL, punit,
                "does not find path (%d, %d) -> (%d, %d)",
@@ -1329,17 +1332,18 @@ void auto_workers_player(struct player *pplayer)
         bool alive;
         unit_activity_handling(punit, ACTIVITY_GOTO, ACTION_NONE);
         alive = adv_follow_path(punit, path, punit->goto_tile);
-        if (alive && same_pos(unit_tile(punit), punit->goto_tile)) {
+        if (alive && punit->goto_tile != nullptr
+          && same_pos(unit_tile(punit), punit->goto_tile)) {
           unit_activity_handling(punit, ACTIVITY_IDLE, ACTION_NONE);
           punit->goto_tile = nullptr;
         }
         send_unit_info(NULL, punit);
+        pf_path_destroy(path);
       } else {
         punit->goto_tile = nullptr;
       }
-    }
-
-    if (punit->activity == ACTIVITY_GOTO && punit->goto_tile == nullptr) {
+      pf_map_destroy(pfm);
+    } else if (punit->activity == ACTIVITY_GOTO && punit->goto_tile == nullptr) {
       unit_activity_handling(punit, ACTIVITY_IDLE, ACTION_NONE);
       send_unit_info(NULL, punit);
     }
@@ -1357,6 +1361,7 @@ void auto_workers_player(struct player *pplayer)
           if (!adv_worker_safe_tile(nmap, pplayer, punit,
                                      unit_tile(punit))) {
             unit_activity_handling(punit, ACTIVITY_IDLE, ACTION_NONE);
+            send_unit_info(NULL, punit);
           }
         } else {
           CALL_PLR_AI_FUNC(settler_cont, pplayer, pplayer, punit, state);
diff --git a/server/advisors/autoworkers.h b/server/advisors/autoworkers.h
index 94ac944b78..ae00b33a2f 100644
--- a/server/advisors/autoworkers.h
+++ b/server/advisors/autoworkers.h
@@ -34,7 +34,7 @@ void auto_worker_findwork(const struct civ_map *nmap,
 enum unit_activity 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 **path,
                             struct tile *best_tile,
                             enum unit_activity best_act,
                             struct extra_type **best_target,
-- 
2.43.0

