Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] (PR#9496) PATCH: Clean up on set-ratio
Home

[Freeciv-Dev] (PR#9496) PATCH: Clean up on set-ratio

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9496) PATCH: Clean up on set-ratio
From: "Marcelo Burda" <mburda@xxxxxxxxx>
Date: Sun, 25 Jul 2004 12:00:41 -0700
Reply-to: rt@xxxxxxxxxxx

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

> [jdorje - Dim. Jul. 25 15:36:13 2004]:
> 
> rwetmore@xxxxxxxxxxxx wrote:
> > <URL: http://rt.freeciv.org/Ticket/Display.html?id=9496 >
> > 
> > 
> > The original code allowed one to define a FlatEarth scenario map
> > with dimensions 3x53 if so desired. This might be used with a
> > "try to take city X in 5 turns". I do not see how you can now do
> > this and thus the new code is broken.
> 
> Scenarios set xsize and ysize directly.  The size and topology (=> 
> ratio) are only converted to an xsize/ysize when a new map is created.
> 
;-)

> However Marcelo, this may argue that the set_ratio() function should be 
> moved into mapgen code, while all other code passes around xsize, ysize, 
> and size directly.
YES
> 
> jason
> 
> 
LAST CHANGES

set_ratio rename as set_sizes and move to server/mapgen.c

in server/mapgen a new gen_map_init_topology set sizes in a per topology
way then call the more general map_init_topology(),
some code move from map_init_topology to gen_map_init_topology to do this.

some sanity check on size move from set_ratio to map_init_topology to
verify saved games and scenario too. (sizes from scenarios are not
corrected but get a assert)

there are no new code, only move it to best places.

