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

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

[Top] [All Lists]

 To: Gregory.Berkolaiko@xxxxxxxxxxxx Subject: [freeciv-ai] Re: (PR#6278) Problems in ai_military_gothere From: "Per I. Mathisen" Date: Thu, 25 Sep 2003 07:30:20 -0700 Reply-to: rt@xxxxxxxxxxxxxx

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

- Per

```
```Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.289
diff -u -r1.289 aiunit.c
--- ai/aiunit.c 21 Sep 2003 09:06:43 -0000      1.289
+++ ai/aiunit.c 25 Sep 2003 15:15:57 -0000
@@ -1323,14 +1323,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;
@@ -1344,7 +1341,7 @@

if (same_pos(dest_x, dest_y, x, y)) {
/* Nowhere to go */
-    return 0;
+    return FALSE;
}

if (is_ground_unit(punit)) {
@@ -1353,6 +1350,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)
@@ -1365,7 +1363,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);
@@ -1399,7 +1397,7 @@
}
} unit_list_iterate_end; /* passengers are safely stowed away */
if (!ai_unit_goto(ferryboat, dest_x, dest_y)) {
-            return -1; /* died */
+            return FALSE; /* died */
}
handle_unit_activity_request(punit, ACTIVITY_IDLE);
} /* else wait, we can GOTO when more passengers come. */
@@ -1421,36 +1419,11 @@

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;
-          }
-          freelog(LOG_DEBUG, "Bodyguard at (%d, %d) is adjacent to (%d, %d)",
-                  i, j, punit->x, punit->y);
-          /* 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;
-    }
-    /* end 'short leash' subroutine */
-
freelog(LOG_DEBUG, "GOTHERE: %s#%d@(%d,%d)->(%d,%d)",
unit_type(punit)->name, punit->id,
punit->x, punit->y, dest_x, dest_y);
if (!ai_unit_goto(punit, dest_x, dest_y)) {
-      return -1;               /* died */
+      return FALSE; /* died */
}
/* liable to bump into someone that will kill us.  Should avoid? */
} else {
@@ -1463,9 +1436,9 @@
CHECK_UNIT(punit);

if (!same_pos(punit->x, punit->y, x, y)) {
-    return 1;                  /* moved */
+    return TRUE; /* moved */
} else {
-    return 0;                  /* didn't move, didn't die */
+    return FALSE; /* didn't move, didn't die */
}
}

@@ -2330,7 +2303,7 @@
* on a ferry. This fixes the problem (usually). */
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;
}
@@ -2374,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);
}
}
}
```
```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 14:39:33
@@ -148,6 +180,16 @@
{
struct unit *charge = find_unit_by_id(punit->ai.charge);
struct unit *bodyguard = find_unit_by_id(punit->ai.bodyguard);
+
+  /* Free our ferry */
+    struct unit *ferryboat = find_unit_by_id(punit->ai.ferryboat);
+
+    punit->ai.ferryboat = 0;
+    if (ferryboat) {
+      ferryboat->ai.passenger = 0;
+    }
+  }

if (punit->activity == ACTIVITY_GOTO) {
/* It would indicate we're going somewhere otherwise */
Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.290
diff -u -r1.290 aiunit.c
--- ai/aiunit.c 2003/09/23 18:57:39     1.290
+++ ai/aiunit.c 2003/09/25 14:39:33
@@ -60,6 +60,7 @@
#include "aiunit.h"

#define LOGLEVEL_RECOVERY LOG_DEBUG
+#define LOGLEVEL_FSTK LOG_DEBUG

static void ai_manage_military(struct player *pplayer,struct unit *punit);
static void ai_manage_caravan(struct player *pplayer, struct unit *punit);
@@ -2522,10 +2528,10 @@
}
if (aunit->ai.bodyguard == BODYGUARD_NONE || bodyguard ||
(pcity && pcity->ai.invasion >= 2)) {
-       if (pcity) {
-         UNIT_LOG(LOG_DEBUG, punit, "Ferrying to %s to %s, invasion = %d, body
= %d",
+       if (pcity) {
+         UNIT_LOG(LOGLEVEL_FSTK, punit, "Ferrying ->%s, invasion = %d",
unit_name(aunit->type), pcity->name,
-                 pcity->ai.invasion, aunit->ai.bodyguard);
+                 pcity->ai.invasion);
}
n++;
handle_unit_activity_request(aunit, ACTIVITY_SENTRY);
@@ -2556,8 +2562,8 @@
return; /* oops! */
}
send_unit_info(pplayer, punit); /* to get the crosshairs right -- Syela
*/
-    } else {
-      UNIT_LOG(LOG_DEBUG, punit, "Ferryboat %d@(%d,%d) stalling.",
+    } else {
+      UNIT_LOG(LOGLEVEL_FSTK, punit, "Ferryboat %d@(%d,%d) stalling.",
punit->id, punit->x, punit->y);
if(is_barbarian(pplayer)) /* just in case */
(void) ai_manage_explorer(punit);
@@ -2573,7 +2579,7 @@

/* ok, not carrying anyone, even the ferryman */
punit->ai.passenger = 0;
-  UNIT_LOG(LOG_DEBUG, punit, "Ferryboat is lonely.");
+  UNIT_LOG(LOGLEVEL_FSTK, punit, "Ferryboat is lonely.");
handle_unit_activity_request(punit, ACTIVITY_IDLE);

/* Release bodyguard and let it roam */
```