(svn r4684) - Backport from trunk (r4591):

Fix: Game no longer errors out when "Many random towns" is selected
  in the scenario editor.
This commit is contained in:
Darkvater 2006-05-02 14:02:23 +00:00
parent 8e4cf4d599
commit 6fc56409e6
4 changed files with 24 additions and 24 deletions

View File

@ -1448,7 +1448,8 @@ void CcBuildTown(bool success, TileIndex tile, uint32 p1, uint32 p2)
static void PlaceProc_Town(TileIndex tile) static void PlaceProc_Town(TileIndex tile)
{ {
DoCommandP(tile, 0, 0, CcBuildTown, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE)); Window *w = FindWindowById(WC_SCEN_TOWN_GEN, 0);
DoCommandP(tile, 1 + FIND_FIRST_BIT(w->click_state >> 7), 0, CcBuildTown, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE));
} }
@ -1470,11 +1471,14 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e)
{ {
switch (e->event) { switch (e->event) {
case WE_PAINT: case WE_PAINT:
w->click_state = (w->click_state & ~(1<<7 | 1<<8 | 1<<9) ) | (1 << (_new_town_size + 7));
DrawWindowWidgets(w); DrawWindowWidgets(w);
DrawStringCentered(80, 56, STR_02A5_TOWN_SIZE, 0); DrawStringCentered(80, 56, STR_02A5_TOWN_SIZE, 0);
break; break;
case WE_CREATE:
w->click_state = 1 << 8; /* medium town size selected */
break;
case WE_CLICK: case WE_CLICK:
switch (e->click.widget) { switch (e->click.widget) {
case 4: /* new town */ case 4: /* new town */
@ -1485,7 +1489,7 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e)
HandleButtonClick(w, 5); HandleButtonClick(w, 5);
_generating_world = true; _generating_world = true;
t = CreateRandomTown(20); t = CreateRandomTown(20, 1 + FIND_FIRST_BIT(w->click_state >> 7));
_generating_world = false; _generating_world = false;
if (t == NULL) { if (t == NULL) {
@ -1500,18 +1504,13 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e)
HandleButtonClick(w, 6); HandleButtonClick(w, 6);
_generating_world = true; _generating_world = true;
_game_mode = GM_NORMAL; // little hack to avoid towns of the same size if (!GenerateTowns()) ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
if (!GenerateTowns()) {
ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
}
_generating_world = false; _generating_world = false;
_game_mode = GM_EDITOR;
break; break;
} }
case 7: case 8: case 9: case 7: case 8: case 9:
_new_town_size = e->click.widget - 7; w->click_state = 1 << e->click.widget;
SetWindowDirty(w); SetWindowDirty(w);
break; break;
} }
@ -1524,7 +1523,7 @@ static void ScenEditTownGenWndProc(Window *w, WindowEvent *e)
_place_proc(e->place.tile); _place_proc(e->place.tile);
break; break;
case WE_ABORT_PLACE_OBJ: case WE_ABORT_PLACE_OBJ:
w->click_state = 0; w->click_state &= (1 << 7 | 1 << 8 | 1 << 9);
SetWindowDirty(w); SetWindowDirty(w);
break; break;
} }

2
town.h
View File

@ -83,7 +83,7 @@ void InitializeTown(void);
void ShowTownViewWindow(uint town); void ShowTownViewWindow(uint town);
void DeleteTown(Town *t); void DeleteTown(Town *t);
void ExpandTown(Town *t); void ExpandTown(Town *t);
Town *CreateRandomTown(uint attempts); Town *CreateRandomTown(uint attempts, uint size_mode);
enum { enum {
ROAD_REMOVE = 0, ROAD_REMOVE = 0,

View File

@ -920,7 +920,7 @@ void UpdateTownMaxPass(Town *t)
t->max_mail = t->population >> 4; t->max_mail = t->population >> 4;
} }
static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts) static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode)
{ {
int x, i; int x, i;
@ -968,9 +968,11 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts)
UpdateTownVirtCoord(t); UpdateTownVirtCoord(t);
_town_sort_dirty = true; _town_sort_dirty = true;
x = (Random() & 0xF) + 8; if (size_mode == 0) {
if (_game_mode == GM_EDITOR) x = (Random() & 0xF) + 8;
x = _new_town_size * 16 + 3; } else {
x = (size_mode - 1) * 16 + 3;
}
t->num_houses += x; t->num_houses += x;
UpdateTownRadius(t); UpdateTownRadius(t);
@ -1013,7 +1015,7 @@ static Town *AllocateTown(void)
* This obviously only works in the scenario editor. Function not removed * This obviously only works in the scenario editor. Function not removed
* as it might be possible in the future to fund your own town :) * as it might be possible in the future to fund your own town :)
* @param x,y coordinates where town is built * @param x,y coordinates where town is built
* @param p1 unused * @param p1 size of the town (1 = small, 2 = medium, 3 = large)
* @param p2 unused * @param p2 unused
*/ */
int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2)
@ -1052,13 +1054,13 @@ int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// Create the town // Create the town
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
_generating_world = true; _generating_world = true;
DoCreateTown(t, tile, townnameparts); DoCreateTown(t, tile, townnameparts, p1);
_generating_world = false; _generating_world = false;
} }
return 0; return 0;
} }
Town *CreateRandomTown(uint attempts) Town *CreateRandomTown(uint attempts, uint size_mode)
{ {
TileIndex tile; TileIndex tile;
TileInfo ti; TileInfo ti;
@ -1089,7 +1091,7 @@ Town *CreateRandomTown(uint attempts)
if (t == NULL) if (t == NULL)
break; break;
DoCreateTown(t, tile, townnameparts); DoCreateTown(t, tile, townnameparts, size_mode);
return t; return t;
} while (--attempts); } while (--attempts);
return NULL; return NULL;
@ -1103,17 +1105,17 @@ bool GenerateTowns(void)
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 { do {
if (CreateRandomTown(20) != NULL) //try 20 times for the first loop if (CreateRandomTown(20, 0) != NULL) //try 20 times to create a random-sized town for the first loop.
num++; num++;
} while (--n); } while (--n);
// give it a last try, but now more aggressive // give it a last try, but now more aggressive
if (num == 0 && CreateRandomTown(10000) == NULL) { if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
Town *t; Town *t;
FOR_ALL_TOWNS(t) { if (IsValidTown(t)) {num = 1; break;}} FOR_ALL_TOWNS(t) { if (IsValidTown(t)) {num = 1; break;}}
//XXX can we handle that more gracefully? //XXX can we handle that more gracefully?
if (num == 0) error("Could not generate any town"); if (num == 0 && _game_mode != GM_EDITOR) error("Could not generate any town");
return false; return false;
} }

View File

@ -292,7 +292,6 @@ VARDEF byte _yearly_expenses_type;
VARDEF TileIndex _terraform_err_tile; VARDEF TileIndex _terraform_err_tile;
VARDEF TileIndex _build_tunnel_endtile; VARDEF TileIndex _build_tunnel_endtile;
VARDEF bool _generating_world; VARDEF bool _generating_world;
VARDEF int _new_town_size;
VARDEF uint _returned_refit_amount; VARDEF uint _returned_refit_amount;
// Deals with the type of the savegame, independent of extension // Deals with the type of the savegame, independent of extension