Marcelo
diff -ruN -Xfreeciv/diff_ignore freeciv/common/map.c freeciv__/common/map.c
--- freeciv/common/map.c        2004-07-25 07:51:41.000000000 +0200
+++ freeciv__/common/map.c      2004-07-25 18:44:19.956385904 +0200
@@ -308,82 +308,10 @@
 }
 
 /****************************************************************************
-  Set the map xsize and ysize based on a base size and ratio (in natural
-  coordinates).
-
-  xsize and ysize are even numbers.  For iso-maps ysize%4 == 0.  This avoids
-  problems in current and future topologies.
-****************************************************************************/
-static void set_ratio(double base_size, int Xratio, int Yratio)
-{
-  /* In TF_ISO we need to double the map.ysize factor, since xsize is
-   * in native coordinates which are compressed 2x in the X direction. */ 
-  const int iso = topo_has_flag(TF_ISO) ? 2 : 1;
-
-  /* We have:
-   *
-   *   size = xsize * ysize
-   *
-   * And to satisfy the ratios and other constraints we set
-   *
-   *   xsize = 2 * isize * xratio
-   *   ysize = 2 * isize * yratio * iso
-   *
-   * For any value of "isize".  The factor of 2 is to ensure even dimensions
-   * (which are important for some topologies).  So with some substitution
-   *
-   *   size = 4 * isize * xratio * isize * yratio * iso
-   *   isize = sqrt(size / (4 * xratio * yratio * iso))
-   *
-   * Remember that size = base_size * 1000.  So this gives us
-   *
-   *   isize = sqrt(base_size * 250 / (xratio * yratio * iso))
-   */
-  int i_size = sqrt(250.0 * base_size / (Xratio * Yratio * iso)) + 0.49;
-
-  /* Make sure the linear size is large enough. */
-  while (MIN(Xratio, iso * Yratio) * 2 * i_size < MAP_MIN_LINEAR_SIZE) {
-    i_size++;
-  }
-
-  /* Now build xsize and ysize value as described above.  This gives and
-   * even xsize and ysize.  For iso maps ysize % 4 == 0. */
-  map.xsize =       2 * Xratio * i_size;
-  map.ysize = iso * 2 * Yratio * i_size;
-
-  /* Now make sure the linear size isn't too large.  If it is, the best thing
-   * we can do is decrease the base_size and try again. */
-  if (MAX(MAP_WIDTH, MAP_HEIGHT) - 1 > MAP_MAX_LINEAR_SIZE ) {
-      /* make a more litle map if possible */
-    assert(base_size > 0.1);
-    set_ratio(base_size - 0.1, Xratio, Yratio);
-    return;
-  }
-
-  if (map.size > base_size + 0.9) {
-    /* Warning when size is set uselessly big */ 
-    freelog(LOG_NORMAL,
-           _("Requested size of %d is too big for this topology."),
-           map.size);
-  }
-  freelog(LOG_VERBOSE, "Creating a map of size of %2.1fk tiles.", base_size);
-  freelog(LOG_VERBOSE, "xsize and ysize are set to %d and %d.",
-         map.xsize, map.ysize);
-}
-
-/*
- * The auto ratios for known topologies
- */
-#define AUTO_RATIO_FLAT           {1, 1}
-#define AUTO_RATIO_CLASSIC        {8, 5} 
-#define AUTO_RATIO_URANUS         {5, 8} 
-#define AUTO_RATIO_TORUS          {1, 1}
-
-/****************************************************************************
   map_init_topology needs to be called after map.topology_id is changed.
 
-  If map.size is changed, call map_init_topology(TRUE) to calculate map.xsize
-  and map.ysize based on the default ratio.
+  If map.size is changed,map.xsize and map.ysize has to be seted befor call
+  map_init_topology(TRUE). This should be done in server by the mapgen code.
 
   If map.xsize and map.ysize are changed, call map_init_topology(FALSE) to
   calculate map.size.  This should be done in the client or when loading
@@ -393,21 +321,22 @@
 {
   enum direction8 dir;
 
-  /* Changing or reordering the topo_flag enum will break this code. */
-  const int default_ratios[4][2] =
-      {AUTO_RATIO_FLAT, AUTO_RATIO_CLASSIC,
-       AUTO_RATIO_URANUS, AUTO_RATIO_TORUS};
-  const int id = 0x3 & map.topology_id;
-  
-  assert(TF_WRAPX == 0x1 && TF_WRAPY == 0x2);
-
-  if (set_sizes) {
-    /* Set map.xsize and map.ysize based on map.size. */
-    set_ratio(map.size, default_ratios[id][0], default_ratios[id][1]);  
-  } else {
+  if (!set_sizes) {
     /* Set map.size based on map.xsize and map.ysize. */
     map.size = (float)(map.xsize * map.ysize) / 1000.0 + 0.5;
   }
+  
+  /* sanity check for iso topologies*/
+  assert(!topo_has_flag(TF_ISO) || (map.ysize % 2) == 0);
+
+  /* The size and ratio must satisfy the minimum and maximum *linear*
+   * restrictions on width */
+
+  assert(MAP_WIDTH >= MAP_MIN_LINEAR_SIZE);
+  assert(MAP_HEIGHT >= MAP_MIN_LINEAR_SIZE);
+  assert(MAP_WIDTH <= MAP_MAX_LINEAR_SIZE);
+  assert(MAP_HEIGHT <= MAP_MAX_LINEAR_SIZE);
+ 
 
   map.num_valid_dirs = map.num_cardinal_dirs = 0;
   for (dir = 0; dir < 8; dir++) {
diff -ruN -Xfreeciv/diff_ignore freeciv/server/mapgen.c 
freeciv__/server/mapgen.c
--- freeciv/server/mapgen.c     2004-07-25 07:51:43.000000000 +0200
+++ freeciv__/server/mapgen.c   2004-07-25 18:36:33.466303176 +0200
@@ -1504,6 +1504,97 @@
   islands = NULL;
 }
 
+/****************************************************************************
+  Set the map xsize and ysize based on a base size and ratio (in natural
+  coordinates).
+****************************************************************************/
+static void set_sizes(double size, int Xratio, int Yratio)
+{
+  /* Future topologies may require even dimensions. */
+  /* Some code in generator assume even dimension,(set this to 2) */  
+  const int even = 2;
+
+  /* In TF_ISO we need to double the map.ysize factor, since xsize is
+   * in native coordinates which are compressed 2x in the X direction. */ 
+  const int iso = topo_has_flag(TF_ISO) ? 2 : 1;
+
+  /* We have:
+   *
+   *   1000 * size = xsize * ysize
+   *
+   * And to satisfy the ratios and other constraints we set
+   *
+   *   xsize = i_size * xratio * even
+   *   ysize = i_size * yratio * even * iso
+   *
+   * For any value of "i_size".  So with some substitution
+   *
+   *   1000 * size = i_size * i_size * xratio * yratio * even * even * iso
+   *   i_size = sqrt(1000 * size / (xratio * yratio * even * even * iso))
+   * 
+   * Make sure to round off i_size to preserve exact wanted ratios,
+   * that may be importante for some topologies.
+   */
+  const int i_size
+    = sqrt((float)(1000 * size)
+          / (float)(Xratio * Yratio * iso * even * even)) + 0.49;
+
+  /* Now build xsize and ysize value as described above. */
+  map.xsize = Xratio * i_size * even;
+  map.ysize = Yratio * i_size * even * iso;
+
+ /* Now make sure the if size isn't too large for this ratio.
+  * is not then decrease the size and try again. */
+  if (MAX(MAP_WIDTH, MAP_HEIGHT) > MAP_MAX_LINEAR_SIZE ) {
+    assert(size > 0.1);
+    set_sizes(size - 0.1, Xratio, Yratio);
+    return;
+  }
+
+  /* if ratio is big for some topology the simplest way to avoid
+     next message is set maximum size smaller for all topo! */
+  if (map.size > size + 0.9) {
+    /* Warning when size is set uselessly big */ 
+    freelog(LOG_NORMAL,
+           _("Requested size of %d is too big for this topology."),
+           map.size);
+  }
+  freelog(LOG_VERBOSE,
+         "Creating a map of size %d x %d = %d tiles (%d requested).",
+         map.xsize, map.ysize, map.xsize * map.ysize, map.size * 1000);
+}
+
+/*
+ * The auto ratios for known topologies
+ *
+ * the facto Xratio * Yratio determine accuracy of size, 
+ * best if small, specially for small sizes
+ */
+#define AUTO_RATIO_FLAT           {1, 1}
+#define AUTO_RATIO_CLASSIC        {3, 2} 
+#define AUTO_RATIO_URANUS         {2, 3} 
+#define AUTO_RATIO_TORUS          {1, 1}
+
+/*************************************************************************** 
+ * This function set sizes in a per topology way then call map_init_topology
+ ***************************************************************************/
+static void gen_map_init_topology( void)
+{
+ /* Changing or reordering the topo_flag enum will break this code. */
+  const int default_ratios[4][2] =
+      {AUTO_RATIO_FLAT, AUTO_RATIO_CLASSIC,
+       AUTO_RATIO_URANUS, AUTO_RATIO_TORUS};
+  const int id = 0x3 & map.topology_id;
+  
+  assert(TF_WRAPX == 0x1 && TF_WRAPY == 0x2);
+
+  /* Set map.xsize and map.ysize based on map.size. */
+  set_sizes(map.size, default_ratios[id][0], default_ratios[id][1]);
+
+  /* Then initialise all topoloicals parameters */
+  map_init_topology(TRUE);
+}
+
 /**************************************************************************
   See stdinhand.c for information on map generation methods.
 
@@ -1526,7 +1617,7 @@
   /* don't generate tiles with mapgen==0 as we've loaded them from file */
   /* also, don't delete (the handcrafted!) tiny islands in a scenario */
   if (map.generator != 0) {
-    map_init_topology(TRUE);  /* initialize map.xsize and map.ysize, etc */
+    gen_map_init_topology();  /* initialize map.xsize and map.ysize, etc */
     map_allocate();
     adjust_terrain_param();
     /* if one mapgenerator fails, it will choose another mapgenerator */

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