From 438c135ad8ade4a37836e88288c41d3aa662f042 Mon Sep 17 00:00:00 2001 From: dingo Date: Fri, 2 Jan 2026 10:49:46 -0500 Subject: [PATCH] Tile fixes2 --- Game/Dungeon/DungeonGeneratorV2.cs | 126 +++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 7 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index e037c85..6a48efd 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -633,6 +633,12 @@ public class DungeonGeneratorV2 string primaryTag = $"TileBaseCore_{familyId}"; string varianceTag = $"TileBase_{familyId}"; + // Debug: Log what we're looking for + if (required.Count > 0) + { + Console.WriteLine($"[Socket] ({x},{y}) Required sockets: {string.Join(", ", required.Select(kv => $"{kv.Key}={kv.Value}"))}"); + } + var primary = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, primaryTag)) @@ -645,6 +651,20 @@ public class DungeonGeneratorV2 .Where(t => SatisfiesSocketRequirementsStrict(t.Id, required)) .ToList(); + // Debug: Log pool sizes + if (required.Count > 0) + { + Console.WriteLine($"[Socket] ({x},{y}) Primary pool: {primary.Count}, Variance pool: {variance.Count}"); + if (variance.Count > 0 && variance.Count <= 5) + { + foreach (var v in variance) + { + var sockets = _tileSocketsStrict.GetValueOrDefault(v.Id); + Console.WriteLine($"[Socket] Variance tile {v.Id} frame={v.FrameIndex}: N={sockets.GetSocketId("N")}, E={sockets.GetSocketId("E")}, S={sockets.GetSocketId("S")}, W={sockets.GetSocketId("W")}"); + } + } + } + if (primary.Count == 0 && variance.Count == 0) { if (required.Count > 0) @@ -658,7 +678,16 @@ public class DungeonGeneratorV2 var pool = pickVariance ? variance : primary; if (pool.Count == 0) pool = pickVariance ? primary : variance; - return pool[_rng.Next(pool.Count)]; + var chosen = pool[_rng.Next(pool.Count)]; + + // Debug: Log what we picked + if (required.Count > 0) + { + var chosenSockets = _tileSocketsStrict.GetValueOrDefault(chosen.Id); + Console.WriteLine($"[Socket] ({x},{y}) PICKED tile {chosen.Id} frame={chosen.FrameIndex}: N={chosenSockets.GetSocketId("N")}, E={chosenSockets.GetSocketId("E")}, S={chosenSockets.GetSocketId("S")}, W={chosenSockets.GetSocketId("W")}"); + } + + return chosen; } /// @@ -963,10 +992,22 @@ public class DungeonGeneratorV2 } } - // Hard fallback + // Hard fallback - prefer all-Open tiles to avoid socket conflicts if (chosen == null) { - chosen = _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + chosen = _tileDefinitions.Values + .Where(t => t.Type == TileType.Floor) + .Where(t => { + if (!_tileSocketsStrict.TryGetValue(t.Id, out var sockets)) return false; + return sockets.IsAllOpen; + }) + .FirstOrDefault(); + + // Last resort - any floor tile + chosen ??= _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + + if (chosen != null) + Console.WriteLine($"[Socket] ({x},{y}) HARD FALLBACK to tile {chosen.Id}"); } int floorId = chosen?.Id ?? -1; @@ -1009,6 +1050,8 @@ public class DungeonGeneratorV2 private void RepairRoomFloorSockets(DungeonLayoutV2 layout, DungeonRoomV2 room) { int familyId = _activeFamilyId; + int repairCount = 0; + int checkCount = 0; for (int x = room.X; x < room.X + room.Width; x++) { @@ -1025,6 +1068,7 @@ public class DungeonGeneratorV2 if (!_tileDefinitions.TryGetValue(id, out var cur)) continue; + checkCount++; TileDefinition? replacement = null; if (_useStrictSockets) @@ -1032,7 +1076,45 @@ public class DungeonGeneratorV2 // Use strict socket checking for all 4 directions if (!IsCompatibleWithNeighborsStrict(layout, x, y, id)) { - replacement = PickRandomFloorTileStrict(layout, x, y, familyId, varianceChance: 0.25); + var curSockets = _tileSocketsStrict.GetValueOrDefault(id); + Console.WriteLine($"[Socket] REPAIR ({x},{y}) tile {id} frame={cur.FrameIndex} incompatible: N={curSockets.GetSocketId("N")}, E={curSockets.GetSocketId("E")}, S={curSockets.GetSocketId("S")}, W={curSockets.GetSocketId("W")}"); + + // Log what neighbors require + if (y > 0 && HasPlacedTile(layout, x, y - 1)) + { + var nSockets = _tileSocketsStrict.GetValueOrDefault(layout.Tiles[x, y - 1]); + Console.WriteLine($"[Socket] North neighbor {layout.Tiles[x, y - 1]} has S={nSockets.GetSocketId("S")}"); + } + if (x > 0 && HasPlacedTile(layout, x - 1, y)) + { + var wSockets = _tileSocketsStrict.GetValueOrDefault(layout.Tiles[x - 1, y]); + Console.WriteLine($"[Socket] West neighbor {layout.Tiles[x - 1, y]} has E={wSockets.GetSocketId("E")}"); + } + if (y < MapHeight - 1 && HasPlacedTile(layout, x, y + 1)) + { + var sSockets = _tileSocketsStrict.GetValueOrDefault(layout.Tiles[x, y + 1]); + Console.WriteLine($"[Socket] South neighbor {layout.Tiles[x, y + 1]} has N={sSockets.GetSocketId("N")}"); + } + if (x < MapWidth - 1 && HasPlacedTile(layout, x + 1, y)) + { + var eSockets = _tileSocketsStrict.GetValueOrDefault(layout.Tiles[x + 1, y]); + Console.WriteLine($"[Socket] East neighbor {layout.Tiles[x + 1, y]} has W={eSockets.GetSocketId("W")}"); + } + + replacement = PickRandomFloorTileStrict(layout, x, y, familyId, varianceChance: 0.0); + + if (replacement == null) + { + Console.WriteLine($"[Socket] No replacement found! Trying all-Open fallback..."); + replacement = _tileDefinitions.Values + .Where(t => t.Type == TileType.Floor) + .Where(t => HasTag(t, $"TileBaseCore_{familyId}") || HasTag(t, $"TileBase_{familyId}")) + .Where(t => { + if (!_tileSocketsStrict.TryGetValue(t.Id, out var sockets)) return false; + return sockets.IsAllOpen; + }) + .FirstOrDefault(); + } } } else @@ -1048,6 +1130,9 @@ public class DungeonGeneratorV2 if (replacement == null || replacement.Id == cur.Id) continue; + Console.WriteLine($"[Socket] REPAIR ({x},{y}) replacing {cur.Id} with {replacement.Id}"); + repairCount++; + layout.Tiles[x, y] = replacement.Id; if (layout.Walkable != null) @@ -1065,6 +1150,8 @@ public class DungeonGeneratorV2 }); } } + + Console.WriteLine($"[Socket] RepairRoomFloorSockets: checked {checkCount} tiles, repaired {repairCount}"); } private List BuildRequiredSocketTags_AllDirs(DungeonLayoutV2 layout, int x, int y) @@ -1230,10 +1317,35 @@ public class DungeonGeneratorV2 int familyId = _activeFamilyId; - var floorTile = _useStrictSockets - ? PickRandomFloorTileStrict(layout, startX, startY, familyId, varianceChance: 0.15) - : PickRandomFloorTile(layout, startX, startY, familyId, varianceChance: 0.15); + // Corridors must use all-Open tiles to connect properly with any room tile + TileDefinition? floorTile = null; + if (_useStrictSockets) + { + // For corridors, we need tiles with ALL sockets Open so they can connect to anything + floorTile = _tileDefinitions.Values + .Where(t => t.Type == TileType.Floor) + .Where(t => HasTag(t, $"TileBaseCore_{familyId}") || HasTag(t, $"TileBase_{familyId}")) + .Where(t => { + if (!_tileSocketsStrict.TryGetValue(t.Id, out var sockets)) return false; + return sockets.IsAllOpen; + }) + .OrderBy(_ => _rng.Next()) + .FirstOrDefault(); + + Console.WriteLine($"[DungeonGenV2][Corridors] Found all-Open tile: {floorTile?.Id} '{floorTile?.Name}'"); + } + + // Fallback to any floor tile with all-Open sockets + floorTile ??= _tileDefinitions.Values + .Where(t => t.Type == TileType.Floor) + .Where(t => { + if (!_tileSocketsStrict.TryGetValue(t.Id, out var sockets)) return false; + return sockets.IsAllOpen; + }) + .FirstOrDefault(); + + // Last resort fallback floorTile ??= _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); int floorId = floorTile?.Id ?? 1;