Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2001:
[Freeciv-Dev] Re: PATCH: fix for smooth_map (PR#991)
Home

[Freeciv-Dev] Re: PATCH: fix for smooth_map (PR#991)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: PATCH: fix for smooth_map (PR#991)
From: Jason Dorje Short <vze2zq63@xxxxxxxxxxx>
Date: Mon, 08 Oct 2001 00:15:53 -0400
Reply-to: jdorje@xxxxxxxxxxxx

Raimar Falke wrote:
> 
> On Fri, Oct 05, 2001 at 04:51:25PM -0400, Jason Dorje Short wrote:
> > Raimar Falke wrote:
> > >
> > > On Fri, Oct 05, 2001 at 12:15:26PM -0700, jdorje@xxxxxxxxxxxxxxxxxxxxx 
> > > wrote:
> >
> > > > -    a += hmap(x, my);
> > > > -    a += hmap(mx, y);
> > > > +    /* what about a new macro, adjc_nearest_iterate? */
> > > > +    for (dir=0; dir<8; dir++) {
> > > > +      int dx, dy, x2, y2;
> > > > +      DIRSTEP(dx, dy, dir); /* can't use MAPSTEP */
> > > > +      x2 = x + dx, y2 = y + dy;
> > > > +      nearest_real_pos(&x2, &y2);
> > >
> > > What about taking a real adjc_iterate macro and count the real tiles?
> > > Substitute "/10" by this count. Both methods (your patch and ignoring
> > > un-real tiles) aren't wrong. They just differ. And we have already
> > > changed the output of this method.
> >
> > This would result in less smoothness at the poles - not necessarily bad,
> > but I see no reason for it.
> 
> The code above is ugly.

OK, ok.  nearest_real_tile is a bit iffy anyway, as Gaute pointed out.

How about this?  It also removes the unnecessary malloc call (which is
present in other parts of the code...) and fixes a small inconsistency
with the randomization.

jason
Index: server/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v
retrieving revision 1.72
diff -u -r1.72 mapgen.c
--- server/mapgen.c     2001/09/30 21:55:34     1.72
+++ server/mapgen.c     2001/10/08 04:14:36
@@ -31,7 +31,7 @@
 #include "mapgen.h"
 
 /* Wrapper for easy access.  It's a macro so it can be a lvalue. */
-#define hmap(x,y) (height_map[(y) * map.xsize + map_adjust_x(x)])
+#define hmap(x,y) (height_map[map_inx(x, y)])
 
 static void make_huts(int number);
 static void add_specials(int prob);
@@ -1333,38 +1333,34 @@
 }
 
 /**************************************************************************
-  smooth_map should be viewed  as a  corrosion function on the map, it levels
-  out the differences in the heightmap.
+  smooth_map should be viewed as a corrosion function on the map; it
+  levels out the differences in the heightmap.
 **************************************************************************/
 static void smooth_map(void)
 {
-  int mx,my,px,py;
-  int a;
-  
-  whole_map_iterate(x, y) {
-    my = map_adjust_y(y - 1);
-    py = map_adjust_y(y + 1);
-    mx = map_adjust_x(x - 1);
-    px = map_adjust_x(x + 1);
-    a = hmap(x, y) * 2;
-
-    a += hmap(px, my);
-    a += hmap(mx, my);
-    a += hmap(mx, py);
-    a += hmap(px, py);
+  /* We make a new height map and then copy it back over the old one.
+     Care must be taken so that the new height map uses the exact
+     same storage structure as the real one - it must be the same
+     size and use the same indexing.  The advantage of the new array
+     is there's no feedback from overwriting in-use values.  --JDS */
+  int new_hmap[map.xsize * map.ysize];
 
-    a += hmap(x, my);
-    a += hmap(mx, y);
+  whole_map_iterate(x, y) {
+    int a = hmap(x, y) * 2; /* double-count this tile */
+    int w = 2; /* weight of counted tiles */
 
-    a += hmap(x, py);
-    a += hmap(px, y);
+    adjc_iterate(x, y, x2, y2) {
+      a += hmap(x2, y2); /* count adjacent tile once */
+      w++;
+    } adjc_iterate_end;
 
-    a += myrand(60);
-    a -= 30;
+    a += myrand(61) - 30; /* random factor: -30..30 */
     if (a < 0)
       a = 0;
-    hmap(x, y) = a / 10;
+    new_hmap[map_inx(x, y)] = a / w;
   } whole_map_iterate_end;
+
+  memcpy(height_map, new_hmap, sizeof(int) * map.xsize * map.ysize);
 }
 
 /**************************************************************************

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