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

Function CheckIfCanLevelIndustryPlatform

src/industry_cmd.cpp:1620–1684  ·  view source on GitHub ↗

* This function tries to flatten out the land below an industry, without * damaging the surroundings too much. */

Source from the content-addressed store, hash-verified

1618 * damaging the surroundings too much.
1619 */
1620static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlags flags, const IndustryTileLayout &layout)
1621{
1622 int max_x = 0;
1623 int max_y = 0;
1624
1625 /* Finds dimensions of largest variant of this industry */
1626 for (const IndustryTileLayoutTile &it : layout) {
1627 if (it.gfx == GFX_WATERTILE_SPECIALCHECK) continue; // watercheck tiles don't count for footprint size
1628 if (it.ti.x > max_x) max_x = it.ti.x;
1629 if (it.ti.y > max_y) max_y = it.ti.y;
1630 }
1631
1632 /* Remember level height */
1633 uint h = TileHeight(tile);
1634
1635 if (TileX(tile) <= _settings_game.construction.industry_platform + 1U || TileY(tile) <= _settings_game.construction.industry_platform + 1U) return false;
1636 /* Check that all tiles in area and surrounding are clear
1637 * this determines that there are no obstructing items */
1638
1639 /* TileArea::Expand is not used here as we need to abort
1640 * instead of clamping if the bounds cannot expanded. */
1641 TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform),
1642 max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform);
1643
1644 if (TileX(ta.tile) + ta.w >= Map::MaxX() || TileY(ta.tile) + ta.h >= Map::MaxY()) return false;
1645
1646 /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
1647 * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
1648 Backup<CompanyID> cur_company(_current_company, OWNER_TOWN);
1649
1650 for (TileIndex tile_walk : ta) {
1651 uint curh = TileHeight(tile_walk);
1652 if (curh != h) {
1653 /* This tile needs terraforming. Check if we can do that without
1654 * damaging the surroundings too much. */
1655 if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
1656 cur_company.Restore();
1657 return false;
1658 }
1659 /* This is not 100% correct check, but the best we can do without modifying the map.
1660 * What is missing, is if the difference in height is more than 1.. */
1661 if (std::get<0>(Command<CMD_TERRAFORM_LAND>::Do(DoCommandFlags{flags}.Reset(DoCommandFlag::Execute), tile_walk, SLOPE_N, curh <= h)).Failed()) {
1662 cur_company.Restore();
1663 return false;
1664 }
1665 }
1666 }
1667
1668 if (flags.Test(DoCommandFlag::Execute)) {
1669 /* Terraform the land under the industry */
1670 for (TileIndex tile_walk : ta) {
1671 uint curh = TileHeight(tile_walk);
1672 while (curh != h) {
1673 /* We give the terraforming for free here, because we can't calculate
1674 * exact cost in the test-round, and as we all know, that will cause
1675 * a nice assert if they don't match ;) */
1676 Command<CMD_TERRAFORM_LAND>::Do(flags, tile_walk, SLOPE_N, curh <= h);
1677 curh += (curh > h) ? -1 : 1;

Callers 1

CreateNewIndustryHelperFunction · 0.85

Calls 9

TileHeightFunction · 0.85
TileXFunction · 0.85
TileYFunction · 0.85
TileDiffXYFunction · 0.85
FailedMethod · 0.80
TestMethod · 0.80
RestoreMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected