From 0dfbfa75afdbe8ccef7dcd444b1ed4fe4c7e9e5a Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 11 Feb 2026 10:21:19 -0500 Subject: [PATCH] Builder changes 2 --- Game/Dungeon/DungeonGeneratorVNext.cs | 66 ++++++++++++++++----------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorVNext.cs b/Game/Dungeon/DungeonGeneratorVNext.cs index 5203781..e6a6e12 100644 --- a/Game/Dungeon/DungeonGeneratorVNext.cs +++ b/Game/Dungeon/DungeonGeneratorVNext.cs @@ -22,7 +22,7 @@ public class DungeonGeneratorVNext private readonly ILogger? _logger; private DungeonPalette? _selectedPalette; private List _roomTemplates = new(); - private readonly Dictionary _roomTemplateUseCounts = new(); + private readonly HashSet _usedRoomTemplateIds = new(); private readonly Dictionary> _templateMobSpawnsByRoom = new(); private readonly Dictionary<(int X, int Y), List> _templateTilesByCell = new(); private readonly Dictionary _tileDefinitionsById = new(); @@ -45,7 +45,7 @@ public class DungeonGeneratorVNext public async Task GenerateAsync() { - _roomTemplateUseCounts.Clear(); + _usedRoomTemplateIds.Clear(); _templateMobSpawnsByRoom.Clear(); _templateTilesByCell.Clear(); _tileDefinitionsById.Clear(); @@ -426,9 +426,18 @@ public class DungeonGeneratorVNext var candidates = GetTemplatesForRoomType(room.Type); if (candidates.Count == 0) continue; - var template = SelectRoomTemplateWithRepeatPenalty(candidates); - _roomTemplateUseCounts.TryGetValue(template.Id, out int useCount); - _roomTemplateUseCounts[template.Id] = useCount + 1; + // Do not keep reusing the same template in a single dungeon. + var availableCandidates = candidates + .Where(t => !_usedRoomTemplateIds.Contains(t.Id)) + .ToList(); + if (availableCandidates.Count == 0) continue; + + // Each room independently rolls whether to be template-based or procedural. + if (!ShouldUseTemplateForRoom(room.Type)) continue; + + var template = SelectWeightedRoomTemplate(availableCandidates); + if (template == null) continue; + _usedRoomTemplateIds.Add(template.Id); room.TemplateId = template.Id; room.TemplateName = template.Name; @@ -452,6 +461,19 @@ public class DungeonGeneratorVNext } } + private bool ShouldUseTemplateForRoom(DungeonRoomType roomType) + { + // Leaves room generation mixed: some runs use few templates, some many. + double chance = roomType switch + { + DungeonRoomType.Entrance => 0.45, + DungeonRoomType.Boss => 0.45, + DungeonRoomType.Treasure => 0.40, + _ => 0.30 + }; + return _rng.NextDouble() < chance; + } + private List GetTemplatesForRoomType(DungeonRoomType roomType) { List candidates = roomType switch @@ -476,37 +498,29 @@ public class DungeonGeneratorVNext return _roomTemplates; } - private BuilderRoomTemplate SelectRoomTemplateWithRepeatPenalty(List templates) + private BuilderRoomTemplate? SelectWeightedRoomTemplate(List templates) { + if (templates.Count == 0) return null; if (templates.Count == 1) return templates[0]; - var unusedTemplates = templates - .Where(t => !_roomTemplateUseCounts.ContainsKey(t.Id)) - .ToList(); - var candidates = unusedTemplates.Count > 0 ? unusedTemplates : templates; - - double totalWeight = 0; - var adjustedWeights = new List(candidates.Count); - - foreach (var template in candidates) + int totalWeight = 0; + var weights = new List(templates.Count); + foreach (var template in templates) { int baseWeight = Math.Max(1, template.Weight); - _roomTemplateUseCounts.TryGetValue(template.Id, out int repeatCount); - double adjustedWeight = baseWeight / Math.Pow(3.0d, repeatCount); - adjustedWeight = Math.Max(0.01d, adjustedWeight); - adjustedWeights.Add(adjustedWeight); - totalWeight += adjustedWeight; + weights.Add(baseWeight); + totalWeight += baseWeight; } - double roll = _rng.NextDouble() * totalWeight; - double cumulative = 0; - for (int i = 0; i < candidates.Count; i++) + int roll = _rng.Next(totalWeight); + int cumulative = 0; + for (int i = 0; i < templates.Count; i++) { - cumulative += adjustedWeights[i]; - if (roll < cumulative) return candidates[i]; + cumulative += weights[i]; + if (roll < cumulative) return templates[i]; } - return candidates[^1]; + return templates[^1]; } private void StampRoomTemplateTiles(DungeonLayoutV2 layout, DungeonRoom room, BuilderRoomTemplate template)