src/town_cmd.cpp
changeset 9292 4f5612017238
parent 9287 2e497ff0d009
child 9302 4aa3c4a44b16
--- a/src/town_cmd.cpp	Tue Apr 01 15:03:59 2008 +0000
+++ b/src/town_cmd.cpp	Tue Apr 01 16:27:01 2008 +0000
@@ -107,6 +107,23 @@
 	this->xy = 0;
 }
 
+/**
+ * Generate a random town road layout.
+ *
+ * The layout is based on the TileHash.
+ */
+void Town::InitializeLayout()
+{
+	this->layout = (TownLayout)(TileHash(TileX(this->xy), TileY(this->xy)) % NUM_TLS);
+
+	/* Set invalid layouts to valid ones */
+	switch (this->layout) {
+		default: break;
+		case TL_RANDOM: this->layout = TL_ORIGINAL; break;
+		case TL_NO_ROADS: this->layout = TL_BETTER_ROADS; break;
+	}
+}
+
 // Local
 static int _grow_town_result;
 
@@ -130,23 +147,6 @@
 };
 
 /**
- * Calculate a hash value from a tile position
- *
- * @param x The X coordinate
- * @param y The Y coordinate
- * @return The hash of the tile
- */
-uint TileHash2Bit(uint x, uint y)
-{
-	uint hash = x >> 4;
-	hash ^= x >> 6;
-	hash ^= y >> 4;
-	hash -= y >> 6;
-	hash &= 3;
-	return hash;
-}
-
-/**
  * Return a random direction
  *
  * @return a random direction
@@ -708,11 +708,12 @@
 /**
  * Check if a Road is allowed on a given tile
  *
+ * @param t The current town
  * @param tile The target tile
  * @param dir The direction in which we want to extend the town
  * @return true if it is allowed else false
  */
-static bool IsRoadAllowedHere(TileIndex tile, DiagDirection dir)
+static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir)
 {
 	if (TileX(tile) < 2 || TileX(tile) >= MapMaxX() || TileY(tile) < 2 || TileY(tile) >= MapMaxY()) return false;
 
@@ -733,7 +734,7 @@
 		if (cur_slope == SLOPE_FLAT) {
 no_slope:
 			/* Tile has no slope */
-			switch (_patches.town_layout) {
+			switch (t->GetActiveLayout()) {
 				default: NOT_REACHED();
 
 				case TL_ORIGINAL: // Disallow the road if any neighboring tile has a road (distance: 1)
@@ -804,13 +805,13 @@
  * @return the RoadBit of the current tile regarding
  *  the selected town layout
  */
-static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile, DiagDirection dir)
+static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir)
 {
 	/* align the grid to the downtown */
 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile
 	RoadBits rcmd = ROAD_NONE;
 
-	switch (_patches.town_layout) {
+	switch (t->GetActiveLayout()) {
 		default: NOT_REACHED();
 
 		case TL_2X2_GRID:
@@ -993,7 +994,7 @@
 		LevelTownLand(tile);
 
 		/* Is a road allowed here? */
-		switch (_patches.town_layout) {
+		switch (t1->GetActiveLayout()) {
 			default: NOT_REACHED();
 
 			case TL_NO_ROADS: /* Disallow Roads */
@@ -1007,7 +1008,7 @@
 
 			case TL_BETTER_ROADS:
 			case TL_ORIGINAL:
-				if (!IsRoadAllowedHere(tile, target_dir)) return;
+				if (!IsRoadAllowedHere(t1, tile, target_dir)) return;
 
 				DiagDirection source_dir = ReverseDiagDir(target_dir);
 
@@ -1016,7 +1017,7 @@
 					do target_dir = RandomDiagDir(); while (target_dir == source_dir);
 				}
 
-				if (!IsRoadAllowedHere(TileAddByDiagDir(tile, target_dir), target_dir)) {
+				if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) {
 					/* A road is not allowed to continue the randomized road,
 					 *  return if the road we're trying to build is curved. */
 					if (target_dir != ReverseDiagDir(source_dir)) return;
@@ -1041,7 +1042,7 @@
 		 * the fitting RoadBits */
 		_grow_town_result = GROWTH_SEARCH_STOPPED;
 
-		switch (_patches.town_layout) {
+		switch (t1->GetActiveLayout()) {
 			default: NOT_REACHED();
 
 			case TL_NO_ROADS: /* Disallow Roads */
@@ -1079,7 +1080,7 @@
 		/* Don't walk into water. */
 		if (IsWaterTile(house_tile)) return;
 
-		switch (_patches.town_layout) {
+		switch (t1->GetActiveLayout()) {
 			default: NOT_REACHED();
 
 			case TL_NO_ROADS:
@@ -1103,7 +1104,7 @@
 				 /* Allow a house at the edge. 60% chance or
 				  * always ok if no road allowed. */
 				rcmd = DiagDirToRoadBits(target_dir);
-				allow_house = (!IsRoadAllowedHere(house_tile, target_dir) || Chance16(6, 10));
+				allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10));
 				break;
 		}
 
@@ -1157,7 +1158,7 @@
 	/* Number of times to search.
 	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
 	 * them a little handicap. */
-	switch (_patches.town_layout) {
+	switch (t->GetActiveLayout()) {
 		case TL_BETTER_ROADS:
 			_grow_town_result = 10 + t->num_houses * 2 / 9;
 			break;
@@ -1447,6 +1448,8 @@
 	UpdateTownVirtCoord(t);
 	_town_sort_dirty = true;
 
+	t->InitializeLayout();
+
 	/* Random town size. */
 	x = (Random() & 0xF) + 8;
 
@@ -1736,7 +1739,7 @@
 {
 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
 
-	switch (_patches.town_layout) {
+	switch (t->GetActiveLayout()) {
 		case TL_2X2_GRID:
 			if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false;
 			break;
@@ -1767,7 +1770,7 @@
 	uint dx = MapSize() + TileX(t->xy) - TileX(tile);
 	uint dy = MapSize() + TileY(t->xy) - TileY(tile);
 
-	switch (_patches.town_layout) {
+	switch (t->GetActiveLayout()) {
 		case TL_2X2_GRID:
 			if ((dx % 3) != 0 || (dy % 3) != 0) return false;
 			break;
@@ -2700,6 +2703,9 @@
 void AfterLoadTown()
 {
 	_town_sort_dirty = true;
+
+	Town *t;
+	FOR_ALL_TOWNS(t) t->InitializeLayout();
 }
 
 extern const ChunkHandler _town_chunk_handlers[] = {