Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2003:
[Freeciv-Dev] (PR#3508) coordinate handling fix in ai_military_gothere
Home

[Freeciv-Dev] (PR#3508) coordinate handling fix in ai_military_gothere

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3508) coordinate handling fix in ai_military_gothere
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 23 Feb 2003 02:18:23 -0800
Reply-to: rt@xxxxxxxxxxxxxx

The attached patch provides a small fix for the handling of coordinates 
in ai_military_gothere.  At the beginning of the function we have

   int boatid = 0, bx = 0, by = 0;

then later there is

   if (...) boatid = find_boat(pplayer, &bx, &by, 2);

and finally

   if (!same_pos(x, y, bx, by)) ai_unit_goto(punit, bx, by);

This check is intended to determine if we found a boat earlier (bx and 
by are only set if a boat is found).  The first bug is that this code 
will always tell the AI to go off toward (0,0) (unless they already 
happen to be there); presumably most of the time this is harmless.  It 
will also, of course, blatantly fail under an isometric topology (since 
(0,0) isn't a real position, there will be a failed assertion in same_pos).

The fix is simple: check boatid before comparing positions.  This gives 
the identical behavior (except in the buggy cases mentioned above) and 
is faster.  We can also set (bx, by) = (-1, -1) at the beginning to make 
sure these values are never accidentally used.

The error may actually go deeper than this, though.  The whole block of 
code surrounding the ai_unit_goto line above is geared toward the 
ferryboat (boatid).  But much of the time this unit doesn't exist 
(boatid==0).  So it might make sense to put the boatid check higher in 
the block.

jason

Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.264
diff -u -r1.264 aiunit.c
--- ai/aiunit.c 2003/02/17 22:49:27     1.264
+++ ai/aiunit.c 2003/02/23 10:02:14
@@ -1307,7 +1307,7 @@
 static int ai_military_gothere(struct player *pplayer, struct unit *punit,
                               int dest_x, int dest_y)
 {
-  int id, x, y, boatid = 0, bx = 0, by = 0;
+  int id, x, y, boatid = 0, bx = -1, by = -1;
   struct unit *ferryboat;
   struct unit *def;
   struct city *dcity;
@@ -1358,7 +1358,7 @@
       punit->ai.ferryboat = boatid;
       freelog(LOG_DEBUG, "%s: %d@(%d, %d): Looking for BOAT (id=%d).",
                    pplayer->name, punit->id, punit->x, punit->y, boatid);
-      if (!same_pos(x, y, bx, by)) {
+      if (boat_id > 0 && !same_pos(x, y, bx, by)) {
        if (!ai_unit_goto(punit, bx, by)) {
          return -1;            /* died */
        }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3508) coordinate handling fix in ai_military_gothere, Jason Short <=