From 563eac90ca20eddd6d359142af36e7387660dff5 Mon Sep 17 00:00:00 2001 From: celestar Date: Sun, 13 Feb 2005 09:42:49 +0000 Subject: (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 | 2 +- town.h | 2 +- town_cmd.c | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/main_gui.c b/main_gui.c index 48e1c9b00..37af61068 100644 --- a/main_gui.c +++ b/main_gui.c @@ -1444,7 +1444,7 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e) HandleButtonClick(w, 4); _generating_world = true; - t = CreateRandomTown(); + t = CreateRandomTown(20); _generating_world = false; if (t != NULL) ScrollMainWindowToTile(t->xy); diff --git a/town.h b/town.h index cf7eee16e..ddd30b960 100644 --- a/town.h +++ b/town.h @@ -82,7 +82,7 @@ void ShowTownViewWindow(uint town); void DeleteTown(Town *t); void ExpandTown(Town *t); bool GrowTown(Town *t); -Town *CreateRandomTown(void); +Town *CreateRandomTown(uint attempts); enum { ROAD_REMOVE = 0, diff --git a/town_cmd.c b/town_cmd.c index d0daaf146..43cdd61f5 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -1027,15 +1027,12 @@ int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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 @@ Town *CreateRandomTown(void) DoCreateTown(t, tile); return t; - } while (--n); + } while (--attempts); return NULL; } @@ -1068,10 +1065,19 @@ static const byte _num_initial_towns[3] = { 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) { -- cgit v1.2.3-70-g09d2