Dungeon Builder Phase 2
This commit is contained in:
@@ -104,6 +104,10 @@ public class DungeonGeneratorV3
|
||||
// Tile data cache
|
||||
private Dictionary<int, TileDefinition> _tileDefinitions = new();
|
||||
private Dictionary<int, TileMapSheet> _tileSheets = new();
|
||||
private List<TileDefinition> _floorTileDefinitions = new();
|
||||
private List<TileDefinition> _wallTileDefinitions = new();
|
||||
private List<TileDefinition> _doorTileDefinitions = new();
|
||||
private Dictionary<string, int> _placedTileDefinitions = new();
|
||||
|
||||
// Bucket rules and tile bucket assignments
|
||||
private Dictionary<int, HashSet<int>> _tileBucketsByDefinitionId = new();
|
||||
@@ -544,6 +548,7 @@ public class DungeonGeneratorV3
|
||||
|
||||
await LoadBucketDataAsync();
|
||||
ApplyBucketFilters();
|
||||
BuildTileDefinitionLists();
|
||||
}
|
||||
|
||||
private async Task<List<DungeonRoomV2>> GenerateRoomsAsync(DungeonLayoutV2 layout)
|
||||
@@ -1009,6 +1014,49 @@ public class DungeonGeneratorV3
|
||||
BuildSpriteToTemplateLookup();
|
||||
}
|
||||
|
||||
private void BuildTileDefinitionLists()
|
||||
{
|
||||
var defs = _tileDefinitions.Values.Where(t => t.IsEnabled).ToList();
|
||||
if (_paletteAllowedBuckets.Count > 0 || _enforceBiomeBuckets)
|
||||
{
|
||||
defs = defs.Where(TileDefinitionMatchesBucketFilters).ToList();
|
||||
}
|
||||
|
||||
_floorTileDefinitions = defs
|
||||
.Where(t => t.IsWalkable && t.Type != TileType.Wall && t.Type != TileType.WallDecor)
|
||||
.ToList();
|
||||
|
||||
_wallTileDefinitions = defs
|
||||
.Where(t => !t.IsWalkable || t.Type == TileType.Wall || t.Type == TileType.WallDecor)
|
||||
.ToList();
|
||||
|
||||
_doorTileDefinitions = defs
|
||||
.Where(t => t.Type == TileType.Door)
|
||||
.ToList();
|
||||
|
||||
Log($"TileDefinitions: floor={_floorTileDefinitions.Count}, wall={_wallTileDefinitions.Count}, door={_doorTileDefinitions.Count}");
|
||||
}
|
||||
|
||||
private bool TileDefinitionMatchesBucketFilters(TileDefinition def)
|
||||
{
|
||||
if (!_tileBucketsByDefinitionId.TryGetValue(def.Id, out var buckets) || buckets.Count == 0)
|
||||
{
|
||||
return _paletteAllowedBuckets.Count == 0 && !_enforceBiomeBuckets;
|
||||
}
|
||||
|
||||
if (_paletteAllowedBuckets.Count > 0 && !buckets.Overlaps(_paletteAllowedBuckets))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_enforceBiomeBuckets && !string.IsNullOrWhiteSpace(_paletteBiome))
|
||||
{
|
||||
return TemplateMatchesBiomeBucket(buckets);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TemplateMatchesBucketFilters(TileTemplate template)
|
||||
{
|
||||
var buckets = GetTemplateBucketIds(template);
|
||||
@@ -1082,43 +1130,55 @@ public class DungeonGeneratorV3
|
||||
|
||||
if (worldX >= 0 && worldX < MapWidth && worldY >= 0 && worldY < MapHeight)
|
||||
{
|
||||
// Ensure the sheet is loaded for client rendering
|
||||
EnsureSheetLoaded(t.SheetId);
|
||||
|
||||
// Use IsWalkable from the tile placement data (set by room editor)
|
||||
// This is the primary source of truth for walkability
|
||||
var layer = t.Layer ?? 0;
|
||||
int tileDefId = t.TileDefinitionId ?? 0;
|
||||
int sheetId = t.SheetId;
|
||||
int frameIndex = t.FrameIndex;
|
||||
bool isWalkable = t.IsWalkable;
|
||||
int templateId = t.TemplateId ?? 0;
|
||||
|
||||
// If no templateId provided, try to find one by sprite lookup (legacy support)
|
||||
if (templateId == 0)
|
||||
|
||||
if (tileDefId > 0 && _tileDefinitions.TryGetValue(tileDefId, out var def))
|
||||
{
|
||||
sheetId = def.TileMapSheetId;
|
||||
frameIndex = def.FrameIndex;
|
||||
if (layer == 0)
|
||||
{
|
||||
isWalkable = def.IsWalkable;
|
||||
}
|
||||
}
|
||||
else if (templateId == 0)
|
||||
{
|
||||
var tileTemplate = FindTemplateBySprite(t.SheetId, t.FrameIndex);
|
||||
if (tileTemplate != null)
|
||||
{
|
||||
templateId = tileTemplate.Id;
|
||||
// Only override isWalkable if not explicitly set in placement
|
||||
// (legacy data won't have IsWalkable set)
|
||||
}
|
||||
}
|
||||
|
||||
layout.Tiles[worldX, worldY] = templateId > 0 ? templateId : 1; // Non-zero to indicate tile is placed
|
||||
layout.Walkable[worldX, worldY] = isWalkable;
|
||||
|
||||
// Track placement for socket matching if we found a template
|
||||
if (templateId > 0)
|
||||
|
||||
EnsureSheetLoaded(sheetId);
|
||||
|
||||
if (layer == 0)
|
||||
{
|
||||
_placedTemplates[$"{worldX},{worldY}"] = templateId;
|
||||
layout.Tiles[worldX, worldY] = tileDefId > 0 ? tileDefId : (templateId > 0 ? templateId : 1);
|
||||
layout.Walkable[worldX, worldY] = isWalkable;
|
||||
if (tileDefId > 0)
|
||||
{
|
||||
_placedTileDefinitions[$"{worldX},{worldY}"] = tileDefId;
|
||||
}
|
||||
else if (templateId > 0)
|
||||
{
|
||||
_placedTemplates[$"{worldX},{worldY}"] = templateId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = worldX,
|
||||
Y = worldY,
|
||||
TileDefinitionId = 0, // No TileDefinition - using template directly
|
||||
SheetId = t.SheetId,
|
||||
FrameIndex = t.FrameIndex,
|
||||
Layer = 0
|
||||
TileDefinitionId = tileDefId,
|
||||
SheetId = sheetId,
|
||||
FrameIndex = frameIndex,
|
||||
Layer = layer
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1369,6 +1429,7 @@ public class DungeonGeneratorV3
|
||||
|
||||
// Clear from placed templates tracking
|
||||
_placedTemplates.Remove($"{x},{cy}");
|
||||
_placedTileDefinitions.Remove($"{x},{cy}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1396,6 +1457,7 @@ public class DungeonGeneratorV3
|
||||
|
||||
// Clear from placed templates tracking
|
||||
_placedTemplates.Remove($"{cx},{y}");
|
||||
_placedTileDefinitions.Remove($"{cx},{y}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1412,58 +1474,93 @@ public class DungeonGeneratorV3
|
||||
{
|
||||
Log($"--- Filling Floor Tiles ---");
|
||||
Log($"Floor templates available: {_floorTemplates.Count}");
|
||||
Log($"Floor definitions available: {_floorTileDefinitions.Count}");
|
||||
Log($"Socket matching: {(UseSocketMatching ? "ENABLED" : "DISABLED")}");
|
||||
|
||||
// Clear placement tracking for socket matching
|
||||
|
||||
_placedTemplates.Clear();
|
||||
|
||||
// Log detailed template info for debugging
|
||||
|
||||
if (_floorTileDefinitions.Count > 0)
|
||||
{
|
||||
int tileCount = 0;
|
||||
bool loggedFirst = false;
|
||||
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < MapWidth; x++)
|
||||
{
|
||||
if (layout.Walkable[x, y] && layout.Tiles[x, y] == 0)
|
||||
{
|
||||
var def = SelectFloorDefinition(x, y, layout);
|
||||
if (def == null) continue;
|
||||
|
||||
if (!loggedFirst)
|
||||
{
|
||||
loggedFirst = true;
|
||||
Log($"First selected floor: TileDef='{def.Name ?? def.Id.ToString()}', Sheet={def.TileMapSheetId}, Frame={def.FrameIndex}");
|
||||
}
|
||||
|
||||
EnsureSheetLoaded(def.TileMapSheetId);
|
||||
layout.Tiles[x, y] = def.Id;
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = def.Id,
|
||||
SheetId = def.TileMapSheetId,
|
||||
FrameIndex = def.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
_placedTileDefinitions[$"{x},{y}"] = def.Id;
|
||||
tileCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log($"Placed {tileCount} floor tiles");
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback to templates
|
||||
foreach (var t in _floorTemplates)
|
||||
{
|
||||
LogTileTemplate(t, "Floor");
|
||||
}
|
||||
|
||||
bool loggedFirst = false;
|
||||
int tileCount = 0;
|
||||
|
||||
|
||||
bool loggedFirstTemplate = false;
|
||||
int templateCount = 0;
|
||||
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < MapWidth; x++)
|
||||
{
|
||||
// Only fill walkable cells that don't have tiles yet
|
||||
if (layout.Walkable[x, y] && layout.Tiles[x, y] == 0)
|
||||
{
|
||||
var result = SelectFloorTemplate(x, y, layout);
|
||||
if (result != null)
|
||||
{
|
||||
if (!loggedFirst)
|
||||
if (!loggedFirstTemplate)
|
||||
{
|
||||
loggedFirst = true;
|
||||
loggedFirstTemplate = true;
|
||||
Log($"First selected floor: Template='{result.Value.TemplateName}', Sheet={result.Value.SheetId}, Frame={result.Value.FrameIndex}");
|
||||
}
|
||||
|
||||
layout.Tiles[x, y] = result.Value.TemplateId; // Use template ID for tracking
|
||||
|
||||
layout.Tiles[x, y] = result.Value.TemplateId;
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = 0, // No TileDefinition - using template directly
|
||||
TileDefinitionId = 0,
|
||||
SheetId = result.Value.SheetId,
|
||||
FrameIndex = result.Value.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
tileCount++;
|
||||
|
||||
if (LogEveryTile)
|
||||
{
|
||||
Log($" Floor ({x},{y}): Template='{result.Value.TemplateName}', Sheet={result.Value.SheetId}, Frame={result.Value.FrameIndex}");
|
||||
}
|
||||
templateCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log($"Placed {tileCount} floor tiles");
|
||||
|
||||
Log($"Placed {templateCount} floor tiles");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1476,6 +1573,65 @@ public class DungeonGeneratorV3
|
||||
public int SheetId;
|
||||
public int FrameIndex;
|
||||
}
|
||||
|
||||
private TileDefinition? SelectFloorDefinition(int x, int y, DungeonLayoutV2 layout)
|
||||
{
|
||||
if (_floorTileDefinitions.Count == 0) return null;
|
||||
var compatible = GetCompatibleTileDefinitions(_floorTileDefinitions, x, y, layout);
|
||||
if (compatible.Count == 0) compatible = _floorTileDefinitions;
|
||||
return compatible[_rng.Next(compatible.Count)];
|
||||
}
|
||||
|
||||
private TileDefinition? SelectWallDefinition(int x, int y, DungeonLayoutV2 layout)
|
||||
{
|
||||
if (_wallTileDefinitions.Count == 0) return null;
|
||||
var compatible = GetCompatibleTileDefinitions(_wallTileDefinitions, x, y, layout);
|
||||
if (compatible.Count == 0) compatible = _wallTileDefinitions;
|
||||
return compatible[_rng.Next(compatible.Count)];
|
||||
}
|
||||
|
||||
private List<TileDefinition> GetCompatibleTileDefinitions(List<TileDefinition> defs, int x, int y, DungeonLayoutV2 layout)
|
||||
{
|
||||
if (!UseBucketMatching || _bucketRules.Count == 0) return defs;
|
||||
return defs.Where(d => IsBucketCompatible(d, x, y, layout)).ToList();
|
||||
}
|
||||
|
||||
private bool IsBucketCompatible(TileDefinition def, int x, int y, DungeonLayoutV2 layout)
|
||||
{
|
||||
if (!UseBucketMatching || _bucketRules.Count == 0) return true;
|
||||
if (!_tileBucketsByDefinitionId.TryGetValue(def.Id, out var sourceBuckets) || sourceBuckets.Count == 0) return true;
|
||||
|
||||
if (y > 0 && _placedTileDefinitions.TryGetValue($"{x},{y-1}", out var northDefId))
|
||||
{
|
||||
if (!BucketsCompatible(sourceBuckets, GetTileBuckets(northDefId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (y < layout.Height - 1 && _placedTileDefinitions.TryGetValue($"{x},{y+1}", out var southDefId))
|
||||
{
|
||||
if (!BucketsCompatible(sourceBuckets, GetTileBuckets(southDefId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x < layout.Width - 1 && _placedTileDefinitions.TryGetValue($"{x+1},{y}", out var eastDefId))
|
||||
{
|
||||
if (!BucketsCompatible(sourceBuckets, GetTileBuckets(eastDefId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x > 0 && _placedTileDefinitions.TryGetValue($"{x-1},{y}", out var westDefId))
|
||||
{
|
||||
if (!BucketsCompatible(sourceBuckets, GetTileBuckets(westDefId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private HashSet<int> GetTileBuckets(int tileDefId)
|
||||
{
|
||||
return _tileBucketsByDefinitionId.TryGetValue(tileDefId, out var buckets) ? buckets : new HashSet<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select a floor template for the given position.
|
||||
@@ -1591,20 +1747,53 @@ public class DungeonGeneratorV3
|
||||
{
|
||||
Log($"--- Placing Wall Tiles ---");
|
||||
Log($"Wall templates available: {_wallTemplates.Count}");
|
||||
Log($"Wall definitions available: {_wallTileDefinitions.Count}");
|
||||
Log($"Socket matching: {(UseSocketMatching ? "ENABLED" : "DISABLED")}");
|
||||
|
||||
// Clear placement tracking for socket matching (separate from floor tiles)
|
||||
|
||||
_placedTemplates.Clear();
|
||||
|
||||
// Log detailed template info for debugging
|
||||
_placedTileDefinitions.Clear();
|
||||
|
||||
if (_wallTileDefinitions.Count > 0)
|
||||
{
|
||||
int wallCount = 0;
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < MapWidth; x++)
|
||||
{
|
||||
if (!layout.Walkable[x, y] && HasAdjacentWalkable(layout, x, y) && layout.Tiles[x, y] == 0)
|
||||
{
|
||||
var def = SelectWallDefinition(x, y, layout);
|
||||
if (def == null) continue;
|
||||
|
||||
EnsureSheetLoaded(def.TileMapSheetId);
|
||||
layout.Tiles[x, y] = def.Id;
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = def.Id,
|
||||
SheetId = def.TileMapSheetId,
|
||||
FrameIndex = def.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
_placedTileDefinitions[$"{x},{y}"] = def.Id;
|
||||
wallCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log($"Placed {wallCount} wall tiles");
|
||||
return;
|
||||
}
|
||||
|
||||
_placedTileDefinitions.Clear();
|
||||
|
||||
foreach (var t in _wallTemplates)
|
||||
{
|
||||
LogTileTemplate(t, "Wall");
|
||||
}
|
||||
|
||||
int wallCount = 0;
|
||||
|
||||
// Find cells adjacent to walkable that aren't walkable themselves
|
||||
|
||||
int templateCount = 0;
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < MapWidth; x++)
|
||||
@@ -1619,23 +1808,18 @@ public class DungeonGeneratorV3
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = 0, // No TileDefinition - using template directly
|
||||
TileDefinitionId = 0,
|
||||
SheetId = result.Value.SheetId,
|
||||
FrameIndex = result.Value.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
wallCount++;
|
||||
|
||||
if (LogEveryTile)
|
||||
{
|
||||
Log($" Wall ({x},{y}): Template='{result.Value.TemplateName}', Sheet={result.Value.SheetId}, Frame={result.Value.FrameIndex}");
|
||||
}
|
||||
templateCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log($"Placed {wallCount} wall tiles");
|
||||
|
||||
Log($"Placed {templateCount} wall tiles");
|
||||
}
|
||||
|
||||
private bool HasAdjacentWalkable(DungeonLayoutV2 layout, int x, int y)
|
||||
@@ -1983,25 +2167,25 @@ public class DungeonGeneratorV3
|
||||
|
||||
if (y > 0 && _placedTemplates.TryGetValue($"{x},{y-1}", out var northTemplateId))
|
||||
{
|
||||
if (!TemplatesBucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(northTemplateId)))
|
||||
if (!BucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(northTemplateId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (y < layout.Height - 1 && _placedTemplates.TryGetValue($"{x},{y+1}", out var southTemplateId))
|
||||
{
|
||||
if (!TemplatesBucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(southTemplateId)))
|
||||
if (!BucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(southTemplateId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x < layout.Width - 1 && _placedTemplates.TryGetValue($"{x+1},{y}", out var eastTemplateId))
|
||||
{
|
||||
if (!TemplatesBucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(eastTemplateId)))
|
||||
if (!BucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(eastTemplateId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x > 0 && _placedTemplates.TryGetValue($"{x-1},{y}", out var westTemplateId))
|
||||
{
|
||||
if (!TemplatesBucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(westTemplateId)))
|
||||
if (!BucketsCompatible(sourceBuckets, GetTemplateBucketIdsById(westTemplateId)))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2017,7 +2201,7 @@ public class DungeonGeneratorV3
|
||||
return template == null ? new HashSet<int>() : GetTemplateBucketIds(template);
|
||||
}
|
||||
|
||||
private bool TemplatesBucketsCompatible(HashSet<int> sourceBuckets, HashSet<int> targetBuckets)
|
||||
private bool BucketsCompatible(HashSet<int> sourceBuckets, HashSet<int> targetBuckets)
|
||||
{
|
||||
if (_bucketRules.Count == 0) return true;
|
||||
if (sourceBuckets.Count == 0 || targetBuckets.Count == 0) return true;
|
||||
@@ -2246,6 +2430,8 @@ public class RoomTilePlacement
|
||||
public int SheetId { get; set; }
|
||||
public int FrameIndex { get; set; }
|
||||
public int? TemplateId { get; set; } // TileTemplate ID (new format)
|
||||
public int? TileDefinitionId { get; set; }
|
||||
public int? Layer { get; set; }
|
||||
public bool IsWalkable { get; set; } = true; // Whether this tile is walkable (new format)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user