changeset 11529:536d28b21537 draft

(svn r15890) -Codechange: unify the way 'can a town be placed here?' checks are done -Change: the requirements for location of 'random' town are now a bit less strict
author smatz <smatz@openttd.org>
date Sun, 29 Mar 2009 15:06:44 +0000
parents 2ee677c87220
children bb3f530f9495
files src/town_cmd.cpp
diffstat 1 files changed, 31 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -1535,6 +1535,31 @@
 	UpdateAirportsNoise();
 }
 
+/**
+ * Checks if it's possible to place a town at given tile
+ * @param tile tile to check
+ * @return error value or zero cost
+ */
+static CommandCost TownCanBePlacedHere(TileIndex tile)
+{
+	/* Check if too close to the edge of map */
+	if (DistanceFromEdge(tile) < 12) {
+		return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP);
+	}
+
+	/* Check distance to all other towns. */
+	if (IsCloseToTown(tile, 20)) {
+		return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
+	}
+
+	/* Can only build on clear flat areas, possibly with trees. */
+	if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
+		return_cmd_error(STR_0239_SITE_UNSUITABLE);
+	}
+
+	return CommandCost();
+}
+
 /** Create a new town.
  * This obviously only works in the scenario editor. Function not removed
  * as it might be possible in the future to fund your own town :)
@@ -1558,22 +1583,11 @@
 
 	if (size > TS_RANDOM) return CMD_ERROR;
 	if (layout > TL_RANDOM) return CMD_ERROR;
+
 	if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
 
-	/* Check if too close to the edge of map */
-	if (DistanceFromEdge(tile) < 12) {
-		return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP);
-	}
-
-	/* Check distance to all other towns. */
-	if (IsCloseToTown(tile, 20)) {
-		return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
-	}
-
-	/* Can only build on clear flat areas, possibly with trees. */
-	if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
-		return_cmd_error(STR_0239_SITE_UNSUITABLE);
-	}
+	CommandCost cost = TownCanBePlacedHere(tile);
+	if (CmdFailed(cost)) return cost;
 
 	/* Allocate town struct */
 	if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
@@ -1606,16 +1620,11 @@
 				break;
 			default: break;
 		}
-		if (DistanceFromEdge(tile) < 20) continue;
-
-		/* Make sure the tile is plain */
-		if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
-
-		/* Check not too close to a town */
-		if (IsCloseToTown(tile, 20)) continue;
+
+		/* Make sure town can be placed here */
+		if (CmdFailed(TownCanBePlacedHere(tile))) continue;
 
 		uint32 townnameparts;
-
 		/* Get a unique name for the town. */
 		if (!GenerateTownName(&townnameparts)) break;