diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index 440fb7a..877826c 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -1088,7 +1088,7 @@ public class DungeonGeneratorV2 int familyId = _activeFamilyId; - // Pick based on start position (or midpoint — either is fine) + // Pick based on start position (or midpoint - either is fine) var floorTile = PickRandomFloorTile(layout, startX, startY, familyId, varianceChance: 0.15) ?? _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); @@ -1749,24 +1749,26 @@ public class DungeonGeneratorV2 } private TileDefinition? PickRandomFloorTile( - DungeonLayoutV2 layout, - int x, int y, - int familyId, - double varianceChance = 0.25) + DungeonLayoutV2 layout, + int x, int y, + int familyId, + double varianceChance = 0.25) { var requiredSocketTags = BuildRequiredSocketTags(layout, x, y); + bool requiresTyped = requiredSocketTags.Any(t => t.Contains('=')); + string primaryTag = $"TileBaseCore_{familyId}"; string varianceTag = $"TileBase_{familyId}"; - var primary = _tileDefinitions.Values + List primary = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, primaryTag)) .Where(t => HasAllTags(t, requiredSocketTags)) .Where(t => CompatibleWithPlacedNeighbors(layout, x, y, t)) .ToList(); - var variance = _tileDefinitions.Values + List variance = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, varianceTag)) .Where(t => HasAllTags(t, requiredSocketTags)) @@ -1775,7 +1777,11 @@ public class DungeonGeneratorV2 if (primary.Count == 0 && variance.Count == 0) { - // Relax socket-tag requirements, but DO NOT relax compatibility. + // If we require typed sockets and can't satisfy them, DO NOT relax. + if (requiresTyped) + return null; + + // Otherwise, relax only the socket-tag requirement but keep compatibility. primary = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, primaryTag)) @@ -1789,17 +1795,17 @@ public class DungeonGeneratorV2 .ToList(); } - if (primary.Count == 0 && variance.Count == 0) - return _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + return null; bool pickVar = _rng.NextDouble() < varianceChance; var pool = pickVar ? variance : primary; if (pool.Count == 0) pool = pickVar ? primary : variance; - return pool.Count == 0 ? null : pool[_rng.Next(pool.Count)]; + return pool[_rng.Next(pool.Count)]; } + private bool CompatibleWithPlacedNeighbors(DungeonLayoutV2 layout, int x, int y, TileDefinition t) { // North neighbor @@ -1926,44 +1932,87 @@ public class DungeonGeneratorV2 _activeFamilyId = families.Count > 0 ? families.ElementAt(_rng.Next(families.Count)) : 1; Console.WriteLine($"[DungeonGenV2] Chosen dungeon tile familyId={_activeFamilyId}"); } + //private bool EdgeSocketsCompatible(int aId, string aEdge, int bId, string bEdge) + //{ + // _tileSocketTypes.TryGetValue(aId, out var aMap); + // _tileSocketTypes.TryGetValue(bId, out var bMap); + + // aMap ??= new Dictionary>(); + // bMap ??= new Dictionary>(); + + // aMap.TryGetValue(aEdge, out var aTypes); + // bMap.TryGetValue(bEdge, out var bTypes); + + // aTypes ??= new HashSet(); + // bTypes ??= new HashSet(); + + // bool aHas = aTypes.Count > 0; + // bool bHas = bTypes.Count > 0; + + // if (!aHas && !bHas) return true; // neither cares + // if (aHas != bHas) return false; // one cares, other doesn't + + // // both have sockets + // bool aTyped = aTypes.Any(t => !string.IsNullOrEmpty(t)); + // bool bTyped = bTypes.Any(t => !string.IsNullOrEmpty(t)); + + // if (!aTyped && !bTyped) + // return true; // both untyped + + // var aTypedSet = aTypes + // .Where(t => !string.IsNullOrEmpty(t)) + // .ToHashSet(StringComparer.OrdinalIgnoreCase); + + // var bTypedSet = bTypes + // .Where(t => !string.IsNullOrEmpty(t)) + // .ToHashSet(StringComparer.OrdinalIgnoreCase); + + // return aTypedSet.Overlaps(bTypedSet); + //} private bool EdgeSocketsCompatible(int aId, string aEdge, int bId, string bEdge) { - _tileSocketTypes.TryGetValue(aId, out var aMap); - _tileSocketTypes.TryGetValue(bId, out var bMap); - - aMap ??= new Dictionary>(); - bMap ??= new Dictionary>(); + if (!_tileSocketTypes.TryGetValue(aId, out var aMap)) return true; // no sockets at all? treat as open? + if (!_tileSocketTypes.TryGetValue(bId, out var bMap)) return true; + // Get socket types for each edge (default to empty if missing) aMap.TryGetValue(aEdge, out var aTypes); bMap.TryGetValue(bEdge, out var bTypes); aTypes ??= new HashSet(); bTypes ??= new HashSet(); - bool aHas = aTypes.Count > 0; - bool bHas = bTypes.Count > 0; + // If either side has no declared socket on this edge - treat as closed + bool aMissing = aTypes.Count == 0 && !aMap.ContainsKey(aEdge); // truly missing key + bool bMissing = bTypes.Count == 0 && !bMap.ContainsKey(bEdge); - if (!aHas && !bHas) return true; // neither cares - if (aHas != bHas) return false; // one cares, other doesn't + // But if they explicitly have "Closed", it's typed + bool aClosed = aTypes.Contains("Closed"); + bool bClosed = bTypes.Contains("Closed"); - // both have sockets - bool aTyped = aTypes.Any(t => !string.IsNullOrEmpty(t)); - bool bTyped = bTypes.Any(t => !string.IsNullOrEmpty(t)); + // Case 1: Both explicitly closed - compatible (shared wall seam) + if (aClosed && bClosed) return true; - if (!aTyped && !bTyped) - return true; // both untyped + // Case 2: One closed, one not - incompatible + if (aClosed || bClosed) return false; - var aTypedSet = aTypes - .Where(t => !string.IsNullOrEmpty(t)) - .ToHashSet(StringComparer.OrdinalIgnoreCase); + // Case 3: One or both missing (no socket declared) - treat as closed + if (aMissing || bMissing) return false; - var bTypedSet = bTypes - .Where(t => !string.IsNullOrEmpty(t)) - .ToHashSet(StringComparer.OrdinalIgnoreCase); + // Case 4: Both have sockets (possibly untyped or typed) + bool aTyped = aTypes.Any(t => !string.IsNullOrEmpty(t) && t != "Closed"); + bool bTyped = bTypes.Any(t => !string.IsNullOrEmpty(t) && t != "Closed"); - return aTypedSet.Overlaps(bTypedSet); + // If either is typed, require matching type + if (aTyped || bTyped) + { + var aTypedSet = aTypes.Where(t => !string.IsNullOrEmpty(t) && t != "Closed").ToHashSet(StringComparer.OrdinalIgnoreCase); + var bTypedSet = bTypes.Where(t => !string.IsNullOrEmpty(t) && t != "Closed").ToHashSet(StringComparer.OrdinalIgnoreCase); + return aTypedSet.Overlaps(bTypedSet); + } + + // Both untyped - always compatible + return true; } - } ///