Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2001:
[Freeciv-Dev] Re: Cleaning up gotohand.c (PR#1068)
Home

[Freeciv-Dev] Re: Cleaning up gotohand.c (PR#1068)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: Cleaning up gotohand.c (PR#1068)
From: Gregory Berkolaiko <gberkolaiko@xxxxxxxxxxx>
Date: Sat, 1 Dec 2001 19:36:00 +0000 (GMT)

 --- Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx> wrote: 
> 
> > In goto_zoc_ok:
> > * as was proposed in the comment, changed the code to take into
> account
> > where we _really_ came from, not to guess the direction from dir_ok
> > * changed the comment accordingly
> 
> Please make a seperate patch.

done.

more to follow.

Patch is short, so I hope for quick review.

G.

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and 
Music Charts
http://uk.my.yahoo.com
? rfile1
Index: server/gotohand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gotohand.c,v
retrieving revision 1.122
diff -u -r1.122 gotohand.c
--- server/gotohand.c   2001/10/08 12:11:17     1.122
+++ server/gotohand.c   2001/12/01 16:26:20
@@ -407,32 +407,6 @@
 }
 
 /**************************************************************************
-Returns false if you are going the in opposite direction of the destination.
-The 3 directions most opposite the one to the target is considered wrong.
-**************************************************************************/
-static int dir_ok(int src_x, int src_y, int dest_x, int dest_y, int dir)
-{
-  int diff_x, diff_y, dx, dy, scalar_product;
-
-  if (dest_x > src_x) {
-    diff_x = dest_x - src_x < map.xsize / 2 ? 1 : -1;
-  } else if (dest_x < src_x) {
-    diff_x = src_x - dest_x < map.xsize / 2 ? -1 : 1;
-  } else {                     /* dest_x == src_x */
-    diff_x = 0;
-  }
-  if (dest_y != src_y)
-    diff_y = dest_y > src_y ? 1 : -1;
-  else
-    diff_y = 0;
-
-  DIRSTEP(dx, dy, dir);
-  scalar_product = diff_x * dx + diff_y * dy;
-
-  return scalar_product >= 0;
-}
-
-/**************************************************************************
 Return the direction that takes us most directly to dest_x,dest_y.
 The direction is a value for use in DIR_DX[] and DIR_DY[] arrays.
 
@@ -465,11 +439,15 @@
 }
 
 /**************************************************************************
-Can we move between for ZOC? (only for land units). Includes a speciel case
+Can we move between for ZOC? (only for land units). Includes a special case
 only relevant for GOTOs (see below). 
+@param came_from is a bit-vector containing the directions we could have 
+come from.  It's intended use is to recieve an element of array 
+"local_vector" from the fucntion find_the_shortest_path where such 
+information is recorded.
 **************************************************************************/
 static int goto_zoc_ok(struct unit *punit, int src_x, int src_y,
-                      int dest_x, int dest_y)
+                      int dest_x, int dest_y, unsigned char came_from)
 {
   if (can_step_taken_wrt_to_zoc
       (punit->type, unit_owner(punit), src_x, src_y, dest_x, dest_y))
@@ -481,12 +459,10 @@
      This code makes sure that if there is a unit in the way that the GOTO
      made a path over (attack), the unit's ZOC effect (which is now
      irrelevant, it is dead) doesn't have any effect.
+     That is, if there is a unit standing on a tile that we (possibly) came 
+     from, we will not take it into account.
      If this wasn't here a path involving two enemy units would not be
-     found by the algoritm.
-
-     FIXME: We currently use dir_ok to asses where we came from; it would
-     be more correct if the function was passed the direction we actually
-     came from (dir) */
+     found by the algoritm. */
 
   /* If we just started the GOTO the enemy unit blocking ZOC on the tile
      we come from is still alive. */
@@ -499,13 +475,15 @@
     struct player *owner = unit_owner(punit);
 
     adjc_dir_iterate(src_x, src_y, x, y, dir) {
-      if (!dir_ok(dest_x, dest_y, punit->goto_dest_x, punit->goto_dest_y, dir))
-       continue;
-      if ((map_get_terrain(x, y) != T_OCEAN)
-         && is_enemy_unit_tile(map_get_tile(x, y), owner))
+      /* if we didn't come from there */
+      if ( (1<<dir & came_from) == 0
+          && (map_get_terrain(x, y) != T_OCEAN)
+          /* and there is an enemy there */
+          && is_enemy_unit_tile(map_get_tile(x, y), owner))
+       /* then it counts in the zoc claculation */
        return 0;
     } adjc_dir_iterate_end;
-    return 0;
+    return 1;
   }
 }
 
@@ -519,10 +497,14 @@
 starttile), and then tries to move in each of the 8 directions from there.
 This then gives us more tiles to more from. Repeat until we meet the
 destination or there is no remaining tiles.
+All possible paths coming out of the starttile are stored in local_vector.
+After we've reached the destination, only the paths connecting starttile
+with the destination are copied from local_vector to the warmap's vector.
+
 Whenever we reach a tile we see how many movepoints it took to get there,
 and compare it to eventual previous moves to the tile. If the route is
 faster or just as fast we mark the direction from which we came on the
-destination tile. (via a vector local to this function). If we come to a
+local_vector array and also record the new movecost. If we come to a
 tile we have meet before, and the cost of the route we have taken is
 smaller than the previous one we add the tile to the list again, to get
 the tiles it leads to updated etc. The change in move_cost for the tile
@@ -691,7 +673,7 @@
            else
              move_cost = SINGLE_MOVE;
          }
-       } else if (!goto_zoc_ok(punit, x, y, x1, y1))
+       } else if (!goto_zoc_ok(punit, x, y, x1, y1, local_vector[x][y]))
          continue;
 
        if (restriction == GOTO_MOVE_STRAIGHTEST && dir == straight_dir)

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