changeset 11012:88712c0e082e draft

(svn r15352) -Codechange: make the arguments passed around while founding a town a bit more readable
author smatz <smatz@openttd.org>
date Thu, 05 Feb 2009 13:33:37 +0000
parents fb38fb72b850
children ec39f4c7f0ea
files src/town.h src/town_cmd.cpp src/town_gui.cpp src/town_type.h
diffstat 4 files changed, 35 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/town.h
+++ b/src/town.h
@@ -250,7 +250,7 @@
 void InitializeTown();
 void ShowTownViewWindow(TownID town);
 void ExpandTown(Town *t);
-Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size, TownLayout layout);
+Town *CreateRandomTown(uint attempts, TownSize size, bool city, TownLayout layout);
 
 enum {
 	ROAD_REMOVE = 0,
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -1428,7 +1428,7 @@
  * @param size_mode How the size should be determined
  * @param size Parameter for size determination
  */
-static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size, TownLayout layout)
+static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout)
 {
 	extern int _nb_orig_names;
 
@@ -1480,26 +1480,11 @@
 
 	t->InitializeLayout(layout);
 
-	/* Random town size. */
-	int x = (Random() & 0xF) + 8;
-
-	switch (size_mode) {
-		default: NOT_REACHED();
-
-		case TSM_RANDOM:
-			t->larger_town = false;
-			break;
-
-		case TSM_FIXED:
-			x = size * 16 + 3;
-			t->larger_town = false;
-			break;
-
-		case TSM_CITY:
-			x *= _settings_game.economy.initial_city_size;
-			t->larger_town = true;
-			break;
-	}
+	t->larger_town = city;
+
+	int x = (int)size * 16 + 3;
+	if (size == TS_RANDOM) x = (Random() & 0xF) + 8;
+	if (city) x *= _settings_game.economy.initial_city_size;
 
 	t->noise_reached = 0;
 
@@ -1521,20 +1506,21 @@
  * as it might be possible in the future to fund your own town :)
  * @param tile coordinates where town is built
  * @param flags type of operation
- * @param p1  0..15 size of the town (0 = small, 1 = medium, 2 = large)
- *           16..31 town road layout
- * @param p2 size mode (@see TownSizeMode)
+ * @param p1  0..1 size of the town (@see TownSize)
+ *               2 true iff it should be a city
+ *            3..5 town road layout (@see TownLayout)
+ * @param p2 unused
  */
 CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
 {
 	/* Only in the scenario editor */
 	if (_game_mode != GM_EDITOR) return CMD_ERROR;
 
-	TownSizeMode tsm = (TownSizeMode)p2;
-	uint size = GB(p1, 0, 16);
-	TownLayout layout = (TownLayout)GB(p1, 16, 16);
-
-	if (tsm > TSM_CITY) return CMD_ERROR;
+	TownSize size = (TownSize)GB(p1, 0, 2);
+	bool city = HasBit(p1, 2);
+	TownLayout layout = (TownLayout)GB(p1, 3, 3);
+
+	if (size > TS_RANDOM) return CMD_ERROR;
 	if (layout > TL_RANDOM) return CMD_ERROR;
 
 	/* Check if too close to the edge of map */
@@ -1564,14 +1550,14 @@
 		Town *t = new Town(tile);
 		_generating_world = true;
 		UpdateNearestTownForRoadTiles(true);
-		DoCreateTown(t, tile, townnameparts, tsm, size, layout);
+		DoCreateTown(t, tile, townnameparts, size, city, layout);
 		UpdateNearestTownForRoadTiles(false);
 		_generating_world = false;
 	}
 	return CommandCost();
 }
 
-Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size, TownLayout layout)
+Town *CreateRandomTown(uint attempts, TownSize size, bool city, TownLayout layout)
 {
 	if (!Town::CanAllocateItem()) return NULL;
 
@@ -1603,7 +1589,7 @@
 		/* Allocate a town struct */
 		Town *t = new Town(tile);
 
-		DoCreateTown(t, tile, townnameparts, mode, size, layout);
+		DoCreateTown(t, tile, townnameparts, size, city, layout);
 		return t;
 	} while (--attempts != 0);
 
@@ -1623,13 +1609,12 @@
 	do {
 		IncreaseGeneratingWorldProgress(GWP_TOWN);
 		/* try 20 times to create a random-sized town for the first loop. */
-		TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM;
-		if (CreateRandomTown(20, mode, _settings_game.economy.initial_city_size, layout) != NULL) num++;
+		if (CreateRandomTown(20, TS_RANDOM, num_cities > 0, layout) != NULL) num++;
 		if (num_cities > 0) num_cities--;
 	} while (--n);
 
 	/* give it a last try, but now more aggressive */
-	if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0, layout) == NULL) {
+	if (num == 0 && CreateRandomTown(10000, TS_RANDOM, false, layout) == NULL) {
 		if (GetNumTowns() == 0) {
 			/* XXX - can we handle that more gracefully? */
 			if (_game_mode != GM_EDITOR) usererror("Could not generate any town");
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -28,7 +28,7 @@
 
 typedef GUIList<const Town*> GUITownList;
 
-static int _scengen_town_size = 1; // depress medium-sized towns per default
+static uint _scengen_town_size = 1; // select medium-sized towns per default
 static TownLayout _scengen_town_layout;
 
 static const Widget _town_authority_widgets[] = {
@@ -603,10 +603,9 @@
 
 static void PlaceProc_Town(TileIndex tile)
 {
-	uint32 size = min(_scengen_town_size, 2);
-	uint32 mode = _scengen_town_size > 2 ? TSM_CITY : TSM_FIXED;
-	uint32 layout = _scengen_town_layout;
-	DoCommandP(tile, size | (layout << 16), mode, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE), CcBuildTown);
+	bool city = _scengen_town_size > (uint)TS_LARGE;
+	TownSize size = city ? TS_RANDOM : (TownSize)_scengen_town_size;
+	DoCommandP(tile, size | city << 2 | (_scengen_town_layout << 3), 0, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE), CcBuildTown);
 }
 
 static const Widget _scen_edit_town_gen_widgets[] = {
@@ -677,14 +676,14 @@
 				break;
 
 			case TSEW_RANDOMTOWN: {
-				Town *t;
-				uint size = min(_scengen_town_size, (int)TSM_CITY);
-				TownSizeMode mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED;
+				bool city = _scengen_town_size == 3;
+				/* cities will always have 'large size' * initial_city_size */
+				TownSize size = city ? TS_RANDOM : (TownSize)_scengen_town_size;
 
 				this->HandleButtonClick(TSEW_RANDOMTOWN);
 				_generating_world = true;
 				UpdateNearestTownForRoadTiles(true);
-				t = CreateRandomTown(20, mode, size, _scengen_town_layout);
+				const Town *t = CreateRandomTown(20, size, city, _scengen_town_layout);
 				UpdateNearestTownForRoadTiles(false);
 				_generating_world = false;
 
--- a/src/town_type.h
+++ b/src/town_type.h
@@ -14,10 +14,12 @@
 struct Town;
 struct HouseSpec;
 
-enum TownSizeMode {
-	TSM_RANDOM,
-	TSM_FIXED,
-	TSM_CITY
+/** Supported initial town sizes */
+enum TownSize {
+	TS_SMALL,  ///< small town
+	TS_MEDIUM, ///< medium town
+	TS_LARGE,  ///< large town
+	TS_RANDOM, ///< random size, bigger than small, smaller than large
 };
 
 enum {