Refresh fix, Dungeon Builder v4

This commit is contained in:
dingo
2026-01-05 08:11:06 -05:00
parent 23f283129b
commit 124fd4f40c
+85 -37
View File
@@ -804,6 +804,15 @@ public class DungeonGeneratorV3
{ {
Console.WriteLine($"[DungeonGenV3] Filling floor tiles with {_floorTemplates.Count} templates"); Console.WriteLine($"[DungeonGenV3] Filling floor tiles with {_floorTemplates.Count} templates");
// Log template details for debugging
foreach (var t in _floorTemplates)
{
Console.WriteLine($"[DungeonGenV3] Floor template: {t.Name}, PrimaryTileId={t.PrimaryTileDefinitionId}, TilesJson={!string.IsNullOrEmpty(t.TilesJson)}");
}
TileDefinition? selectedTile = null;
int tileCount = 0;
for (int y = 0; y < MapHeight; y++) for (int y = 0; y < MapHeight; y++)
{ {
for (int x = 0; x < MapWidth; x++) for (int x = 0; x < MapWidth; x++)
@@ -814,6 +823,12 @@ public class DungeonGeneratorV3
var tile = SelectFloorTile(x, y); var tile = SelectFloorTile(x, y);
if (tile != null) if (tile != null)
{ {
if (selectedTile == null)
{
selectedTile = tile;
Console.WriteLine($"[DungeonGenV3] Selected floor tile: ID={tile.Id}, Sheet={tile.TileMapSheetId}, Frame={tile.FrameIndex}");
}
layout.Tiles[x, y] = tile.Id; layout.Tiles[x, y] = tile.Id;
layout.TileDetails.Add(new PlacedTile layout.TileDetails.Add(new PlacedTile
{ {
@@ -824,10 +839,13 @@ public class DungeonGeneratorV3
FrameIndex = tile.FrameIndex, FrameIndex = tile.FrameIndex,
Layer = 0 Layer = 0
}); });
tileCount++;
} }
} }
} }
} }
Console.WriteLine($"[DungeonGenV3] Placed {tileCount} floor tiles");
} }
private TileDefinition? SelectFloorTile(int x, int y) private TileDefinition? SelectFloorTile(int x, int y)
@@ -836,45 +854,49 @@ public class DungeonGeneratorV3
if (_floorTemplates.Count > 0) if (_floorTemplates.Count > 0)
{ {
var template = SelectWeightedTemplate(_floorTemplates); var template = SelectWeightedTemplate(_floorTemplates);
if (template != null && template.PrimaryTileDefinitionId.HasValue) if (template != null)
{ {
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef)) // Try PrimaryTileDefinitionId first
if (template.PrimaryTileDefinitionId.HasValue)
{ {
if (!UsedFloorTemplates.Contains(template.Name)) if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
UsedFloorTemplates.Add(template.Name);
return tileDef;
}
}
// Try to parse TilesJson for the template
if (template != null && !string.IsNullOrEmpty(template.TilesJson))
{
try
{
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson);
if (tiles?.Count > 0)
{ {
var first = tiles[0]; if (!UsedFloorTemplates.Contains(template.Name))
// Find matching tile definition UsedFloorTemplates.Add(template.Name);
var match = _tileDefinitions.Values return tileDef;
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
if (match != null)
{
if (!UsedFloorTemplates.Contains(template.Name))
UsedFloorTemplates.Add(template.Name);
return match;
}
} }
} }
catch { }
// Try to parse TilesJson for the template (when PrimaryTileDefinitionId is not set)
if (!string.IsNullOrEmpty(template.TilesJson))
{
try
{
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson);
if (tiles?.Count > 0)
{
var first = tiles[0];
// Find matching tile definition by sheet and frame
var match = _tileDefinitions.Values
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
if (match != null)
{
if (!UsedFloorTemplates.Contains(template.Name))
UsedFloorTemplates.Add(template.Name);
return match;
}
}
}
catch { }
}
} }
} }
// Fallback to any walkable tile // Fallback: use the FIRST walkable tile consistently (not random)
var walkableTiles = _tileDefinitions.Values.Where(t => t.IsWalkable).ToList(); var walkableTiles = _tileDefinitions.Values.Where(t => t.IsWalkable).OrderBy(t => t.Id).ToList();
if (walkableTiles.Count > 0) if (walkableTiles.Count > 0)
{ {
return walkableTiles[_rng.Next(walkableTiles.Count)]; return walkableTiles[0]; // Use first, not random
} }
return _tileDefinitions.Values.FirstOrDefault(); return _tileDefinitions.Values.FirstOrDefault();
@@ -930,22 +952,48 @@ public class DungeonGeneratorV3
if (_wallTemplates.Count > 0) if (_wallTemplates.Count > 0)
{ {
var template = SelectWeightedTemplate(_wallTemplates); var template = SelectWeightedTemplate(_wallTemplates);
if (template != null && template.PrimaryTileDefinitionId.HasValue) if (template != null)
{ {
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef)) // Try PrimaryTileDefinitionId first
if (template.PrimaryTileDefinitionId.HasValue)
{ {
if (!UsedWallTemplates.Contains(template.Name)) if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
UsedWallTemplates.Add(template.Name); {
return tileDef; if (!UsedWallTemplates.Contains(template.Name))
UsedWallTemplates.Add(template.Name);
return tileDef;
}
}
// Try to parse TilesJson for the template
if (!string.IsNullOrEmpty(template.TilesJson))
{
try
{
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson);
if (tiles?.Count > 0)
{
var first = tiles[0];
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);
return match;
}
}
}
catch { }
} }
} }
} }
// Fallback to any non-walkable tile // Fallback: use the FIRST non-walkable tile consistently
var wallTiles = _tileDefinitions.Values.Where(t => !t.IsWalkable).ToList(); var wallTiles = _tileDefinitions.Values.Where(t => !t.IsWalkable).OrderBy(t => t.Id).ToList();
if (wallTiles.Count > 0) if (wallTiles.Count > 0)
{ {
return wallTiles[_rng.Next(wallTiles.Count)]; return wallTiles[0];
} }
return null; return null;