From 36adb0dab41edfc92f5450a9009769657b453e3a Mon Sep 17 00:00:00 2001 From: dingo Date: Fri, 9 Jan 2026 17:49:18 -0500 Subject: [PATCH] TSing sockets --- Game/Dungeon/DungeonGeneratorV3.cs | 74 ++++++++++++++++++++++++++++-- data/entities/SocketName.cs | 4 ++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorV3.cs b/Game/Dungeon/DungeonGeneratorV3.cs index 662236a..77b3b36 100644 --- a/Game/Dungeon/DungeonGeneratorV3.cs +++ b/Game/Dungeon/DungeonGeneratorV3.cs @@ -69,6 +69,12 @@ public class DungeonGeneratorV3 /// private Dictionary _placedTemplates = new(); + /// + /// Lookup TileTemplate by (sheetId, frameIndex) for room stamping. + /// Key = "sheetId:frameIndex", Value = TileTemplate + /// + private Dictionary _tileTemplatesBySprite = new(); + // ============================================================ // PALETTE & TEMPLATES // ============================================================ @@ -391,6 +397,7 @@ public class DungeonGeneratorV3 CategorizeRoomTemplates(); LogTemplateStats(); ParseSocketConfigurations(); + BuildSpriteToTemplateLookup(); } private async Task LoadAllTemplatesAsync() @@ -433,6 +440,7 @@ public class DungeonGeneratorV3 CategorizeRoomTemplates(); LogTemplateStats(); ParseSocketConfigurations(); + BuildSpriteToTemplateLookup(); } private void CategorizeRoomTemplates() @@ -941,10 +949,21 @@ public class DungeonGeneratorV3 // Ensure the sheet is loaded for client rendering EnsureSheetLoaded(t.SheetId); - // Room template tiles are assumed walkable (they define the room interior) - // Walls will be placed around them by PlaceWallTiles - layout.Tiles[worldX, worldY] = 1; // Non-zero to indicate tile is placed - layout.Walkable[worldX, worldY] = true; + // Try to find a matching TileTemplate for this sprite + var tileTemplate = FindTemplateBySprite(t.SheetId, t.FrameIndex); + + // Use template properties if found, otherwise default to walkable floor + bool isWalkable = tileTemplate?.IsWalkable ?? true; + int templateId = tileTemplate?.Id ?? 0; + + layout.Tiles[worldX, worldY] = templateId > 0 ? templateId : 1; // Non-zero to indicate tile is placed + layout.Walkable[worldX, worldY] = isWalkable; + + // Track placement for socket matching if we found a template + if (templateId > 0) + { + _placedTemplates[$"{worldX},{worldY}"] = templateId; + } layout.TileDetails.Add(new PlacedTile { @@ -1529,6 +1548,53 @@ public class DungeonGeneratorV3 Log($"Parsed socket configurations for {parsed} templates"); } + /// + /// Build a lookup of TileTemplates by their sprite (sheetId:frameIndex). + /// Used when stamping room templates to get tile properties. + /// + private void BuildSpriteToTemplateLookup() + { + _tileTemplatesBySprite.Clear(); + + var allTemplates = _floorTemplates + .Concat(_wallTemplates) + .Concat(_doorTemplates); + + foreach (var template in allTemplates) + { + if (string.IsNullOrEmpty(template.TilesJson)) continue; + + try + { + var tiles = JsonSerializer.Deserialize>(template.TilesJson, _jsonOptions); + if (tiles?.Count > 0) + { + var first = tiles[0]; + var key = $"{first.SheetId}:{first.FrameIndex}"; + + // Only store if not already present (first template wins) + if (!_tileTemplatesBySprite.ContainsKey(key)) + { + _tileTemplatesBySprite[key] = template; + } + } + } + catch { /* ignore parse errors */ } + } + + Log($"Built sprite->template lookup with {_tileTemplatesBySprite.Count} entries"); + } + + /// + /// Find a TileTemplate by sheetId and frameIndex. + /// Returns null if no matching template exists. + /// + private TileTemplate? FindTemplateBySprite(int sheetId, int frameIndex) + { + var key = $"{sheetId}:{frameIndex}"; + return _tileTemplatesBySprite.TryGetValue(key, out var template) ? template : null; + } + /// /// Parse a template's socket configuration from JSON or legacy fields. /// diff --git a/data/entities/SocketName.cs b/data/entities/SocketName.cs index f846e37..1757808 100644 --- a/data/entities/SocketName.cs +++ b/data/entities/SocketName.cs @@ -145,6 +145,10 @@ public class SocketConfiguration } // Open↔Open or Closed↔Closed + if (a.Type == SocketType.Closed && b.Type == SocketType.Closed) + { + return false; + } return true; } }