From f1a7e5b8af6c5e32e228ca3bc003cf453fbc8392 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 16 Feb 2024 23:57:49 +0200
Subject: [PATCH 24/24] Unhardcode wld.map from unit_order_list_is_sane()

See RM #265

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/city.c               |  5 +++--
 common/unit.c               | 10 ++++++----
 common/unit.h               |  6 ++++--
 server/savegame/savegame2.c |  2 +-
 server/savegame/savegame3.c |  2 +-
 server/unithand.c           |  7 ++++---
 6 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/common/city.c b/common/city.c
index a94cbb7d28..d9f0a7769b 100644
--- a/common/city.c
+++ b/common/city.c
@@ -3615,7 +3615,7 @@ void city_rally_point_clear(struct city *pcity)
 }
 
 /**********************************************************************//**
-  Fill city rally point from the packet.
+  Fill city rally point from a packet.
 **************************************************************************/
 void city_rally_point_receive(const struct packet_city_rally_point *packet,
                               struct city *pcity)
@@ -3646,7 +3646,8 @@ void city_rally_point_receive(const struct packet_city_rally_point *packet,
       pcity->rally_point.orders = NULL;
     }
   } else {
-    checked_orders = create_unit_orders(packet->length, packet->orders);
+    checked_orders = create_unit_orders(&(wld.map),
+                                        packet->length, packet->orders);
     if (!checked_orders) {
       pcity->rally_point.length = 0;
       log_error("invalid rally point orders for %s.",
diff --git a/common/unit.c b/common/unit.c
index a4a3984ad2..46d048def5 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -2686,7 +2686,8 @@ bool unit_is_cityfounder(const struct unit *punit)
 /**********************************************************************//**
   Returns TRUE iff the unit order array is sane.
 **************************************************************************/
-bool unit_order_list_is_sane(int length, const struct unit_order *orders)
+bool unit_order_list_is_sane(const struct civ_map *nmap,
+                             int length, const struct unit_order *orders)
 {
   int i;
 
@@ -2754,7 +2755,7 @@ bool unit_order_list_is_sane(int length, const struct unit_order *orders)
       paction = action_by_number(orders[i].action);
 
       /* Validate main target. */
-      if (index_to_tile(&(wld.map), orders[i].target) == NULL) {
+      if (index_to_tile(nmap, orders[i].target) == NULL) {
         log_error("at index %d, invalid tile target %d for the action %d.",
                   i, orders[i].target, orders[i].action);
         return FALSE;
@@ -2870,12 +2871,13 @@ bool unit_order_list_is_sane(int length, const struct unit_order *orders)
   Sanity-check unit order arrays from a packet and create a unit_order array
   from their contents if valid.
 **************************************************************************/
-struct unit_order *create_unit_orders(int length,
+struct unit_order *create_unit_orders(const struct civ_map *nmap,
+                                      int length,
                                       const struct unit_order *orders)
 {
   struct unit_order *unit_orders;
 
-  if (!unit_order_list_is_sane(length, orders)) {
+  if (!unit_order_list_is_sane(nmap, length, orders)) {
     return NULL;
   }
 
diff --git a/common/unit.h b/common/unit.h
index 0bde79cb81..f6a083ce74 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -573,8 +573,10 @@ struct iterator *cargo_iter_init(struct cargo_iter *iter,
                   cargo_iter_sizeof, cargo_iter_init, _ptrans)
 #define unit_cargo_iterate_end generic_iterate_end
 
-bool unit_order_list_is_sane(int length, const struct unit_order *orders);
-struct unit_order *create_unit_orders(int length,
+bool unit_order_list_is_sane(const struct civ_map *nmap,
+                             int length, const struct unit_order *orders);
+struct unit_order *create_unit_orders(const struct civ_map *nmap,
+                                      int length,
                                       const struct unit_order *orders);
 
 enum gen_action activity_default_action(enum unit_activity act);
diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c
index 98e02bfb2c..a390c50475 100644
--- a/server/savegame/savegame2.c
+++ b/server/savegame/savegame2.c
@@ -5654,7 +5654,7 @@ static void sg_load_sanitycheck(struct loaddata *loading)
 
   players_iterate(pplayer) {
     unit_list_iterate_safe(pplayer->units, punit) {
-      if (!unit_order_list_is_sane(punit->orders.length,
+      if (!unit_order_list_is_sane(&(wld.map), punit->orders.length,
                                    punit->orders.list)) {
         log_sg("Invalid unit orders for unit %d.", punit->id);
         free_unit_orders(punit);
diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c
index ff6dd54a76..2523a3901b 100644
--- a/server/savegame/savegame3.c
+++ b/server/savegame/savegame3.c
@@ -8121,7 +8121,7 @@ static void sg_load_sanitycheck(struct loaddata *loading)
 
   players_iterate(pplayer) {
     unit_list_iterate_safe(pplayer->units, punit) {
-      if (!unit_order_list_is_sane(punit->orders.length,
+      if (!unit_order_list_is_sane(&(wld.map), punit->orders.length,
                                    punit->orders.list)) {
         log_sg("Invalid unit orders for unit %d.", punit->id);
         free_unit_orders(punit);
diff --git a/server/unithand.c b/server/unithand.c
index 3178b10533..e3c9a144cf 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -6738,7 +6738,8 @@ void handle_unit_orders(struct player *pplayer,
 {
   int length = packet->length;
   struct unit *punit = player_unit_by_number(pplayer, packet->unit_id);
-  struct tile *src_tile = index_to_tile(&(wld.map), packet->src_tile);
+  const struct civ_map *nmap = &(wld.map);
+  struct tile *src_tile = index_to_tile(nmap, packet->src_tile);
   struct unit_order *order_list;
 #ifdef FREECIV_DEBUG
   int i;
@@ -6776,7 +6777,7 @@ void handle_unit_orders(struct player *pplayer,
   }
 
   if (length) {
-    order_list = create_unit_orders(length, packet->orders);
+    order_list = create_unit_orders(nmap, length, packet->orders);
     if (!order_list) {
       log_error("received invalid orders from %s for %s (%d).",
                 player_name(pplayer), unit_rule_name(punit), packet->unit_id);
@@ -6810,7 +6811,7 @@ void handle_unit_orders(struct player *pplayer,
   }
 
   if (!packet->repeat) {
-    punit->goto_tile = index_to_tile(&(wld.map), packet->dest_tile);
+    punit->goto_tile = index_to_tile(nmap, packet->dest_tile);
   } else {
     /* Make sure that no old goto_tile remains. */
     punit->goto_tile = NULL;
-- 
2.43.0

