summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorplanetmaker <planetmaker@openttd.org>2013-10-17 21:25:15 +0000
committerplanetmaker <planetmaker@openttd.org>2013-10-17 21:25:15 +0000
commit0bc86ff9ee22529d7de391c4b0fbacdb687cb60f (patch)
tree4e155a319c678d3f7cad667f6395ed02c5a6f2ef /src
parent0a481d5ea66ec04c85ec0c6cd9beb098c84d6345 (diff)
downloadopenttd-0bc86ff9ee22529d7de391c4b0fbacdb687cb60f.tar.xz
(svn r25871) -Codechange: Move placement rules for lighthouses to its own function
Diffstat (limited to 'src')
-rw-r--r--src/object_cmd.cpp80
1 files changed, 45 insertions, 35 deletions
diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp
index ae9b2c701..3aabb3471 100644
--- a/src/object_cmd.cpp
+++ b/src/object_cmd.cpp
@@ -644,6 +644,48 @@ static bool HasTransmitter(TileIndex tile, void *user)
}
/**
+ * Try to build a lighthouse.
+ * @return True iff building a lighthouse succeeded.
+ */
+static bool TryBuildLightHouse()
+{
+ uint maxx = MapMaxX();
+ uint maxy = MapMaxY();
+ uint r = Random();
+
+ /* Scatter the lighthouses more evenly around the perimeter */
+ int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
+ DiagDirection dir;
+ for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
+ perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
+ }
+
+ TileIndex tile;
+ switch (dir) {
+ default:
+ case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
+ case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
+ case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
+ case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
+ }
+
+ /* Only build lighthouses at tiles where the border is sea. */
+ if (!IsTileType(tile, MP_WATER)) return false;
+
+ for (int j = 0; j < 19; j++) {
+ int h;
+ if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) {
+ BuildObject(OBJECT_LIGHTHOUSE, tile);
+ assert(tile < MapSize());
+ return true;
+ }
+ tile += TileOffsByDiagDir(dir);
+ if (!IsValidTile(tile)) return false;
+ }
+ return false;
+}
+
+/**
* Try to build a transmitter.
* @return True iff a transmitter was built.
*/
@@ -694,42 +736,10 @@ void GenerateObjects()
}
/* add lighthouses */
- uint maxx = MapMaxX();
- uint maxy = MapMaxY();
for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0 && Object::CanAllocateItem(); loop_count++) {
- uint r = Random();
-
- /* Scatter the lighthouses more evenly around the perimeter */
- int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
- DiagDirection dir;
- for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
- perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
- }
-
- TileIndex tile;
- switch (dir) {
- default:
- case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
- case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
- case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
- case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
- }
-
- /* Only build lighthouses at tiles where the border is sea. */
- if (!IsTileType(tile, MP_WATER)) continue;
-
- for (int j = 0; j < 19; j++) {
- int h;
- if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) {
- BuildObject(OBJECT_LIGHTHOUSE, tile);
- IncreaseGeneratingWorldProgress(GWP_OBJECT);
- lighthouses_to_build--;
- assert(tile < MapSize());
- break;
- }
- tile += TileOffsByDiagDir(dir);
- if (!IsValidTile(tile)) break;
- }
+ if (!TryBuildLightHouse()) continue;
+ IncreaseGeneratingWorldProgress(GWP_OBJECT);
+ lighthouses_to_build--;
}
}