From 678904b3869e28252fc170d297266b9d5de46048 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 15 Nov 2026 11:31:29 +0200
Subject: [PATCH 73/73] Refactor adv_unit_move()

See RM #2236

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actions.c          |   4 +-
 common/actions.h          |   4 +-
 server/advisors/advgoto.c | 110 ++++++++++----------------------------
 3 files changed, 32 insertions(+), 86 deletions(-)

diff --git a/common/actions.c b/common/actions.c
index 3bb881f8be..e7c8909e9b 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -7602,7 +7602,7 @@ enum gen_action select_actres_action_unit_on_stack(struct civ_map *nmap,
   Can unit do any action with the given action result to tile?
   Returns one of the possible actions, or ACTION_NONE.
 ****************************************************************************/
-enum gen_action select_actres_action_unit_on_tile(struct civ_map *nmap,
+enum gen_action select_actres_action_unit_on_tile(const struct civ_map *nmap,
                                                   enum action_result actres,
                                                   struct unit *punit,
                                                   struct tile *ptile)
@@ -7652,7 +7652,7 @@ enum gen_action select_actres_action_unit_on_city(struct civ_map *nmap,
   Can unit do any action with the given action result to unit?
   Returns one of the possible actions, or ACTION_NONE.
 ****************************************************************************/
-enum gen_action select_actres_action_unit_on_unit(struct civ_map *nmap,
+enum gen_action select_actres_action_unit_on_unit(const struct civ_map *nmap,
                                                   enum action_result actres,
                                                   struct unit *punit,
                                                   struct unit *target)
diff --git a/common/actions.h b/common/actions.h
index 6bcda3b5ef..a3dc198db8 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -775,7 +775,7 @@ enum gen_action select_actres_action_unit_on_stack(struct civ_map *nmap,
                                                    enum action_result actres,
                                                    struct unit *punit,
                                                    struct tile *ptile);
-enum gen_action select_actres_action_unit_on_tile(struct civ_map *nmap,
+enum gen_action select_actres_action_unit_on_tile(const struct civ_map *nmap,
                                                   enum action_result actres,
                                                   struct unit *punit,
                                                   struct tile *ptile);
@@ -783,7 +783,7 @@ enum gen_action select_actres_action_unit_on_city(struct civ_map *nmap,
                                                   enum action_result actres,
                                                   struct unit *punit,
                                                   struct city *pcity);
-enum gen_action select_actres_action_unit_on_unit(struct civ_map *nmap,
+enum gen_action select_actres_action_unit_on_unit(const struct civ_map *nmap,
                                                   enum action_result actres,
                                                   struct unit *punit,
                                                   struct unit *target);
diff --git a/server/advisors/advgoto.c b/server/advisors/advgoto.c
index 7232c5850e..b7b49ff83a 100644
--- a/server/advisors/advgoto.c
+++ b/server/advisors/advgoto.c
@@ -161,6 +161,7 @@ static bool adv_unit_move(struct unit *punit, struct tile *ptile)
   struct player *pplayer = unit_owner(punit);
   struct unit *ptrans = nullptr;
   const struct civ_map *nmap = &(wld.map);
+  enum gen_action act;
 
   /* If enemy, stop and give a chance for the human player to
      handle this case */
@@ -171,91 +172,36 @@ static bool adv_unit_move(struct unit *punit, struct tile *ptile)
   }
 
   /* Select move kind. */
+  act = ACTION_NONE;
   if (!can_unit_survive_at_tile(nmap, punit, ptile)
-      && ((ptrans = transporter_for_unit_at(punit, ptile)))
-      && is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_EMBARK,
-                                        punit, ptrans)) {
-    /* "Transport Embark". */
-    paction = action_by_number(ACTION_TRANSPORT_EMBARK);
-  } else if (!can_unit_survive_at_tile(nmap, punit, ptile)
-             && ptrans != nullptr
-             && is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_EMBARK2,
-                                               punit, ptrans)) {
-    /* "Transport Embark 2". */
-    paction = action_by_number(ACTION_TRANSPORT_EMBARK2);
-  } else if (!can_unit_survive_at_tile(nmap, punit, ptile)
-             && ptrans != nullptr
-             && is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_EMBARK3,
-                                               punit, ptrans)) {
-    /* "Transport Embark 3". */
-    paction = action_by_number(ACTION_TRANSPORT_EMBARK3);
-  } else if (!can_unit_survive_at_tile(nmap, punit, ptile)
-             && ptrans != nullptr
-             && is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_EMBARK4,
-                                               punit, ptrans)) {
-    /* "Transport Embark 4". */
-    paction = action_by_number(ACTION_TRANSPORT_EMBARK4);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_TRANSPORT_DISEMBARK1,
-                                            punit, ptile, nullptr)) {
-    /* "Transport Disembark". */
-    paction = action_by_number(ACTION_TRANSPORT_DISEMBARK1);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_TRANSPORT_DISEMBARK2,
-                                            punit, ptile, nullptr)) {
-    /* "Transport Disembark 2". */
-    paction = action_by_number(ACTION_TRANSPORT_DISEMBARK2);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_TRANSPORT_DISEMBARK3,
-                                            punit, ptile, nullptr)) {
-    /* "Transport Disembark 3". */
-    paction = action_by_number(ACTION_TRANSPORT_DISEMBARK3);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_TRANSPORT_DISEMBARK4,
-                                            punit, ptile, nullptr)) {
-    /* "Transport Disembark 4". */
-    paction = action_by_number(ACTION_TRANSPORT_DISEMBARK4);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_ENTER,
-                                            punit, ptile, nullptr)) {
-    /* "Enter Hut". */
-    paction = action_by_number(ACTION_HUT_ENTER);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_ENTER2,
-                                            punit, ptile, nullptr)) {
-    /* "Enter Hut 2". */
-    paction = action_by_number(ACTION_HUT_ENTER2);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_ENTER3,
-                                            punit, ptile, nullptr)) {
-    /* "Enter Hut 3". */
-    paction = action_by_number(ACTION_HUT_ENTER3);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_ENTER4,
-                                            punit, ptile, nullptr)) {
-    /* "Enter Hut 4". */
-    paction = action_by_number(ACTION_HUT_ENTER4);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_FRIGHTEN,
-                                            punit, ptile, nullptr)) {
-    /* "Frighten Hut". */
-    paction = action_by_number(ACTION_HUT_FRIGHTEN);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_FRIGHTEN2,
-                                            punit, ptile, nullptr)) {
-    /* "Frighten Hut 2". */
-    paction = action_by_number(ACTION_HUT_FRIGHTEN2);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_FRIGHTEN3,
-                                            punit, ptile, nullptr)) {
-    /* "Frighten Hut 3". */
-    paction = action_by_number(ACTION_HUT_FRIGHTEN3);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_HUT_FRIGHTEN4,
-                                            punit, ptile, nullptr)) {
-    /* "Frighten Hut 4". */
-    paction = action_by_number(ACTION_HUT_FRIGHTEN4);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_UNIT_MOVE,
-                                            punit, ptile, nullptr)) {
-    /* "Unit Move". */
-    paction = action_by_number(ACTION_UNIT_MOVE);
-  } else if (is_action_enabled_unit_on_tile(nmap, ACTION_UNIT_MOVE2,
-                                            punit, ptile, nullptr)) {
-    /* "Unit Move 2". */
-    paction = action_by_number(ACTION_UNIT_MOVE2);
-  } else {
-    /* "Unit Move 3". */
-    paction = action_by_number(ACTION_UNIT_MOVE3);
+      && ((ptrans = transporter_for_unit_at(punit, ptile)))) {
+
+    act = select_actres_action_unit_on_unit(nmap, ACTRES_TRANSPORT_EMBARK,
+                                            punit, ptrans);
+  }
+
+  if (act == ACTION_NONE) {
+    enum action_result actress[] = {
+      ACTRES_TRANSPORT_DISEMBARK,
+      ACTRES_HUT_ENTER,
+      ACTRES_HUT_FRIGHTEN,
+      ACTRES_UNIT_MOVE,
+      ACTRES_LAST
+    };
+    int i;
+
+    for (i = 0; actress[i] != ACTRES_LAST && act == ACTION_NONE; i++) {
+      act = select_actres_action_unit_on_tile(nmap, actress[i], punit, ptile);
+    }
   }
 
+  if (act == ACTION_NONE) {
+    UNIT_LOG(LOG_DEBUG, punit, "no movement action possible");
+    return FALSE;
+  }
+
+  paction = action_by_number(act);
+
   /* Try not to end move next to an enemy if we can avoid it by waiting */
   if (action_has_result(paction, ACTRES_UNIT_MOVE)
       || action_has_result(paction, ACTRES_TRANSPORT_DISEMBARK)) {
-- 
2.53.0

