Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2000:
[Freeciv-Dev] patch: nicer rand_neighbour()
Home

[Freeciv-Dev] patch: nicer rand_neighbour()

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] patch: nicer rand_neighbour()
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Sun, 23 Jan 2000 14:32:56 +1100 (EST)

This patch uses a slightly nicer method, especially for handling 
north and south poles, in barbarian.c:rand_neighbour().

-- David
--- freeciv-mod/server/barbarian.c      Sat Jan 22 18:00:25 2000
+++ fc-adv/server/barbarian.c   Sat Jan 22 18:02:03 2000
@@ -78,47 +78,26 @@
 static void rand_neighbour( int x0, int y0, int *x, int *y)
 {
   int choice;
+  int xoff[] = { -1,  0,  1, -1,  1, -1,  0,  1 };
+  int yoff[] = { -1, -1, -1,  0,  0,  1,  1,  1 };
 
-  choice = myrand(8);
-  switch( choice ) {
-    case 0:
-      *x = x0-1;
-      *y = y0-1;
-      break;
-    case 1:
-      *x = x0;
-      *y = y0-1;
-      break;
-    case 2:
-      *x = x0+1;
-      *y = y0-1;
-      break;
-    case 3:
-      *x = x0-1;
-      *y = y0;
-      break;
-    case 4:
-      *x = x0+1;
-      *y = y0;
-      break;
-    case 5:
-      *x = x0-1;
-      *y = y0+1;
-      break;
-    case 6:
-      *x = x0;
-      *y = y0+1;
-      break;
-    case 7:
-      *x = x0+1;
-      *y = y0+1;
-      break;
-    default:
-      break;
+  if (y0 == 0) {
+    choice = 3 + myrand(5);
+  } else if(y0 == map.ysize-1){
+    choice = myrand(5);
+  } else {
+    choice = myrand(8);
   }
+  *x = x0 + xoff[choice];
+  *y = y0 + yoff[choice];
+  
+#if 0
+  /* shouldn't need this now: */
   *y = map_adjust_y(*y);
   if( *x == x0 && *y == y0 ) /* might happen after y adjustments */
     *x += 1;                 /* stupid but safe, rare anyway     */
+#endif
+  
   *x = map_adjust_x(*x);
 }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] patch: nicer rand_neighbour(), David Pfitzner <=