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

[Freeciv-Dev] (PR#6167) gen-topologies safe version of normalize_map_pos

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6167) gen-topologies safe version of normalize_map_pos
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 15 Sep 2003 16:12:07 -0700
Reply-to: rt@xxxxxxxxxxxxxx

This simple patch changes normalize_map_pos to be done in native 
coordinates, and to check for wrapping in both X and Y dimensions.

A helper macro WRAP() is added to shared.h.

jason

Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.142
diff -u -r1.142 map.c
--- common/map.c        2003/09/11 11:30:42     1.142
+++ common/map.c        2003/09/15 23:10:26
@@ -1331,19 +1331,35 @@
 
 /**************************************************************************
   If the position is real, it will be normalized and TRUE will be returned.
-  If the position is unreal, it will be wrapped and FALSE will be returned.
+  If the position is unreal, it will be left unchanged and FALSE will be
+  returned.
 
   Note, we need to leave x and y with sane values even in the unreal case.
   Some callers may for instance call nearest_real_pos on these values.
 **************************************************************************/
 bool normalize_map_pos(int *x, int *y)
 {
-  while (*x < 0)
-    *x += map.xsize;
-  while (*x >= map.xsize)
-    *x -= map.xsize;
+  int nat_x, nat_y;
 
-  return (0 <= *y && *y < map.ysize);
+  /* Normalization is best done in native coordinatees */
+  map_to_native_pos(&nat_x, &nat_y, *x, *y);
+
+  if (!((topo_has_flag(TF_WRAPX) || (nat_x >= 0 && nat_x < map.xsize))
+       && (topo_has_flag(TF_WRAPY) || (nat_y >= 0 && nat_y < map.ysize)))) {
+    return FALSE;
+  }
+
+  if (topo_has_flag(TF_WRAPX)) {
+    nat_x = WRAP(nat_x, map.xsize);
+  }
+
+  if (topo_has_flag(TF_WRAPY)) {
+    nat_y = WRAP(nat_y, map.ysize);
+  }
+
+  /* Now map things back to standard map coordinates. */
+  native_to_map_pos(x, y, nat_x, nat_y);
+  return TRUE;
 }
 
 /**************************************************************************
Index: common/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/shared.h,v
retrieving revision 1.116
diff -u -r1.116 shared.h
--- common/shared.h     2003/08/15 02:39:59     1.116
+++ common/shared.h     2003/09/15 23:10:26
@@ -83,6 +83,10 @@
 #endif
 #define CLIP(lower,this,upper) \
     ((this)<(lower)?(lower):(this)>(upper)?(upper):(this))
+#define WRAP(value, range)                                                  \
+    ((value) < 0                                                            \
+     ? ((value) % (range) != 0 ? (value) % (range) + (range) : 0)           \
+     : ((value) >= (range) ? (value) % (range) : (value)))
 
 #define BOOL_VAL(x) ((x)!=0)
 

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