(svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
authorcelestar
Sun, 13 Feb 2005 09:42:49 +0000
changeset 1362 bceb3c57353d
parent 1361 5833194df433
child 1363 775a7ee52369
(svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
error() is called, some more graceful handling should be implemented
later.
main_gui.c
town.h
town_cmd.c
--- a/main_gui.c	Sun Feb 13 08:12:03 2005 +0000
+++ b/main_gui.c	Sun Feb 13 09:42:49 2005 +0000
@@ -1444,7 +1444,7 @@
 
 			HandleButtonClick(w, 4);
 			_generating_world = true;
-			t = CreateRandomTown();
+			t = CreateRandomTown(20);
 			_generating_world = false;
 			if (t != NULL)
 				ScrollMainWindowToTile(t->xy);
--- a/town.h	Sun Feb 13 08:12:03 2005 +0000
+++ b/town.h	Sun Feb 13 09:42:49 2005 +0000
@@ -82,7 +82,7 @@
 void DeleteTown(Town *t);
 void ExpandTown(Town *t);
 bool GrowTown(Town *t);
-Town *CreateRandomTown(void);
+Town *CreateRandomTown(uint attempts);
 
 enum {
 	ROAD_REMOVE = 0,
--- a/town_cmd.c	Sun Feb 13 08:12:03 2005 +0000
+++ b/town_cmd.c	Sun Feb 13 09:42:49 2005 +0000
@@ -1027,15 +1027,12 @@
 	return 0;
 }
 
-Town *CreateRandomTown(void)
+Town *CreateRandomTown(uint attempts)
 {
 	uint tile;
 	TileInfo ti;
 	Town *t;
-	int n;
 
-	// Try 20 times.
-	n = 20;
 	do {
 		// Generate a tile index not too close from the edge
 		tile = TILE_MASK(Random());
@@ -1058,7 +1055,7 @@
 
 		DoCreateTown(t, tile);
 		return t;
-	} while (--n);
+	} while (--attempts);
 	return NULL;
 }
 
@@ -1068,10 +1065,19 @@
 
 void GenerateTowns(void)
 {
+	uint num = 0;
 	uint n =
 		ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
 
-	do CreateRandomTown(); while (--n);
+	do {
+		if (CreateRandomTown(20) != NULL) 	//try 20 times for the first loop
+			num++;
+	} while (--n);
+
+	if (num == 0 && CreateRandomTown(10000) == NULL) {
+		//XXX can we handle that more gracefully?
+		error("Could not generate any town");
+	}
 }
 
 static bool CheckBuildHouseMode(Town *t1, uint tile, uint tileh, int mode) {