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 03:32:12 -0700
Reply-to: rt@xxxxxxxxxxx

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

I realise set_ratio is a very bad name, and rename it as set_sizes. 
diff -ruN -Xfreeciv/diff_ignore freeciv/common/map.c freeciv__/common/map.c
--- freeciv/common/map.c        2004-07-25 07:51:41.327673656 +0200
+++ freeciv__/common/map.c      2004-07-25 10:29:04.013167264 +0200
@@ -310,73 +310,78 @@
 /****************************************************************************
   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)
+static void set_sizes(double size, int Xratio, int Yratio)
 {
+  /* Future topologies may require even dimensions. */
+  /* Some code 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:
    *
-   *   size = xsize * ysize
+   *   1000 * 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
+   *   xsize = i_size * xratio * even
+   *   ysize = i_size * yratio * even * iso
    *
-   *   size = 4 * isize * xratio * isize * yratio * iso
-   *   isize = sqrt(size / (4 * xratio * yratio * iso))
+   * For any value of "i_size".  So with some substitution
    *
-   * Remember that size = base_size * 1000.  So this gives us
-   *
-   *   isize = sqrt(base_size * 250 / (xratio * yratio * iso))
+   *   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.
    */
-  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);
+  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;
+
+  /* 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);
+ 
+ /* 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 (map.size > base_size + 0.9) {
+  /* 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 of %2.1fk tiles.", base_size);
-  freelog(LOG_VERBOSE, "xsize and ysize are set to %d and %d.",
-         map.xsize, map.ysize);
+  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        {8, 5} 
-#define AUTO_RATIO_URANUS         {5, 8} 
+#define AUTO_RATIO_CLASSIC        {3, 2} 
+#define AUTO_RATIO_URANUS         {2, 3} 
 #define AUTO_RATIO_TORUS          {1, 1}
 
 /****************************************************************************
@@ -389,7 +394,7 @@
   calculate map.size.  This should be done in the client or when loading
   savegames, since the [xy]size values are already known.
 ****************************************************************************/
-void map_init_topology(bool set_sizes)
+void map_init_topology(bool Set_Sizes)
 {
   enum direction8 dir;
 
@@ -401,9 +406,9 @@
   
   assert(TF_WRAPX == 0x1 && TF_WRAPY == 0x2);
 
-  if (set_sizes) {
+  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]);  
+    set_sizes(map.size, default_ratios[id][0], default_ratios[id][1]);  
   } else {
     /* Set map.size based on map.xsize and map.ysize. */
     map.size = (float)(map.xsize * map.ysize) / 1000.0 + 0.5;

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