Builder changes 2
This commit is contained in:
@@ -22,7 +22,7 @@ public class DungeonGeneratorVNext
|
|||||||
private readonly ILogger<DungeonGeneratorVNext>? _logger;
|
private readonly ILogger<DungeonGeneratorVNext>? _logger;
|
||||||
private DungeonPalette? _selectedPalette;
|
private DungeonPalette? _selectedPalette;
|
||||||
private List<BuilderRoomTemplate> _roomTemplates = new();
|
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<DungeonRoom, List<TemplateMobSpawn>> _templateMobSpawnsByRoom = new();
|
||||||
private readonly Dictionary<(int X, int Y), List<PlacedTile>> _templateTilesByCell = new();
|
private readonly Dictionary<(int X, int Y), List<PlacedTile>> _templateTilesByCell = new();
|
||||||
private readonly Dictionary<int, TileDefinition> _tileDefinitionsById = new();
|
private readonly Dictionary<int, TileDefinition> _tileDefinitionsById = new();
|
||||||
@@ -45,7 +45,7 @@ public class DungeonGeneratorVNext
|
|||||||
|
|
||||||
public async Task<DungeonLayoutV2> GenerateAsync()
|
public async Task<DungeonLayoutV2> GenerateAsync()
|
||||||
{
|
{
|
||||||
_roomTemplateUseCounts.Clear();
|
_usedRoomTemplateIds.Clear();
|
||||||
_templateMobSpawnsByRoom.Clear();
|
_templateMobSpawnsByRoom.Clear();
|
||||||
_templateTilesByCell.Clear();
|
_templateTilesByCell.Clear();
|
||||||
_tileDefinitionsById.Clear();
|
_tileDefinitionsById.Clear();
|
||||||
@@ -426,9 +426,18 @@ public class DungeonGeneratorVNext
|
|||||||
var candidates = GetTemplatesForRoomType(room.Type);
|
var candidates = GetTemplatesForRoomType(room.Type);
|
||||||
if (candidates.Count == 0) continue;
|
if (candidates.Count == 0) continue;
|
||||||
|
|
||||||
var template = SelectRoomTemplateWithRepeatPenalty(candidates);
|
// Do not keep reusing the same template in a single dungeon.
|
||||||
_roomTemplateUseCounts.TryGetValue(template.Id, out int useCount);
|
var availableCandidates = candidates
|
||||||
_roomTemplateUseCounts[template.Id] = useCount + 1;
|
.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.TemplateId = template.Id;
|
||||||
room.TemplateName = template.Name;
|
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)
|
private List<BuilderRoomTemplate> GetTemplatesForRoomType(DungeonRoomType roomType)
|
||||||
{
|
{
|
||||||
List<BuilderRoomTemplate> candidates = roomType switch
|
List<BuilderRoomTemplate> candidates = roomType switch
|
||||||
@@ -476,37 +498,29 @@ public class DungeonGeneratorVNext
|
|||||||
return _roomTemplates;
|
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];
|
if (templates.Count == 1) return templates[0];
|
||||||
|
|
||||||
var unusedTemplates = templates
|
int totalWeight = 0;
|
||||||
.Where(t => !_roomTemplateUseCounts.ContainsKey(t.Id))
|
var weights = new List<int>(templates.Count);
|
||||||
.ToList();
|
foreach (var template in templates)
|
||||||
var candidates = unusedTemplates.Count > 0 ? unusedTemplates : templates;
|
|
||||||
|
|
||||||
double totalWeight = 0;
|
|
||||||
var adjustedWeights = new List<double>(candidates.Count);
|
|
||||||
|
|
||||||
foreach (var template in candidates)
|
|
||||||
{
|
{
|
||||||
int baseWeight = Math.Max(1, template.Weight);
|
int baseWeight = Math.Max(1, template.Weight);
|
||||||
_roomTemplateUseCounts.TryGetValue(template.Id, out int repeatCount);
|
weights.Add(baseWeight);
|
||||||
double adjustedWeight = baseWeight / Math.Pow(3.0d, repeatCount);
|
totalWeight += baseWeight;
|
||||||
adjustedWeight = Math.Max(0.01d, adjustedWeight);
|
|
||||||
adjustedWeights.Add(adjustedWeight);
|
|
||||||
totalWeight += adjustedWeight;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double roll = _rng.NextDouble() * totalWeight;
|
int roll = _rng.Next(totalWeight);
|
||||||
double cumulative = 0;
|
int cumulative = 0;
|
||||||
for (int i = 0; i < candidates.Count; i++)
|
for (int i = 0; i < templates.Count; i++)
|
||||||
{
|
{
|
||||||
cumulative += adjustedWeights[i];
|
cumulative += weights[i];
|
||||||
if (roll < cumulative) return candidates[i];
|
if (roll < cumulative) return templates[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return candidates[^1];
|
return templates[^1];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StampRoomTemplateTiles(DungeonLayoutV2 layout, DungeonRoom room, BuilderRoomTemplate template)
|
private void StampRoomTemplateTiles(DungeonLayoutV2 layout, DungeonRoom room, BuilderRoomTemplate template)
|
||||||
|
|||||||
Reference in New Issue
Block a user