Builder changes 2

This commit is contained in:
dingo
2026-02-11 10:21:19 -05:00
parent 5da9cbedac
commit 0dfbfa75af
+40 -26
View File
@@ -22,7 +22,7 @@ public class DungeonGeneratorVNext
private readonly ILogger<DungeonGeneratorVNext>? _logger;
private DungeonPalette? _selectedPalette;
private List<BuilderRoomTemplate> _roomTemplates = new();
private readonly Dictionary<int, int> _roomTemplateUseCounts = new();
private readonly HashSet<int> _usedRoomTemplateIds = new();
private readonly Dictionary<DungeonRoom, List<TemplateMobSpawn>> _templateMobSpawnsByRoom = new();
private readonly Dictionary<(int X, int Y), List<PlacedTile>> _templateTilesByCell = new();
private readonly Dictionary<int, TileDefinition> _tileDefinitionsById = new();
@@ -45,7 +45,7 @@ public class DungeonGeneratorVNext
public async Task<DungeonLayoutV2> 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<BuilderRoomTemplate> GetTemplatesForRoomType(DungeonRoomType roomType)
{
List<BuilderRoomTemplate> candidates = roomType switch
@@ -476,37 +498,29 @@ public class DungeonGeneratorVNext
return _roomTemplates;
}
private BuilderRoomTemplate SelectRoomTemplateWithRepeatPenalty(List<BuilderRoomTemplate> templates)
private BuilderRoomTemplate? SelectWeightedRoomTemplate(List<BuilderRoomTemplate> 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<double>(candidates.Count);
foreach (var template in candidates)
int totalWeight = 0;
var weights = new List<int>(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)