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 12:04:15 -0400
Reply-to: jdorje@xxxxxxxxxxxx

Jason Dorje Short wrote:
> 
> Greg Wooledge wrote:
> >
> > Raimar Falke (hawk@xxxxxxxxxxxxxxxxxxxxxxx) wrote:
> >
> > > >  static void smooth_map(void)
> > > >  {
> > > > +  int new_hmap[map.xsize * map.ysize];
> > >
> > > I think that such constructs aren't allow in C. IMHO the size has to
> > > be a compile time constant. Either I'm wrong and this is ok for local
> > > function variables or it is an gcc extension.
> >
> > You're correct -- it's not allowed in standard C.  You need to use one
> > of the malloc() family, and free() when you're done.
> 
> Wow, that's too bad.  I guess I'll fix it - and all of the other places
> where I've done the same thing.

New (hopefully final) patch attached.

Let's get this thing over with.

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 16:02:29
@@ -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,35 @@
 }
 
 /**************************************************************************
-  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 = fc_malloc(sizeof(int) * 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);
+  free(new_hmap);
 }
 
 /**************************************************************************

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