Switch VNext wall placement to exact directional mask buckets

- Replace cardinal/corner two-pass resolver with single-pass exact 8-direction mask matching

- Implement curated directional mask set (N/S/E/W + large/single corner buckets)

- Require wall tiles to map to exactly one directional bucket while still allowing extra non-directional buckets

- Add alias support for new bucket naming conventions (Wall_N style and corner names)
This commit is contained in:
dingo
2026-02-10 12:15:48 -05:00
parent 04db7b71d2
commit ec7f432f09
+49 -221
View File
@@ -528,55 +528,17 @@ public class DungeonGeneratorVNext
}
}
// Pass 1: place cardinal walls following floor adjacency only.
// Single-pass strict directional wall placement (exact 8-direction mask).
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;
// Prefer exact 8-direction corner bucket first (includes diagonal signal).
string? exact8 = ResolveEightDirectionBucketName(mask8);
if (!string.IsNullOrEmpty(exact8) && CornerPriorityEightBuckets.Contains(exact8))
string? directionalBucket = ResolveDirectionalWallBucket(mask8);
if (string.IsNullOrEmpty(directionalBucket))
{
wallDef = SelectWallDefinitionForBucket(tileContext, exact8);
}
if (wallDef == null)
{
string? derivedCornerBucket = ResolveElbowCornerBucket(mask4, mask8);
if (!string.IsNullOrEmpty(derivedCornerBucket))
{
wallDef = SelectWallDefinitionForBucket(tileContext, derivedCornerBucket);
}
continue;
}
TileDefinition? wallDef = SelectWallDefinitionForBucket(tileContext, directionalBucket);
if (wallDef == null)
{
continue;
@@ -681,23 +643,29 @@ public class DungeonGeneratorVNext
var canonicalWallBucketIds = ResolveCanonicalWallBucketIds(bucketIdsByName, allowedBucketIds);
var wallDefsByCanonicalBucket = new Dictionary<string, List<TileDefinition>>(StringComparer.OrdinalIgnoreCase);
int ambiguousDirectionalCount = 0;
foreach (var wallDef in wallCandidates)
{
if (!tileBuckets.TryGetValue(wallDef.Id, out var defBuckets) || defBuckets.Count == 0) continue;
var canonicalMatches = new List<string>();
var directionalMatches = new List<string>();
foreach (var kvp in canonicalWallBucketIds)
{
if (defBuckets.Contains(kvp.Value))
{
canonicalMatches.Add(kvp.Key);
directionalMatches.Add(kvp.Key);
}
}
// Enforce "wall tiles belong to one wall bucket" for placement selection.
if (canonicalMatches.Count != 1) continue;
// Strict directional constraint: exactly one directional bucket per wall tile.
if (directionalMatches.Count == 0) continue;
if (directionalMatches.Count > 1)
{
ambiguousDirectionalCount++;
continue;
}
string canonical = canonicalMatches[0];
string canonical = directionalMatches[0];
if (!wallDefsByCanonicalBucket.TryGetValue(canonical, out var defs))
{
defs = new List<TileDefinition>();
@@ -713,6 +681,12 @@ public class DungeonGeneratorVNext
floorCandidates.Count,
wallCandidates.Count,
wallDefsByCanonicalBucket.Count);
if (ambiguousDirectionalCount > 0)
{
_logger?.LogWarning(
"VNext tile context skipped {Count} wall definitions because they were assigned to multiple directional wall buckets.",
ambiguousDirectionalCount);
}
return new TileBuildContext(
sheetsById,
@@ -781,12 +755,6 @@ public class DungeonGeneratorVNext
return defs[_rng.Next(defs.Count)];
}
private TileDefinition? SelectFallbackWallDefinition(TileBuildContext context)
{
if (context.WallCandidates.Count == 0) return null;
return context.WallCandidates[_rng.Next(context.WallCandidates.Count)];
}
private static void PlaceWallTile(DungeonLayoutV2 layout, int x, int y, TileDefinition def)
{
layout.Tiles[x, y] = def.Id;
@@ -818,16 +786,6 @@ public class DungeonGeneratorVNext
IsWalkable(layout, x - 1, y);
}
private static int ComputeCardinalMask(DungeonLayoutV2 layout, int x, int y)
{
// Bit order N, E, S, W (MSB -> LSB).
int n = IsWalkable(layout, x, y - 1) ? 1 : 0;
int e = IsWalkable(layout, x + 1, y) ? 1 : 0;
int s = IsWalkable(layout, x, y + 1) ? 1 : 0;
int w = IsWalkable(layout, x - 1, y) ? 1 : 0;
return (n << 3) | (e << 2) | (s << 1) | w;
}
private static int ComputeEightDirectionMask(DungeonLayoutV2 layout, int x, int y)
{
// Bit order N, NE, E, SE, S, SW, W, NW (MSB -> LSB).
@@ -849,132 +807,18 @@ public class DungeonGeneratorVNext
return layout.Walkable![x, y];
}
private static string? ResolvePrimaryCardinalBucket(int mask4)
private static string? ResolveDirectionalWallBucket(int mask8)
{
// 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<string> ResolveWallBucketCandidates(int mask4, int mask8)
{
// mask4 bit order: N,E,S,W
// Strict selection to avoid impossible tiles:
// - one-side touch => straight wall bucket
// - adjacent-two => corner buckets only
// - opposite-two/all-three/all-four => full/complex buckets
return mask4 switch
{
// One cardinal neighbor.
0b0010 => new[] { "WallTop" }, // S
0b0001 => new[] { "WallSideRight" }, // W
0b0100 => new[] { "WallSideLeft" }, // E
0b1000 => new[] { "WallBottom" }, // N
// Adjacent two-neighbor elbows must use corner families.
0b0110 => new[]
{
"WallDiagonalCornerDownRight",
"WallInnerCornerDownRight",
"WallDiagonalCornerUpLeft",
"WallInnerCornerDownLeft"
},
0b0011 => new[]
{
"WallDiagonalCornerDownLeft",
"WallInnerCornerDownLeft",
"WallDiagonalCornerUpRight",
"WallInnerCornerDownRight"
},
0b1100 => new[]
{
"WallDiagonalCornerUpRight",
"WallInnerCornerDownRight",
"WallDiagonalCornerDownLeft",
"WallInnerCornerDownLeft"
},
0b1001 => new[]
{
"WallDiagonalCornerUpLeft",
"WallInnerCornerDownLeft",
"WallDiagonalCornerDownRight",
"WallInnerCornerDownRight"
},
// Opposite-two / three / four neighbors: use complex/full buckets.
_ => ResolveComplexBucketCandidates(mask8)
};
}
private static IReadOnlyList<string> ResolveComplexBucketCandidates(int mask8)
{
string? exact8 = ResolveEightDirectionBucketName(mask8);
if (!string.IsNullOrEmpty(exact8))
{
return new[] { exact8, "WallFullEightDirections", "WallFull", "WallTop" };
}
return new[] { "WallFullEightDirections", "WallFull", "WallTop" };
}
private static string? ResolveEightDirectionBucketName(int mask8)
{
foreach (var mapping in EightDirectionBucketMasks)
foreach (var mapping in DirectionalWallBucketMasks)
{
if (mapping.Value.Contains(mask8))
{
return mapping.Key;
}
}
return null;
}
private static string? ResolveElbowCornerBucket(int mask4, int mask8)
{
// For elbow masks, use the elbow diagonal bit to pick inner vs diagonal corner bucket.
// Bit order (mask8): N,NE,E,SE,S,SW,W,NW.
return mask4 switch
{
// E + S, uses SE diagonal (bit 4)
0b0110 => (mask8 & (1 << 4)) != 0
? "WallInnerCornerDownRight"
: "WallDiagonalCornerDownRight",
// S + W, uses SW diagonal (bit 2)
0b0011 => (mask8 & (1 << 2)) != 0
? "WallInnerCornerDownLeft"
: "WallDiagonalCornerDownLeft",
// N + W, uses NW diagonal (bit 0)
0b1001 => (mask8 & (1 << 0)) != 0
? "WallInnerCornerDownRight"
: "WallDiagonalCornerUpLeft",
// N + E, uses NE diagonal (bit 6)
0b1100 => (mask8 & (1 << 6)) != 0
? "WallInnerCornerDownLeft"
: "WallDiagonalCornerUpRight",
_ => null
};
}
private sealed record TileBuildContext(
Dictionary<int, TileMapSheet> SheetsById,
List<TileDefinition> FloorCandidates,
@@ -984,50 +828,34 @@ public class DungeonGeneratorVNext
private static readonly Dictionary<string, string[]> WallBucketAliases = new(StringComparer.OrdinalIgnoreCase)
{
["WallTop"] = new[] { "walltop" },
["WallSideLeft"] = new[] { "wallsideleft" },
["WallSideRight"] = new[] { "wallsideright" },
["WallBottom"] = new[] { "wallbottom", "wallbottm" },
["WallInnerCornerDownLeft"] = new[] { "wallinnercornerdownleft" },
["WallInnerCornerDownRight"] = new[] { "wallinnercornerdownright" },
["WallDiagonalCornerDownLeft"] = new[] { "walldiagonalcornerdownleft" },
["WallDiagonalCornerDownRight"] = new[] { "walldiagonalcornerdownright" },
["WallDiagonalCornerUpLeft"] = new[] { "walldiagonalcornerupleft" },
["WallDiagonalCornerUpRight"] = new[] { "walldiagonalcornerupright" },
["WallFull"] = new[] { "wallfull" },
["WallFullEightDirections"] = new[] { "wallfulleightdirections" },
["WallBottomEightDirections"] = new[] { "wallbottmeightdirections", "wallbottomeightdirections" }
["Wall_N"] = new[] { "wall_n", "walln", "north", "wall_north", "walltop" },
["Wall_S"] = new[] { "wall_s", "walls", "south", "wall_south", "wallbottom", "wallbottm" },
["Wall_E"] = new[] { "wall_e", "walle", "east", "wall_east", "wallsideleft" },
["Wall_W"] = new[] { "wall_w", "wallw", "west", "wall_west", "wallsideright" },
["NWCornerLarge"] = new[] { "nwcornerlarge", "wall_nwcornerlarge", "wall_nwnne" },
["NECornerLarge"] = new[] { "necornerlarge", "wall_necornerlarge", "wall_nne" },
["SECornerLarge"] = new[] { "secornerlarge", "wall_secornerlarge" },
["SWCornerLarge"] = new[] { "swcornerlarge", "wall_swcornerlarge" },
["NWCornerSingle"] = new[] { "nwcornersingle", "wall_nwcornersingle" },
["NECornerSingle"] = new[] { "necornersingle", "wall_necornersingle" },
["SECornerSingle"] = new[] { "secornersingle", "wall_secornersingle" },
["SWCornerSingle"] = new[] { "swcornersingle", "wall_swcornersingle" }
};
private static readonly KeyValuePair<string, HashSet<int>>[] CardinalBucketMasks =
private static readonly KeyValuePair<string, HashSet<int>>[] DirectionalWallBucketMasks =
{
new("WallTop", new HashSet<int> { 15, 6, 3, 2, 10, 12, 14, 11, 7 }),
new("WallSideLeft", new HashSet<int> { 4 }),
new("WallSideRight", new HashSet<int> { 1 }),
new("WallBottom", new HashSet<int> { 8 }),
new("WallFull", new HashSet<int> { 13, 5, 9 })
};
private static readonly KeyValuePair<string, HashSet<int>>[] EightDirectionBucketMasks =
{
new("WallInnerCornerDownLeft", new HashSet<int> { 241, 224, 240, 225, 160, 81, 209, 97, 208, 113, 17, 177, 161, 144, 49, 176, 33, 145 }),
new("WallInnerCornerDownRight", new HashSet<int> { 199, 195, 131, 135, 130, 69, 197, 67, 133, 71, 68, 198, 194, 132, 70, 134, 196, 66 }),
new("WallDiagonalCornerDownLeft", new HashSet<int> { 64 }),
new("WallDiagonalCornerDownRight", new HashSet<int> { 1 }),
new("WallDiagonalCornerUpLeft", new HashSet<int> { 16, 80 }),
new("WallDiagonalCornerUpRight", new HashSet<int> { 4, 5 }),
new("WallBottomEightDirections", new HashSet<int> { 65 }),
new("WallFullEightDirections", new HashSet<int> { 20, 228, 147, 116, 23, 22, 52, 21, 84, 18, 36, 19, 100, 151, 244, 150, 180, 229, 211, 245, 215, 117, 87, 101, 83, 82, 37, 53, 86, 213, 212, 149 })
};
private static readonly HashSet<string> CornerPriorityEightBuckets = new(StringComparer.OrdinalIgnoreCase)
{
"WallInnerCornerDownLeft",
"WallInnerCornerDownRight",
"WallDiagonalCornerDownLeft",
"WallDiagonalCornerDownRight",
"WallDiagonalCornerUpLeft",
"WallDiagonalCornerUpRight"
new("Wall_N", new HashSet<int> { 128, 129, 192, 193 }),
new("Wall_S", new HashSet<int> { 8, 24, 12, 28 }),
new("Wall_E", new HashSet<int> { 32, 96, 48, 112 }),
new("Wall_W", new HashSet<int> { 2, 6, 3, 7 }),
new("NWCornerLarge", new HashSet<int> { 131 }),
new("NECornerLarge", new HashSet<int> { 224 }),
new("SECornerLarge", new HashSet<int> { 56 }),
new("SWCornerLarge", new HashSet<int> { 14 }),
new("NWCornerSingle", new HashSet<int> { 1 }),
new("NECornerSingle", new HashSet<int> { 64 }),
new("SECornerSingle", new HashSet<int> { 16 }),
new("SWCornerSingle", new HashSet<int> { 4 })
};
private static float Distance(int x1, int y1, int x2, int y2)