From 5403257dca8a8504bce85b1b68b4ee9e4580f871 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 8 Jul 2026 22:23:55 +0100 Subject: [PATCH] Codechange: Don't calculate bridge height difference twice (#15831) --- src/station_cmd.cpp | 6 ++---- src/water_cmd.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index b41292e87c..1800b851b2 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -911,11 +911,9 @@ static CommandCost IsStationBridgeAboveOk(TileIndex tile, std::span::Do(DoCommandFlag::Auto, tile); } - if (GetTileMaxZ(tile) + height > bridge_height) { - int height_diff = (GetTileMaxZ(tile) + height - bridge_height) * TILE_HEIGHT_STEP; - return CommandCostWithParam(GetBridgeTooLowMessageForStationType(type), height_diff); - } + int height_diff = GetTileMaxZ(tile) + height - bridge_height; + if (height_diff > 0) return CommandCostWithParam(GetBridgeTooLowMessageForStationType(type), height_diff * TILE_HEIGHT_STEP); return CommandCost{}; } diff --git a/src/water_cmd.cpp b/src/water_cmd.cpp index 1681605dc3..1b2dd3d48c 100644 --- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -371,9 +371,9 @@ static CommandCost DoBuildLock(TileIndex tile, DiagDirection dir, DoCommandFlags WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WaterClass::Canal; for (LockPart lock_part = LockPart::Middle; TileIndex t : {tile, tile - delta, tile + delta}) { - if (IsBridgeAbove(t) && GetBridgeHeight(GetSouthernBridgeEnd(t)) < GetTileMaxZ(t) + GetLockPartMinimalBridgeHeight(lock_part)) { - int height_diff = (GetTileMaxZ(tile) + GetLockPartMinimalBridgeHeight(lock_part) - GetBridgeHeight(GetSouthernBridgeEnd(t))) * TILE_HEIGHT_STEP; - return CommandCostWithParam(STR_ERROR_BRIDGE_TOO_LOW_FOR_LOCK, height_diff); + if (IsBridgeAbove(t)) { + int height_diff = GetTileMaxZ(tile) + GetLockPartMinimalBridgeHeight(lock_part) - GetBridgeHeight(GetSouthernBridgeEnd(t)); + if (height_diff > 0) return CommandCostWithParam(STR_ERROR_BRIDGE_TOO_LOW_FOR_LOCK, height_diff * TILE_HEIGHT_STEP); } ++lock_part; } @@ -1470,9 +1470,9 @@ static CommandCost CheckBuildAbove_Water(TileIndex tile, DoCommandFlags flags, [ { if (IsWater(tile) || IsCoast(tile)) return CommandCost(); if (IsLock(tile)) { - if (GetTileMaxZ(tile) + GetLockPartMinimalBridgeHeight(GetLockPart(tile)) <= height) return CommandCost(); - int height_diff = (GetTileMaxZ(tile) + GetLockPartMinimalBridgeHeight(GetLockPart(tile)) - height) * TILE_HEIGHT_STEP; - return CommandCostWithParam(STR_ERROR_BRIDGE_TOO_LOW_FOR_LOCK, height_diff); + int height_diff = GetTileMaxZ(tile) + GetLockPartMinimalBridgeHeight(GetLockPart(tile)) - height; + if (height_diff > 0) return CommandCostWithParam(STR_ERROR_BRIDGE_TOO_LOW_FOR_LOCK, height_diff * TILE_HEIGHT_STEP); + return CommandCost{}; } return Command::Do(flags, tile); }