Dungeon Sockets may be fixed
This commit is contained in:
@@ -1288,11 +1288,14 @@ public class DungeonGeneratorV3
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get socket-compatible templates
|
// Get socket-compatible templates (already sorted by score, best first)
|
||||||
var compatibleTemplates = GetCompatibleTemplates(_floorTemplates, x, y, layout);
|
var compatibleTemplates = GetCompatibleTemplates(_floorTemplates, x, y, layout);
|
||||||
|
|
||||||
// Shuffle templates by weight, then try each until one has valid TilesJson
|
// Get the best templates (those with the highest score)
|
||||||
var shuffledTemplates = ShuffleByWeight(compatibleTemplates);
|
var bestTemplates = GetTopScoredTemplates(compatibleTemplates, x, y, layout);
|
||||||
|
|
||||||
|
// Shuffle only among the best templates by weight
|
||||||
|
var shuffledTemplates = ShuffleByWeight(bestTemplates);
|
||||||
|
|
||||||
foreach (var template in shuffledTemplates)
|
foreach (var template in shuffledTemplates)
|
||||||
{
|
{
|
||||||
@@ -1461,11 +1464,14 @@ public class DungeonGeneratorV3
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get socket-compatible templates
|
// Get socket-compatible templates (already sorted by score, best first)
|
||||||
var compatibleTemplates = GetCompatibleTemplates(_wallTemplates, x, y, layout);
|
var compatibleTemplates = GetCompatibleTemplates(_wallTemplates, x, y, layout);
|
||||||
|
|
||||||
// Shuffle templates by weight, then try each until one has valid TilesJson
|
// Get the best templates (those with the highest score)
|
||||||
var shuffledTemplates = ShuffleByWeight(compatibleTemplates);
|
var bestTemplates = GetTopScoredTemplates(compatibleTemplates, x, y, layout);
|
||||||
|
|
||||||
|
// Shuffle only among the best templates by weight
|
||||||
|
var shuffledTemplates = ShuffleByWeight(bestTemplates);
|
||||||
|
|
||||||
foreach (var template in shuffledTemplates)
|
foreach (var template in shuffledTemplates)
|
||||||
{
|
{
|
||||||
@@ -1518,6 +1524,33 @@ public class DungeonGeneratorV3
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get templates with the highest socket score from a pre-sorted list.
|
||||||
|
/// If no templates have Custom socket matches, returns all compatible templates.
|
||||||
|
/// </summary>
|
||||||
|
private List<TileTemplate> GetTopScoredTemplates(List<TileTemplate> sortedTemplates, int x, int y, DungeonLayoutV2 layout)
|
||||||
|
{
|
||||||
|
if (sortedTemplates.Count == 0) return sortedTemplates;
|
||||||
|
|
||||||
|
// Calculate scores (templates should already be sorted by score from GetCompatibleTemplates)
|
||||||
|
var scores = sortedTemplates
|
||||||
|
.Select(t => new { Template = t, Score = CalculateSocketScore(t, x, y, layout) })
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var topScore = scores.Max(s => s.Score);
|
||||||
|
|
||||||
|
// If top score is from Custom matches (10+), only return those
|
||||||
|
// Otherwise return all (they're all equally generic matches)
|
||||||
|
if (topScore >= 10)
|
||||||
|
{
|
||||||
|
var topTemplates = scores.Where(s => s.Score >= 10).Select(s => s.Template).ToList();
|
||||||
|
LogVerbose($" Filtering to {topTemplates.Count} templates with Custom socket matches (score >= 10)");
|
||||||
|
return topTemplates;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sortedTemplates;
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// SOCKET MATCHING METHODS
|
// SOCKET MATCHING METHODS
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -1739,13 +1772,108 @@ public class DungeonGeneratorV3
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Calculate a socket compatibility score for a template at a given position.
|
||||||
|
/// Higher score = better match.
|
||||||
|
///
|
||||||
|
/// Scoring:
|
||||||
|
/// - Custom socket match: +10 points per edge
|
||||||
|
/// - Open/Closed match: +1 point per edge
|
||||||
|
/// - No neighbor to check: +0 points
|
||||||
|
///
|
||||||
|
/// This prioritizes templates that match Custom sockets over generic Open/Closed.
|
||||||
|
/// </summary>
|
||||||
|
private int CalculateSocketScore(TileTemplate template, int x, int y, DungeonLayoutV2 layout)
|
||||||
|
{
|
||||||
|
if (!_templateSocketConfigs.TryGetValue(template.Id, out var config)) return 0;
|
||||||
|
|
||||||
|
int score = 0;
|
||||||
|
|
||||||
|
// Check West neighbor (most important for left-to-right filling)
|
||||||
|
if (x > 0 && _placedTemplates.TryGetValue($"{x-1},{y}", out var westTemplateId))
|
||||||
|
{
|
||||||
|
if (_templateSocketConfigs.TryGetValue(westTemplateId, out var westConfig))
|
||||||
|
{
|
||||||
|
var ourSocket = config.West.FirstOrDefault();
|
||||||
|
var theirSocket = westConfig.East.FirstOrDefault();
|
||||||
|
|
||||||
|
if (ourSocket != null && theirSocket != null && SocketConfiguration.CanConnect(ourSocket, theirSocket))
|
||||||
|
{
|
||||||
|
// Custom matches are worth more
|
||||||
|
if (ourSocket.Type == SocketType.Custom && theirSocket.Type == SocketType.Custom)
|
||||||
|
score += 10;
|
||||||
|
else
|
||||||
|
score += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check North neighbor
|
||||||
|
if (y > 0 && _placedTemplates.TryGetValue($"{x},{y-1}", out var northTemplateId))
|
||||||
|
{
|
||||||
|
if (_templateSocketConfigs.TryGetValue(northTemplateId, out var northConfig))
|
||||||
|
{
|
||||||
|
var ourSocket = config.North.FirstOrDefault();
|
||||||
|
var theirSocket = northConfig.South.FirstOrDefault();
|
||||||
|
|
||||||
|
if (ourSocket != null && theirSocket != null && SocketConfiguration.CanConnect(ourSocket, theirSocket))
|
||||||
|
{
|
||||||
|
if (ourSocket.Type == SocketType.Custom && theirSocket.Type == SocketType.Custom)
|
||||||
|
score += 10;
|
||||||
|
else
|
||||||
|
score += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check South neighbor (if already placed, e.g., from room templates)
|
||||||
|
if (_placedTemplates.TryGetValue($"{x},{y+1}", out var southTemplateId))
|
||||||
|
{
|
||||||
|
if (_templateSocketConfigs.TryGetValue(southTemplateId, out var southConfig))
|
||||||
|
{
|
||||||
|
var ourSocket = config.South.FirstOrDefault();
|
||||||
|
var theirSocket = southConfig.North.FirstOrDefault();
|
||||||
|
|
||||||
|
if (ourSocket != null && theirSocket != null && SocketConfiguration.CanConnect(ourSocket, theirSocket))
|
||||||
|
{
|
||||||
|
if (ourSocket.Type == SocketType.Custom && theirSocket.Type == SocketType.Custom)
|
||||||
|
score += 10;
|
||||||
|
else
|
||||||
|
score += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check East neighbor (if already placed, e.g., from room templates)
|
||||||
|
if (_placedTemplates.TryGetValue($"{x+1},{y}", out var eastTemplateId))
|
||||||
|
{
|
||||||
|
if (_templateSocketConfigs.TryGetValue(eastTemplateId, out var eastConfig))
|
||||||
|
{
|
||||||
|
var ourSocket = config.East.FirstOrDefault();
|
||||||
|
var theirSocket = eastConfig.West.FirstOrDefault();
|
||||||
|
|
||||||
|
if (ourSocket != null && theirSocket != null && SocketConfiguration.CanConnect(ourSocket, theirSocket))
|
||||||
|
{
|
||||||
|
if (ourSocket.Type == SocketType.Custom && theirSocket.Type == SocketType.Custom)
|
||||||
|
score += 10;
|
||||||
|
else
|
||||||
|
score += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all templates from a list that are socket-compatible at position (x,y).
|
/// Get all templates from a list that are socket-compatible at position (x,y).
|
||||||
|
/// Templates are sorted by socket match quality - Custom matches are prioritized.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<TileTemplate> GetCompatibleTemplates(List<TileTemplate> templates, int x, int y, DungeonLayoutV2 layout)
|
private List<TileTemplate> GetCompatibleTemplates(List<TileTemplate> templates, int x, int y, DungeonLayoutV2 layout)
|
||||||
{
|
{
|
||||||
if (!UseSocketMatching) return templates;
|
if (!UseSocketMatching) return templates;
|
||||||
|
|
||||||
|
// Filter to compatible templates
|
||||||
var compatible = templates.Where(t => IsSocketCompatible(t, x, y, layout)).ToList();
|
var compatible = templates.Where(t => IsSocketCompatible(t, x, y, layout)).ToList();
|
||||||
|
|
||||||
if (compatible.Count == 0)
|
if (compatible.Count == 0)
|
||||||
@@ -1754,8 +1882,22 @@ public class DungeonGeneratorV3
|
|||||||
return templates; // Fallback to all if none match
|
return templates; // Fallback to all if none match
|
||||||
}
|
}
|
||||||
|
|
||||||
LogVerbose($" Socket-compatible: {compatible.Count}/{templates.Count} templates at ({x},{y})");
|
// Score and sort by socket match quality (highest first)
|
||||||
return compatible;
|
var scored = compatible
|
||||||
|
.Select(t => new { Template = t, Score = CalculateSocketScore(t, x, y, layout) })
|
||||||
|
.OrderByDescending(x => x.Score)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// Log the top matches
|
||||||
|
if (VerboseLogging && scored.Count > 0)
|
||||||
|
{
|
||||||
|
var topScore = scored[0].Score;
|
||||||
|
var topMatches = scored.Where(s => s.Score == topScore).ToList();
|
||||||
|
LogVerbose($" Socket-compatible: {compatible.Count}/{templates.Count} templates at ({x},{y}), top score={topScore} ({topMatches.Count} templates)");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return templates sorted by score (best matches first)
|
||||||
|
return scored.Select(s => s.Template).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user