From: sam+civ@hotaru.chaosring.org
To: freeciv-dev@complete.org
Subject: Updated patch which checks # of islands with 'gen 1' and 'gen 5' maps
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

My last patch which allows people to use 'gen 1' and 'gen 5' maps and 
have each player have their own island had a nasty memory leak.  I have
plugged the memory leak (and had this patch generate nearly 1.000.000 
maps to make sure the memory leak was plugged), and here is the revised
patch.

- Sam

diff -ur freeciv-1.14.0/server/mapgen.c freeciv-1.14.0-checkisles/server/mapgen.c
--- freeciv-1.14.0/server/mapgen.c	Fri Oct 11 16:35:50 2002
+++ freeciv-1.14.0-checkisles/server/mapgen.c	Tue Mar 11 11:36:12 2003
@@ -941,8 +941,9 @@
  Allocate islands array and fill in values.
  Note this is only use for map.generator<=1, since others
  setups islands and starters explicitly.
+ Output: The number of good islands that this map has
 **************************************************************************/
-static void setup_isledata(void)
+int setup_isledata(void)
 {
   int x;
   int good, mingood, maxgood;
@@ -1104,6 +1105,7 @@
   }
   freelog(LOG_DEBUG, "The map has %i starting positions on %i isles.",
 	  starters, goodisles);
+  return goodisles;
 }
 
 /**************************************************************************
@@ -1113,17 +1115,47 @@
   FIXME: MAXTRIES used to be 1.000.000, but has been raised to 10.000.000
          because a set of values hit the limit. At some point we want to
          make a better solution.
+  Output: Whether this is a good map; 1 if yes, 0 if no
 **************************************************************************/
 #define MAXTRIES 10000000
-void create_start_positions(void)
+int create_start_positions(void)
 {
   int nr=0;
   int dist=40;
   int x, y, j=0, k, sum;
   int counter = 0;
+  int isles;
+
+  isles = 10000; /* Big number so this passes the "own island" test */
 
   if (!islands)		/* already setup for generators 2,3, and 4 */
-    setup_isledata();
+    isles = setup_isledata();
+
+  if(isles < game.nplayers) { /* Each player gets their own island */
+      printf("Seed %d does not create enough islands for %d players.\n",
+            map.seed,game.nplayers);
+      map.seed++; 
+      free(islands);
+      free(height_map);
+      free(river_map);
+      islands = NULL;
+      height_map = NULL;
+      river_map = NULL;
+      maxval = 0;
+      forests = 0;
+      free(map.tiles);
+      map.tiles = NULL;
+      map.num_continents = 0;
+      map.num_start_positions = 0;
+      map.have_specials = FALSE;
+      map.have_huts = FALSE;
+      /* Initializing the next two may not be necessary */
+      map.fixed_start_positions = FALSE;
+      map.have_rivers_overlay = FALSE;
+      return 0;
+      }
+
+  printf("Using seed %d\n",map.seed);
 
   if(dist>= map.xsize/2)
     dist= map.xsize/2;
@@ -1169,6 +1201,7 @@
 
   free(islands);
   islands = NULL;
+  return 1; /* Good map */
 }
 
 /**************************************************************************
diff -ur freeciv-1.14.0/server/mapgen.h freeciv-1.14.0-checkisles/server/mapgen.h
--- freeciv-1.14.0/server/mapgen.h	Tue Feb  5 11:05:51 2002
+++ freeciv-1.14.0-checkisles/server/mapgen.h	Tue Mar 11 11:36:12 2003
@@ -15,7 +15,7 @@
 
 void assign_continent_numbers(void);
 void map_fractal_generate(void);
-void create_start_positions(void);
+int create_start_positions(void);
 void adjust_terrain_param(void);
 
 #endif  /* FC__MAPGEN_H */
diff -ur freeciv-1.14.0/server/srv_main.c freeciv-1.14.0-checkisles/server/srv_main.c
--- freeciv-1.14.0/server/srv_main.c	Fri Oct 11 16:35:50 2002
+++ freeciv-1.14.0-checkisles/server/srv_main.c	Tue Mar 11 11:45:05 2003
@@ -1798,6 +1798,7 @@
 void srv_main(void)
 {
   int i;
+  int is_good_map;
 
   /* make sure it's initialized */
   if (!has_been_srv_init) {
@@ -1943,7 +1944,11 @@
     
   if(game.is_new_game)
     generate_ai_players();
-   
+ 
+  /* This is a loop which will recreate the map in case the map
+     generator decides the generated map is not good enough */
+  do {
+
   /* if we have a tile map, and map.generator==0, call map_fractal_generate
      anyway, to make the specials and huts */
   if(map_is_empty() || (map.generator == 0 && game.is_new_game))
@@ -1972,13 +1977,23 @@
     } players_iterate_end;
     game.max_players=game.nplayers;
 
+    is_good_map = 1;
     /* we don't want random start positions in a scenario which already
        provides them.  -- Gudy */
     if(map.num_start_positions==0) {
-      create_start_positions();
+      is_good_map = create_start_positions();
     }
   }
 
+  /* Save the whales, free the mallocs */
+  if(is_good_map == 0) {
+      players_iterate(pplayer) {
+          free(pplayer->private_map);
+          } players_iterate_end;
+      }
+
+  } while(is_good_map == 0);
+
   initialize_move_costs(); /* this may be the wrong place to do this */
   generate_minimap(); /* for city_desire; saves a lot of calculations */
 
