Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] (PR#10124) PATCH: new generator 1, hmap generator (1,5) mv
Home

[Freeciv-Dev] (PR#10124) PATCH: new generator 1, hmap generator (1,5) mv

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#10124) PATCH: new generator 1, hmap generator (1,5) mv to height_map.[ch]
From: "Marcelo Burda" <mburda@xxxxxxxxx>
Date: Tue, 21 Sep 2004 08:55:11 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10124 >

> [mburda - Mar. Sep. 21 15:18:12 2004]:
> 
> UPDATE 
> 

Index: server/generator/height_map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/generator/height_map.c,v
retrieving revision 1.3
diff -u -r1.3 height_map.c
--- server/generator/height_map.c       17 Sep 2004 09:14:01 -0000      1.3
+++ server/generator/height_map.c       21 Sep 2004 15:52:45 -0000
@@ -1,5 +1,5 @@
 /********************************************************************** 
- Freeciv - Copyright (C) 2004 - Marcelo J. Burda
+   Copyright (C) 1996 - 2004  The Freeciv Project
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
@@ -14,9 +14,208 @@
 #include <config.h>
 #endif
 
-#include "shared.h"
+#include "map.h"
+#include "rand.h"
 
 #include "height_map.h"
+#include "mapgen_topology.h"
+#include "utilities.h" 
 
 int *height_map = NULL;
 int hmap_shore_level = 0, hmap_mountain_level = 0;
+
+/****************************************************************************
+  Lower the land near the polar region to avoid too much land there.
+
+  See also renomalize_hmap_poles
+****************************************************************************/
+void normalize_hmap_poles(void)
+{
+  whole_map_iterate(x, y) {
+    if (near_singularity(x, y)) {
+      hmap(x, y) = 0;
+    } else if (map_colatitude(x, y) < 2 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= map_colatitude(x, y) / (2.5 * ICE_BASE_LEVEL);
+    } else if (map.separatepoles 
+              && map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= 0.1;
+    } else if (map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= map_colatitude(x, y) / (2.5 * ICE_BASE_LEVEL);
+    }
+  } whole_map_iterate_end;
+}
+
+/****************************************************************************
+  Invert the effects of normalize_hmap_poles so that we have accurate heights
+  for texturing the poles.
+****************************************************************************/
+void renormalize_hmap_poles(void)
+{
+  whole_map_iterate(x, y) {
+    if (hmap(x, y) == 0 || map_colatitude(x, y) == 0) {
+      /* Nothing. */
+    } else if (map_colatitude(x, y) < 2 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= (2.5 * ICE_BASE_LEVEL) / map_colatitude(x, y);
+    } else if (map.separatepoles 
+              && map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= 10;
+    } else if (map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
+      hmap(x, y) *= (2.5 * ICE_BASE_LEVEL) /  map_colatitude(x, y);
+    }
+  } whole_map_iterate_end;
+}
+
+/**********************************************************************
+ Create uncorrelated rand map and do some call to smoth to correlate 
+ it a little and creante randoms shapes
+ **********************************************************************/
+void make_random_hmap(int smooth)
+{
+  int i = 0;
+  height_map = fc_malloc (sizeof(int) * MAX_MAP_INDEX);
+
+  INITIALIZE_ARRAY(height_map, MAX_MAP_INDEX, myrand(1000 * smooth) );
+
+  for (; i < smooth; i++) {
+    smooth_int_map(height_map, TRUE);
+  }
+
+  adjust_int_map(height_map, hmap_max_level);
+}
+
+/**************************************************************************
+  Recursive function which does the work for generator 5.
+
+  All (x0,y0) and (x1,y1) are in native coordinates.
+**************************************************************************/
+static void gen5rec(int step, int x0, int y0, int x1, int y1)
+{
+  int val[2][2];
+  int x1wrap = x1; /* to wrap correctly */ 
+  int y1wrap = y1; 
+
+  /* All x and y values are native. */
+
+  if (((y1 - y0 <= 0) || (x1 - x0 <= 0)) 
+      || ((y1 - y0 == 1) && (x1 - x0 == 1))) {
+    return;
+  }
+
+  if (x1 == map.xsize)
+    x1wrap = 0;
+  if (y1 == map.ysize)
+    y1wrap = 0;
+
+  val[0][0] = hnat(x0, y0);
+  val[0][1] = hnat(x0, y1wrap);
+  val[1][0] = hnat(x1wrap, y0);
+  val[1][1] = hnat(x1wrap, y1wrap);
+
+  /* set midpoints of sides to avg of side's vertices plus a random factor */
+  /* unset points are zero, don't reset if set */
+#define set_midpoints(X, Y, V)                                         \
+  do_in_map_pos(map_x, map_y, (X), (Y)) {                               \
+    if (!near_singularity(map_x, map_y)                                        
\
+       && map_colatitude(map_x, map_y) >  ICE_BASE_LEVEL/2             \
+       && hnat((X), (Y)) == 0) {                                       \
+      hnat((X), (Y)) = (V);                                            \
+    }                                                                  \
+  } do_in_map_pos_end;
+
+  set_midpoints((x0 + x1) / 2, y0,
+               (val[0][0] + val[1][0]) / 2 + myrand(step) - step / 2);
+  set_midpoints((x0 + x1) / 2,  y1wrap,
+               (val[0][1] + val[1][1]) / 2 + myrand(step) - step / 2);
+  set_midpoints(x0, (y0 + y1)/2,
+               (val[0][0] + val[0][1]) / 2 + myrand(step) - step / 2);  
+  set_midpoints(x1wrap,  (y0 + y1) / 2,
+               (val[1][0] + val[1][1]) / 2 + myrand(step) - step / 2);  
+
+  /* set middle to average of midpoints plus a random factor, if not set */
+  set_midpoints((x0 + x1) / 2, (y0 + y1) / 2,
+               ((val[0][0] + val[0][1] + val[1][0] + val[1][1]) / 4
+                + myrand(step) - step / 2));
+
+#undef set_midpoints
+
+  /* now call recursively on the four subrectangles */
+  gen5rec(2 * step / 3, x0, y0, (x1 + x0) / 2, (y1 + y0) / 2);
+  gen5rec(2 * step / 3, x0, (y1 + y0) / 2, (x1 + x0) / 2, y1);
+  gen5rec(2 * step / 3, (x1 + x0) / 2, y0, x1, (y1 + y0) / 2);
+  gen5rec(2 * step / 3, (x1 + x0) / 2, (y1 + y0) / 2, x1, y1);
+}
+
+/**************************************************************************
+Generator 5 makes earthlike worlds with one or more large continents and
+a scattering of smaller islands. It does so by dividing the world into
+blocks and on each block raising or lowering the corners, then the 
+midpoints and middle and so on recursively.  Fiddling with 'xdiv' and 
+'ydiv' will change the size of the initial blocks and, if the map does not 
+wrap in at least one direction, fiddling with 'avoidedge' will change the 
+liklihood of continents butting up to non-wrapped edges.
+
+  All X and Y values used in this function are in native coordinates.
+**************************************************************************/
+void make_pseudofractal1_hmap(void)
+{
+  const bool xnowrap = !topo_has_flag(TF_WRAPX);
+  const bool ynowrap = !topo_has_flag(TF_WRAPY);
+
+  /* 
+   * How many blocks should the x and y directions be divided into
+   * initially. 
+   */
+  const int xdiv = 6;          
+  const int ydiv = 5;
+
+  int xdiv2 = xdiv + (xnowrap ? 1 : 0);
+  int ydiv2 = ydiv + (ynowrap ? 1 : 0);
+
+  int xmax = map.xsize - (xnowrap ? 1 : 0);
+  int ymax = map.ysize - (ynowrap ? 1 : 0);
+  int xn, yn;
+  /* just need something > log(max(xsize, ysize)) for the recursion */
+  int step = map.xsize + map.ysize; 
+  /* edges are avoided more strongly as this increases */
+  int avoidedge = (50 - map.landpercent) * step / 100 + step / 3; 
+
+  height_map = fc_malloc(sizeof(int) * MAX_MAP_INDEX);
+
+ /* initialize map */
+  INITIALIZE_ARRAY(height_map, MAX_MAP_INDEX, 0);
+
+  /* set initial points */
+  for (xn = 0; xn < xdiv2; xn++) {
+    for (yn = 0; yn < ydiv2; yn++) {
+      do_in_map_pos(x, y, (xn * xmax / xdiv), (yn * ymax / ydiv)) {
+       /* set initial points */
+       hmap(x, y) = myrand(2 * step) - (2 * step) / 2;
+
+       if (near_singularity(x, y)) {
+         /* avoid edges (topological singularities) */
+         hmap(x, y) -= avoidedge;
+       }
+
+       if (map_colatitude(x, y) <= ICE_BASE_LEVEL / 2 ) {
+         /* separate poles and avoid too much land at poles */
+         hmap(x, y) -= myrand(avoidedge);
+       }
+      } do_in_map_pos_end;
+    }
+  }
+
+  /* calculate recursively on each block */
+  for (xn = 0; xn < xdiv; xn++) {
+    for (yn = 0; yn < ydiv; yn++) {
+      gen5rec(step, xn * xmax / xdiv, yn * ymax / ydiv, 
+             (xn + 1) * xmax / xdiv, (yn + 1) * ymax / ydiv);
+    }
+  }
+
+  /* put in some random fuzz */
+  whole_map_iterate(x, y) {
+    hmap(x, y) = 8 * hmap(x, y) + myrand(4) - 2;
+  } whole_map_iterate_end;
+
+  adjust_int_map(height_map, hmap_max_level);
+}
Index: server/generator/height_map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/generator/height_map.h,v
retrieving revision 1.1
diff -u -r1.1 height_map.h
--- server/generator/height_map.h       14 Sep 2004 08:23:00 -0000      1.1
+++ server/generator/height_map.h       21 Sep 2004 15:52:45 -0000
@@ -1,5 +1,5 @@
 /********************************************************************** 
- Freeciv - Copyright (C) 2004 - Marcelo J. Burda
+   Copyright (C) 1996 - 2004  The Freeciv Project
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
@@ -35,4 +35,9 @@
 extern int *height_map;
 extern int hmap_shore_level, hmap_mountain_level;
 
+void normalize_hmap_poles(void);
+void renormalize_hmap_poles(void);
+void make_random_hmap(int smooth);
+void make_pseudofractal1_hmap(void);
+
 #endif  /* FC__HEIGHT__MAP_H */
Index: server/generator/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/generator/mapgen.c,v
retrieving revision 1.9
diff -u -r1.9 mapgen.c
--- server/generator/mapgen.c   21 Sep 2004 08:34:18 -0000      1.9
+++ server/generator/mapgen.c   21 Sep 2004 15:52:46 -0000
@@ -51,12 +51,9 @@
 
 static void make_huts(int number);
 static void add_specials(int prob);
-static void mapgenerator1(void);
 static void mapgenerator2(void);
 static void mapgenerator3(void);
 static void mapgenerator4(void);
-static void mapgenerator5(void);
-static void smooth_map(void);
 static void adjust_terrain_param(void);
 
 #define RIVERS_MAXTRIES 32767
@@ -912,47 +909,6 @@
   river_map = NULL;
 }
 
-/****************************************************************************
-  Lower the land near the polar region to avoid too much land there.
-
-  See also renomalize_hmap_poles
-****************************************************************************/
-static void normalize_hmap_poles(void)
-{
-  whole_map_iterate(x, y) {
-    if (near_singularity(x, y)) {
-      hmap(x, y) = 0;
-    } else if (map_colatitude(x, y) < 2 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= map_colatitude(x, y) / (2.5 * ICE_BASE_LEVEL);
-    } else if (map.separatepoles 
-              && map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= 0.1;
-    } else if (map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= map_colatitude(x, y) / (2.5 * ICE_BASE_LEVEL);
-    }
-  } whole_map_iterate_end;
-}
-
-/****************************************************************************
-  Invert the effects of normalize_hmap_poles so that we have accurate heights
-  for texturing the poles.
-****************************************************************************/
-static void renormalize_hmap_poles(void)
-{
-  whole_map_iterate(x, y) {
-    if (hmap(x, y) == 0 || map_colatitude(x, y) == 0) {
-      /* Nothing. */
-    } else if (map_colatitude(x, y) < 2 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= (2.5 * ICE_BASE_LEVEL) / map_colatitude(x, y);
-    } else if (map.separatepoles 
-              && map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= 10;
-    } else if (map_colatitude(x, y) <= 2.5 * ICE_BASE_LEVEL) {
-      hmap(x, y) *= (2.5 * ICE_BASE_LEVEL) /  map_colatitude(x, y);
-    }
-  } whole_map_iterate_end;
-}
-
 /**************************************************************************
   make land simply does it all based on a generated heightmap
   1) with map.landpercent it generates a ocean/grassland map 
@@ -960,7 +916,7 @@
 **************************************************************************/
 static void make_land(void)
 {
-  adjust_int_map(height_map, hmap_max_level);
+  
   if (HAS_POLES) {
     normalize_hmap_poles();
   }
@@ -1060,8 +1016,8 @@
     adjust_terrain_param();
     /* if one mapgenerator fails, it will choose another mapgenerator */
     /* with a lower number to try again */
-    if (map.generator == 5) {
-      mapgenerator5();
+    if (map.generator == 5 ) {
+      make_pseudofractal1_hmap();
     }
     if (map.generator == 4) {
       mapgenerator4();
@@ -1072,8 +1028,15 @@
     if (map.generator == 2) {
       mapgenerator2();
     }
-    if (map.generator == 1) {
-      mapgenerator1();
+    if (map.generator == 1 ) {
+      make_random_hmap(1 + SQSIZE);
+    }
+
+    /* if hmap only generator make anything else */
+    if (map.generator == 1 || map.generator == 5) {
+      make_land();
+      free(height_map);
+      height_map = NULL;
     }
     if (!map.tinyisles) {
       remove_tiny_islands();
@@ -1126,82 +1089,6 @@
   desert_pct = factor * (map.temperature * 10 + (100 - map.wetness) * 10) ;
 }
 
-/**************************************************************************
-  mapgenerator1, highlevel function, that calls all the previous functions
-**************************************************************************/
-static void mapgenerator1(void)
-{
-  int i;
-  height_map = fc_malloc (sizeof(int) * MAX_MAP_INDEX);
-
-  INITIALIZE_ARRAY(height_map, MAX_MAP_INDEX, myrand(40) );
-
-  for (i=0;i<1500;i++) {
-    int x, y;
-
-    rand_map_pos(&x, &y);
-
-    if (near_singularity(x, y)
-       || map_colatitude(x, y) <= ICE_BASE_LEVEL / 2) { 
-      /* Avoid land near singularities or at the poles. */
-      hmap(x, y) -= myrand(5000);
-    } else { 
-      hmap(x, y) += myrand(5000);
-    }
-    if ((i % 100) == 0) {
-      smooth_map(); 
-    }
-  }
-
-  smooth_map(); 
-  smooth_map(); 
-  smooth_map(); 
-
-  make_land();
-  free(height_map);
-  height_map = NULL;
-}
-
-/**************************************************************************
-  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)
-{
-  /* 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.
-   */
-  int *new_hmap = fc_malloc(sizeof(int) * map.xsize * map.ysize);
-  
-  whole_map_iterate(x, y) {
-    /* double-count this tile */
-    int height_sum = hmap(x, y) * 2;
-
-    /* weight of counted tiles */
-    int counter = 2;
-
-    adjc_iterate(x, y, x2, y2) {
-      /* count adjacent tile once */
-      height_sum += hmap(x2, y2);
-      counter++;
-    } adjc_iterate_end;
-
-    /* random factor: -30..30 */
-    height_sum += myrand(61) - 30;
-
-    if (height_sum < 0)
-      height_sum = 0;
-    new_hmap[map_pos_to_index(x, y)] = height_sum / counter;
-  } whole_map_iterate_end;
-
-  memcpy(height_map, new_hmap, sizeof(int) * map.xsize * map.ysize);
-  free(new_hmap);
-}
-
-
 /****************************************************************************
   Return TRUE if a safe tile is in a radius of 1.  This function is used to
   test where to place specials on the sea.
@@ -2032,144 +1919,3 @@
 }
 
 #undef DMSIS
-
-/**************************************************************************
-  Recursive function which does the work for generator 5.
-
-  All (x0,y0) and (x1,y1) are in native coordinates.
-**************************************************************************/
-static void gen5rec(int step, int x0, int y0, int x1, int y1)
-{
-  int val[2][2];
-  int x1wrap = x1; /* to wrap correctly */ 
-  int y1wrap = y1; 
-
-  /* All x and y values are native. */
-
-  if (((y1 - y0 <= 0) || (x1 - x0 <= 0)) 
-      || ((y1 - y0 == 1) && (x1 - x0 == 1))) {
-    return;
-  }
-
-  if (x1 == map.xsize) {
-    x1wrap = 0;
-  }
-  if (y1 == map.ysize) {
-    y1wrap = 0;
-  }
-
-  val[0][0] = hnat(x0, y0);
-  val[0][1] = hnat(x0, y1wrap);
-  val[1][0] = hnat(x1wrap, y0);
-  val[1][1] = hnat(x1wrap, y1wrap);
-
-  /* set midpoints of sides to avg of side's vertices plus a random factor */
-  /* unset points are zero, don't reset if set */
-#define set_midpoints(X, Y, V)                                         \
-  do_in_map_pos(map_x, map_y, (X), (Y)) {                               \
-    if (!near_singularity(map_x, map_y)                                        
\
-       && map_colatitude(map_x, map_y) >  ICE_BASE_LEVEL/2             \
-       && hnat((X), (Y)) == 0) {                                       \
-      hnat((X), (Y)) = (V);                                            \
-    }                                                                  \
-  } do_in_map_pos_end;
-
-  set_midpoints((x0 + x1) / 2, y0,
-               (val[0][0] + val[1][0]) / 2 + myrand(step) - step / 2);
-  set_midpoints((x0 + x1) / 2,  y1wrap,
-               (val[0][1] + val[1][1]) / 2 + myrand(step) - step / 2);
-  set_midpoints(x0, (y0 + y1)/2,
-               (val[0][0] + val[0][1]) / 2 + myrand(step) - step / 2);  
-  set_midpoints(x1wrap,  (y0 + y1) / 2,
-               (val[1][0] + val[1][1]) / 2 + myrand(step) - step / 2);  
-
-  /* set middle to average of midpoints plus a random factor, if not set */
-  set_midpoints((x0 + x1) / 2, (y0 + y1) / 2,
-               ((val[0][0] + val[0][1] + val[1][0] + val[1][1]) / 4
-                + myrand(step) - step / 2));
-
-#undef set_midpoints
-
-  /* now call recursively on the four subrectangles */
-  gen5rec(2 * step / 3, x0, y0, (x1 + x0) / 2, (y1 + y0) / 2);
-  gen5rec(2 * step / 3, x0, (y1 + y0) / 2, (x1 + x0) / 2, y1);
-  gen5rec(2 * step / 3, (x1 + x0) / 2, y0, x1, (y1 + y0) / 2);
-  gen5rec(2 * step / 3, (x1 + x0) / 2, (y1 + y0) / 2, x1, y1);
-}
-
-/**************************************************************************
-Generator 5 makes earthlike worlds with one or more large continents and
-a scattering of smaller islands. It does so by dividing the world into
-blocks and on each block raising or lowering the corners, then the 
-midpoints and middle and so on recursively.  Fiddling with 'xdiv' and 
-'ydiv' will change the size of the initial blocks and, if the map does not 
-wrap in at least one direction, fiddling with 'avoidedge' will change the 
-liklihood of continents butting up to non-wrapped edges.
-
-  All X and Y values used in this function are in native coordinates.
-**************************************************************************/
-static void mapgenerator5(void)
-{
-  const bool xnowrap = !topo_has_flag(TF_WRAPX);
-  const bool ynowrap = !topo_has_flag(TF_WRAPY);
-
-  /* 
-   * How many blocks should the x and y directions be divided into
-   * initially. 
-   */
-  const int xdiv = 6;          
-  const int ydiv = 5;
-
-  int xdiv2 = xdiv + (xnowrap ? 1 : 0);
-  int ydiv2 = ydiv + (ynowrap ? 1 : 0);
-
-  int xmax = map.xsize - (xnowrap ? 1 : 0);
-  int ymax = map.ysize - (ynowrap ? 1 : 0);
-  int xn, yn;
-  /* just need something > log(max(xsize, ysize)) for the recursion */
-  int step = map.xsize + map.ysize; 
-  /* edges are avoided more strongly as this increases */
-  int avoidedge = (50 - map.landpercent) * step / 100 + step / 3; 
-
-  height_map = fc_malloc(sizeof(int) * MAX_MAP_INDEX);
-
- /* initialize map */
-  INITIALIZE_ARRAY(height_map, MAX_MAP_INDEX, 0);
-
-  /* set initial points */
-  for (xn = 0; xn < xdiv2; xn++) {
-    for (yn = 0; yn < ydiv2; yn++) {
-      do_in_map_pos(x, y, (xn * xmax / xdiv), (yn * ymax / ydiv)) {
-       /* set initial points */
-       hmap(x, y) = myrand(2 * step) - (2 * step) / 2;
-
-       if (near_singularity(x, y)) {
-         /* avoid edges (topological singularities) */
-         hmap(x, y) -= avoidedge;
-       }
-
-       if (map_colatitude(x, y) <= ICE_BASE_LEVEL / 2) {
-         /* separate poles and avoid too much land at poles */
-         hmap(x, y) -= myrand(avoidedge);
-       }
-      } do_in_map_pos_end;
-    }
-  }
-
-  /* calculate recursively on each block */
-  for (xn = 0; xn < xdiv; xn++) {
-    for (yn = 0; yn < ydiv; yn++) {
-      gen5rec(step, xn * xmax / xdiv, yn * ymax / ydiv, 
-             (xn + 1) * xmax / xdiv, (yn + 1) * ymax / ydiv);
-    }
-  }
-
-  /* put in some random fuzz */
-  whole_map_iterate(x, y) {
-    hmap(x, y) = 8 * hmap(x, y) + myrand(4) - 2;
-  } whole_map_iterate_end;
-
-  make_land();
-  free(height_map);
-  height_map = NULL;
-}
Index: server/generator/utilities.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/generator/utilities.c,v
retrieving revision 1.4
diff -u -r1.4 utilities.c
--- server/generator/utilities.c        21 Sep 2004 08:34:19 -0000      1.4
+++ server/generator/utilities.c        21 Sep 2004 15:52:46 -0000
@@ -103,13 +103,13 @@
 **************************************************************************/
 void adjust_int_map(int *int_map, int int_map_max)
 {
-  int i, minval = *int_map, maxval = minval;
+  int minval = *int_map, maxval = minval;
 
   /* Determine minimum and maximum value. */
-  for (i = 0; i < MAX_MAP_INDEX; i++) {
-    maxval = MAX(maxval, int_map[i]);
-    minval = MIN(minval, int_map[i]);
-  }
+  whole_map_iterate_index(j) {
+    maxval = MAX(maxval, int_map[j]);
+    minval = MIN(minval, int_map[j]);
+  } whole_map_iterate_index_end;
 
   {
     int const size = 1 + maxval - minval;
@@ -120,10 +120,10 @@
     /* Translate value so the minimum value is 0
        and count the number of occurencies of all values to initialize the 
        frequencies[] */
-    for (i = 0; i < MAX_MAP_INDEX; i++) {
-      int_map[i] = (int_map[i] - minval);
-      frequencies[int_map[i]]++;
-    }
+    whole_map_iterate_index(j) {
+      int_map[j] = (int_map[j] - minval);
+      frequencies[int_map[j]]++;
+    } whole_map_iterate_index_end ;
 
     /* create the linearize function as "incremental" frequencies */
     for(i =  0; i < size; i++) {
@@ -132,8 +132,73 @@
     }
 
     /* apply the linearize function */
-    for (i = 0; i < MAX_MAP_INDEX; i++) {
-      int_map[i] = frequencies[int_map[i]];
-    }
+    whole_map_iterate_index(j) {
+      int_map[j] = frequencies[int_map[j]];
+    } whole_map_iterate_index_end;
+  }
+}
+bool normalize_nat_pos(int *x, int  *y) 
+{
+    int map_x, map_y;
+    bool return_value;
+
+    NATIVE_TO_MAP_POS(&map_x, &map_y, *x, *y);
+    return_value = normalize_map_pos(&map_x, &map_y);
+    MAP_TO_NATIVE_POS(x, y, map_x, map_y);
+
+    return return_value;
+}
+
+bool is_normal_nat_pos(int x, int y)
+{
+  do_in_map_pos(map_x, map_y, x, y) {
+    return is_normal_map_pos(map_x, map_y);
+  } do_in_map_pos_end;
+}
+
+/****************************************************************************
+ * Apply a Gaussian difusion filtre on the map
+ * the size of the map is MAX_MAP_INDEX and the map is indexed by 
+ * native_pos_to_index function
+ * if zeroes_at_edges is set, any unreal position on difusion has 0 value
+ * if zeroes_at_edges in unset the unreal position are not counted.
+ ****************************************************************************/
+ void smooth_int_map(int *int_map, bool zeroes_at_edges)
+{
+  assert(int_map != NULL);
+  float weight[5] =  {0.35,  0.5 ,1 , 0.5, 0.35};
+  float total_weight = 2.70;
+  bool axe = TRUE;
+  int alt_int_map[MAX_MAP_INDEX];
+  int *target_map, *source_map;
+
+  target_map = alt_int_map;
+  source_map = int_map;
+
+  do {
+    whole_map_iterate_index( j ) {
+      int  N = 0, D = 0;
+      iterate_axe(j1, i, j, 2, axe) {
+       D += weight[i + 2];
+       N += weight[i + 2] * source_map[j1];
+      } iterate_axe_end;
+      if(zeroes_at_edges) {
+       D = total_weight;
+      }
+      target_map[j] = N / D;
+    } whole_map_iterate_index_end;
+
+    if (topo_has_flag(TF_ISO) || topo_has_flag(TF_HEX)) {
+    weight[0] = weight[4] = 0.5;
+    weight[1] = weight[3] = 0.7;
+    total_weight = 3.4;  
   }
+
+  axe = !axe;
+
+  source_map = alt_int_map;
+  target_map = int_map;
+
+  } while ( !axe );
 }
+
Index: server/generator/utilities.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/generator/utilities.h,v
retrieving revision 1.1
diff -u -r1.1 utilities.h
--- server/generator/utilities.h        14 Sep 2004 08:23:00 -0000      1.1
+++ server/generator/utilities.h        21 Sep 2004 15:52:46 -0000
@@ -17,20 +17,66 @@
  *   do_in_map_pos(mx, my, xn, yn) {
  *     map_set_terrain(mx, my, T_OCEAN);
  *   } do_in_map_pos_end;
- * Note that changing the value of the map coordinates won't change the native
- * coordinates.
+ * Note: that the map position is declared as const and can't be changed
+ * inside the block.
  */
 #define do_in_map_pos(map_x, map_y, nat_x, nat_y)                           \
 {                                                                           \
-  int map_x, map_y;                                                         \
-  NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);                          \
-  {                                                                         
+  int _tmp_x = (nat_x), _tmp_y = (nat_y);                                   \
+  NATIVE_TO_MAP_POS(&_tmp_x, &_tmp_y, _tmp_x, _tmp_y);                      \
+  {                                                                         \
+      const int map_x = _tmp_x, map_y = _tmp_y;
 
 #define do_in_map_pos_end                                                   \
   }                                                                         \
 }
 
+/***************************************************************************
+ iterate axe iterate on selected axe ( x if Xaxe is TRUE) over a intervale
+ of -dist to dist arround the tile indexed by index0
+ this iterator create 2 vars:
+ index : the map index of the iterate pointed tile
+ i : the position in the intervale of iteration (from -dist to dist)
+ index0, dist, Xaxe are side effect safe.
+ ***************************************************************************/
+#define iterate_axe(index, i, index0, dist, Xaxe) \
+{ \
+    const int ___dist = (dist), ___index0 = (index0); \
+    const bool ___Xaxe = (Xaxe); \
+    int i, index; \
+    int ___x, ___y, ___x0, ___y0; \
+  \
+    index_to_native_pos(&___x0, &___y0, ___index0); \
+    for (i = -___dist; i <= ___dist; i++) { \
+       ___x = ___x0 + (___Xaxe ? i : 0); \
+       ___y = ___y0 + (___Xaxe ? 0 : i); \
+       if(!normalize_nat_pos(&___x, &___y)) { \
+           continue; \
+       }; \
+     index = native_pos_to_index( ___x, ___y);
+
+#define iterate_axe_end \
+    } \
+} 
+
+#define whole_map_iterate_index( index )  \
+{                                 \
+  int index = 0;                  \
+                                 \
+  for (; index < MAX_MAP_INDEX; index++) {
+
+#define whole_map_iterate_index_end \
+   } \
+} 
+
+bool normalize_nat_pos(int *x, int  *y);
+bool is_normal_nat_pos(int x, int y);
+
+/* int maps tools */
 void adjust_int_map(int *int_map, int int_map_max);
+void smooth_int_map(int *int_map, bool zeroes_at_edges);
+
+/* placed_map tool*/
 void create_placed_map(void);
 void destroy_placed_map(void);
 void map_set_placed(int x, int y);
@@ -41,4 +87,5 @@
 void set_placed_near_pos(int map_x, int map_y, int dist);
 
 
+
 #endif  /* FC__UTILITIES_H */

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