diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index f243228f78..e04fccadb6 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -895,26 +895,26 @@ static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd) * * @param t The current town * @param tile The current tile - * @param rcmd The RoadBits which are possible on this tile + * @param bridge_dir The valid direction in which to grow a bridge * @return true if a bridge has been build else false */ -static bool GrowTownWithBridge(const Town *t, TileIndex tile, RoadBits rcmd) +static bool GrowTownWithBridge(const Town *t, TileIndex tile, DiagDirection bridge_dir) { - DiagDirection bridge_dir; // The direction of a bridge we maybe want to build + assert(bridge_dir < DIAGDIR_END); - /* Determine direction of slope, - * and build a road if not a special slope. */ - switch (GetTileSlope(tile, NULL)) { - case SLOPE_SW: bridge_dir = DIAGDIR_NE; break; - case SLOPE_SE: bridge_dir = DIAGDIR_NW; break; - case SLOPE_NW: bridge_dir = DIAGDIR_SE; break; - case SLOPE_NE: bridge_dir = DIAGDIR_SW; break; + const Slope slope = GetTileSlope(tile, NULL); + if (slope == SLOPE_FLAT) return false; // no slope, no bridge - default: return false; - } - - /* Check if the bridge will be compatible to the RoadBits */ - if (!(rcmd & DiagDirToRoadBits(ReverseDiagDir(bridge_dir)))) return false; + /* Make sure the direction is compatible with the slope. + * If any of the following bits match, the slope is forbidden for + * that diagdir. Total of 5 slopes per direction. + * 0 -> 0b1100 + * 1 -> 0b0110 + * 2 -> 0b0011 + * 3 -> 0b1001 + * 0xCC is 0b11001100, so we just shift it right with + * the direction to get the forbidden slope mask. */ + if (HASBITS(slope & 0x0F, 0xCC >> bridge_dir)) return false; /* We are in the right direction */ uint32 bridge_length = 0; // This value stores the length of the possible bridge @@ -1120,7 +1120,10 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t rcmd = CleanUpRoadBits(tile, rcmd); if (rcmd == ROAD_NONE) return; - if (GrowTownWithBridge(t1, tile, rcmd)) return; + /* Only use the target direction for bridges to ensure they're connected. + * The target_dir is as computed previously according to town layout, so + * it will match it perfectly. */ + if (GrowTownWithBridge(t1, tile, target_dir)) return; GrowTownWithRoad(t1, tile, rcmd); } diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 7eb6ec8d66..1145913e2e 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -258,9 +258,8 @@ CommandCost CmdBuildBridge(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p if (z_start != z_end) return_cmd_error(STR_BRIDGEHEADS_NOT_SAME_HEIGHT); - /* Towns are not allowed to use bridges on slopes. */ allow_on_slopes = (!_is_old_ai_player - && _current_player != OWNER_TOWN && _patches.build_on_slopes); + && _patches.build_on_slopes); TransportType transport_type = railtype == INVALID_RAILTYPE ? TRANSPORT_ROAD : TRANSPORT_RAIL;