<URL: http://bugs.freeciv.org/Ticket/Display.html?id=34727 >

 Helper function for units that might control boat. Barbarian leader
converted to use it. I expect more users in near future.


 - ML

diff -Nurd -X.diff_ignore freeciv/ai/aiferry.c freeciv/ai/aiferry.c
--- freeciv/ai/aiferry.c	2007-01-28 00:33:32.000000000 +0200
+++ freeciv/ai/aiferry.c	2007-01-28 19:40:54.000000000 +0200
@@ -342,6 +342,28 @@
 }
 
 /****************************************************************************
+  Check if unit is boss in ferry
+****************************************************************************/
+bool is_boss_of_boat(struct unit *punit)
+{
+  struct unit *ferry;
+
+  if (punit->transported_by == -1) {
+    /* Not even in boat */
+    return FALSE;
+  }
+
+  ferry = player_find_unit_by_id(unit_owner(punit), punit->transported_by);
+
+  if (ferry != NULL
+      && ferry->ai.passenger == punit->id) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/****************************************************************************
   Proper and real PF function for finding a boat.  If you don't require
   the path to the ferry, pass path=NULL.
   Return the unit ID of the boat; punit is the passenger.
diff -Nurd -X.diff_ignore freeciv/ai/aiferry.h freeciv/ai/aiferry.h
--- freeciv/ai/aiferry.h	2007-01-28 00:33:32.000000000 +0200
+++ freeciv/ai/aiferry.h	2007-01-28 19:38:22.000000000 +0200
@@ -60,6 +60,7 @@
 				    struct pft_amphibious *parameter);
 
 bool is_boat_free(struct unit *boat, struct unit *punit, int cap);
+bool is_boss_of_boat(struct unit *punit);
 
 /*
  * Main boat managing function.  Gets units on board to where they want to
diff -Nurd -X.diff_ignore freeciv/ai/aiunit.c freeciv/ai/aiunit.c
--- freeciv/ai/aiunit.c	2007-01-27 18:26:36.000000000 +0200
+++ freeciv/ai/aiunit.c	2007-01-28 19:41:57.000000000 +0200
@@ -2309,38 +2309,33 @@
       return;
   }
 
-  if (leader->transported_by != -1) {
-    /* Leader is at boat */
-    struct unit *boat = find_unit_by_id(leader->transported_by);
-
-    if (boat->ai.passenger == leader->id) {
-      /* We are in charge. Of course, since we are the leader...
-       * But maybe somebody more militaristic should lead our ship to battle! */
-
-      /* First release boat from leaders lead */
-      aiferry_clear_boat(leader);
+  if (is_boss_of_boat(leader)) {
+    /* We are in charge. Of course, since we are the leader...
+     * But maybe somebody more militaristic should lead our ship to battle! */
 
-      unit_list_iterate(leader->tile->units, warrior) {
-        if (!unit_has_role(unit_type(warrior), L_BARBARIAN_LEADER)
-            && get_transporter_capacity(warrior) == 0
-            && warrior->moves_left > 0) {
-          /* This seems like a good warrior to lead us in to conquest! */
-          ai_manage_unit(pplayer, warrior);
+    /* First release boat from leaders lead */
+    aiferry_clear_boat(leader);
 
-          /* If we reached our destination, ferryboat already called
-           * ai_manage_unit() for leader. So no need to continue here.
-           * Leader might even be dead.
-           * If this return is removed, surronding unit_list_iterate()
-           * has to be replaced with unit_list_iterate_safe()*/
-          return;
-        }
-      } unit_list_iterate_end;
-    }
+    unit_list_iterate(leader->tile->units, warrior) {
+      if (!unit_has_role(unit_type(warrior), L_BARBARIAN_LEADER)
+          && get_transporter_capacity(warrior) == 0
+          && warrior->moves_left > 0) {
+        /* This seems like a good warrior to lead us in to conquest! */
+        ai_manage_unit(pplayer, warrior);
 
-    /* If we are not in charge of the boat, continue as if we
-     * were not in a boat - we may want to leave the ship now. */
+        /* If we reached our destination, ferryboat already called
+         * ai_manage_unit() for leader. So no need to continue here.
+         * Leader might even be dead.
+         * If this return is removed, surronding unit_list_iterate()
+         * has to be replaced with unit_list_iterate_safe()*/
+        return;
+      }
+    } unit_list_iterate_end;
   }
 
+  /* If we are not in charge of the boat, continue as if we
+   * were not in a boat - we may want to leave the ship now. */
+
   /* the following takes much CPU time and could be avoided */
   generate_warmap(tile_get_city(leader->tile), leader);
 
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to