Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2001:
[Freeciv-Dev] Re: buglet in normalization in init_new_game() (PR#1063)
Home

[Freeciv-Dev] Re: buglet in normalization in init_new_game() (PR#1063)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: buglet in normalization in init_new_game() (PR#1063)
From: jdorje@xxxxxxxxxxxxxxxxxxxxx
Date: Wed, 14 Nov 2001 13:08:50 -0800 (PST)

jdorje@xxxxxxxxxxxxxxxxxxxxx wrote:
> 
> Raimar Falke wrote:
> 
> > IMHO it looks like a job for a new circle_iterate.
> 
> The attached patch is an adequate implementation of circle_iterate
> (actually, I'm not happy with the use of sqrt() but I'm not sure how to
> do without it), but doesn't quite work for this case because the current
> code there adds 1 to the radius before comparing it to the maximum
> radius.  So if you play with this patch the initial visible area will be
> larger than it's supposed to be.
> 
> IMO it is the current implementation that is broken, but perhaps there's
> some reason for that.  My solution would be to adjust
> game.rgame.init_vis_radius_sq accordingly.

My mistake.  The problem was that show_area itself takes a radius, for
which "1" was being passed.  Thus, it was necessary to restrict the
circle to 1 unit smaller than it was supposed to be.

Changing this value to 0 should give the desired results.

jason
? rc
? old
? topology
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.103
diff -u -r1.103 map.h
--- common/map.h        2001/11/11 17:46:20     1.103
+++ common/map.h        2001/11/14 21:06:13
@@ -13,7 +13,9 @@
 #ifndef FC__MAP_H
 #define FC__MAP_H
 
-#include "assert.h"
+#include <assert.h>
+#include <math.h>
+
 #include "player.h"
 #include "terrain.h"
 #include "unit.h"
@@ -429,6 +431,32 @@
     }                                                                         \
   }                                                                           \
 }
+
+/* Iterate through all tiles in a circle with given center and squared
+   radius.  Positions returned will have adjusted (x, y); those with 
+   illegal (unreal) positions will be automatically discarded. */
+#define circle_iterate(center_x, center_y, sq_radius, x_itr, y_itr)           \
+{                                                                             \
+  int _x_itr, _y_itr;                                                         \
+  int _center_x = (center_x), _center_y = (center_y);                         \
+  int _sq_radius = (sq_radius), _radius = (int)sqrt(_sq_radius);              \
+  CHECK_MAP_POS(_center_x, _center_y);                                        \
+  for (_y_itr = -_radius;                                                     \
+       _y_itr <= _radius;                                                     \
+       _y_itr++)                                                              \
+    for (_x_itr = -_radius;                                                   \
+        _x_itr <= _radius;                                                   \
+        _x_itr++)                                                            \
+      if (_y_itr * _y_itr + _x_itr * _x_itr <= _sq_radius) {                  \
+        int x_itr = _center_x + _x_itr;                                       \
+        int y_itr = _center_y + _y_itr;                                       \
+        if (normalize_map_pos(&x_itr, &y_itr)) {
+
+#define circle_iterate_end                                                    \
+        }                                                                     \
+      }                                                                       \
+}
+                                                                              
 
 /* Iterate through all tiles adjacent to a tile */
 #define adjc_iterate(RI_center_x, RI_center_y, RI_x_itr, RI_y_itr)            \
Index: server/gamehand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamehand.c,v
retrieving revision 1.98
diff -u -r1.98 gamehand.c
--- server/gamehand.c   2001/10/18 16:45:34     1.98
+++ server/gamehand.c   2001/11/14 21:06:14
@@ -35,7 +35,7 @@
 void init_new_game(void)
 {
   int i, j, x, y;
-  int vx, vy, dx, dy;
+  int dx, dy;
   Unit_Type_id utype;
   int start_pos[MAX_NUM_PLAYERS]; /* indices into map.start_positions[] */
 
@@ -130,16 +130,9 @@
                game.players[i].name);
       }
       /* Expose visible area. */
-      for (vx = 1; (vx * vx) <= game.rgame.init_vis_radius_sq; vx++) {
-       for (vy = 1; (vy * vy) <= game.rgame.init_vis_radius_sq; vy++) {
-         if (((vx *vx) + (vy *vy)) <= game.rgame.init_vis_radius_sq) {
-           show_area(&game.players[i], dx-vx+1, dy-vy+1, 1);
-           show_area(&game.players[i], dx+vx-1, dy-vy+1, 1);
-           show_area(&game.players[i], dx-vx+1, dy+vy-1, 1);
-           show_area(&game.players[i], dx+vx-1, dy+vy-1, 1);
-         }
-       }
-      }
+      circle_iterate(dx, dy, game.rgame.init_vis_radius_sq, cx, cy) {
+       show_area(&game.players[i], cx, cy, 0);
+      } circle_iterate_end;
       /* Create the unit of an appropriate type. */
       utype = get_role_unit((j < game.settlers) ? F_CITIES : L_EXPLORER, 0);
       create_unit(&game.players[i], dx, dy, utype, 0, 0, -1);

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