Super Logging on Dungeon Generator V3
This commit is contained in:
@@ -11,11 +11,45 @@ namespace Server.Game.Dungeon;
|
||||
/// </summary>
|
||||
public class DungeonGeneratorV3
|
||||
{
|
||||
// ============================================================
|
||||
// CONFIGURATION - Set these to control logging verbosity
|
||||
// ============================================================
|
||||
|
||||
/// <summary>
|
||||
/// Enable verbose logging to trace tile selection decisions.
|
||||
/// Set to true to debug tile/template selection issues.
|
||||
/// </summary>
|
||||
public static bool VerboseLogging { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Log every single tile placement (WARNING: extremely verbose for large dungeons)
|
||||
/// </summary>
|
||||
public static bool LogEveryTile { get; set; } = true;
|
||||
|
||||
// ============================================================
|
||||
// DEPENDENCIES
|
||||
// ============================================================
|
||||
|
||||
private readonly GameDbContext _db;
|
||||
private readonly Random _rng;
|
||||
private readonly int _seed;
|
||||
private readonly int _tier;
|
||||
|
||||
/// <summary>
|
||||
/// JSON options for case-insensitive deserialization.
|
||||
/// JavaScript saves with lowercase: {"sheetId":3,"frameIndex":273}
|
||||
/// C# classes use PascalCase: SheetId, FrameIndex
|
||||
/// Without this option, all values deserialize to 0!
|
||||
/// </summary>
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
// ============================================================
|
||||
// PALETTE & TEMPLATES
|
||||
// ============================================================
|
||||
|
||||
// Palette-based configuration
|
||||
private DungeonPalette? _palette;
|
||||
private List<TileTemplate> _floorTemplates = new();
|
||||
@@ -72,7 +106,7 @@ public class DungeonGeneratorV3
|
||||
/// </summary>
|
||||
public async Task<DungeonLayoutV2> GenerateAsync(int? paletteId = null, string? biome = null)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Starting generation: seed={_seed}, tier={_tier}, palette={paletteId}, biome={biome}");
|
||||
Log($" Starting generation: seed={_seed}, tier={_tier}, palette={paletteId}, biome={biome}");
|
||||
|
||||
// Load palette and templates (auto-selects if no palette/biome specified)
|
||||
await LoadPaletteAsync(paletteId, biome);
|
||||
@@ -127,20 +161,35 @@ public class DungeonGeneratorV3
|
||||
});
|
||||
}
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Generation complete: {rooms.Count} rooms, {layout.TileDetails.Count} tiles, {layout.Objects.Count} objects, {layout.TileMapSheets.Count} sheets");
|
||||
Log($" Generation complete: {rooms.Count} rooms, {layout.TileDetails.Count} tiles, {layout.Objects.Count} objects, {layout.TileMapSheets.Count} sheets");
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load a dungeon palette and its associated templates.
|
||||
///
|
||||
/// Priority order:
|
||||
/// 1. Specific palette by ID (if provided)
|
||||
/// 2. Random palette matching biome (if provided)
|
||||
/// 3. Random palette from all enabled palettes
|
||||
/// 4. Fall back to loading all enabled templates (no palette)
|
||||
/// </summary>
|
||||
private async Task LoadPaletteAsync(int? paletteId, string? biome)
|
||||
{
|
||||
Log($"--- Loading Palette ---");
|
||||
|
||||
// Try to load specific palette by ID
|
||||
if (paletteId.HasValue)
|
||||
{
|
||||
_palette = await _db.DungeonPalettes.FindAsync(paletteId.Value);
|
||||
if (_palette != null)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Using specified palette: {_palette.Name}");
|
||||
Log($"Loaded palette by ID: {_palette.Id} - '{_palette.Name}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"WARNING: Palette ID {paletteId} not found!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,11 +200,13 @@ public class DungeonGeneratorV3
|
||||
.Where(p => p.IsEnabled && p.Biome == biome)
|
||||
.ToListAsync();
|
||||
|
||||
Log($"Found {biomePalettes.Count} palettes for biome '{biome}'");
|
||||
|
||||
if (biomePalettes.Count > 0)
|
||||
{
|
||||
// Randomly select from matching biome palettes
|
||||
_palette = biomePalettes[_rng.Next(biomePalettes.Count)];
|
||||
Console.WriteLine($"[DungeonGenV3] Randomly selected palette '{_palette.Name}' from biome '{biome}'");
|
||||
Log($"Randomly selected palette: {_palette.Id} - '{_palette.Name}'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,23 +217,35 @@ public class DungeonGeneratorV3
|
||||
.Where(p => p.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Found {allPalettes.Count} enabled palettes");
|
||||
Log($"Found {allPalettes.Count} total enabled palettes");
|
||||
|
||||
// List all available palettes for debugging
|
||||
foreach (var p in allPalettes)
|
||||
{
|
||||
LogVerbose($" - Palette {p.Id}: '{p.Name}' (Biome: {p.Biome ?? "none"}, Enabled: {p.IsEnabled})");
|
||||
}
|
||||
|
||||
if (allPalettes.Count > 0)
|
||||
{
|
||||
// Randomly select any enabled palette
|
||||
_palette = allPalettes[_rng.Next(allPalettes.Count)];
|
||||
Console.WriteLine($"[DungeonGenV3] Randomly selected palette: {_palette.Name} (biome: {_palette.Biome ?? "none"})");
|
||||
Log($"Randomly selected palette: {_palette.Id} - '{_palette.Name}' (Biome: {_palette.Biome ?? "none"})");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] WARNING: No enabled palettes found!");
|
||||
Log($"WARNING: No enabled palettes found in database!");
|
||||
}
|
||||
}
|
||||
|
||||
if (_palette != null)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Using palette: {_palette.Name}");
|
||||
Log($"--- Applying Palette Settings ---");
|
||||
Log($"Palette: '{_palette.Name}'");
|
||||
Log($" Biome: {_palette.Biome ?? "none"}");
|
||||
Log($" Rooms: {_palette.MinRooms}-{_palette.MaxRooms}");
|
||||
Log($" Room Size: {_palette.MinRoomWidth}-{_palette.MaxRoomWidth} x {_palette.MinRoomHeight}-{_palette.MaxRoomHeight}");
|
||||
Log($" Corridor Width: {_palette.MinCorridorWidth}-{_palette.MaxCorridorWidth}");
|
||||
Log($" Densities: Decor={_palette.DecorationDensity}%, Trap={_palette.TrapDensity}%, Container={_palette.ContainerDensity}%");
|
||||
|
||||
// Apply palette generation parameters
|
||||
_minRooms = _palette.MinRooms;
|
||||
@@ -201,15 +264,25 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] No palette found, loading all enabled templates");
|
||||
Log($"No palette found - loading ALL enabled templates");
|
||||
await LoadAllTemplatesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load templates specified in the palette's JSON arrays.
|
||||
///
|
||||
/// Palette stores template IDs as JSON arrays:
|
||||
/// - FloorTemplateIdsJson: [1, 2, 3]
|
||||
/// - WallTemplateIdsJson: [4, 5]
|
||||
/// - RoomTemplateIdsJson: [10, 11]
|
||||
/// </summary>
|
||||
private async Task LoadTemplatesFromPaletteAsync()
|
||||
{
|
||||
if (_palette == null) return;
|
||||
|
||||
Log($"--- Loading Templates from Palette ---");
|
||||
|
||||
// Parse template ID arrays
|
||||
var floorIds = ParseIds(_palette.FloorTemplateIdsJson);
|
||||
var wallIds = ParseIds(_palette.WallTemplateIdsJson);
|
||||
@@ -219,6 +292,15 @@ public class DungeonGeneratorV3
|
||||
var trapIds = ParseIds(_palette.TrapIdsJson);
|
||||
var roomIds = ParseIds(_palette.RoomTemplateIdsJson);
|
||||
|
||||
Log($"Palette template IDs:");
|
||||
Log($" Floor IDs: [{string.Join(", ", floorIds)}]");
|
||||
Log($" Wall IDs: [{string.Join(", ", wallIds)}]");
|
||||
Log($" Door IDs: [{string.Join(", ", doorIds)}]");
|
||||
Log($" Decoration IDs: [{string.Join(", ", decorIds)}]");
|
||||
Log($" Container IDs: [{string.Join(", ", containerIds)}]");
|
||||
Log($" Trap IDs: [{string.Join(", ", trapIds)}]");
|
||||
Log($" Room IDs: [{string.Join(", ", roomIds)}]");
|
||||
|
||||
// Load tile templates
|
||||
if (floorIds.Any())
|
||||
{
|
||||
@@ -226,6 +308,7 @@ public class DungeonGeneratorV3
|
||||
.Where(t => floorIds.Contains(t.Id) && t.IsEnabled)
|
||||
.Where(t => t.MinTier <= _tier && (t.MaxTier == 0 || t.MaxTier >= _tier))
|
||||
.ToListAsync();
|
||||
Log($"Loaded {_floorTemplates.Count} floor templates");
|
||||
}
|
||||
|
||||
if (wallIds.Any())
|
||||
@@ -234,6 +317,7 @@ public class DungeonGeneratorV3
|
||||
.Where(t => wallIds.Contains(t.Id) && t.IsEnabled)
|
||||
.Where(t => t.MinTier <= _tier && (t.MaxTier == 0 || t.MaxTier >= _tier))
|
||||
.ToListAsync();
|
||||
Log($"Loaded {_wallTemplates.Count} wall templates");
|
||||
}
|
||||
|
||||
if (doorIds.Any())
|
||||
@@ -274,11 +358,11 @@ public class DungeonGeneratorV3
|
||||
.Where(r => roomIds.Contains(r.Id) && r.IsEnabled)
|
||||
.Where(r => r.MinTier <= _tier && (r.MaxTier == 0 || r.MaxTier >= _tier))
|
||||
.ToListAsync();
|
||||
Console.WriteLine($"[DungeonGenV3] Loaded {_roomTemplates.Count} room templates from palette IDs: [{string.Join(",", roomIds)}]");
|
||||
Log($"Loaded {_roomTemplates.Count} room templates from palette IDs: [{string.Join(",", roomIds)}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Palette has no room template IDs configured");
|
||||
Log($" Palette has no room template IDs configured");
|
||||
}
|
||||
|
||||
CategorizeRoomTemplates();
|
||||
@@ -351,20 +435,32 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Log summary of loaded templates</summary>
|
||||
private void LogTemplateStats()
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Loaded templates:");
|
||||
Console.WriteLine($" - Floor: {_floorTemplates.Count}");
|
||||
Console.WriteLine($" - Wall: {_wallTemplates.Count}");
|
||||
Console.WriteLine($" - Door: {_doorTemplates.Count}");
|
||||
Console.WriteLine($" - Decoration: {_decorationTemplates.Count}");
|
||||
Console.WriteLine($" - Container: {_containerTemplates.Count}");
|
||||
Console.WriteLine($" - Trap: {_trapTemplates.Count}");
|
||||
Console.WriteLine($" - Rooms: {_roomTemplates.Count} (Entrance:{_entranceRooms.Count}, Exit:{_exitRooms.Count}, Boss:{_bossRooms.Count}, Treasure:{_treasureRooms.Count}, Normal:{_normalRooms.Count})");
|
||||
Log($"--- Template Summary ---");
|
||||
Log($" Floor templates: {_floorTemplates.Count}");
|
||||
Log($" Wall templates: {_wallTemplates.Count}");
|
||||
Log($" Door templates: {_doorTemplates.Count}");
|
||||
Log($" Decoration templates: {_decorationTemplates.Count}");
|
||||
Log($" Container templates: {_containerTemplates.Count}");
|
||||
Log($" Trap templates: {_trapTemplates.Count}");
|
||||
Log($" Room templates: {_roomTemplates.Count}");
|
||||
Log($" - Entrance: {_entranceRooms.Count}");
|
||||
Log($" - Exit: {_exitRooms.Count}");
|
||||
Log($" - Boss: {_bossRooms.Count}");
|
||||
Log($" - Treasure: {_treasureRooms.Count}");
|
||||
Log($" - Normal: {_normalRooms.Count}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load all tile sheets and tile definitions from database.
|
||||
/// Builds lookup dictionaries for fast tile resolution.
|
||||
/// </summary>
|
||||
private async Task LoadTileDataAsync()
|
||||
{
|
||||
Log($"--- Loading Tile Data ---");
|
||||
|
||||
// Load all tile sheets with their tiles
|
||||
var sheets = await _db.TileMapSheets
|
||||
.Include(s => s.Tiles)
|
||||
@@ -374,17 +470,26 @@ public class DungeonGeneratorV3
|
||||
foreach (var sheet in sheets)
|
||||
{
|
||||
_tileSheets[sheet.Id] = sheet;
|
||||
Log($"Sheet {sheet.Id}: '{sheet.Name}' ({sheet.SheetWidth}x{sheet.SheetHeight}, TileSize: {sheet.TileWidth}x{sheet.TileHeight})");
|
||||
|
||||
if (sheet.Tiles != null)
|
||||
{
|
||||
foreach (var tile in sheet.Tiles)
|
||||
{
|
||||
tile.TileMapSheet = sheet;
|
||||
_tileDefinitions[tile.Id] = tile;
|
||||
LogVerbose($" - TileDef {tile.Id}: Frame={tile.FrameIndex}, Walkable={tile.IsWalkable}, Name='{tile.Name}'");
|
||||
}
|
||||
Log($" -> {sheet.Tiles.Count} tile definitions");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Loaded {_tileDefinitions.Count} tile definitions from {sheets.Count} sheets");
|
||||
Log($"Total: {_tileDefinitions.Count} tile definitions from {sheets.Count} sheets");
|
||||
|
||||
// Log walkable vs non-walkable summary
|
||||
var walkable = _tileDefinitions.Values.Count(t => t.IsWalkable);
|
||||
var nonWalkable = _tileDefinitions.Values.Count(t => !t.IsWalkable);
|
||||
Log($" Walkable: {walkable}, Non-walkable: {nonWalkable}");
|
||||
}
|
||||
|
||||
private async Task<List<DungeonRoomV2>> GenerateRoomsAsync(DungeonLayoutV2 layout)
|
||||
@@ -392,7 +497,7 @@ public class DungeonGeneratorV3
|
||||
var rooms = new List<DungeonRoomV2>();
|
||||
int targetRooms = _rng.Next(_minRooms, _maxRooms + 1);
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Generating {targetRooms} rooms");
|
||||
Log($"--- Generating {targetRooms} Rooms ---");
|
||||
|
||||
// Place entrance room first
|
||||
var entrance = await PlaceRoomAsync(layout, rooms, RoomType.Entrance, MapWidth / 4, MapHeight / 2);
|
||||
@@ -538,7 +643,7 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
private async Task PlaceObjectsAsync(DungeonLayoutV2 layout, List<DungeonRoomV2> rooms)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Placing objects (decor:{_decorationDensity}%, trap:{_trapDensity}%, container:{_containerDensity}%)");
|
||||
Log($" Placing objects (decor:{_decorationDensity}%, trap:{_trapDensity}%, container:{_containerDensity}%)");
|
||||
|
||||
foreach (var room in rooms)
|
||||
{
|
||||
@@ -736,7 +841,7 @@ public class DungeonGeneratorV3
|
||||
|
||||
layout.TotalMobs = layout.MobSpawns.Count;
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Spawn points: Player=({layout.PlayerSpawnX},{layout.PlayerSpawnY}), Exit=({layout.ExitX},{layout.ExitY}), Mobs={layout.MobSpawns.Count}");
|
||||
Log($" Spawn points: Player=({layout.PlayerSpawnX},{layout.PlayerSpawnY}), Exit=({layout.ExitX},{layout.ExitY}), Mobs={layout.MobSpawns.Count}");
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
@@ -789,7 +894,7 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
private async Task StampRoomTemplateAsync(DungeonLayoutV2 layout, DungeonRoomV2 room, BuilderRoomTemplate template)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Stamping room template '{template.Name}' at ({room.X},{room.Y})");
|
||||
Log($" Stamping room template '{template.Name}' at ({room.X},{room.Y})");
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
@@ -847,7 +952,7 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Error parsing TileGridJson: {ex.Message}");
|
||||
Log($" Error parsing TileGridJson: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -934,7 +1039,7 @@ public class DungeonGeneratorV3
|
||||
|
||||
private void CarveProceduralRoom(DungeonLayoutV2 layout, DungeonRoomV2 room)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Carving procedural room at ({room.X},{room.Y}) size {room.Width}x{room.Height}");
|
||||
Log($" Carving procedural room at ({room.X},{room.Y}) size {room.Width}x{room.Height}");
|
||||
|
||||
// Carve floor
|
||||
for (int y = room.Y; y < room.Y + room.Height; y++)
|
||||
@@ -1067,22 +1172,27 @@ public class DungeonGeneratorV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill all walkable cells with floor tiles.
|
||||
///
|
||||
/// For each walkable cell without a tile:
|
||||
/// 1. Call SelectFloorTile to get appropriate tile
|
||||
/// 2. Set layout.Tiles and add to TileDetails
|
||||
/// </summary>
|
||||
private void FillFloorTiles(DungeonLayoutV2 layout)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Filling floor tiles with {_floorTemplates.Count} templates");
|
||||
Log($"--- Filling Floor Tiles ---");
|
||||
Log($"Floor templates available: {_floorTemplates.Count}");
|
||||
|
||||
// Log template details for debugging
|
||||
// Log detailed template info for debugging
|
||||
foreach (var t in _floorTemplates)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Floor template: {t.Name}, PrimaryTileId={t.PrimaryTileDefinitionId}, HasTilesJson={!string.IsNullOrEmpty(t.TilesJson)}");
|
||||
if (!string.IsNullOrEmpty(t.TilesJson))
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] TilesJson preview: {t.TilesJson.Substring(0, Math.Min(200, t.TilesJson.Length))}...");
|
||||
}
|
||||
LogTileTemplate(t, "Floor");
|
||||
}
|
||||
|
||||
TileDefinition? selectedTile = null;
|
||||
TileDefinition? firstSelectedTile = null;
|
||||
int tileCount = 0;
|
||||
int fallbackCount = 0;
|
||||
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
@@ -1091,13 +1201,13 @@ public class DungeonGeneratorV3
|
||||
// Only fill walkable cells that don't have tiles yet
|
||||
if (layout.Walkable[x, y] && layout.Tiles[x, y] == 0)
|
||||
{
|
||||
var tile = SelectFloorTile(x, y);
|
||||
var tile = SelectFloorTile(x, y, out bool usedFallback);
|
||||
if (tile != null)
|
||||
{
|
||||
if (selectedTile == null)
|
||||
if (firstSelectedTile == null)
|
||||
{
|
||||
selectedTile = tile;
|
||||
Console.WriteLine($"[DungeonGenV3] Selected floor tile: ID={tile.Id}, Sheet={tile.TileMapSheetId}, Frame={tile.FrameIndex}");
|
||||
firstSelectedTile = tile;
|
||||
Log($"First selected floor tile: ID={tile.Id}, Name='{tile.Name}', Sheet={tile.TileMapSheetId}, Frame={tile.FrameIndex}");
|
||||
}
|
||||
|
||||
layout.Tiles[x, y] = tile.Id;
|
||||
@@ -1111,43 +1221,79 @@ public class DungeonGeneratorV3
|
||||
Layer = 0
|
||||
});
|
||||
tileCount++;
|
||||
if (usedFallback) fallbackCount++;
|
||||
|
||||
if (LogEveryTile)
|
||||
{
|
||||
Log($" Floor ({x},{y}): TileDef={tile.Id}, Sheet={tile.TileMapSheetId}, Frame={tile.FrameIndex}{(usedFallback ? " [FALLBACK]" : "")}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"[DungeonGenV3] Placed {tileCount} floor tiles");
|
||||
Log($"Placed {tileCount} floor tiles ({fallbackCount} used fallback)");
|
||||
}
|
||||
|
||||
private TileDefinition? SelectFloorTile(int x, int y)
|
||||
/// <summary>
|
||||
/// Select a floor tile for the given position.
|
||||
///
|
||||
/// SELECTION PROCESS:
|
||||
/// 1. If _floorTemplates has templates:
|
||||
/// a. Select one via weighted random (higher Weight = more likely)
|
||||
/// b. Try template.PrimaryTileDefinitionId first - if set, look up that TileDefinition
|
||||
/// c. If no PrimaryTileDefinitionId, parse template.TilesJson
|
||||
/// d. TilesJson format: [{"x":0,"y":0,"sheetId":3,"frameIndex":273}]
|
||||
/// e. Take first tile's sheetId and frameIndex
|
||||
/// f. Search _tileDefinitions for match: TileMapSheetId==sheetId AND FrameIndex==frameIndex
|
||||
/// 2. FALLBACK: Return first walkable TileDefinition (ordered by ID)
|
||||
/// </summary>
|
||||
private TileDefinition? SelectFloorTile(int x, int y, out bool usedFallback)
|
||||
{
|
||||
usedFallback = false;
|
||||
|
||||
// First try to use floor templates
|
||||
if (_floorTemplates.Count > 0)
|
||||
{
|
||||
var template = SelectWeightedTemplate(_floorTemplates);
|
||||
if (template != null)
|
||||
{
|
||||
// Try PrimaryTileDefinitionId first
|
||||
LogVerbose($"SelectFloorTile({x},{y}): Template '{template.Name}' (ID={template.Id})");
|
||||
|
||||
// Method 1: Try PrimaryTileDefinitionId first
|
||||
if (template.PrimaryTileDefinitionId.HasValue)
|
||||
{
|
||||
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
||||
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
||||
{
|
||||
if (!UsedFloorTemplates.Contains(template.Name))
|
||||
UsedFloorTemplates.Add(template.Name);
|
||||
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
||||
return tileDef;
|
||||
}
|
||||
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
||||
}
|
||||
|
||||
// Try to parse TilesJson for the template (when PrimaryTileDefinitionId is not set)
|
||||
// Method 2: Try to parse TilesJson for the template (when PrimaryTileDefinitionId is not set)
|
||||
if (!string.IsNullOrEmpty(template.TilesJson))
|
||||
{
|
||||
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, options);
|
||||
if (tiles?.Count > 0)
|
||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
||||
if (tiles == null)
|
||||
{
|
||||
LogVerbose($" -> Deserialized to NULL!");
|
||||
}
|
||||
else if (tiles.Count == 0)
|
||||
{
|
||||
LogVerbose($" -> Deserialized to EMPTY list!");
|
||||
}
|
||||
else
|
||||
{
|
||||
var first = tiles[0];
|
||||
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
||||
|
||||
// Find matching tile definition by sheet and frame
|
||||
var match = _tileDefinitions.Values
|
||||
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
||||
@@ -1155,35 +1301,62 @@ public class DungeonGeneratorV3
|
||||
{
|
||||
if (!UsedFloorTemplates.Contains(template.Name))
|
||||
UsedFloorTemplates.Add(template.Name);
|
||||
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
||||
return match;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] WARNING: No tile definition found for Sheet={first.SheetId}, Frame={first.FrameIndex}");
|
||||
Log($" -> WARNING: No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}!");
|
||||
Log($" Available TileDefinitions for Sheet {first.SheetId}:");
|
||||
foreach (var td in _tileDefinitions.Values.Where(t => t.TileMapSheetId == first.SheetId).Take(5))
|
||||
{
|
||||
Log($" TileDef {td.Id}: Frame={td.FrameIndex}, '{td.Name}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Error parsing TilesJson: {ex.Message}");
|
||||
Log($" -> ERROR parsing TilesJson: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogVerbose($" TilesJson is empty/null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: use the FIRST walkable tile consistently (not random)
|
||||
// FALLBACK: use the FIRST walkable tile consistently (not random)
|
||||
usedFallback = true;
|
||||
var walkableTiles = _tileDefinitions.Values.Where(t => t.IsWalkable).OrderBy(t => t.Id).ToList();
|
||||
if (walkableTiles.Count > 0)
|
||||
{
|
||||
return walkableTiles[0]; // Use first, not random
|
||||
LogVerbose($"SelectFloorTile({x},{y}): FALLBACK TileDef {walkableTiles[0].Id} '{walkableTiles[0].Name}'");
|
||||
return walkableTiles[0];
|
||||
}
|
||||
|
||||
LogVerbose($"SelectFloorTile({x},{y}): No walkable tiles found!");
|
||||
return _tileDefinitions.Values.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Place wall tiles around walkable areas.
|
||||
/// Walls are placed on non-walkable cells adjacent to walkable cells.
|
||||
/// </summary>
|
||||
private void PlaceWallTiles(DungeonLayoutV2 layout)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Placing wall tiles with {_wallTemplates.Count} templates");
|
||||
Log($"--- Placing Wall Tiles ---");
|
||||
Log($"Wall templates available: {_wallTemplates.Count}");
|
||||
|
||||
// Log detailed template info for debugging
|
||||
foreach (var t in _wallTemplates)
|
||||
{
|
||||
LogTileTemplate(t, "Wall");
|
||||
}
|
||||
|
||||
int wallCount = 0;
|
||||
int fallbackCount = 0;
|
||||
|
||||
// Find cells adjacent to walkable that aren't walkable themselves
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
@@ -1205,10 +1378,13 @@ public class DungeonGeneratorV3
|
||||
FrameIndex = tile.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
wallCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log($"Placed {wallCount} wall tiles");
|
||||
}
|
||||
|
||||
private bool HasAdjacentWalkable(DungeonLayoutV2 layout, int x, int y)
|
||||
@@ -1226,6 +1402,17 @@ public class DungeonGeneratorV3
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select a wall tile for the given position.
|
||||
///
|
||||
/// SELECTION PROCESS:
|
||||
/// 1. If _wallTemplates has templates:
|
||||
/// a. Select one via weighted random
|
||||
/// b. Try template.PrimaryTileDefinitionId first
|
||||
/// c. If no PrimaryTileDefinitionId, parse template.TilesJson
|
||||
/// d. Look up TileDefinition by SheetId + FrameIndex
|
||||
/// 2. FALLBACK: Return first non-walkable TileDefinition
|
||||
/// </summary>
|
||||
private TileDefinition? SelectWallTile(int x, int y, DungeonLayoutV2 layout)
|
||||
{
|
||||
if (_wallTemplates.Count > 0)
|
||||
@@ -1233,59 +1420,146 @@ public class DungeonGeneratorV3
|
||||
var template = SelectWeightedTemplate(_wallTemplates);
|
||||
if (template != null)
|
||||
{
|
||||
// Try PrimaryTileDefinitionId first
|
||||
LogVerbose($"SelectWallTile({x},{y}): Template '{template.Name}' (ID={template.Id})");
|
||||
|
||||
// Method 1: Try PrimaryTileDefinitionId first
|
||||
if (template.PrimaryTileDefinitionId.HasValue)
|
||||
{
|
||||
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
||||
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
||||
{
|
||||
if (!UsedWallTemplates.Contains(template.Name))
|
||||
UsedWallTemplates.Add(template.Name);
|
||||
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
||||
return tileDef;
|
||||
}
|
||||
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
||||
}
|
||||
|
||||
// Try to parse TilesJson for the template
|
||||
// Method 2: Try to parse TilesJson for the template
|
||||
if (!string.IsNullOrEmpty(template.TilesJson))
|
||||
{
|
||||
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, options);
|
||||
if (tiles?.Count > 0)
|
||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
||||
if (tiles == null)
|
||||
{
|
||||
LogVerbose($" -> Deserialized to NULL!");
|
||||
}
|
||||
else if (tiles.Count == 0)
|
||||
{
|
||||
LogVerbose($" -> Deserialized to EMPTY list!");
|
||||
}
|
||||
else
|
||||
{
|
||||
var first = tiles[0];
|
||||
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
||||
|
||||
var match = _tileDefinitions.Values
|
||||
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
||||
if (match != null)
|
||||
{
|
||||
if (!UsedWallTemplates.Contains(template.Name))
|
||||
UsedWallTemplates.Add(template.Name);
|
||||
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
||||
return match;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] WARNING: No wall tile definition found for Sheet={first.SheetId}, Frame={first.FrameIndex}");
|
||||
Log($" -> WARNING: No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}!");
|
||||
Log($" Available TileDefinitions for Sheet {first.SheetId}:");
|
||||
foreach (var td in _tileDefinitions.Values.Where(t => t.TileMapSheetId == first.SheetId).Take(5))
|
||||
{
|
||||
Log($" TileDef {td.Id}: Frame={td.FrameIndex}, '{td.Name}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] Error parsing wall TilesJson: {ex.Message}");
|
||||
} }
|
||||
Log($" -> ERROR parsing TilesJson: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogVerbose($" TilesJson is empty/null");
|
||||
}
|
||||
}
|
||||
// Fallback: use the FIRST non-walkable tile consistently
|
||||
}
|
||||
|
||||
// FALLBACK: use the FIRST non-walkable tile consistently
|
||||
var wallTiles = _tileDefinitions.Values.Where(t => !t.IsWalkable).OrderBy(t => t.Id).ToList();
|
||||
if (wallTiles.Count > 0)
|
||||
{
|
||||
LogVerbose($"SelectWallTile({x},{y}): FALLBACK TileDef {wallTiles[0].Id} '{wallTiles[0].Name}'");
|
||||
return wallTiles[0];
|
||||
}
|
||||
|
||||
LogVerbose($"SelectWallTile({x},{y}): No non-walkable tiles found!");
|
||||
return null;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// LOGGING HELPERS
|
||||
// ============================================================
|
||||
|
||||
/// <summary>Always log this message</summary>
|
||||
private void Log(string message)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] {message}");
|
||||
}
|
||||
|
||||
/// <summary>Only log if VerboseLogging is enabled</summary>
|
||||
private void LogVerbose(string message)
|
||||
{
|
||||
if (VerboseLogging)
|
||||
{
|
||||
Console.WriteLine($"[DungeonGenV3] {message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Log detailed information about a TileTemplate.
|
||||
/// Shows how the template will resolve to an actual tile.
|
||||
/// </summary>
|
||||
private void LogTileTemplate(TileTemplate t, string category)
|
||||
{
|
||||
Log($" {category} Template {t.Id}: '{t.Name}'");
|
||||
Log($" Category={t.Category}, Biome={t.Biome ?? "any"}, Tags={t.Tags ?? "none"}");
|
||||
Log($" Weight={t.Weight}, Walkable={t.IsWalkable}, BlocksMovement={t.BlocksMovement}");
|
||||
Log($" PrimaryTileDefinitionId={t.PrimaryTileDefinitionId?.ToString() ?? "NULL"}");
|
||||
|
||||
|
||||
}
|
||||
if (!string.IsNullOrEmpty(t.TilesJson))
|
||||
{
|
||||
Log($" TilesJson={t.TilesJson}");
|
||||
try
|
||||
{
|
||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(t.TilesJson, _jsonOptions);
|
||||
if (tiles?.Count > 0)
|
||||
{
|
||||
var first = tiles[0];
|
||||
Log($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
||||
|
||||
var match = _tileDefinitions.Values
|
||||
.FirstOrDefault(td => td.TileMapSheetId == first.SheetId && td.FrameIndex == first.FrameIndex);
|
||||
if (match != null)
|
||||
Log($" -> Resolves to TileDef {match.Id}: '{match.Name}' (Walkable={match.IsWalkable})");
|
||||
else
|
||||
Log($" -> WARNING: No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($" -> ERROR: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($" TilesJson=EMPTY");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user