Complete.Org: Mailing Lists: Archives: freeciv-ai: September 2003:
[freeciv-ai] Re: (PR#6278) Problems in ai_military_gothere
Home

[freeciv-ai] Re: (PR#6278) Problems in ai_military_gothere

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [freeciv-ai] Re: (PR#6278) Problems in ai_military_gothere
From: "Gregory Berkolaiko" <Gregory.Berkolaiko@xxxxxxxxxxxx>
Date: Thu, 25 Sep 2003 09:51:34 -0700
Reply-to: rt@xxxxxxxxxxxxxx

On Thu, 25 Sep 2003, Per I. Mathisen wrote:

> Two small patches that might be somewhat related. More comin'.

Same but updated and slightly changed.

Not perfect, but improve ferrying quite a bit.

Please check and apply.

G.

Index: ai/aitools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v
retrieving revision 1.88
diff -u -r1.88 aitools.c
--- ai/aitools.c        2003/08/08 22:11:41     1.88
+++ ai/aitools.c        2003/09/25 17:10:00
@@ -149,6 +149,17 @@
   struct unit *charge = find_unit_by_id(punit->ai.charge);
   struct unit *bodyguard = find_unit_by_id(punit->ai.bodyguard);
 
+  /* Free our ferry */
+  if (task == AIUNIT_NONE || task == AIUNIT_DEFEND_HOME) {
+    struct unit *ferryboat = find_unit_by_id(punit->ai.ferryboat);
+
+    punit->ai.ferryboat = 0;
+    if (ferryboat && ferryboat->ai.passenger == punit->id) {
+      /* Do not free somebody else's ferry... */
+      ferryboat->ai.passenger = 0;
+    }
+  }
+
   if (punit->activity == ACTIVITY_GOTO) {
     /* It would indicate we're going somewhere otherwise */
     handle_unit_activity_request(punit, ACTIVITY_IDLE);
Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.292
diff -u -r1.292 aiunit.c
--- ai/aiunit.c 2003/09/25 16:55:03     1.292
+++ ai/aiunit.c 2003/09/25 17:40:15
@@ -1332,14 +1332,11 @@
 }
 
 /**************************************************************************
-  Return values: 
-  -1: died
-  0: didn't move
-  1: moved
-  TODO: Convert to bool.
+  Return values: FALSE if died or stuck, TRUE otherwise. (This function
+  is not server-side autoattack safe.)
 **************************************************************************/
-static int ai_military_gothere(struct player *pplayer, struct unit *punit,
-                              int dest_x, int dest_y)
+static bool ai_military_gothere(struct player *pplayer, struct unit *punit,
+                                int dest_x, int dest_y)
 {
   int id, x, y, boatid = 0, bx = -1, by = -1;
   struct unit *ferryboat = NULL;
@@ -1353,7 +1350,7 @@
 
   if (same_pos(dest_x, dest_y, x, y)) {
     /* Nowhere to go */
-    return 0;
+    return FALSE;
   }
 
   if (is_ground_unit(punit)) { 
@@ -1362,6 +1359,7 @@
     ferryboat = unit_list_find(&(map_get_tile(x, y)->units), boatid);
   }
 
+  /* See if we need a bodyguard at our destination */
   ai_gothere_bodyguard(punit, dest_x, dest_y);
   
   if (!goto_is_sane(punit, dest_x, dest_y, TRUE) 
@@ -1373,7 +1371,7 @@
       /* FIXME: this can lose bodyguard */
       if (!ai_unit_goto(punit, bx, by)) {
         /* Died. */
-        return -1;
+        return FALSE;
       }
     }
     ptile = map_get_tile(punit->x, punit->y);
@@ -1390,28 +1388,23 @@
 
       /* Last ingredient: a beachhead. */
       if (find_beachhead(punit, dest_x, dest_y, &boat_x, &boat_y)) {
-        UNIT_LOG(LOG_DEBUG, punit, "Found beachhead (%d,%d)", boat_x, boat_y);
+        UNIT_LOG(LOG_DEBUG, punit, "Found beachhead (%d,%d), all aboard", 
+                 boat_x, boat_y);
        set_goto_dest(ferryboat, boat_x, boat_y);
        set_goto_dest(punit, dest_x, dest_y);
-        if (ground_unit_transporter_capacity(punit->x, punit->y, pplayer)
-            <= 0) {
-          /* FIXME: perhaps we should only require only two passengers */
-          UNIT_LOG(LOG_DEBUG, ferryboat, "All aboard!");
-          unit_list_iterate(ptile->units, mypass) {
-            if (mypass->ai.ferryboat == ferryboat->id
-                && punit->owner == mypass->owner) {
-              handle_unit_activity_request(mypass, ACTIVITY_SENTRY);
-              def = unit_list_find(&ptile->units, mypass->ai.bodyguard);
-              if (def) {
-                handle_unit_activity_request(def, ACTIVITY_SENTRY);
-              }
+        unit_list_iterate(ptile->units, mypass) {
+          if (mypass->ai.ferryboat == ferryboat->id
+              && punit->owner == mypass->owner) {
+            handle_unit_activity_request(mypass, ACTIVITY_SENTRY);
+            def = unit_list_find(&ptile->units, mypass->ai.bodyguard);
+            if (def) {
+              handle_unit_activity_request(def, ACTIVITY_SENTRY);
             }
-          } unit_list_iterate_end; /* passengers are safely stowed away */
-          if (!ai_unit_goto(ferryboat, dest_x, dest_y)) {
-            return -1; /* died */
           }
-          handle_unit_activity_request(punit, ACTIVITY_IDLE);
-        } /* else wait, we can GOTO when more passengers come. */
+        } unit_list_iterate_end; /* passengers are safely stowed away */
+        if (!ai_unit_goto(ferryboat, dest_x, dest_y)) {
+          return FALSE; /* died */
+        }
       }
     } 
   }
@@ -1420,47 +1413,26 @@
     /* we are on a ferry! did we arrive? */
     boat_arrived = same_pos(ferryboat->x, ferryboat->y,
                             goto_dest_x(ferryboat), goto_dest_y(ferryboat))
-                   || is_tiles_adjacent(ferryboat->x, ferryboat->y,
-                            goto_dest_x(ferryboat), goto_dest_y(ferryboat));
+      || is_tiles_adjacent(ferryboat->x, ferryboat->y,
+                           goto_dest_x(ferryboat), goto_dest_y(ferryboat));
   } else {
     boat_arrived = FALSE;
   }
 
+  if (boat_arrived) {
+    handle_unit_activity_request(punit, ACTIVITY_IDLE);
+    UNIT_LOG(LOG_DEBUG, punit, "Our boat has arrived");
+  }
+
   /* Go where we should be going if we can, and are at our destination 
    * if we are on a ferry */
   if (goto_is_sane(punit, dest_x, dest_y, TRUE) && punit->moves_left > 0
       && (!ferryboat || boat_arrived)) {
     set_goto_dest(punit, dest_x, dest_y);
-    
-    /* The following code block is supposed to stop units from running away 
-     * from their bodyguards, and not interfere with those that don't have 
-     * bodyguards nearby -- Syela */
-    /* The case where the bodyguard has moves left and could meet us en route 
-     * is not handled properly.  There should be a way to do it with dir_ok 
-     * but I'm tired now. -- Syela */
-    if (punit->ai.bodyguard == BODYGUARD_WANTED) { 
-      adjc_iterate(punit->x, punit->y, i, j) {
-        unit_list_iterate(map_get_tile(i, j)->units, aunit) {
-          if (aunit->ai.charge != punit->id || punit->owner != aunit->owner) {
-            continue;
-          }
-          UNIT_LOG(LOGLEVEL_BODYGUARD, punit, 
-                   "our bodyguard %s[%d] is next to us at (%d, %d)",
-                   unit_type(aunit)->name, aunit->id, i, j);
-          /* FIXME: What is happening here? */
-          if (aunit->moves_left > 0) {
-            return 0;
-          } else {
-            return (ai_unit_move(punit, i, j) ? 1 : 0);
-          }
-        } unit_list_iterate_end;
-      } adjc_iterate_end;
-    }
-    /* end 'short leash' subroutine */
-    
     UNIT_LOG(LOG_DEBUG, punit, "Attempt to walk to (%d,%d)", dest_x, dest_y);
     if (!ai_unit_goto(punit, dest_x, dest_y)) {
-      return -1;               /* died */
+      /* died */
+      return FALSE;
     }
     /* liable to bump into someone that will kill us.  Should avoid? */
   } else {
@@ -1470,11 +1442,7 @@
   /* Dead unit shouldn't reach this point */
   CHECK_UNIT(punit);
   
-  if (!same_pos(punit->x, punit->y, x, y)) {
-    return 1;                  /* moved */
-  } else {
-    return 0;                  /* didn't move, didn't die */
-  }
+  return (!same_pos(punit->x, punit->y, x, y));
 }
 
 /*************************************************************************
@@ -2333,9 +2301,9 @@
           || (could_unit_move_to_tile(punit, dest_x, dest_y) == 0)) {
         /* Can't attack or move usually means we are adjacent but
          * on a ferry. This fixes the problem (usually). */
-        UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> %d, %d", 
+        UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d,%d)", 
                  dest_x, dest_y);
-        if (ai_military_gothere(pplayer, punit, dest_x, dest_y) <= 0) {
+        if (!ai_military_gothere(pplayer, punit, dest_x, dest_y)) {
           /* Died or got stuck */
           return;
         }
@@ -2379,14 +2347,14 @@
     if ((pc = dist_nearest_city(pplayer, punit->x, punit->y, FALSE, TRUE))) {
       if (!is_ocean(map_get_terrain(punit->x, punit->y))) {
         UNIT_LOG(LOG_DEBUG, punit, "Barbarian marching to conquer %s", 
pc->name);
-        ai_military_gothere(pplayer, punit, pc->x, pc->y);
+        (void) ai_military_gothere(pplayer, punit, pc->x, pc->y);
       } else {
         /* sometimes find_beachhead is not enough */
         if (!find_beachhead(punit, pc->x, pc->y, &fx, &fy)) {
           find_city_beach(pc, punit, &fx, &fy);
         }
         UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to %s", pc->name);
-        ai_military_gothere(pplayer, punit, fx, fy);
+        (void) ai_military_gothere(pplayer, punit, fx, fy);
       }
     }
   }

[Prev in Thread] Current Thread [Next in Thread]