TSing sockets
This commit is contained in:
+196
-149
@@ -1270,7 +1270,7 @@ public class DungeonGeneratorV3
|
|||||||
/// SELECTION PROCESS (with socket matching):
|
/// SELECTION PROCESS (with socket matching):
|
||||||
/// 1. If _floorTemplates has templates:
|
/// 1. If _floorTemplates has templates:
|
||||||
/// a. Filter to socket-compatible templates (if UseSocketMatching)
|
/// a. Filter to socket-compatible templates (if UseSocketMatching)
|
||||||
/// b. Select one via weighted random from compatible list
|
/// b. Try each template in weighted order until one resolves to a TileDefinition
|
||||||
/// c. Try template.PrimaryTileDefinitionId first
|
/// c. Try template.PrimaryTileDefinitionId first
|
||||||
/// d. If no PrimaryTileDefinitionId, parse template.TilesJson
|
/// d. If no PrimaryTileDefinitionId, parse template.TilesJson
|
||||||
/// e. Track placed template for future socket matching
|
/// e. Track placed template for future socket matching
|
||||||
@@ -1286,84 +1286,26 @@ public class DungeonGeneratorV3
|
|||||||
// Get socket-compatible templates
|
// Get socket-compatible templates
|
||||||
var compatibleTemplates = GetCompatibleTemplates(_floorTemplates, x, y, layout);
|
var compatibleTemplates = GetCompatibleTemplates(_floorTemplates, x, y, layout);
|
||||||
|
|
||||||
var template = SelectWeightedTemplate(compatibleTemplates);
|
// Shuffle templates by weight, then try each until one resolves
|
||||||
if (template != null)
|
var shuffledTemplates = ShuffleByWeight(compatibleTemplates);
|
||||||
|
|
||||||
|
foreach (var template in shuffledTemplates)
|
||||||
{
|
{
|
||||||
LogVerbose($"SelectFloorTile({x},{y}): Template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleTemplates.Count}/{_floorTemplates.Count}]");
|
var tileDef = TryResolveFloorTemplate(template, x, y, compatibleTemplates.Count);
|
||||||
|
if (tileDef != null)
|
||||||
// Method 1: Try PrimaryTileDefinitionId first
|
|
||||||
if (template.PrimaryTileDefinitionId.HasValue)
|
|
||||||
{
|
{
|
||||||
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
// Track placement for socket matching
|
||||||
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
_placedTemplates[$"{x},{y}"] = template.Id;
|
||||||
{
|
|
||||||
if (!UsedFloorTemplates.Contains(template.Name))
|
if (!UsedFloorTemplates.Contains(template.Name))
|
||||||
UsedFloorTemplates.Add(template.Name);
|
UsedFloorTemplates.Add(template.Name);
|
||||||
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
|
||||||
|
return tileDef;
|
||||||
// Track placement for socket matching
|
|
||||||
_placedTemplates[$"{x},{y}"] = template.Id;
|
|
||||||
|
|
||||||
return tileDef;
|
|
||||||
}
|
|
||||||
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method 2: Try to parse TilesJson for the template (when PrimaryTileDefinitionId is not set)
|
|
||||||
if (!string.IsNullOrEmpty(template.TilesJson))
|
|
||||||
{
|
|
||||||
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
|
||||||
if (tiles == null)
|
|
||||||
{
|
|
||||||
LogVerbose($" -> Deserialized to NULL!");
|
|
||||||
}
|
|
||||||
else if (tiles.Count == 0)
|
|
||||||
{
|
|
||||||
LogVerbose($" -> Deserialized to EMPTY list!");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var first = tiles[0];
|
|
||||||
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
|
||||||
|
|
||||||
// Find matching tile definition by sheet and frame
|
|
||||||
var match = _tileDefinitions.Values
|
|
||||||
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
|
||||||
if (match != null)
|
|
||||||
{
|
|
||||||
if (!UsedFloorTemplates.Contains(template.Name))
|
|
||||||
UsedFloorTemplates.Add(template.Name);
|
|
||||||
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
|
||||||
|
|
||||||
// Track placement for socket matching
|
|
||||||
_placedTemplates[$"{x},{y}"] = template.Id;
|
|
||||||
|
|
||||||
return match;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log($" -> WARNING: No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}!");
|
|
||||||
Log($" Available TileDefinitions for Sheet {first.SheetId}:");
|
|
||||||
foreach (var td in _tileDefinitions.Values.Where(t => t.TileMapSheetId == first.SheetId).Take(5))
|
|
||||||
{
|
|
||||||
Log($" TileDef {td.Id}: Frame={td.FrameIndex}, '{td.Name}'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log($" -> ERROR parsing TilesJson: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogVerbose($" TilesJson is empty/null");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No template resolved - log warning
|
||||||
|
Log($"SelectFloorTile({x},{y}): WARNING - None of {compatibleTemplates.Count} compatible templates could resolve to a TileDefinition!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// FALLBACK: use the FIRST walkable tile consistently (not random)
|
// FALLBACK: use the FIRST walkable tile consistently (not random)
|
||||||
@@ -1379,6 +1321,72 @@ public class DungeonGeneratorV3
|
|||||||
return _tileDefinitions.Values.FirstOrDefault();
|
return _tileDefinitions.Values.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Try to resolve a floor template to a TileDefinition.
|
||||||
|
/// Returns null if the template cannot be resolved.
|
||||||
|
/// </summary>
|
||||||
|
private TileDefinition? TryResolveFloorTemplate(TileTemplate template, int x, int y, int compatibleCount)
|
||||||
|
{
|
||||||
|
LogVerbose($"SelectFloorTile({x},{y}): Trying template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleCount}/{_floorTemplates.Count}]");
|
||||||
|
|
||||||
|
// Method 1: Try PrimaryTileDefinitionId first
|
||||||
|
if (template.PrimaryTileDefinitionId.HasValue)
|
||||||
|
{
|
||||||
|
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
||||||
|
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
||||||
|
return tileDef;
|
||||||
|
}
|
||||||
|
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method 2: Try to parse TilesJson for the template
|
||||||
|
if (!string.IsNullOrEmpty(template.TilesJson))
|
||||||
|
{
|
||||||
|
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
||||||
|
if (tiles == null)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Deserialized to NULL!");
|
||||||
|
}
|
||||||
|
else if (tiles.Count == 0)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Deserialized to EMPTY list!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var first = tiles[0];
|
||||||
|
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
||||||
|
|
||||||
|
var match = _tileDefinitions.Values
|
||||||
|
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
||||||
|
if (match != null)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogVerbose($" -> No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> ERROR parsing TilesJson: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogVerbose($" TilesJson is empty/null");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null; // Could not resolve this template
|
||||||
|
}
|
||||||
|
|
||||||
// Overload for backward compatibility (without layout parameter)
|
// Overload for backward compatibility (without layout parameter)
|
||||||
private TileDefinition? SelectFloorTile(int x, int y, out bool usedFallback)
|
private TileDefinition? SelectFloorTile(int x, int y, out bool usedFallback)
|
||||||
{
|
{
|
||||||
@@ -1500,7 +1508,7 @@ public class DungeonGeneratorV3
|
|||||||
/// SELECTION PROCESS (with socket matching):
|
/// SELECTION PROCESS (with socket matching):
|
||||||
/// 1. If _wallTemplates has templates:
|
/// 1. If _wallTemplates has templates:
|
||||||
/// a. Filter to socket-compatible templates (if UseSocketMatching)
|
/// a. Filter to socket-compatible templates (if UseSocketMatching)
|
||||||
/// b. Select one via weighted random
|
/// b. Try each template in weighted order until one resolves to a TileDefinition
|
||||||
/// c. Try template.PrimaryTileDefinitionId first
|
/// c. Try template.PrimaryTileDefinitionId first
|
||||||
/// d. If no PrimaryTileDefinitionId, parse template.TilesJson
|
/// d. If no PrimaryTileDefinitionId, parse template.TilesJson
|
||||||
/// e. Track placed template for future socket matching
|
/// e. Track placed template for future socket matching
|
||||||
@@ -1513,83 +1521,26 @@ public class DungeonGeneratorV3
|
|||||||
// Get socket-compatible templates
|
// Get socket-compatible templates
|
||||||
var compatibleTemplates = GetCompatibleTemplates(_wallTemplates, x, y, layout);
|
var compatibleTemplates = GetCompatibleTemplates(_wallTemplates, x, y, layout);
|
||||||
|
|
||||||
var template = SelectWeightedTemplate(compatibleTemplates);
|
// Shuffle templates by weight, then try each until one resolves
|
||||||
if (template != null)
|
var shuffledTemplates = ShuffleByWeight(compatibleTemplates);
|
||||||
|
|
||||||
|
foreach (var template in shuffledTemplates)
|
||||||
{
|
{
|
||||||
LogVerbose($"SelectWallTile({x},{y}): Template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleTemplates.Count}/{_wallTemplates.Count}]");
|
var tileDef = TryResolveTemplate(template, x, y, compatibleTemplates.Count);
|
||||||
|
if (tileDef != null)
|
||||||
// Method 1: Try PrimaryTileDefinitionId first
|
|
||||||
if (template.PrimaryTileDefinitionId.HasValue)
|
|
||||||
{
|
{
|
||||||
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
// Track placement for socket matching
|
||||||
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
_placedTemplates[$"{x},{y}"] = template.Id;
|
||||||
{
|
|
||||||
if (!UsedWallTemplates.Contains(template.Name))
|
if (!UsedWallTemplates.Contains(template.Name))
|
||||||
UsedWallTemplates.Add(template.Name);
|
UsedWallTemplates.Add(template.Name);
|
||||||
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
|
||||||
|
return tileDef;
|
||||||
// Track placement for socket matching
|
|
||||||
_placedTemplates[$"{x},{y}"] = template.Id;
|
|
||||||
|
|
||||||
return tileDef;
|
|
||||||
}
|
|
||||||
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method 2: Try to parse TilesJson for the template
|
|
||||||
if (!string.IsNullOrEmpty(template.TilesJson))
|
|
||||||
{
|
|
||||||
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
|
||||||
if (tiles == null)
|
|
||||||
{
|
|
||||||
LogVerbose($" -> Deserialized to NULL!");
|
|
||||||
}
|
|
||||||
else if (tiles.Count == 0)
|
|
||||||
{
|
|
||||||
LogVerbose($" -> Deserialized to EMPTY list!");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var first = tiles[0];
|
|
||||||
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
|
||||||
|
|
||||||
var match = _tileDefinitions.Values
|
|
||||||
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
|
||||||
if (match != null)
|
|
||||||
{
|
|
||||||
if (!UsedWallTemplates.Contains(template.Name))
|
|
||||||
UsedWallTemplates.Add(template.Name);
|
|
||||||
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
|
||||||
|
|
||||||
// Track placement for socket matching
|
|
||||||
_placedTemplates[$"{x},{y}"] = template.Id;
|
|
||||||
|
|
||||||
return match;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log($" -> WARNING: No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}!");
|
|
||||||
Log($" Available TileDefinitions for Sheet {first.SheetId}:");
|
|
||||||
foreach (var td in _tileDefinitions.Values.Where(t => t.TileMapSheetId == first.SheetId).Take(5))
|
|
||||||
{
|
|
||||||
Log($" TileDef {td.Id}: Frame={td.FrameIndex}, '{td.Name}'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log($" -> ERROR parsing TilesJson: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogVerbose($" TilesJson is empty/null");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No template resolved - log warning
|
||||||
|
Log($"SelectWallTile({x},{y}): WARNING - None of {compatibleTemplates.Count} compatible templates could resolve to a TileDefinition!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// FALLBACK: use the FIRST non-walkable tile consistently
|
// FALLBACK: use the FIRST non-walkable tile consistently
|
||||||
@@ -1604,6 +1555,102 @@ public class DungeonGeneratorV3
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shuffle templates weighted by their Weight property.
|
||||||
|
/// Higher weight = more likely to appear earlier in the list.
|
||||||
|
/// </summary>
|
||||||
|
private List<TileTemplate> ShuffleByWeight(List<TileTemplate> templates)
|
||||||
|
{
|
||||||
|
var result = new List<TileTemplate>();
|
||||||
|
var remaining = templates.ToList();
|
||||||
|
|
||||||
|
while (remaining.Count > 0)
|
||||||
|
{
|
||||||
|
var totalWeight = remaining.Sum(t => t.Weight);
|
||||||
|
var roll = _rng.Next(totalWeight);
|
||||||
|
var cumulative = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < remaining.Count; i++)
|
||||||
|
{
|
||||||
|
cumulative += remaining[i].Weight;
|
||||||
|
if (roll < cumulative)
|
||||||
|
{
|
||||||
|
result.Add(remaining[i]);
|
||||||
|
remaining.RemoveAt(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Try to resolve a template to a TileDefinition.
|
||||||
|
/// Returns null if the template cannot be resolved.
|
||||||
|
/// </summary>
|
||||||
|
private TileDefinition? TryResolveTemplate(TileTemplate template, int x, int y, int compatibleCount)
|
||||||
|
{
|
||||||
|
LogVerbose($"SelectWallTile({x},{y}): Trying template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleCount}/{_wallTemplates.Count}]");
|
||||||
|
|
||||||
|
// Method 1: Try PrimaryTileDefinitionId first
|
||||||
|
if (template.PrimaryTileDefinitionId.HasValue)
|
||||||
|
{
|
||||||
|
LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}");
|
||||||
|
if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef))
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'");
|
||||||
|
return tileDef;
|
||||||
|
}
|
||||||
|
LogVerbose($" -> NOT FOUND in _tileDefinitions!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method 2: Try to parse TilesJson for the template
|
||||||
|
if (!string.IsNullOrEmpty(template.TilesJson))
|
||||||
|
{
|
||||||
|
LogVerbose($" Trying TilesJson: {template.TilesJson}");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tiles = JsonSerializer.Deserialize<List<TemplateTilePlacement>>(template.TilesJson, _jsonOptions);
|
||||||
|
if (tiles == null)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Deserialized to NULL!");
|
||||||
|
}
|
||||||
|
else if (tiles.Count == 0)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Deserialized to EMPTY list!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var first = tiles[0];
|
||||||
|
LogVerbose($" -> Parsed: SheetId={first.SheetId}, FrameIndex={first.FrameIndex}");
|
||||||
|
|
||||||
|
var match = _tileDefinitions.Values
|
||||||
|
.FirstOrDefault(t => t.TileMapSheetId == first.SheetId && t.FrameIndex == first.FrameIndex);
|
||||||
|
if (match != null)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> Match: TileDef {match.Id} '{match.Name}'");
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogVerbose($" -> No TileDefinition for Sheet={first.SheetId}, Frame={first.FrameIndex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogVerbose($" -> ERROR parsing TilesJson: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogVerbose($" TilesJson is empty/null");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null; // Could not resolve this template
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// SOCKET MATCHING METHODS
|
// SOCKET MATCHING METHODS
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|||||||
BIN
Binary file not shown.
Reference in New Issue
Block a user