Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2004:
[Freeciv-Dev] (PR#9799) PATCH: rewrited adjust_map()
Home

[Freeciv-Dev] (PR#9799) PATCH: rewrited adjust_map()

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9799) PATCH: rewrited adjust_map()
From: "Marcelo Burda" <mburda@xxxxxxxxx>
Date: Tue, 24 Aug 2004 13:30:36 -0700
Reply-to: rt@xxxxxxxxxxx

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

adjust_map has be rewrie to do a linear distribution of values from 0 to
maxval. this allow to handle map very easy

sample:
(hmap < 0.33 * maxval) handel the 33% lowerest part of the map

this is used in make_land and make_relief to avoid some loops

Marcelo
diff -ruN -Xfreeciv/diff_ignore freeciv/server/mapgen.c freeciv_/server/mapgen.c
--- freeciv/server/mapgen.c     2004-08-24 19:23:44.977333648 +0200
+++ freeciv_/server/mapgen.c    2004-08-24 20:21:53.235037872 +0200
@@ -75,7 +75,8 @@
 static int *river_map;
 
 static int *height_map;
-static int maxval=0;
+static const int maxval = 1000;
+static int seaval=0, reliefval =0;
 static int forests=0;
 
 struct isledata {
@@ -101,6 +102,17 @@
    ? MAX_TEMP * (3 + 2 * SQSIZE) / (100 * SQSIZE)              \
    : 5 * MAX_TEMP / 100  /* 5% for all maps */)
 
+/********************************************************************
+ * used to initialize a array with some value
+ ********************************************************************/
+#define INITIALIZE_ARRAY(A, MAX, VAL)                            \
+  { \
+    int INI_INEX; \
+    for(INI_INEX = 0; INI_INEX < (MAX);INI_INEX++) { \
+      (A)[INI_INEX] = (VAL); \
+     } \
+  } 
+
 /****************************************************************************
   Returns the temperature of this map position.  This is a value in the
   range of 0 to MAX_TEMP (inclusive).
@@ -283,33 +295,16 @@
 }
 
 /**************************************************************************
-  make_mountains() will convert all squares that are higher than thill to
+  make_relief() will convert all squares that are higher than thill to
   mountains and hills. Notice that thill will be adjusted according to
   the map.mountains value, so increase map.mountains and you'll get more 
   hills and mountains (and vice versa).
 **************************************************************************/
-static void make_mountains(int thill)
+static void make_relief()
 {
-  int mount;
-  int j;
-
-  for (j = 0; j < 10; j++) {
-    mount = 0;
-    whole_map_iterate(x, y) {
-      if (hmap(x, y) > thill) {
-       mount++;
-      }
-    } whole_map_iterate_end;
-    if (mount < (map_num_tiles() * map.mountains) / 1000) {
-      thill *= 95;
-    } else {
-      thill *= 105;
-    }
-    thill /= 100;
-  }
-  
+  reliefval = ((maxval - seaval)* (100 - map.mountains)) / 100 + seaval;
   whole_map_iterate(x, y) {
-    if (hmap(x, y) > thill && !is_ocean(map_get_terrain(x,y))) { 
+    if (hmap(x, y) > reliefval && !is_ocean(map_get_terrain(x,y))) { 
       /* Randomly place hills and mountains on "high" tiles.  But don't
        * put hills near the poles (they're too green). */
       if (myrand(100) > 75 
@@ -1125,37 +1120,20 @@
 **************************************************************************/
 static void make_land(void)
 {
-  int tres;
-  int count=0;
-  int total = (map_num_tiles() * map.landpercent) / 100;
-  int forever=0;
-
   adjust_map();
   normalize_hmap_poles();
-  tres = (maxval * map.landpercent) / 100;
-
-  do {
-    forever++;
-    if (forever>50) break; /* loop elimination */
-    count=0;
-    whole_map_iterate(x, y) {
-      if (hmap(x, y) < tres)
-       map_set_terrain(x, y, T_OCEAN);
-      else {
-       map_set_terrain(x, y, T_GRASSLAND);
-       count++;
-      }
-    } whole_map_iterate_end;
-    if (count>total)
-      tres*=11;
-    else
-      tres*=9;
-    tres/=10;
-  } while (abs(total-count)> maxval/40);
+  seaval = (maxval *(100 - map.landpercent)) / 100;
+  whole_map_iterate(x, y) {
+    if (hmap(x, y) < seaval)
+      map_set_terrain(x, y, T_OCEAN);
+    else {
+      map_set_terrain(x, y, T_GRASSLAND);
+    }
+  } whole_map_iterate_end;
 
   renormalize_hmap_poles();
   make_polar_land(); /* make extra land at poles*/
-  make_mountains(maxval*8/10);
+  make_relief();
   make_arctic();
   make_tundra();
   make_forests();
@@ -1690,25 +1668,43 @@
 }
 
 /**************************************************************************
-  Adjust the map so that its minimum height is 0.  This raises or lowers
-  every position by a fixed amount and sets the "maxval" global variable
-  to hold the maximum height.
+  Adjust the hmap to be a linearize distribution of heights
+  form 0 to maxval (maxval is a global const )
+  sample
+  ( hmap[x,y] < 0.13 * maxval) handle the 13% lowerest part of the map
 **************************************************************************/
 static void adjust_map(void)
 {
-  int minval = maxval = hnat(0, 0);
+  int i, min = hnat(0, 0), max = hnat(0, 0);
+  int count=0, f[maxval];
+  
+  INITIALIZE_ARRAY(f,maxval,0);
 
   /* Determine minimum and maximum heights. */
   whole_map_iterate(x, y) {
-    maxval = MAX(maxval, hmap(x, y));
-    minval = MIN(minval, hmap(x, y));
+    max = MAX(max, hmap(x, y));
+    min = MIN(min, hmap(x, y));
   } whole_map_iterate_end;
 
-  /* Translate heights so the minimum height is 0. */
-  maxval -= minval;
+  /* Translate heights so the minimum height is 0 and  maximum is maxval.
+     and count pos in granularity */
+  max -= min;
   whole_map_iterate(x, y) {
-    hmap(x, y) -= minval;
+    hmap(x, y) = (hmap(x, y) - min) * maxval;
+    hmap(x, y) /= max;
+    f[hmap(x, y)]++;
   } whole_map_iterate_end;
+
+  /* create the linearize function */
+  for(i =  -1; i++ < maxval;) {
+      count += f[i];
+      f[i] = (count * maxval) / (map.xsize * map.ysize) ;
+     }
+
+  /* apply the linearize function */
+  whole_map_iterate(x, y) {
+    hmap(x, y) = f[hmap(x, y)];
+  } whole_map_iterate_end; 
 }
 
 /**************************************************************************

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