Implement VNext palette-scoped tile placement with two-pass wall bucket resolution
- Replace placeholder VNext tile output with palette-driven floor/wall TileDefinition placement - Enforce wall placement flow: cardinal mask buckets first, then unresolved cells via 8-direction mask buckets - Restrict wall/floor picks to buckets available to the selected palette - Enforce wall definition selection from a single canonical wall bucket and fallback safely when unresolved - Populate TileMapSheets from actually used tile sheets in generated layouts
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace GameServer.Dungeon;
|
||||
|
||||
@@ -11,6 +14,7 @@ namespace GameServer.Dungeon;
|
||||
/// </summary>
|
||||
public class DungeonGeneratorVNext
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
private readonly int _seed;
|
||||
private readonly int _tier;
|
||||
private readonly Random _rng;
|
||||
@@ -18,6 +22,7 @@ public class DungeonGeneratorVNext
|
||||
|
||||
public DungeonGeneratorVNext(GameDbContext db, int seed, int tier = 1, ILogger<DungeonGeneratorVNext>? logger = null)
|
||||
{
|
||||
_db = db;
|
||||
_seed = seed;
|
||||
_tier = Math.Max(1, tier);
|
||||
_rng = new Random(seed);
|
||||
@@ -30,7 +35,7 @@ public class DungeonGeneratorVNext
|
||||
public int CenterY => Y + Height / 2;
|
||||
}
|
||||
|
||||
public Task<DungeonLayoutV2> GenerateAsync()
|
||||
public async Task<DungeonLayoutV2> GenerateAsync()
|
||||
{
|
||||
int size = 72 + (_tier * 8);
|
||||
if (_tier >= 6) size += (_tier - 5) * 12;
|
||||
@@ -73,7 +78,7 @@ public class DungeonGeneratorVNext
|
||||
AddLoopConnections(layout, rooms, Math.Max(2, rooms.Count / 4));
|
||||
AssignRoomTypes(rooms);
|
||||
GenerateSpawnsAndChests(layout, rooms);
|
||||
BuildSimpleTileDetails(layout);
|
||||
await BuildPaletteDrivenTileDetailsAsync(layout);
|
||||
|
||||
layout.Rooms = rooms;
|
||||
var entrance = rooms.FirstOrDefault(r => r.Type == DungeonRoomType.Entrance) ?? rooms[0];
|
||||
@@ -94,7 +99,7 @@ public class DungeonGeneratorVNext
|
||||
layout.TotalChests,
|
||||
layout.MobSpawns.Count(m => m.MobType == DungeonMobSpawnType.Boss));
|
||||
|
||||
return Task.FromResult(layout);
|
||||
return layout;
|
||||
}
|
||||
|
||||
private List<DungeonRoom> GenerateRooms(int mapWidth, int mapHeight, int targetRooms)
|
||||
@@ -470,9 +475,118 @@ public class DungeonGeneratorVNext
|
||||
return (x, y);
|
||||
}
|
||||
|
||||
private void BuildSimpleTileDetails(DungeonLayoutV2 layout)
|
||||
private async Task BuildPaletteDrivenTileDetailsAsync(DungeonLayoutV2 layout)
|
||||
{
|
||||
layout.TileDetails.Clear();
|
||||
layout.TileMapSheets.Clear();
|
||||
|
||||
var tileContext = await BuildTileContextAsync();
|
||||
|
||||
if (tileContext.FloorCandidates.Count == 0 && tileContext.WallCandidates.Count == 0)
|
||||
{
|
||||
_logger?.LogWarning("VNext tile context had no palette candidates; using fallback tile placeholders.");
|
||||
BuildFallbackTileDetails(layout);
|
||||
return;
|
||||
}
|
||||
|
||||
var usedSheetIds = new HashSet<int>();
|
||||
|
||||
for (int y = 0; y < layout.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < layout.Width; x++)
|
||||
{
|
||||
if (!layout.Walkable![x, y]) continue;
|
||||
|
||||
var floorDef = SelectFloorDefinition(tileContext);
|
||||
if (floorDef == null) continue;
|
||||
|
||||
layout.Tiles[x, y] = floorDef.Id;
|
||||
usedSheetIds.Add(floorDef.TileMapSheetId);
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
TileDefinitionId = floorDef.Id,
|
||||
SheetId = floorDef.TileMapSheetId,
|
||||
FrameIndex = floorDef.FrameIndex,
|
||||
Layer = floorDef.RenderLayer,
|
||||
Rotation = 0,
|
||||
FlipX = false,
|
||||
FlipY = false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var wallCells = new List<(int X, int Y)>();
|
||||
for (int y = 0; y < layout.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < layout.Width; x++)
|
||||
{
|
||||
if (layout.Walkable![x, y]) continue;
|
||||
if (!HasAdjacentWalkable(layout, x, y)) continue;
|
||||
wallCells.Add((x, y));
|
||||
}
|
||||
}
|
||||
|
||||
var unresolved = new List<(int X, int Y)>();
|
||||
|
||||
// Pass 1: cardinal-direction masks first.
|
||||
foreach (var cell in wallCells)
|
||||
{
|
||||
int mask4 = ComputeCardinalMask(layout, cell.X, cell.Y);
|
||||
string? bucketName = ResolveCardinalBucketName(mask4);
|
||||
if (string.IsNullOrEmpty(bucketName))
|
||||
{
|
||||
unresolved.Add(cell);
|
||||
continue;
|
||||
}
|
||||
|
||||
var wallDef = SelectWallDefinitionForBucket(tileContext, bucketName);
|
||||
if (wallDef == null)
|
||||
{
|
||||
unresolved.Add(cell);
|
||||
continue;
|
||||
}
|
||||
|
||||
PlaceWallTile(layout, cell.X, cell.Y, wallDef);
|
||||
usedSheetIds.Add(wallDef.TileMapSheetId);
|
||||
}
|
||||
|
||||
// Pass 2: only fill unresolved cells with 8-direction masks.
|
||||
foreach (var cell in unresolved)
|
||||
{
|
||||
int mask8 = ComputeEightDirectionMask(layout, cell.X, cell.Y);
|
||||
string? bucketName = ResolveEightDirectionBucketName(mask8);
|
||||
var wallDef = !string.IsNullOrEmpty(bucketName)
|
||||
? SelectWallDefinitionForBucket(tileContext, bucketName)
|
||||
: null;
|
||||
|
||||
wallDef ??= SelectFallbackWallDefinition(tileContext);
|
||||
if (wallDef == null) continue;
|
||||
|
||||
PlaceWallTile(layout, cell.X, cell.Y, wallDef);
|
||||
usedSheetIds.Add(wallDef.TileMapSheetId);
|
||||
}
|
||||
|
||||
foreach (int sheetId in usedSheetIds)
|
||||
{
|
||||
if (!tileContext.SheetsById.TryGetValue(sheetId, out var sheet)) continue;
|
||||
layout.TileMapSheets.Add(new TileMapSheetInfo
|
||||
{
|
||||
Id = sheet.Id,
|
||||
CdnPath = sheet.CdnPath,
|
||||
TileWidth = sheet.TileWidth,
|
||||
TileHeight = sheet.TileHeight,
|
||||
SheetWidth = sheet.SheetWidth,
|
||||
SheetHeight = sheet.SheetHeight,
|
||||
Columns = sheet.Columns,
|
||||
Rows = sheet.Rows
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void BuildFallbackTileDetails(DungeonLayoutV2 layout)
|
||||
{
|
||||
for (int y = 0; y < layout.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < layout.Width; x++)
|
||||
@@ -495,6 +609,302 @@ public class DungeonGeneratorVNext
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<TileBuildContext> BuildTileContextAsync()
|
||||
{
|
||||
var sheets = await _db.TileMapSheets
|
||||
.Where(s => s.IsEnabled)
|
||||
.ToListAsync();
|
||||
var sheetsById = sheets.ToDictionary(s => s.Id);
|
||||
|
||||
var palette = await SelectPaletteAsync();
|
||||
var allowedBucketIds = ParseIds(palette?.AllowedBucketIdsJson).ToHashSet();
|
||||
|
||||
var allTiles = await _db.TileDefinitions
|
||||
.Where(t => t.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
var links = await _db.TileDefinitionBuckets.ToListAsync();
|
||||
var tileBuckets = new Dictionary<int, HashSet<int>>();
|
||||
foreach (var link in links)
|
||||
{
|
||||
if (!tileBuckets.TryGetValue(link.TileDefinitionId, out var set))
|
||||
{
|
||||
set = new HashSet<int>();
|
||||
tileBuckets[link.TileDefinitionId] = set;
|
||||
}
|
||||
set.Add(link.TileBucketId);
|
||||
}
|
||||
|
||||
var buckets = await _db.TileBuckets
|
||||
.Where(b => b.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
var bucketNamesById = buckets.ToDictionary(b => b.Id, b => b.Name);
|
||||
var bucketIdsByName = buckets
|
||||
.GroupBy(b => b.Name.Trim().ToLowerInvariant())
|
||||
.ToDictionary(g => g.Key, g => g.First().Id);
|
||||
|
||||
bool TileAllowedByPalette(TileDefinition t)
|
||||
{
|
||||
if (allowedBucketIds.Count == 0) return true;
|
||||
if (!tileBuckets.TryGetValue(t.Id, out var ids) || ids.Count == 0) return false;
|
||||
return ids.Overlaps(allowedBucketIds);
|
||||
}
|
||||
|
||||
var floorCandidates = allTiles
|
||||
.Where(t => t.IsWalkable && t.Type != TileType.Wall && t.Type != TileType.WallDecor)
|
||||
.Where(TileAllowedByPalette)
|
||||
.ToList();
|
||||
|
||||
var wallCandidates = allTiles
|
||||
.Where(t => !t.IsWalkable || t.Type == TileType.Wall || t.Type == TileType.WallDecor)
|
||||
.Where(TileAllowedByPalette)
|
||||
.ToList();
|
||||
|
||||
var canonicalWallBucketIds = ResolveCanonicalWallBucketIds(bucketIdsByName, allowedBucketIds);
|
||||
var wallDefsByCanonicalBucket = new Dictionary<string, List<TileDefinition>>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var wallDef in wallCandidates)
|
||||
{
|
||||
if (!tileBuckets.TryGetValue(wallDef.Id, out var defBuckets) || defBuckets.Count == 0) continue;
|
||||
|
||||
var canonicalMatches = new List<string>();
|
||||
foreach (var kvp in canonicalWallBucketIds)
|
||||
{
|
||||
if (defBuckets.Contains(kvp.Value))
|
||||
{
|
||||
canonicalMatches.Add(kvp.Key);
|
||||
}
|
||||
}
|
||||
|
||||
// Enforce "wall tiles belong to one wall bucket" for placement selection.
|
||||
if (canonicalMatches.Count != 1) continue;
|
||||
|
||||
string canonical = canonicalMatches[0];
|
||||
if (!wallDefsByCanonicalBucket.TryGetValue(canonical, out var defs))
|
||||
{
|
||||
defs = new List<TileDefinition>();
|
||||
wallDefsByCanonicalBucket[canonical] = defs;
|
||||
}
|
||||
defs.Add(wallDef);
|
||||
}
|
||||
|
||||
_logger?.LogInformation(
|
||||
"VNext tile context: palette={PaletteId}, allowedBuckets={AllowedBuckets}, floorDefs={FloorDefs}, wallDefs={WallDefs}, resolvedWallBuckets={ResolvedBuckets}",
|
||||
palette?.Id ?? 0,
|
||||
allowedBucketIds.Count,
|
||||
floorCandidates.Count,
|
||||
wallCandidates.Count,
|
||||
wallDefsByCanonicalBucket.Count);
|
||||
|
||||
return new TileBuildContext(
|
||||
sheetsById,
|
||||
floorCandidates,
|
||||
wallCandidates,
|
||||
wallDefsByCanonicalBucket,
|
||||
bucketNamesById);
|
||||
}
|
||||
|
||||
private async Task<DungeonPalette?> SelectPaletteAsync()
|
||||
{
|
||||
var palettes = await _db.DungeonPalettes
|
||||
.Where(p => p.IsEnabled)
|
||||
.OrderBy(p => p.Id)
|
||||
.ToListAsync();
|
||||
if (palettes.Count == 0) return null;
|
||||
return palettes[_rng.Next(palettes.Count)];
|
||||
}
|
||||
|
||||
private static List<int> ParseIds(string? json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json)) return new List<int>();
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<List<int>>(json) ?? new List<int>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<int>();
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<string, int> ResolveCanonicalWallBucketIds(
|
||||
Dictionary<string, int> bucketIdsByName,
|
||||
HashSet<int> allowedBucketIds)
|
||||
{
|
||||
var resolved = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var alias in WallBucketAliases)
|
||||
{
|
||||
foreach (var name in alias.Value)
|
||||
{
|
||||
if (!bucketIdsByName.TryGetValue(name, out int id)) continue;
|
||||
if (allowedBucketIds.Count > 0 && !allowedBucketIds.Contains(id)) continue;
|
||||
resolved[alias.Key] = id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
private TileDefinition? SelectFloorDefinition(TileBuildContext context)
|
||||
{
|
||||
if (context.FloorCandidates.Count == 0) return null;
|
||||
return context.FloorCandidates[_rng.Next(context.FloorCandidates.Count)];
|
||||
}
|
||||
|
||||
private TileDefinition? SelectWallDefinitionForBucket(TileBuildContext context, string canonicalBucketName)
|
||||
{
|
||||
if (!context.WallDefinitionsByCanonicalBucket.TryGetValue(canonicalBucketName, out var defs) || defs.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return defs[_rng.Next(defs.Count)];
|
||||
}
|
||||
|
||||
private TileDefinition? SelectFallbackWallDefinition(TileBuildContext context)
|
||||
{
|
||||
if (context.WallCandidates.Count == 0) return null;
|
||||
return context.WallCandidates[_rng.Next(context.WallCandidates.Count)];
|
||||
}
|
||||
|
||||
private static void PlaceWallTile(DungeonLayoutV2 layout, int x, int y, TileDefinition def)
|
||||
{
|
||||
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 = def.RenderLayer,
|
||||
Rotation = 0,
|
||||
FlipX = false,
|
||||
FlipY = false
|
||||
});
|
||||
}
|
||||
|
||||
private static bool HasAdjacentWalkable(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
for (int oy = -1; oy <= 1; oy++)
|
||||
{
|
||||
for (int ox = -1; ox <= 1; ox++)
|
||||
{
|
||||
if (ox == 0 && oy == 0) continue;
|
||||
int nx = x + ox;
|
||||
int ny = y + oy;
|
||||
if (nx < 0 || ny < 0 || nx >= layout.Width || ny >= layout.Height) continue;
|
||||
if (layout.Walkable![nx, ny]) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int ComputeCardinalMask(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
// Bit order N, E, S, W (MSB -> LSB).
|
||||
int n = IsWalkable(layout, x, y - 1) ? 1 : 0;
|
||||
int e = IsWalkable(layout, x + 1, y) ? 1 : 0;
|
||||
int s = IsWalkable(layout, x, y + 1) ? 1 : 0;
|
||||
int w = IsWalkable(layout, x - 1, y) ? 1 : 0;
|
||||
return (n << 3) | (e << 2) | (s << 1) | w;
|
||||
}
|
||||
|
||||
private static int ComputeEightDirectionMask(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
// Bit order N, NE, E, SE, S, SW, W, NW (MSB -> LSB).
|
||||
int n = IsWalkable(layout, x, y - 1) ? 1 : 0;
|
||||
int ne = IsWalkable(layout, x + 1, y - 1) ? 1 : 0;
|
||||
int e = IsWalkable(layout, x + 1, y) ? 1 : 0;
|
||||
int se = IsWalkable(layout, x + 1, y + 1) ? 1 : 0;
|
||||
int s = IsWalkable(layout, x, y + 1) ? 1 : 0;
|
||||
int sw = IsWalkable(layout, x - 1, y + 1) ? 1 : 0;
|
||||
int w = IsWalkable(layout, x - 1, y) ? 1 : 0;
|
||||
int nw = IsWalkable(layout, x - 1, y - 1) ? 1 : 0;
|
||||
|
||||
return (n << 7) | (ne << 6) | (e << 5) | (se << 4) | (s << 3) | (sw << 2) | (w << 1) | nw;
|
||||
}
|
||||
|
||||
private static bool IsWalkable(DungeonLayoutV2 layout, int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= layout.Width || y >= layout.Height) return false;
|
||||
return layout.Walkable![x, y];
|
||||
}
|
||||
|
||||
private static string? ResolveCardinalBucketName(int mask4)
|
||||
{
|
||||
foreach (var mapping in CardinalBucketMasks)
|
||||
{
|
||||
if (mapping.Value.Contains(mask4))
|
||||
{
|
||||
return mapping.Key;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string? ResolveEightDirectionBucketName(int mask8)
|
||||
{
|
||||
foreach (var mapping in EightDirectionBucketMasks)
|
||||
{
|
||||
if (mapping.Value.Contains(mask8))
|
||||
{
|
||||
return mapping.Key;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private sealed record TileBuildContext(
|
||||
Dictionary<int, TileMapSheet> SheetsById,
|
||||
List<TileDefinition> FloorCandidates,
|
||||
List<TileDefinition> WallCandidates,
|
||||
Dictionary<string, List<TileDefinition>> WallDefinitionsByCanonicalBucket,
|
||||
Dictionary<int, string> BucketNamesById);
|
||||
|
||||
private static readonly Dictionary<string, string[]> WallBucketAliases = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["WallTop"] = new[] { "walltop" },
|
||||
["WallSideLeft"] = new[] { "wallsideleft" },
|
||||
["WallSideRight"] = new[] { "wallsideright" },
|
||||
["WallBottom"] = new[] { "wallbottom", "wallbottm" },
|
||||
["WallInnerCornerDownLeft"] = new[] { "wallinnercornerdownleft" },
|
||||
["WallInnerCornerDownRight"] = new[] { "wallinnercornerdownright" },
|
||||
["WallDiagonalCornerDownLeft"] = new[] { "walldiagonalcornerdownleft" },
|
||||
["WallDiagonalCornerDownRight"] = new[] { "walldiagonalcornerdownright" },
|
||||
["WallDiagonalCornerUpLeft"] = new[] { "walldiagonalcornerupleft" },
|
||||
["WallDiagonalCornerUpRight"] = new[] { "walldiagonalcornerupright" },
|
||||
["WallFull"] = new[] { "wallfull" },
|
||||
["WallFullEightDirections"] = new[] { "wallfulleightdirections" },
|
||||
["WallBottomEightDirections"] = new[] { "wallbottmeightdirections", "wallbottomeightdirections" }
|
||||
};
|
||||
|
||||
private static readonly KeyValuePair<string, HashSet<int>>[] CardinalBucketMasks =
|
||||
{
|
||||
new("WallTop", new HashSet<int> { 15, 6, 3, 2, 10, 12, 14, 11, 7 }),
|
||||
new("WallSideLeft", new HashSet<int> { 4 }),
|
||||
new("WallSideRight", new HashSet<int> { 1 }),
|
||||
new("WallBottom", new HashSet<int> { 8 }),
|
||||
new("WallFull", new HashSet<int> { 13, 5, 9 })
|
||||
};
|
||||
|
||||
private static readonly KeyValuePair<string, HashSet<int>>[] EightDirectionBucketMasks =
|
||||
{
|
||||
new("WallInnerCornerDownLeft", new HashSet<int> { 241, 224, 240, 225, 160, 81, 209, 97, 208, 113, 17, 177, 161, 144, 49, 176, 33, 145 }),
|
||||
new("WallInnerCornerDownRight", new HashSet<int> { 199, 195, 131, 135, 130, 69, 197, 67, 133, 71, 68, 198, 194, 132, 70, 134, 196, 66 }),
|
||||
new("WallDiagonalCornerDownLeft", new HashSet<int> { 64 }),
|
||||
new("WallDiagonalCornerDownRight", new HashSet<int> { 1 }),
|
||||
new("WallDiagonalCornerUpLeft", new HashSet<int> { 16, 80 }),
|
||||
new("WallDiagonalCornerUpRight", new HashSet<int> { 4, 5 }),
|
||||
new("WallBottomEightDirections", new HashSet<int> { 65 }),
|
||||
new("WallFullEightDirections", new HashSet<int> { 20, 228, 147, 116, 23, 22, 52, 21, 84, 18, 36, 19, 100, 151, 244, 150, 180, 229, 211, 245, 215, 117, 87, 101, 83, 82, 37, 53, 86, 213, 212, 149 })
|
||||
};
|
||||
|
||||
private static float Distance(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
int dx = x2 - x1;
|
||||
|
||||
Reference in New Issue
Block a user