diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index dda30f2..0881397 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -16,6 +16,7 @@ public class DungeonGeneratorV2 private readonly int _seed; private readonly int _tier; private readonly int _tileSize = 32; + private int _activeFamilyId = 1; private TileTagIndex? _tagIndex; private const string DefaultBiome = "dungeon"; @@ -67,39 +68,6 @@ public class DungeonGeneratorV2 return c; } - //private TileDefinition? SelectBestFloorTileForTags(HashSet requiredTags) - //{ - // var floors = _tileDefinitions.Values.Where(t => t.Type == TileType.Floor).ToList(); - // if (floors.Count == 0) return null; - - // if (requiredTags.Count == 0) - // return floors[0]; - - // TileDefinition best = floors[0]; - // int bestScore = -1; - // foreach (var f in floors) - // { - // var tags = ParseTags(f.Tags); - // int score = CountOverlap(requiredTags, tags); - // if (score > bestScore) - // { - // best = f; - // bestScore = score; - // } - // } - // return best; - //} - private TileDefinition? SelectBestFloorTileForTags(HashSet requiredTags) - { - // If you want biome-aware floors later, pass biome from generator settings. - if (_tagIndex != null) - return _tagIndex.FindBestFloor(requiredTags, biome: DefaultBiome); - - // Fallback to your old behavior if index isn't available for some reason - var floors = _tileDefinitions.Values.Where(t => t.Type == TileType.Floor).ToList(); - return floors.FirstOrDefault(); - } - private RoomTemplate? SelectHallwayTemplate(HashSet requiredTags, string direction) { if (_hallwayTemplates.Count == 0) return null; @@ -154,27 +122,7 @@ public class DungeonGeneratorV2 return best; } - private TileDefinition? PickRandomTile(HashSet tags) - { - // Generate a cache key based on sorted tags (e.g., "dungeon,floor,stone") - string key = string.Join(",", tags.OrderBy(t => t)); - - if (!_tileGroups.ContainsKey(key)) - { - // Find all matches, not just the single best one - var matches = _tileDefinitions.Values - .Where(t => tags.All(req => ParseTags(t.Tags).Contains(req))) - .ToList(); - - _tileGroups[key] = matches; - } - - var candidates = _tileGroups[key]; - if (candidates.Count == 0) return null; - - // Return a random one from the list - return candidates[_rng.Next(candidates.Count)]; - } + private bool TryStampHallwayRun( DungeonLayoutV2 layout, RoomTemplate hallway, @@ -390,79 +338,6 @@ public class DungeonGeneratorV2 return layout; } - //private async Task LoadTemplatesAsync() - //{ - // var templates = await _db.RoomTemplates - // .Where(r => r.IsEnabled) - // .Where(r => r.MinTier <= _tier && (r.MaxTier == 0 || r.MaxTier >= _tier)) - // .ToListAsync(); - - // _entranceTemplates = templates.Where(t => t.Type == RoomType.Entrance).ToList(); - // _exitTemplates = templates.Where(t => t.Type == RoomType.Exit).ToList(); - // _bossTemplates = templates.Where(t => t.Type == RoomType.Boss).ToList(); - // _treasureTemplates = templates.Where(t => t.Type == RoomType.Treasure).ToList(); - // _normalTemplates = templates.Where(t => t.Type == RoomType.Normal).ToList(); - // _hallwayTemplates = templates.Where(t => t.Type == RoomType.Hallway).ToList(); - - // Console.WriteLine($"[DungeonGenV2] Loaded templates for tier {_tier}: Entrance={_entranceTemplates.Count}, Exit={_exitTemplates.Count}, Boss={_bossTemplates.Count}, Treasure={_treasureTemplates.Count}, Normal={_normalTemplates.Count}"); - - // // Load tile definitions from templates - // var tileIds = templates - // .Where(t => !string.IsNullOrEmpty(t.TileDataJson)) - // .SelectMany(t => { - // try - // { - // var placements = JsonSerializer.Deserialize>(t.TileDataJson!); - // return placements?.Select(p => p.TileDefinitionId) ?? Enumerable.Empty(); - // } - // catch { return Enumerable.Empty(); } - // }) - // .Distinct() - // .ToList(); - - // if (tileIds.Any()) - // { - // var tiles = await _db.TileDefinitions - // .Include(t => t.TileMapSheet) - // .Where(t => tileIds.Contains(t.Id)) - // .ToListAsync(); - - // _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"); - // } - - // // Always load a default sheet for fallback generation - // if (_defaultSheet == null) - // { - // _defaultSheet = await _db.TileMapSheets - // .Include(s => s.Tiles) - // .FirstOrDefaultAsync(s => s.IsEnabled); - - // if (_defaultSheet?.Tiles != null) - // { - // foreach (var tile in _defaultSheet.Tiles) - // { - // tile.TileMapSheet = _defaultSheet; // Ensure navigation property is set - // _tileDefinitions[tile.Id] = tile; - // } - // Console.WriteLine($"[DungeonGenV2] Loaded {_defaultSheet.Tiles.Count} tiles from default sheet '{_defaultSheet.Name}'"); - // } - // else - // { - // Console.WriteLine($"[DungeonGenV2] WARNING: No tilemap sheets found in database!"); - // } - // } - - // Console.WriteLine($"[DungeonGenV2] Total tile definitions: {_tileDefinitions.Count}"); - //} private async Task LoadTemplatesAsync() { var templates = await _db.RoomTemplates @@ -569,6 +444,7 @@ public class DungeonGeneratorV2 Console.WriteLine($"[DungeonGenV2] WARNING: No tile definitions available; tag index/caches not built."); } + ChooseDungeonFamilyFromSheet(); } private RoomType DetermineNextRoomType(List existing, int targetCount) @@ -889,7 +765,7 @@ public class DungeonGeneratorV2 const bool VerboseFloorLogging = true; // 1) Decide which TileBase family to use for this room. - int familyId = 2; + int familyId = _activeFamilyId; Console.WriteLine($"[DungeonGenV2][Floors] CarveBasicRoom START room=({room.X},{room.Y}) size=({room.Width}x{room.Height}) familyId={familyId} tags=[{string.Join(",", room.TemplateTags ?? new HashSet())}]"); @@ -1094,7 +970,7 @@ public class DungeonGeneratorV2 /// private void RepairRoomFloorSockets(DungeonLayoutV2 layout, DungeonRoomV2 room) { - int familyId = 2; + int familyId = _activeFamilyId; for (int x = room.X; x < room.X + room.Width; x++) { @@ -1203,12 +1079,14 @@ public class DungeonGeneratorV2 .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, primaryTag)) .Where(t => HasAllTags(t, requiredSocketTags)) + .Where(t => CompatibleWithAllNeighbors(layout, x, y, t)) .ToList(); var variance = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, varianceTag)) .Where(t => HasAllTags(t, requiredSocketTags)) + .Where(t => CompatibleWithAllNeighbors(layout, x, y, t)) .ToList(); if (primary.Count == 0 && variance.Count == 0) @@ -1220,7 +1098,28 @@ public class DungeonGeneratorV2 return pool.Count == 0 ? null : pool[_rng.Next(pool.Count)]; } + + private bool CompatibleWithAllNeighbors( + DungeonLayoutV2 layout, int x, int y, TileDefinition t) + { + if (y > 0 && layout.IsWalkable(x, y - 1)) + if (!EdgeSocketsCompatible(t.Id, "N", layout.Tiles[x, y - 1], "S")) + return false; + if (y < layout.Height - 1 && layout.IsWalkable(x, y + 1)) + if (!EdgeSocketsCompatible(t.Id, "S", layout.Tiles[x, y + 1], "N")) + return false; + + if (x > 0 && layout.IsWalkable(x - 1, y)) + if (!EdgeSocketsCompatible(t.Id, "W", layout.Tiles[x - 1, y], "E")) + return false; + + if (x < layout.Width - 1 && layout.IsWalkable(x + 1, y)) + if (!EdgeSocketsCompatible(t.Id, "E", layout.Tiles[x + 1, y], "W")) + return false; + + return true; + } //private async Task ConnectRoomsAsync(DungeonLayoutV2 layout, DungeonRoomV2 a, DungeonRoomV2 b) //{ // // Determine tag requirements for this connection. @@ -1397,9 +1296,7 @@ public class DungeonGeneratorV2 endY = b.CenterY; } - // ---- NEW: pick a corridor floor tile via PickRandomFloorTile (family-aware) ---- - // TODO: derive familyId from room tags if/when you add that convention - int familyId = 2; + int familyId = _activeFamilyId; // Pick based on start position (or midpoint — either is fine) var floorTile = @@ -2076,12 +1973,14 @@ public class DungeonGeneratorV2 .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, primaryTag)) .Where(t => HasAllTags(t, requiredSocketTags)) + .Where(t => CompatibleWithPlacedNeighbors(layout, x, y, t)) .ToList(); var variance = _tileDefinitions.Values .Where(t => t.Type == TileType.Floor) .Where(t => HasTag(t, varianceTag)) .Where(t => HasAllTags(t, requiredSocketTags)) + .Where(t => CompatibleWithPlacedNeighbors(layout, x, y, t)) .ToList(); if (primary.Count == 0 && variance.Count == 0) @@ -2107,6 +2006,26 @@ public class DungeonGeneratorV2 return pool.Count == 0 ? null : pool[_rng.Next(pool.Count)]; } + private bool CompatibleWithPlacedNeighbors(DungeonLayoutV2 layout, int x, int y, TileDefinition t) + { + // North neighbor + if (y > 0 && layout.IsWalkable(x, y - 1)) + { + int nId = layout.Tiles[x, y - 1]; + if (!EdgeSocketsCompatible(t.Id, "N", nId, "S")) + return false; + } + + // West neighbor + if (x > 0 && layout.IsWalkable(x - 1, y)) + { + int wId = layout.Tiles[x - 1, y]; + if (!EdgeSocketsCompatible(t.Id, "W", wId, "E")) + return false; + } + + return true; + } private static string OppDir(string dir) => dir switch { @@ -2188,6 +2107,68 @@ public class DungeonGeneratorV2 _ => dir }; + private void ChooseDungeonFamilyFromSheet() + { + if (_tileDefinitions.Count == 0) { _activeFamilyId = 1; return; } + + var families = new HashSet(); + + foreach (var t in _tileDefinitions.Values) + { + if (t.Type != TileType.Floor) continue; + if (!_tileTags.TryGetValue(t.Id, out var tags)) continue; + + foreach (var tag in tags) + { + // TileBaseCore_2 + if (tag.StartsWith("TileBaseCore_", StringComparison.OrdinalIgnoreCase) && + int.TryParse(tag.Substring("TileBaseCore_".Length), out var fam)) + { + families.Add(fam); + } + } + } + + _activeFamilyId = families.Count > 0 ? families.ElementAt(_rng.Next(families.Count)) : 1; + Console.WriteLine($"[DungeonGenV2] Chosen dungeon tile familyId={_activeFamilyId}"); + } + private bool EdgeSocketsCompatible(int aId, string aEdge, int bId, string bEdge) + { + _tileSocketTypes.TryGetValue(aId, out var aMap); + _tileSocketTypes.TryGetValue(bId, out var bMap); + + aMap ??= new Dictionary>(); + bMap ??= new Dictionary>(); + + aMap.TryGetValue(aEdge, out var aTypes); + bMap.TryGetValue(bEdge, out var bTypes); + + aTypes ??= new HashSet(); + bTypes ??= new HashSet(); + + bool aHas = aTypes.Count > 0; + bool bHas = bTypes.Count > 0; + + if (!aHas && !bHas) return true; // neither cares + if (aHas != bHas) return false; // one cares, other doesn't + + // both have sockets + bool aTyped = aTypes.Any(t => !string.IsNullOrEmpty(t)); + bool bTyped = bTypes.Any(t => !string.IsNullOrEmpty(t)); + + if (!aTyped && !bTyped) + return true; // both untyped + + var aTypedSet = aTypes + .Where(t => !string.IsNullOrEmpty(t)) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + var bTypedSet = bTypes + .Where(t => !string.IsNullOrEmpty(t)) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + return aTypedSet.Overlaps(bTypedSet); + } }