diff --git a/Game/Dungeon/DungeonGeneratorV3.cs b/Game/Dungeon/DungeonGeneratorV3.cs index ccb336f..199a041 100644 --- a/Game/Dungeon/DungeonGeneratorV3.cs +++ b/Game/Dungeon/DungeonGeneratorV3.cs @@ -1270,7 +1270,7 @@ public class DungeonGeneratorV3 /// SELECTION PROCESS (with socket matching): /// 1. If _floorTemplates has templates: /// 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 /// d. If no PrimaryTileDefinitionId, parse template.TilesJson /// e. Track placed template for future socket matching @@ -1286,84 +1286,26 @@ public class DungeonGeneratorV3 // Get socket-compatible templates var compatibleTemplates = GetCompatibleTemplates(_floorTemplates, x, y, layout); - var template = SelectWeightedTemplate(compatibleTemplates); - if (template != null) + // Shuffle templates by weight, then try each until one resolves + var shuffledTemplates = ShuffleByWeight(compatibleTemplates); + + foreach (var template in shuffledTemplates) { - LogVerbose($"SelectFloorTile({x},{y}): Template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleTemplates.Count}/{_floorTemplates.Count}]"); - - // Method 1: Try PrimaryTileDefinitionId first - if (template.PrimaryTileDefinitionId.HasValue) + var tileDef = TryResolveFloorTemplate(template, x, y, compatibleTemplates.Count); + if (tileDef != null) { - LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}"); - if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef)) - { - if (!UsedFloorTemplates.Contains(template.Name)) - UsedFloorTemplates.Add(template.Name); - LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'"); - - // 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>(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"); + // Track placement for socket matching + _placedTemplates[$"{x},{y}"] = template.Id; + + if (!UsedFloorTemplates.Contains(template.Name)) + UsedFloorTemplates.Add(template.Name); + + return tileDef; } } + + // 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) @@ -1379,6 +1321,72 @@ public class DungeonGeneratorV3 return _tileDefinitions.Values.FirstOrDefault(); } + /// + /// Try to resolve a floor template to a TileDefinition. + /// Returns null if the template cannot be resolved. + /// + 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>(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) private TileDefinition? SelectFloorTile(int x, int y, out bool usedFallback) { @@ -1500,7 +1508,7 @@ public class DungeonGeneratorV3 /// SELECTION PROCESS (with socket matching): /// 1. If _wallTemplates has templates: /// 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 /// d. If no PrimaryTileDefinitionId, parse template.TilesJson /// e. Track placed template for future socket matching @@ -1513,83 +1521,26 @@ public class DungeonGeneratorV3 // Get socket-compatible templates var compatibleTemplates = GetCompatibleTemplates(_wallTemplates, x, y, layout); - var template = SelectWeightedTemplate(compatibleTemplates); - if (template != null) + // Shuffle templates by weight, then try each until one resolves + var shuffledTemplates = ShuffleByWeight(compatibleTemplates); + + foreach (var template in shuffledTemplates) { - LogVerbose($"SelectWallTile({x},{y}): Template '{template.Name}' (ID={template.Id}) [Compatible: {compatibleTemplates.Count}/{_wallTemplates.Count}]"); - - // Method 1: Try PrimaryTileDefinitionId first - if (template.PrimaryTileDefinitionId.HasValue) + var tileDef = TryResolveTemplate(template, x, y, compatibleTemplates.Count); + if (tileDef != null) { - LogVerbose($" Trying PrimaryTileDefinitionId: {template.PrimaryTileDefinitionId.Value}"); - if (_tileDefinitions.TryGetValue(template.PrimaryTileDefinitionId.Value, out var tileDef)) - { - if (!UsedWallTemplates.Contains(template.Name)) - UsedWallTemplates.Add(template.Name); - LogVerbose($" -> Found TileDef {tileDef.Id} '{tileDef.Name}'"); - - // 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>(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"); + // Track placement for socket matching + _placedTemplates[$"{x},{y}"] = template.Id; + + if (!UsedWallTemplates.Contains(template.Name)) + UsedWallTemplates.Add(template.Name); + + return tileDef; } } + + // 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 @@ -1604,6 +1555,102 @@ public class DungeonGeneratorV3 return null; } + /// + /// Shuffle templates weighted by their Weight property. + /// Higher weight = more likely to appear earlier in the list. + /// + private List ShuffleByWeight(List templates) + { + var result = new List(); + 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; + } + + /// + /// Try to resolve a template to a TileDefinition. + /// Returns null if the template cannot be resolved. + /// + 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>(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 // ============================================================ diff --git a/server.zip b/server.zip deleted file mode 100644 index 316b475..0000000 Binary files a/server.zip and /dev/null differ