MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GrowTownWithBridge

Function GrowTownWithBridge

src/town_cmd.cpp:1312–1389  ·  view source on GitHub ↗

* Grows the town with a bridge. * At first we check if a bridge is reasonable. * If so we check if we are able to build it. * * @param t The current town * @param tile The current tile * @param bridge_dir The valid direction in which to grow a bridge * @return true if a bridge has been build else false */

Source from the content-addressed store, hash-verified

1310 * @return true if a bridge has been build else false
1311 */
1312static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
1313{
1314 assert(bridge_dir < DIAGDIR_END);
1315
1316 const Slope slope = GetTileSlope(tile);
1317
1318 /* Make sure the direction is compatible with the slope.
1319 * Well we check if the slope has an up bit set in the
1320 * reverse direction. */
1321 if (slope != SLOPE_FLAT && slope & InclinedSlope(bridge_dir)) return false;
1322
1323 /* Assure that the bridge is connectable to the start side */
1324 if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
1325
1326 /* We are in the right direction */
1327 uint bridge_length = 0; // This value stores the length of the possible bridge
1328 TileIndex bridge_tile = tile; // Used to store the other waterside
1329
1330 const TileIndexDiff delta = TileOffsByDiagDir(bridge_dir);
1331
1332 /* To prevent really small towns from building disproportionately
1333 * long bridges, make the max a function of its population. */
1334 const uint TOWN_BRIDGE_LENGTH_CAP = 11;
1335 uint base_bridge_length = 5;
1336 uint max_bridge_length = std::min(t->cache.population / 1000 + base_bridge_length, TOWN_BRIDGE_LENGTH_CAP);
1337
1338 if (slope == SLOPE_FLAT) {
1339 /* Bridges starting on flat tiles are only allowed when crossing rivers, rails or one-way roads. */
1340 do {
1341 if (bridge_length++ >= base_bridge_length) {
1342 /* Allow to cross rivers, not big lakes, nor large amounts of rails or one-way roads. */
1343 return false;
1344 }
1345 bridge_tile += delta;
1346 } while (IsValidTile(bridge_tile) && ((IsWaterTile(bridge_tile) && !IsSea(bridge_tile)) || IsPlainRailTile(bridge_tile) || (IsNormalRoadTile(bridge_tile) && GetDisallowedRoadDirections(bridge_tile) != DRD_NONE)));
1347 } else {
1348 do {
1349 if (bridge_length++ >= max_bridge_length) {
1350 /* Ensure the bridge is not longer than the max allowed length. */
1351 return false;
1352 }
1353 bridge_tile += delta;
1354 } while (IsValidTile(bridge_tile) && (IsWaterTile(bridge_tile) || IsPlainRailTile(bridge_tile) || (IsNormalRoadTile(bridge_tile) && GetDisallowedRoadDirections(bridge_tile) != DRD_NONE)));
1355 }
1356
1357 /* Don't allow a bridge where the start and end tiles are adjacent with no span between. */
1358 if (bridge_length == 1) return false;
1359
1360 /* Make sure the road can be continued past the bridge. At this point, bridge_tile holds the end tile of the bridge. */
1361 if (!CanRoadContinueIntoNextTile(t, bridge_tile, bridge_dir)) return false;
1362
1363 /* If another parallel bridge exists nearby, this one would be redundant and shouldn't be built. We don't care about flat bridges. */
1364 if (slope != SLOPE_FLAT) {
1365 for (auto search : SpiralTileSequence(tile, bridge_length, 0, 0)) {
1366 /* Only consider bridge head tiles. */
1367 if (!IsBridgeTile(search)) continue;
1368
1369 /* Only consider road bridges. */

Callers 1

GrowTownInTileFunction · 0.85

Calls 15

GetTileSlopeFunction · 0.85
InclinedSlopeFunction · 0.85
GetTownRoadBitsFunction · 0.85
TileAddByDiagDirFunction · 0.85
ReverseDiagDirFunction · 0.85
DiagDirToRoadBitsFunction · 0.85
TileOffsByDiagDirFunction · 0.85
IsValidTileFunction · 0.85
IsWaterTileFunction · 0.85
IsSeaFunction · 0.85
IsPlainRailTileFunction · 0.85
IsNormalRoadTileFunction · 0.85

Tested by

no test coverage detected