From 4d657bd4fb780cb86dcfd7f6edc631963a446a49 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 31 Dec 2025 11:06:06 -0500 Subject: [PATCH] Dungeon Building -Verbose logging on Floor tiles. --- Game/Dungeon/DungeonGeneratorV2.cs | 242 +++++++++++++++++++++++++---- 1 file changed, 215 insertions(+), 27 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index d132e68..f7b2b15 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -707,74 +707,262 @@ public class DungeonGeneratorV2 // } // } //} + //private void CarveBasicRoom(DungeonLayoutV2 layout, DungeonRoomV2 room) + //{ + // // 1) Decide which TileBase family to use for this room. + // // For now: default to family 1 unless the room tags explicitly choose another. + // // You can extend this to read "TileBaseFamily_2" etc. from room.TemplateTags. + // int familyId = 1; + + // // 2) Optional: choose a single "primary" tile for the room to reduce speckle. + // // We'll still enforce sockets per-tile; if the chosen primary doesn't fit a spot, + // // we'll fall back to the general picker for that cell. + // TileDefinition? roomPrimary = PickRandomFloorTile(layout, room.X, room.Y, familyId, varianceChance: 0.0); + + // for (int x = room.X; x < room.X + room.Width; x++) + // { + // for (int y = room.Y; y < room.Y + room.Height; y++) + // { + // if (x < 0 || x >= MapWidth || y < 0 || y >= MapHeight) + // continue; + + // // Always carve as walkable space. + // if (layout.Walkable != null) + // layout.Walkable[x, y] = true; + + // // Remove any previous visuals at this location (safety, consistent with corridors) + // layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y); + + // // 3) Pick a tile using: TileBaseCore/TileBase variance + hard socket requirements from neighbors (N/W scan order) + // // 85% primary, 15% variance, BUT constrained by required socket tags. + // TileDefinition? floorTile = null; + + // // Try room primary first most of the time to keep consistent "material" + // // (still must satisfy socket constraints for this cell). + // if (roomPrimary != null && SatisfiesSocketRequirements(layout, x, y, roomPrimary)) + // { + // // Small variance chance to break up repetition + // if (_rng.NextDouble() < 0.15) + // floorTile = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 1.0); // force variance pool + // else + // floorTile = roomPrimary; + // } + // else + // { + // floorTile = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 0.15); + // } + + // // Hard fallback: any floor + // floorTile ??= _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + + // int floorId = floorTile?.Id ?? -1; // -1 means no visual tile, just walkable + // layout.Tiles[x, y] = floorId > 0 ? floorId : 1; // 1 = walkable but no visual + + // if (floorId > 0 && floorTile != null) + // { + // layout.TileDetails.Add(new PlacedTile + // { + // X = x, + // Y = y, + // TileDefinitionId = floorId, + // SheetId = floorTile.TileMapSheetId, + // FrameIndex = floorTile.FrameIndex, + // Layer = 0 + // }); + // } + // } + // } + + // // Optional but recommended: repair pass to fix E/S mismatches after scan-order placement. + // RepairRoomFloorSockets(layout, room); + //} private void CarveBasicRoom(DungeonLayoutV2 layout, DungeonRoomV2 room) { - // 1) Decide which TileBase family to use for this room. - // For now: default to family 1 unless the room tags explicitly choose another. - // You can extend this to read "TileBaseFamily_2" etc. from room.TemplateTags. - int familyId = 1; + // Toggle this if you want to silence per-cell spam quickly. + const bool VerboseFloorLogging = true; - // 2) Optional: choose a single "primary" tile for the room to reduce speckle. - // We'll still enforce sockets per-tile; if the chosen primary doesn't fit a spot, - // we'll fall back to the general picker for that cell. + // 1) Decide which TileBase family to use for this room. + int familyId = 2; + + Console.WriteLine($"[DungeonGenV2][Floors] CarveBasicRoom START room=({room.X},{room.Y}) size=({room.Width}x{room.Height}) familyId={familyId} tags=[{string.Join(",", room.TemplateTags ?? new HashSet())}]"); + + // 2) Choose a primary tile for the room (varianceChance=0 => strongly prefers core tiles). + // NOTE: This does NOT mean every tile will be the same; it's just a "material anchor". TileDefinition? roomPrimary = PickRandomFloorTile(layout, room.X, room.Y, familyId, varianceChance: 0.0); + if (roomPrimary != null) + { + Console.WriteLine($"[DungeonGenV2][Floors] roomPrimary chosen id={roomPrimary.Id} name='{roomPrimary.Name}' sheet={roomPrimary.TileMapSheetId} frame={roomPrimary.FrameIndex} tags='{roomPrimary.Tags ?? ""}'"); + } + else + { + Console.WriteLine($"[DungeonGenV2][Floors] roomPrimary chosen = null (will rely on per-cell picking)"); + } + + int placedCount = 0; + int primaryCount = 0; + int varianceCount = 0; + int perCellPickCount = 0; + int primaryRejectedBySockets = 0; + int fallbackAnyFloorCount = 0; + int forcedVariancePickNullCount = 0; + int cellPickNullCount = 0; + for (int x = room.X; x < room.X + room.Width; x++) { for (int y = room.Y; y < room.Y + room.Height; y++) { if (x < 0 || x >= MapWidth || y < 0 || y >= MapHeight) + { + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] skip OOB ({x},{y})"); continue; + } // Always carve as walkable space. if (layout.Walkable != null) layout.Walkable[x, y] = true; - // Remove any previous visuals at this location (safety, consistent with corridors) + // Remove any previous visuals at this location layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y); - // 3) Pick a tile using: TileBaseCore/TileBase variance + hard socket requirements from neighbors (N/W scan order) - // 85% primary, 15% variance, BUT constrained by required socket tags. - TileDefinition? floorTile = null; - - // Try room primary first most of the time to keep consistent "material" - // (still must satisfy socket constraints for this cell). - if (roomPrimary != null && SatisfiesSocketRequirements(layout, x, y, roomPrimary)) + // Log neighborhood + required sockets (N/W scan-order) + var req = BuildRequiredSocketTags(layout, x, y); + if (VerboseFloorLogging) { - // Small variance chance to break up repetition - if (_rng.NextDouble() < 0.15) - floorTile = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 1.0); // force variance pool + int nId = (y > 0) ? layout.Tiles[x, y - 1] : -999; + int wId = (x > 0) ? layout.Tiles[x - 1, y] : -999; + + string nInfo = (y > 0 && _tileDefinitions.TryGetValue(nId, out var nd)) + ? $"N=({x},{y - 1}) id={nId} '{nd.Name}' walk={layout.IsWalkable(x, y - 1)}" + : $"N=({x},{y - 1}) id={nId} walk={(y > 0 ? layout.IsWalkable(x, y - 1) : false)}"; + + string wInfo = (x > 0 && _tileDefinitions.TryGetValue(wId, out var wd)) + ? $"W=({x - 1},{y}) id={wId} '{wd.Name}' walk={layout.IsWalkable(x - 1, y)}" + : $"W=({x - 1},{y}) id={wId} walk={(x > 0 ? layout.IsWalkable(x - 1, y) : false)}"; + + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) reqSockets=[{string.Join(",", req)}] {nInfo} {wInfo}"); + } + + TileDefinition? chosen = null; + bool usedPrimary = false; + bool usedVariance = false; + + // 3) 85% primary, 15% variance, constrained by required sockets. + // Important: The old behavior that "only uses one tile" happens if: + // - roomPrimary always fits and variance never succeeds (or isn't present) + // - or varianceChance logic never triggers/always returns same candidate list + // We'll log every decision so you can see why. + + bool primaryFits = roomPrimary != null && SatisfiesSocketRequirements(layout, x, y, roomPrimary); + + if (roomPrimary != null && !primaryFits) + primaryRejectedBySockets++; + + // Roll for variance (we roll first, then attempt primary/variance accordingly) + double roll = _rng.NextDouble(); + bool wantVariance = roll < 0.15; + + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) roll={roll:0.000} wantVariance={wantVariance} primaryFits={primaryFits}"); + + if (primaryFits) + { + // If we want variance, try to pick variance; if that fails, use primary. + if (wantVariance) + { + chosen = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 1.0); // force variance pool + if (chosen == null) + { + forcedVariancePickNullCount++; + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) forced variance returned NULL -> using roomPrimary"); + chosen = roomPrimary; + usedPrimary = true; + } + else + { + usedVariance = true; + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) picked VARIANCE id={chosen.Id} '{chosen.Name}' tags='{chosen.Tags ?? ""}'"); + } + } else - floorTile = roomPrimary; + { + chosen = roomPrimary; + usedPrimary = true; + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) used PRIMARY id={chosen.Id} '{chosen.Name}'"); + } } else { - floorTile = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 0.15); + // Primary doesn't fit or doesn't exist: do full per-cell pick (85/15 inside the picker) + chosen = PickRandomFloorTile(layout, x, y, familyId, varianceChance: 0.15); + perCellPickCount++; + + if (chosen == null) + { + cellPickNullCount++; + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) per-cell pick returned NULL"); + } + else + { + // classify + if (_tileTags.TryGetValue(chosen.Id, out var tags)) + { + if (tags.Contains($"TileBaseCore_{familyId}")) usedPrimary = true; + else if (tags.Contains($"TileBase_{familyId}")) usedVariance = true; + } + + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) per-cell picked id={chosen.Id} '{chosen.Name}' tags='{chosen.Tags ?? ""}'"); + } } // Hard fallback: any floor - floorTile ??= _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + if (chosen == null) + { + fallbackAnyFloorCount++; + chosen = _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor); + if (VerboseFloorLogging) + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) HARD FALLBACK any-floor => {(chosen != null ? $"{chosen.Id} '{chosen.Name}'" : "NULL")}"); + } - int floorId = floorTile?.Id ?? -1; // -1 means no visual tile, just walkable - layout.Tiles[x, y] = floorId > 0 ? floorId : 1; // 1 = walkable but no visual + int floorId = chosen?.Id ?? -1; + layout.Tiles[x, y] = floorId > 0 ? floorId : 1; - if (floorId > 0 && floorTile != null) + if (floorId > 0 && chosen != null) { layout.TileDetails.Add(new PlacedTile { X = x, Y = y, TileDefinitionId = floorId, - SheetId = floorTile.TileMapSheetId, - FrameIndex = floorTile.FrameIndex, + SheetId = chosen.TileMapSheetId, + FrameIndex = chosen.FrameIndex, Layer = 0 }); + placedCount++; + + if (usedPrimary) primaryCount++; + if (usedVariance) varianceCount++; + } + + if (VerboseFloorLogging && chosen != null) + { + Console.WriteLine($"[DungeonGenV2][Floors] cell=({x},{y}) FINAL id={chosen.Id} '{chosen.Name}' primary={usedPrimary} variance={usedVariance}"); } } } - // Optional but recommended: repair pass to fix E/S mismatches after scan-order placement. + Console.WriteLine($"[DungeonGenV2][Floors] CarveBasicRoom END placed={placedCount} primary={primaryCount} variance={varianceCount} perCellPick={perCellPickCount} primaryRejectedBySockets={primaryRejectedBySockets} forcedVarNull={forcedVariancePickNullCount} perCellNull={cellPickNullCount} hardFallbackAnyFloor={fallbackAnyFloorCount}"); + + // Repair pass logs (you can also add verbosity inside RepairRoomFloorSockets if needed) + Console.WriteLine($"[DungeonGenV2][Floors] RepairRoomFloorSockets START room=({room.X},{room.Y}) size=({room.Width}x{room.Height})"); RepairRoomFloorSockets(layout, room); + Console.WriteLine($"[DungeonGenV2][Floors] RepairRoomFloorSockets END room=({room.X},{room.Y})"); } ///