Dungeon Building Tweaks
This commit is contained in:
@@ -37,12 +37,18 @@ public class DungeonGeneratorV2
|
||||
private List<RoomTemplate> _treasureTemplates = new();
|
||||
private List<RoomTemplate> _normalTemplates = new();
|
||||
private List<RoomTemplate> _hallwayTemplates = new();
|
||||
private Dictionary<string, List<TileDefinition>> _tileGroups = new();
|
||||
|
||||
|
||||
|
||||
// Tile definitions cache
|
||||
private Dictionary<int, TileDefinition> _tileDefinitions = new();
|
||||
private TileMapSheet? _defaultSheet;
|
||||
|
||||
private Dictionary<string, List<TileDefinition>> _tileGroups = new();
|
||||
private Dictionary<int, HashSet<string>> _tileTags = new();
|
||||
private Dictionary<int, Dictionary<string, string>> _tileSockets = new();
|
||||
private Dictionary<int, Dictionary<string, HashSet<string>>> _tileSocketTypes = new();
|
||||
|
||||
private static HashSet<string> ParseTags(string? tags)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(tags)) return new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
@@ -424,6 +430,12 @@ public class DungeonGeneratorV2
|
||||
_tileDefinitions = tiles.ToDictionary(t => t.Id);
|
||||
_tagIndex = new TileTagIndex(_tileDefinitions.Values);
|
||||
_defaultSheet = tiles.FirstOrDefault()?.TileMapSheet;
|
||||
_tileTags = _tileDefinitions.Values.ToDictionary(t => t.Id, t => ParseTags(t.Tags));
|
||||
_tileSocketTypes = _tileDefinitions.Values.ToDictionary(
|
||||
t => t.Id,
|
||||
t => ParseSocketTypes(_tileTags[t.Id])
|
||||
);
|
||||
|
||||
Console.WriteLine($"[DungeonGenV2] Loaded {tiles.Count} tile definitions from templates");
|
||||
}
|
||||
|
||||
@@ -656,44 +668,262 @@ public class DungeonGeneratorV2
|
||||
return room;
|
||||
}
|
||||
|
||||
//private void CarveBasicRoom(DungeonLayoutV2 layout, DungeonRoomV2 room)
|
||||
//{
|
||||
// // Find floor tile from default sheet
|
||||
// //var floorTile = SelectBestFloorTileForTags(room.TemplateTags) ?? _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor);
|
||||
// //int floorId = floorTile?.Id ?? -1; // -1 means no visual tile, just walkable
|
||||
|
||||
// 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)
|
||||
// {
|
||||
// var floorTile = PickRandomTile(room.TemplateTags) ?? _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
|
||||
|
||||
// // Set walkability - basic carved rooms are always walkable
|
||||
// if (layout.Walkable != null)
|
||||
// {
|
||||
// layout.Walkable[x, y] = true;
|
||||
// }
|
||||
|
||||
// if (floorId > 0 && floorTile != null)
|
||||
// {
|
||||
|
||||
// layout.TileDetails.Add(new PlacedTile
|
||||
// {
|
||||
// X = x,
|
||||
// Y = y,
|
||||
// TileDefinitionId = floorId,
|
||||
// SheetId = floorTile.TileMapSheetId,
|
||||
// FrameIndex = floorTile.FrameIndex,
|
||||
// Layer = 0
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
private void CarveBasicRoom(DungeonLayoutV2 layout, DungeonRoomV2 room)
|
||||
{
|
||||
// Find floor tile from default sheet
|
||||
//var floorTile = SelectBestFloorTileForTags(room.TemplateTags) ?? _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor);
|
||||
//int floorId = floorTile?.Id ?? -1; // -1 means no visual tile, just walkable
|
||||
// 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)
|
||||
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))
|
||||
{
|
||||
var floorTile = PickRandomTile(room.TemplateTags) ?? _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
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Set walkability - basic carved rooms are always walkable
|
||||
if (layout.Walkable != null)
|
||||
{
|
||||
layout.Walkable[x, y] = true;
|
||||
}
|
||||
|
||||
if (floorId > 0 && floorTile != null)
|
||||
{
|
||||
// Hard fallback: any floor
|
||||
floorTile ??= _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor);
|
||||
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = floorId,
|
||||
SheetId = floorTile.TileMapSheetId,
|
||||
FrameIndex = floorTile.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given tile satisfies the required socket tags derived from already-placed neighbors.
|
||||
/// This uses the same neighbor logic as PickRandomFloorTile (typically N/W scan order).
|
||||
/// </summary>
|
||||
private bool SatisfiesSocketRequirements(DungeonLayoutV2 layout, int x, int y, TileDefinition candidate)
|
||||
{
|
||||
var required = BuildRequiredSocketTags(layout, x, y);
|
||||
if (required.Count == 0) return true;
|
||||
|
||||
if (!_tileTags.TryGetValue(candidate.Id, out var tags)) return false;
|
||||
foreach (var r in required)
|
||||
if (!tags.Contains(r)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Second-pass repair: ensures mutual socket compatibility with E/S neighbors too,
|
||||
/// replacing tiles that don't agree. This makes the result look "locked in" rather than scan-order dependent.
|
||||
/// </summary>
|
||||
private void RepairRoomFloorSockets(DungeonLayoutV2 layout, DungeonRoomV2 room)
|
||||
{
|
||||
int familyId = 1;
|
||||
|
||||
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;
|
||||
|
||||
if (!layout.IsWalkable(x, y))
|
||||
continue;
|
||||
|
||||
int id = layout.Tiles[x, y];
|
||||
if (!_tileDefinitions.TryGetValue(id, out var cur))
|
||||
continue;
|
||||
|
||||
// Build full constraints (N/E/S/W) based on existing neighbors
|
||||
var required = BuildRequiredSocketTags_AllDirs(layout, x, y);
|
||||
|
||||
// If current tile already satisfies, keep it.
|
||||
if (HasAllTags(cur, required))
|
||||
continue;
|
||||
|
||||
// Try to find a better fitting tile from the same family
|
||||
var replacement = PickRandomFloorTile_AllDirs(layout, x, y, familyId, varianceChance: 0.15);
|
||||
if (replacement == null || replacement.Id == cur.Id)
|
||||
continue;
|
||||
|
||||
// Apply replacement
|
||||
layout.Tiles[x, y] = replacement.Id;
|
||||
|
||||
if (layout.Walkable != null)
|
||||
layout.Walkable[x, y] = true;
|
||||
|
||||
layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y);
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = replacement.Id,
|
||||
SheetId = replacement.TileMapSheetId,
|
||||
FrameIndex = replacement.FrameIndex,
|
||||
Layer = 0
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build required socket tags from ALL directions (N/E/S/W), used for repair.
|
||||
/// Typed socket tags are respected (SocketN_Lava etc.).
|
||||
/// </summary>
|
||||
private List<string> BuildRequiredSocketTags_AllDirs(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
var req = new List<string>();
|
||||
|
||||
// North neighbor => our N must match neighbor's S
|
||||
if (y > 0 && layout.IsWalkable(x, y - 1))
|
||||
{
|
||||
int nid = layout.Tiles[x, y - 1];
|
||||
var r = GetRequiredSocketTagFromNeighbor(nid, neighborEdgeFacingUs: "S", ourDir: "N");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
// South neighbor => our S must match neighbor's N
|
||||
if (y < MapHeight - 1 && layout.IsWalkable(x, y + 1))
|
||||
{
|
||||
int sid = layout.Tiles[x, y + 1];
|
||||
var r = GetRequiredSocketTagFromNeighbor(sid, neighborEdgeFacingUs: "N", ourDir: "S");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
// West neighbor => our W must match neighbor's E
|
||||
if (x > 0 && layout.IsWalkable(x - 1, y))
|
||||
{
|
||||
int wid = layout.Tiles[x - 1, y];
|
||||
var r = GetRequiredSocketTagFromNeighbor(wid, neighborEdgeFacingUs: "E", ourDir: "W");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
// East neighbor => our E must match neighbor's W
|
||||
if (x < MapWidth - 1 && layout.IsWalkable(x + 1, y))
|
||||
{
|
||||
int eid = layout.Tiles[x + 1, y];
|
||||
var r = GetRequiredSocketTagFromNeighbor(eid, neighborEdgeFacingUs: "W", ourDir: "E");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A stricter picker used by the repair pass: filters candidates using ALL-direction socket constraints.
|
||||
/// </summary>
|
||||
private TileDefinition? PickRandomFloorTile_AllDirs(
|
||||
DungeonLayoutV2 layout,
|
||||
int x, int y,
|
||||
int familyId,
|
||||
double varianceChance = 0.15)
|
||||
{
|
||||
var requiredSocketTags = BuildRequiredSocketTags_AllDirs(layout, x, y);
|
||||
|
||||
string primaryTag = $"TileBaseCore_{familyId}";
|
||||
string varianceTag = $"TileBase_{familyId}";
|
||||
|
||||
var primary = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor)
|
||||
.Where(t => HasTag(t, primaryTag))
|
||||
.Where(t => HasAllTags(t, requiredSocketTags))
|
||||
.ToList();
|
||||
|
||||
var variance = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor)
|
||||
.Where(t => HasTag(t, varianceTag))
|
||||
.Where(t => HasAllTags(t, requiredSocketTags))
|
||||
.ToList();
|
||||
|
||||
if (primary.Count == 0 && variance.Count == 0)
|
||||
return null;
|
||||
|
||||
bool pickVar = _rng.NextDouble() < varianceChance;
|
||||
var pool = pickVar ? variance : primary;
|
||||
if (pool.Count == 0) pool = pickVar ? primary : variance;
|
||||
|
||||
return pool.Count == 0 ? null : pool[_rng.Next(pool.Count)];
|
||||
}
|
||||
|
||||
private async Task ConnectRoomsAsync(DungeonLayoutV2 layout, DungeonRoomV2 a, DungeonRoomV2 b)
|
||||
@@ -1361,6 +1591,165 @@ public class DungeonGeneratorV2
|
||||
layout.TotalMobs = layout.MobSpawns.Count;
|
||||
layout.TotalChests = layout.ChestSpawns.Count;
|
||||
}
|
||||
private static Dictionary<string, HashSet<string>> ParseSocketTypes(HashSet<string> tags)
|
||||
{
|
||||
var map = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["N"] = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
["E"] = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
["S"] = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
["W"] = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
};
|
||||
|
||||
foreach (var t in tags)
|
||||
{
|
||||
if (!t.StartsWith("Socket", StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
// SocketN or SocketN_Lava
|
||||
// SocketE_Stone etc.
|
||||
var rest = t.Substring("Socket".Length); // e.g. "N" or "N_Lava"
|
||||
if (string.IsNullOrWhiteSpace(rest)) continue;
|
||||
|
||||
var parts = rest.Split('_', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
var dir = parts[0]; // "N"
|
||||
if (!(dir is "N" or "E" or "S" or "W")) continue;
|
||||
|
||||
var type = parts.Length == 2 ? parts[1] : ""; // "" == untyped
|
||||
map[dir].Add(type);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
private TileDefinition? PickRandomFloorTile(
|
||||
DungeonLayoutV2 layout,
|
||||
int x, int y,
|
||||
int familyId,
|
||||
double varianceChance = 0.15)
|
||||
{
|
||||
var requiredSocketTags = BuildRequiredSocketTags(layout, x, y);
|
||||
|
||||
string primaryTag = $"TileBaseCore_{familyId}";
|
||||
string varianceTag = $"TileBase_{familyId}";
|
||||
|
||||
var primary = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor)
|
||||
.Where(t => HasTag(t, primaryTag))
|
||||
.Where(t => HasAllTags(t, requiredSocketTags))
|
||||
.ToList();
|
||||
|
||||
var variance = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor)
|
||||
.Where(t => HasTag(t, varianceTag))
|
||||
.Where(t => HasAllTags(t, requiredSocketTags))
|
||||
.ToList();
|
||||
|
||||
if (primary.Count == 0 && variance.Count == 0)
|
||||
{
|
||||
// Fallback: relax socket requirements (so you don't dead-end)
|
||||
// You can also log this so you know your tileset is missing variants.
|
||||
primary = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor && HasTag(t, primaryTag))
|
||||
.ToList();
|
||||
|
||||
variance = _tileDefinitions.Values
|
||||
.Where(t => t.Type == TileType.Floor && HasTag(t, varianceTag))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
if (primary.Count == 0 && variance.Count == 0)
|
||||
return _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor);
|
||||
|
||||
bool pickVar = _rng.NextDouble() < varianceChance;
|
||||
var pool = pickVar ? variance : primary;
|
||||
if (pool.Count == 0) pool = pickVar ? primary : variance;
|
||||
|
||||
return pool.Count == 0 ? null : pool[_rng.Next(pool.Count)];
|
||||
}
|
||||
|
||||
|
||||
private static string OppDir(string dir) => dir switch
|
||||
{
|
||||
"N" => "S",
|
||||
"S" => "N",
|
||||
"E" => "W",
|
||||
"W" => "E",
|
||||
_ => dir
|
||||
};
|
||||
|
||||
private bool NeighborRequiresTyped(int neighborTileId, string neighborDir)
|
||||
{
|
||||
// neighborDir is the edge on the neighbor facing us (e.g. neighbor's "S" if it is north of us)
|
||||
if (!_tileSocketTypes.TryGetValue(neighborTileId, out var sockets)) return false;
|
||||
if (!sockets.TryGetValue(neighborDir, out var types)) return false;
|
||||
|
||||
// typed if it has any non-empty type
|
||||
return types.Any(t => !string.IsNullOrEmpty(t));
|
||||
}
|
||||
|
||||
private string? GetRequiredSocketTagFromNeighbor(int neighborTileId, string neighborEdgeFacingUs, string ourDir)
|
||||
{
|
||||
if (!_tileSocketTypes.TryGetValue(neighborTileId, out var sockets)) return null;
|
||||
if (!sockets.TryGetValue(neighborEdgeFacingUs, out var types)) return null;
|
||||
|
||||
// If neighbor supports typed types on that edge, require a matching typed socket
|
||||
var typed = types.FirstOrDefault(t => !string.IsNullOrEmpty(t));
|
||||
if (!string.IsNullOrEmpty(typed))
|
||||
return $"Socket{ourDir}_{typed}";
|
||||
|
||||
// Otherwise require untyped
|
||||
if (types.Contains(""))
|
||||
return $"Socket{ourDir}";
|
||||
|
||||
// neighbor has no socket facing us => no requirement (or treat as mismatch)
|
||||
return null;
|
||||
}
|
||||
private List<string> BuildRequiredSocketTags(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
var req = new List<string>();
|
||||
|
||||
// North neighbor => our N must match neighbor's S
|
||||
if (y > 0 && layout.IsWalkable(x, y - 1))
|
||||
{
|
||||
int nid = layout.Tiles[x, y - 1];
|
||||
var r = GetRequiredSocketTagFromNeighbor(nid, neighborEdgeFacingUs: "S", ourDir: "N");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
// West neighbor => our W must match neighbor's E
|
||||
if (x > 0 && layout.IsWalkable(x - 1, y))
|
||||
{
|
||||
int wid = layout.Tiles[x - 1, y];
|
||||
var r = GetRequiredSocketTagFromNeighbor(wid, neighborEdgeFacingUs: "E", ourDir: "W");
|
||||
if (r != null) req.Add(r);
|
||||
}
|
||||
|
||||
return req;
|
||||
}
|
||||
private bool HasAllTags(TileDefinition t, IEnumerable<string> required)
|
||||
{
|
||||
if (!_tileTags.TryGetValue(t.Id, out var tags)) return false;
|
||||
foreach (var r in required)
|
||||
if (!tags.Contains(r)) return false;
|
||||
return true;
|
||||
}
|
||||
private bool HasTag(int tileId, string tag)
|
||||
=> _tileTags.TryGetValue(tileId, out var tags) && tags.Contains(tag);
|
||||
|
||||
private bool HasTag(TileDefinition t, string tag)
|
||||
=> _tileTags.TryGetValue(t.Id, out var tags) && tags.Contains(tag);
|
||||
|
||||
private static string Opp(string dir) => dir switch
|
||||
{
|
||||
"N" => "S",
|
||||
"S" => "N",
|
||||
"E" => "W",
|
||||
"W" => "E",
|
||||
_ => dir
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1402,8 +1791,10 @@ public class DungeonRoomV2 : DungeonRoom
|
||||
{
|
||||
return (X + cp.X, Y + cp.Y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Placed tile with details
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user