Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2003:
[Freeciv-Dev] (PR#6256) gen-topologies version of map_distance_vector
Home

[Freeciv-Dev] (PR#6256) gen-topologies version of map_distance_vector

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6256) gen-topologies version of map_distance_vector
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Sep 2003 09:52:53 -0700
Reply-to: rt@xxxxxxxxxxxxxx

The attached patch changes map_distance_vector to work with gen-topologies.

Wrapping must be done in native coordinates, but the final vector is 
given in map coordinates.  To do this the function converts to native 
coordinates, finds the native vector, converts this back into a pair of 
native coordinates, changes these back to map coordinates, and finally 
finds the map vector.

The CHECK_MAP_POS calls are removed in the process because this 
operation can easily be done on non-normal positions, and some callers 
might actually want to.  Also map_distance_vector is used almost 
entirely as a helper function in other core map operations which should 
already do this check.

jason

? rc
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.144
diff -u -r1.144 map.c
--- common/map.c        2003/09/23 16:10:23     1.144
+++ common/map.c        2003/09/23 16:52:42
@@ -1420,17 +1420,33 @@
 **************************************************************************/
 void map_distance_vector(int *dx, int *dy, int x0, int y0, int x1, int y1)
 {
-  CHECK_MAP_POS(x0, y0);
-  CHECK_MAP_POS(x1, y1);
+  if (topo_has_flag(TF_WRAPX) || topo_has_flag(TF_WRAPY)) {
+    /* Wrapping is done in native coordinates. */
+    map_to_native_pos(&x0, &y0, x0, y0);
+    map_to_native_pos(&x1, &y1, x1, y1);
 
-  *dx = x1 - x0;
+    /* Find the "native" distance vector. This corresponds closely to the
+     * map distance vector but is easier to wrap. */
+    *dx = x1 - x0;
+    *dy = y1 - y0;
+    if (topo_has_flag(TF_WRAPX)) {
+      /* Wrap dx to be in [-map.xsize/2, map.xsize/2). */
+      *dx = WRAP(*dx + map.xsize / 2, map.xsize) - map.xsize / 2;
+    }
+    if (topo_has_flag(TF_WRAPY)) {
+      /* Wrap dy to be in [-map.ysize/2, map.ysize/2). */
+      *dy = WRAP(*dy + map.ysize / 2, map.ysize) - map.ysize / 2;
+    }
 
-  if (*dx > map.xsize / 2) {
-    *dx -= map.xsize;
-  } else if (*dx <= -map.xsize / 2) {
-    *dx += map.xsize;
+    /* Convert the native delta vector back to a pair of map positions. */
+    x1 = x0 + *dx;
+    y1 = y0 + *dy;
+    native_to_map_pos(&x0, &y0, x0, y0);
+    native_to_map_pos(&x1, &y1, x1, y1);
   }
 
+  /* Find the final (map) vector. */
+  *dx = x1 - x0;
   *dy = y1 - y0;
 }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#6256) gen-topologies version of map_distance_vector, Jason Short <=