1
0
Fork 0

Codechange: simplify terraform recursion logic

pull/13012/head
Rubidium 2024-10-19 14:01:57 +02:00 committed by rubidium42
parent 8ca417baa9
commit 9d2e07b1f6
1 changed files with 14 additions and 26 deletions

View File

@ -135,35 +135,23 @@ static std::tuple<CommandCost, TileIndex> TerraformTileHeight(TerraformerState *
total_cost.AddCost(_price[PR_TERRAFORM]); total_cost.AddCost(_price[PR_TERRAFORM]);
/* Recurse to neighboured corners if height difference is larger than 1 */ /* Recurse to neighboured corners if height difference is larger than 1 */
{ for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
TileIndex orig_tile = tile; TileIndex neighbour_tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
static const TileIndexDiffC _terraform_tilepos[] = {
{ 1, 0}, // move to tile in SE
{-2, 0}, // undo last move, and move to tile in NW
{ 1, 1}, // undo last move, and move to tile in SW
{ 0, -2} // undo last move, and move to tile in NE
};
for (const auto &ttm : _terraform_tilepos) { /* Not using IsValidTile as we want to also change MP_VOID tiles, which IsValidTile excludes. */
tile += ToTileIndexDiff(ttm); if (neighbour_tile == INVALID_TILE) continue;
if (tile >= Map::Size()) continue; /* Get TileHeight of neighboured tile as of current terraform progress */
/* Make sure we don't wrap around the map */ int r = TerraformGetHeightOfTile(ts, neighbour_tile);
if (Delta(TileX(orig_tile), TileX(tile)) == Map::SizeX() - 1) continue; int height_diff = height - r;
if (Delta(TileY(orig_tile), TileY(tile)) == Map::SizeY() - 1) continue;
/* Get TileHeight of neighboured tile as of current terraform progress */ /* Is the height difference to the neighboured corner greater than 1? */
int r = TerraformGetHeightOfTile(ts, tile); if (abs(height_diff) > 1) {
int height_diff = height - r; /* Terraform the neighboured corner. The resulting height difference should be 1. */
height_diff += (height_diff < 0 ? 1 : -1);
/* Is the height difference to the neighboured corner greater than 1? */ auto [cost, err_tile] = TerraformTileHeight(ts, neighbour_tile, r + height_diff);
if (abs(height_diff) > 1) { if (cost.Failed()) return { cost, err_tile };
/* Terraform the neighboured corner. The resulting height difference should be 1. */ total_cost.AddCost(cost);
height_diff += (height_diff < 0 ? 1 : -1);
auto [cost, err_tile] = TerraformTileHeight(ts, tile, r + height_diff);
if (cost.Failed()) return { cost, err_tile };
total_cost.AddCost(cost);
}
} }
} }