summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2005-09-16 00:33:33 +0000
committerDarkvater <Darkvater@openttd.org>2005-09-16 00:33:33 +0000
commit78e4cbbfda606a6f23329dfaffb7adf6f1581b0f (patch)
treea457fd89edc14bd8597031996ddfaca339bfc74f
parent80e2126cc657f21cd9a8a76e8d7c3927642ff0e5 (diff)
downloadopenttd-78e4cbbfda606a6f23329dfaffb7adf6f1581b0f.tar.xz
(svn r2956) - Fix: [ 1253736 ] creating many town crash to desktop. Now it 'dies' with an ingame error message informing the gamer if it couldn't generate any towns in user-space. Still if it happens during new-game generation it crashes since we don't yet have actions to do in such a circumstance.
-rw-r--r--lang/english.txt2
-rw-r--r--main_gui.c13
-rw-r--r--misc.c2
-rw-r--r--town_cmd.c18
4 files changed, 24 insertions, 11 deletions
diff --git a/lang/english.txt b/lang/english.txt
index e287a2a16..2bd6eb5b8 100644
--- a/lang/english.txt
+++ b/lang/english.txt
@@ -575,6 +575,8 @@ STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP :{WHITE}...too c
STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN :{WHITE}...too close to another town
STR_0239_SITE_UNSUITABLE :{WHITE}...site unsuitable
STR_023A_TOO_MANY_TOWNS :{WHITE}...too many towns
+STR_CANNOT_GENERATE_TOWN :{WHITE}Can't build any towns
+STR_NO_SPACE_FOR_TOWN :{WHITE}...there is no more space on the map
STR_023B_INCREASE_SIZE_OF_TOWN :{BLACK}Increase size of town
STR_023C_EXPAND :{BLACK}Expand
STR_023D_RANDOM_TOWN :{BLACK}Random Town
diff --git a/main_gui.c b/main_gui.c
index 88bceae17..d599bc4b1 100644
--- a/main_gui.c
+++ b/main_gui.c
@@ -42,7 +42,7 @@ static byte _last_built_railtype;
extern void GenerateWorld(int mode, uint size_x, uint size_y);
extern void GenerateIndustries(void);
-extern void GenerateTowns(void);
+extern bool GenerateTowns(void);
void HandleOnEditTextCancel(void)
{
@@ -1505,16 +1505,23 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e)
_generating_world = true;
t = CreateRandomTown(20);
_generating_world = false;
- if (t != NULL)
+
+ if (t == NULL) {
+ ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
+ } else
ScrollMainWindowToTile(t->xy);
+
break;
}
case 6: {/* many random towns */
HandleButtonClick(w, 6);
+
_generating_world = true;
_game_mode = GM_NORMAL; // little hack to avoid towns of the same size
- GenerateTowns();
+ if (!GenerateTowns())
+ ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
_generating_world = false;
+
_game_mode = GM_EDITOR;
break;
}
diff --git a/misc.c b/misc.c
index 80435a32e..90fd7228e 100644
--- a/misc.c
+++ b/misc.c
@@ -113,7 +113,7 @@ void GenerateClearTile(void);
void GenerateIndustries(void);
void GenerateUnmovables(void);
-void GenerateTowns(void);
+bool GenerateTowns(void);
void StartupPlayers(void);
void StartupDisasters(void);
diff --git a/town_cmd.c b/town_cmd.c
index 2ba9e5fa0..2a4ca21e0 100644
--- a/town_cmd.c
+++ b/town_cmd.c
@@ -1107,25 +1107,29 @@ Town *CreateRandomTown(uint attempts)
return NULL;
}
-static const byte _num_initial_towns[3] = {
- 11, 23, 46
-};
+static const byte _num_initial_towns[3] = {11, 23, 46};
-void GenerateTowns(void)
+bool GenerateTowns(void)
{
uint num = 0;
- uint n =
- ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
+ uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
do {
if (CreateRandomTown(20) != NULL) //try 20 times for the first loop
num++;
} while (--n);
+ // give it a last try, but now more aggressive
if (num == 0 && CreateRandomTown(10000) == NULL) {
+ Town *t;
+ FOR_ALL_TOWNS(t) { if (IsValidTown(t)) {num = 1; break;}}
+
//XXX can we handle that more gracefully?
- error("Could not generate any town");
+ if (num == 0) error("Could not generate any town");
+ return false;
}
+
+ return true;
}
static bool CheckBuildHouseMode(Town *t1, TileIndex tile, uint tileh, int mode)