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

On 28/01/07, Marko Lindqvist <[EMAIL PROTECTED]> wrote:
>
>  Helper function for units that might control boat. Barbarian leader
> converted to use it. I expect more users in near future.

 Updated against svn


 - ML

diff -Nurd -X.diff_ignore freeciv/ai/aiferry.c freeciv/ai/aiferry.c
--- freeciv/ai/aiferry.c	2007-08-03 22:14:15.000000000 +0300
+++ freeciv/ai/aiferry.c	2007-08-03 22:22:25.000000000 +0300
@@ -349,6 +349,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-08-03 22:22:25.000000000 +0300
@@ -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-08-03 22:14:15.000000000 +0300
+++ freeciv/ai/aiunit.c	2007-08-03 22:42:33.000000000 +0300
@@ -2351,38 +2351,33 @@
       return;
   }
 
-  if (leader->transported_by != -1) {
-    /* Leader is at boat */
-    struct unit *boat = game_find_unit_by_number(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);
-
-      unit_list_iterate(leader->tile->units, warrior) {
-        if (!unit_has_type_role(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 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 (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! */
+  
+    /* First release boat from leaders lead */
+    aiferry_clear_boat(leader);
+  
+    unit_list_iterate(leader->tile->units, warrior) {
+      if (!unit_has_type_role(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