From 1faa296626bd4a05ae1901a2f5bfde0c83979d50 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 10 Feb 2026 10:39:26 -0500 Subject: [PATCH] Implement explicit 2-step wall placement flow - Step 1: place cardinal wall tiles based on floor-touch cardinal directions - Step 2: revisit two-cardinal elbow cells and override with corner tiles using exact 8-neighbor mask (including diagonal) - Use upsert placement so pass2 cleanly replaces pass1 walls at corner cells --- Game/Dungeon/DungeonGeneratorVNext.cs | 111 ++++++++++++++++++++------ 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorVNext.cs b/Game/Dungeon/DungeonGeneratorVNext.cs index ee75c3d..58d82cb 100644 --- a/Game/Dungeon/DungeonGeneratorVNext.cs +++ b/Game/Dungeon/DungeonGeneratorVNext.cs @@ -528,45 +528,62 @@ public class DungeonGeneratorVNext } } - var unresolved = new List<(int X, int Y)>(); - - // Pass 1: cardinal-direction masks first. + // Pass 1: place cardinal walls following floor adjacency only. foreach (var cell in wallCells) { int mask4 = ComputeCardinalMask(layout, cell.X, cell.Y); + string? cardinalBucket = ResolvePrimaryCardinalBucket(mask4); + TileDefinition? wallDef = !string.IsNullOrEmpty(cardinalBucket) + ? SelectWallDefinitionForBucket(tileContext, cardinalBucket) + : null; + if (wallDef == null) + { + wallDef = SelectFallbackWallDefinition(tileContext); + } + if (wallDef == null) + { + continue; + } + + UpsertWallTile(layout, cell.X, cell.Y, wallDef); + usedSheetIds.Add(wallDef.TileMapSheetId); + } + + // Pass 2: cells touching exactly two adjacent cardinal floors -> corner resolution. + foreach (var cell in wallCells) + { + int mask4 = ComputeCardinalMask(layout, cell.X, cell.Y); + if (!IsAdjacentTwoCardinal(mask4)) + { + continue; + } + int mask8 = ComputeEightDirectionMask(layout, cell.X, cell.Y); TileDefinition? wallDef = null; - // Strict geometry-driven bucket candidate order based on floor adjacency. - foreach (var bucketCandidate in ResolveWallBucketCandidates(mask4, mask8)) + // Prefer exact 8-direction corner bucket first (includes diagonal signal). + string? exact8 = ResolveEightDirectionBucketName(mask8); + if (!string.IsNullOrEmpty(exact8) && CornerPriorityEightBuckets.Contains(exact8)) { - wallDef = SelectWallDefinitionForBucket(tileContext, bucketCandidate); - if (wallDef != null) break; + wallDef = SelectWallDefinitionForBucket(tileContext, exact8); + } + + // Fallback: orientation corner candidates for this elbow mask. + if (wallDef == null) + { + foreach (var cornerBucket in ResolveCornerBucketCandidatesForElbow(mask4)) + { + wallDef = SelectWallDefinitionForBucket(tileContext, cornerBucket); + if (wallDef != null) break; + } } if (wallDef == null) { - unresolved.Add(cell); continue; } - PlaceWallTile(layout, cell.X, cell.Y, wallDef); - usedSheetIds.Add(wallDef.TileMapSheetId); - } - - // Pass 2: only fill unresolved cells with 8-direction masks. - foreach (var cell in unresolved) - { - int mask8 = ComputeEightDirectionMask(layout, cell.X, cell.Y); - string? bucketName = ResolveEightDirectionBucketName(mask8); - var wallDef = !string.IsNullOrEmpty(bucketName) - ? SelectWallDefinitionForBucket(tileContext, bucketName) - : null; - - wallDef ??= SelectFallbackWallDefinition(tileContext); - if (wallDef == null) continue; - - PlaceWallTile(layout, cell.X, cell.Y, wallDef); + UpsertWallTile(layout, cell.X, cell.Y, wallDef); usedSheetIds.Add(wallDef.TileMapSheetId); } @@ -788,6 +805,12 @@ public class DungeonGeneratorVNext }); } + private static void UpsertWallTile(DungeonLayoutV2 layout, int x, int y, TileDefinition def) + { + layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y); + PlaceWallTile(layout, x, y, def); + } + private static bool HasAdjacentCardinalWalkable(DungeonLayoutV2 layout, int x, int y) { return IsWalkable(layout, x, y - 1) || @@ -827,6 +850,44 @@ public class DungeonGeneratorVNext return layout.Walkable![x, y]; } + private static string? ResolvePrimaryCardinalBucket(int mask4) + { + // Bit order N,E,S,W. Prefer visible top/side profiles first. + if ((mask4 & 0b0010) != 0) return "WallTop"; // floor south of wall + if ((mask4 & 0b0100) != 0) return "WallSideLeft"; // floor east of wall + if ((mask4 & 0b0001) != 0) return "WallSideRight"; // floor west of wall + if ((mask4 & 0b1000) != 0) return "WallBottom"; // floor north of wall + return null; + } + + private static bool IsAdjacentTwoCardinal(int mask4) + { + int bits = ((mask4 & 0b1000) != 0 ? 1 : 0) + + ((mask4 & 0b0100) != 0 ? 1 : 0) + + ((mask4 & 0b0010) != 0 ? 1 : 0) + + ((mask4 & 0b0001) != 0 ? 1 : 0); + if (bits != 2) return false; + + // Exclude opposite pairs (N+S=1010, E+W=0101). + return mask4 != 0b1010 && mask4 != 0b0101; + } + + private static IReadOnlyList ResolveCornerBucketCandidatesForElbow(int mask4) + { + return mask4 switch + { + // E + S + 0b0110 => new[] { "WallDiagonalCornerDownRight", "WallInnerCornerDownRight", "WallDiagonalCornerUpLeft", "WallInnerCornerDownLeft" }, + // S + W + 0b0011 => new[] { "WallDiagonalCornerDownLeft", "WallInnerCornerDownLeft", "WallDiagonalCornerUpRight", "WallInnerCornerDownRight" }, + // N + E + 0b1100 => new[] { "WallDiagonalCornerUpRight", "WallInnerCornerDownRight", "WallDiagonalCornerDownLeft", "WallInnerCornerDownLeft" }, + // N + W + 0b1001 => new[] { "WallDiagonalCornerUpLeft", "WallInnerCornerDownLeft", "WallDiagonalCornerDownRight", "WallInnerCornerDownRight" }, + _ => Array.Empty() + }; + } + private static IReadOnlyList ResolveWallBucketCandidates(int mask4, int mask8) { // mask4 bit order: N,E,S,W