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: mburda@xxxxxxxxx
Subject: [Freeciv-Dev] (PR#9496) PATCH: Clean up on set-ratio
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 27 Jul 2004 09:35:01 -0700
Reply-to: rt@xxxxxxxxxxx

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

> [mburda - Sun Jul 25 19:00:40 2004]:

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

I made a few small changes, including:

- Some hex fixes.
- Some LOG improvements.
- Some comment fixes.
- Some function naming improvments (IMO).

The patch didn't apply cleanly (because of the hex changes) so I hope I
didn't miss anything.

Marcelo, is this okay?

jason

Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.183
diff -u -r1.183 map.c
--- common/map.c        26 Jul 2004 03:38:29 -0000      1.183
+++ common/map.c        27 Jul 2004 16:33:02 -0000
@@ -308,82 +308,11 @@
 }
 
 /****************************************************************************
-  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) || topo_has_flag(TF_HEX)) ? 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 must be set before
+  calling map_init_topology(TRUE).  This is done by the mapgen code
+  (server) and packhand code (client).
 
   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 +322,21 @@
 {
   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) || topo_has_flag(TF_HEX))
+        || (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++) {
Index: server/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v
retrieving revision 1.140
diff -u -r1.140 mapgen.c
--- server/mapgen.c     24 Jul 2004 04:02:36 -0000      1.140
+++ server/mapgen.c     27 Jul 2004 16:33:03 -0000
@@ -1504,6 +1504,98 @@
   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)
+{
+  /* Some code in generator assumes even dimension, so this is set to 2.
+   * Future topologies may also require even dimensions. */
+  const int even = 2;
+
+  /* In iso-maps 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) || topo_has_flag(TF_HEX)) ? 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 size isn't too large for this ratio.  If it is
+   * 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 the ratio is too big for some topology the simplest way to avoid
+   * this error is to set the maximum size smaller for all topologies! */
+  if (map.size > size + 0.9) {
+    /* Warning when size is set uselessly big */ 
+    freelog(LOG_ERROR,
+           "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 default ratios for known topologies
+ *
+ * The factor Xratio * Yratio determines the accuracy of the size.
+ * Small ratios work better than large ones; 3:2 is not the same as 6:4
+ */
+#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 sets sizes in a topology-specific way then calls
+  map_init_topology.
+***************************************************************************/
+static void generator_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 +1618,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 */
+    generator_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]