diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index 7543145..99dc928 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -934,360 +934,157 @@ public class DungeonGeneratorV2 /// /// Automatically generate wall tiles around floor tiles that border void /// - //private void GenerateAutoWalls(DungeonLayoutV2 layout) - //{ - // // Find wall tiles for each direction - // var wallTiles = _tileDefinitions.Values.Where(t => t.Type == TileType.Wall).ToList(); - // if (wallTiles.Count == 0) - // { - // Console.WriteLine("[DungeonGenV2] No wall tiles found - skipping auto-wall generation"); - // return; - // } - - // Console.WriteLine($"[DungeonGenV2] Found {wallTiles.Count} wall tiles for auto-wall generation"); - // foreach (var wt in wallTiles) - // { - // Console.WriteLine($" - {wt.Id}: {wt.Name} (tags: {wt.Tags ?? "none"})"); - // } - - // // Helper to match tags. - // // NOTE: Some of your data has tags separated by commas *and/or* whitespace (e.g. "corner_nw void"), - // // so we split on both to be resilient. - // bool HasTag(TileDefinition t, string tag) - // { - // if (string.IsNullOrWhiteSpace(t.Tags)) return false; - // return t.Tags - // .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - // .Any(s => s.Equals(tag, StringComparison.OrdinalIgnoreCase)); - // } - - // // Try to find directional walls by tags, fall back to first wall - // var wallNorth = wallTiles.FirstOrDefault(t => HasTag(t, "north")) ?? wallTiles[0]; - // var wallSouth = wallTiles.FirstOrDefault(t => HasTag(t, "south")) ?? wallTiles[0]; - // var wallEast = wallTiles.FirstOrDefault(t => HasTag(t, "east")) ?? wallTiles[0]; - // var wallWest = wallTiles.FirstOrDefault(t => HasTag(t, "west")) ?? wallTiles[0]; - // // Corner tiles: - // // - corner_* indicates which *quadrant of the tile contains the wall mass* (tile itself lives in void space) - // // - many sets also include "void"-tagged corner variants meant specifically for outside/diagonal void corners. - // var wallCornerNE = wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne") && !HasTag(t, "void")) - // ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne")) - // ?? wallTiles[0]; - // var wallCornerNW = wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw") && !HasTag(t, "void")) - // ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw")) - // ?? wallTiles[0]; - // var wallCornerSE = wallTiles.FirstOrDefault(t => HasTag(t, "corner_se") && !HasTag(t, "void")) - // ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_se")) - // ?? wallTiles[0]; - // var wallCornerSW = wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw") && !HasTag(t, "void")) - // ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw")) - // ?? wallTiles[0]; - - // // Prefer these when a corner tile borders void on TWO orthogonal sides (classic outside corner). - // var wallCornerNEVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne") && HasTag(t, "void")); - // var wallCornerNWVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw") && HasTag(t, "void")); - // var wallCornerSEVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_se") && HasTag(t, "void")); - // var wallCornerSWVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw") && HasTag(t, "void")); - - // TileDefinition PickCorner(string cornerTag, bool preferVoid) - // { - // // cornerTag is one of: corner_ne, corner_nw, corner_se, corner_sw - // return cornerTag.ToLowerInvariant() switch - // { - // "corner_ne" => (preferVoid ? wallCornerNEVoid : null) ?? wallCornerNE, - // "corner_nw" => (preferVoid ? wallCornerNWVoid : null) ?? wallCornerNW, - // "corner_se" => (preferVoid ? wallCornerSEVoid : null) ?? wallCornerSE, - // "corner_sw" => (preferVoid ? wallCornerSWVoid : null) ?? wallCornerSW, - // _ => wallCornerNW - // }; - // } - - // Console.WriteLine($"[DungeonGenV2] Wall tile mapping: N={wallNorth.Id}, S={wallSouth.Id}, E={wallEast.Id}, W={wallWest.Id}"); - - // // Track positions that need walls - // var wallPositions = new List<(int X, int Y, TileDefinition Tile)>(); - - // // Check each void tile to see if it borders a floor - // for (int x = 0; x < MapWidth; x++) - // { - // for (int y = 0; y < MapHeight; y++) - // { - // // Skip if not void - // if (layout.Tiles[x, y] != 0) continue; - - // // Check neighbors for floor tiles (walkable) - // bool hasFloorNorth = y > 0 && layout.IsWalkable(x, y - 1); - // bool hasFloorSouth = y < MapHeight - 1 && layout.IsWalkable(x, y + 1); - // bool hasFloorEast = x < MapWidth - 1 && layout.IsWalkable(x + 1, y); - // bool hasFloorWest = x > 0 && layout.IsWalkable(x - 1, y); - - // // If any adjacent tile is floor, this should be a wall - // if (hasFloorNorth || hasFloorSouth || hasFloorEast || hasFloorWest) - // { - // // Determine wall type based on which direction has floor - // TileDefinition wallTile; - - // // Check for corners first. - // // IMPORTANT: corner_* tags indicate the quadrant where the *wall mass* lives inside this tile. - // // That quadrant matches the direction of the interior (walkable) corner. - // // Example: if the interior is to the NW (diagonal floor at NW), use corner_nw. - // // If this is an *outside* corner (the two orthogonal sides away from the interior are void), - // // prefer a corner variant that includes the "void" tag. - // bool outsideN = IsOutsideAt(x, y - 1); - - // bool outsideS = IsOutsideAt(x, y + 1); - - // bool outsideE = IsOutsideAt(x + 1, y); - - // bool outsideW = IsOutsideAt(x - 1, y); - - // if (hasFloorNorth && hasFloorWest) - // wallTile = PickCorner("corner_nw", preferVoid: (outsideS && outsideE)); - // else if (hasFloorNorth && hasFloorEast) - // wallTile = PickCorner("corner_ne", preferVoid: (outsideS && outsideW)); - // else if (hasFloorSouth && hasFloorWest) - // wallTile = PickCorner("corner_sw", preferVoid: (outsideN && outsideE)); - // else if (hasFloorSouth && hasFloorEast) - // wallTile = PickCorner("corner_se", preferVoid: (outsideN && outsideW)); - // else if (hasFloorSouth) - // wallTile = wallNorth; // Wall is north of floor, so it's a north-facing wall - // else if (hasFloorNorth) - // wallTile = wallSouth; - // else if (hasFloorEast) - // wallTile = wallWest; - // else - // wallTile = wallEast; - - // wallPositions.Add((x, y, wallTile)); - // } - // } - // } - - // // Place wall tiles - // foreach (var (x, y, tile) in wallPositions) - // { - // layout.Tiles[x, y] = tile.Id; - - // // Walls are not walkable - // if (layout.Walkable != null) - // { - // layout.Walkable[x, y] = false; - // } - - // layout.TileDetails.Add(new PlacedTile - // { - // X = x, - // Y = y, - // TileDefinitionId = tile.Id, - // SheetId = tile.TileMapSheetId, - // FrameIndex = tile.FrameIndex, - // Layer = 2 // Walls on layer 2 - // }); - // } - - // // Index existing wall visuals so we can update/replace them without duplicating TileDetails entries. - // var wallDetailIndex = new Dictionary<(int X, int Y), int>(); - // for (int i = 0; i < layout.TileDetails.Count; i++) - // { - // var d = layout.TileDetails[i]; - // if (d.Layer == 2) - // wallDetailIndex[(d.X, d.Y)] = i; - // } - - // // Void-corner fill pass: when corridors/walls are thicker, some corner "caps" end up as void tiles that - // // only see walkable space diagonally. Fill those with the appropriate connected corner wall so we don't - // // get little void bite-marks at corridor turns and room borders. - // bool IsWallAt(int wx, int wy) - // { - // if (wx < 0 || wx >= MapWidth || wy < 0 || wy >= MapHeight) return false; - // int tid = layout.Tiles[wx, wy]; - // if (tid == 0) return false; - // return _tileDefinitions.TryGetValue(tid, out var td) && td.Type == TileType.Wall; - // } - - - // bool IsOutsideAt(int wx, int wy) - // { - // // "Outside"/"void" in your terminology: a tile that is NOT walkable and NOT a wall. - // // This prevents treating floor as "void" while still recognizing unfilled space. - // if (wx < 0 || wx >= MapWidth || wy < 0 || wy >= MapHeight) return true; // out of bounds counts as outside - // if (layout.IsWalkable(wx, wy)) return false; - // return !IsWallAt(wx, wy); - // } - - // void PlaceWall(int wx, int wy, TileDefinition tdef) - // { - // layout.Tiles[wx, wy] = tdef.Id; - // if (layout.Walkable != null) layout.Walkable[wx, wy] = false; - - // if (wallDetailIndex.TryGetValue((wx, wy), out var existingIdx)) - // { - // var d = layout.TileDetails[existingIdx]; - // d.TileDefinitionId = tdef.Id; - // d.SheetId = tdef.TileMapSheetId; - // d.FrameIndex = tdef.FrameIndex; - // layout.TileDetails[existingIdx] = d; - // } - // else - // { - // wallDetailIndex[(wx, wy)] = layout.TileDetails.Count; - // layout.TileDetails.Add(new PlacedTile - // { - // X = wx, - // Y = wy, - // TileDefinitionId = tdef.Id, - // SheetId = tdef.TileMapSheetId, - // FrameIndex = tdef.FrameIndex, - // Layer = 2 - // }); - // } - // } - - // for (int x = 1; x < MapWidth - 1; x++) - // { - // for (int y = 1; y < MapHeight - 1; y++) - // { - // if (layout.Tiles[x, y] != 0) continue; // only fill void - // // Don't overwrite walkable tiles (shouldn't happen because walkable implies non-zero, but safe). - // if (layout.IsWalkable(x, y)) continue; - - // bool floorNE = layout.IsWalkable(x + 1, y - 1); - // bool floorNW = layout.IsWalkable(x - 1, y - 1); - // bool floorSE = layout.IsWalkable(x + 1, y + 1); - // bool floorSW = layout.IsWalkable(x - 1, y + 1); - - // bool wallN = IsWallAt(x, y - 1); - // bool wallS = IsWallAt(x, y + 1); - // bool wallE = IsWallAt(x + 1, y); - // bool wallW = IsWallAt(x - 1, y); - - // // Only fill if there's "supporting" wall structure nearby; prevents random diagonal filling. - // // The diagonal walkable indicates the interior corner; the adjacent walls indicate we are on the border. - // // Diagonal-walkable indicates the interior corner. Choose the corner wall variant that visually - // // connects the two bordering wall runs (this tileset uses the opposite naming compared to the - // // earlier assumption, so NW<->SE and NE<->SW). - // // Outside-void corners: if the two orthogonal tiles away from the interior are void, prefer "void" corner variants. - // bool vN = IsOutsideAt(x, y - 1); - - // bool vS = IsOutsideAt(x, y + 1); - - // bool vE = IsOutsideAt(x + 1, y); - - // bool vW = IsOutsideAt(x - 1, y); - - // if (floorSE && (wallS || wallE)) PlaceWall(x, y, PickCorner("corner_se", preferVoid: (vN && vW))); - // else if (floorSW && (wallS || wallW)) PlaceWall(x, y, PickCorner("corner_sw", preferVoid: (vN && vE))); - // else if (floorNE && (wallN || wallE)) PlaceWall(x, y, PickCorner("corner_ne", preferVoid: (vS && vW))); - // else if (floorNW && (wallN || wallW)) PlaceWall(x, y, PickCorner("corner_nw", preferVoid: (vS && vE))); - // } - // } - - // // Corner fix-up pass: if a wall tile meets void on the outside but is adjacent to walkable - // // only diagonally (common with thicker walls / multi-tile corridors), swap in the connected - // // corner tile so the silhouette doesn't look "broken". - // // - // // Example: a boundary wall tile that is NW of a floor tile might not see floor South/East - // // directly (those may be other wall tiles), but it *will* see floor at SE diagonally. - // for (int x = 1; x < MapWidth - 1; x++) - // { - // for (int y = 1; y < MapHeight - 1; y++) - // { - // int id = layout.Tiles[x, y]; - // if (id == 0) continue; - // if (!_tileDefinitions.TryGetValue(id, out var def)) continue; - // if (def.Type != TileType.Wall) continue; - - // bool floorN = layout.IsWalkable(x, y - 1); - // bool floorS = layout.IsWalkable(x, y + 1); - // bool floorE = layout.IsWalkable(x + 1, y); - // bool floorW = layout.IsWalkable(x - 1, y); - - // bool floorNE = layout.IsWalkable(x + 1, y - 1); - // bool floorNW = layout.IsWalkable(x - 1, y - 1); - // bool floorSE = layout.IsWalkable(x + 1, y + 1); - // bool floorSW = layout.IsWalkable(x - 1, y + 1); - - // TileDefinition? replace = null; - - // // Choose connected outer corners using diagonal floor hints. - // bool vN2 = IsOutsideAt(x, y - 1); - - // bool vS2 = IsOutsideAt(x, y + 1); - - // bool vE2 = IsOutsideAt(x + 1, y); - - // bool vW2 = IsOutsideAt(x - 1, y); - - // if (floorSE && !floorS && !floorE) replace = PickCorner("corner_se", preferVoid: (vN2 && vW2)); - // else if (floorSW && !floorS && !floorW) replace = PickCorner("corner_sw", preferVoid: (vN2 && vE2)); - // else if (floorNE && !floorN && !floorE) replace = PickCorner("corner_ne", preferVoid: (vS2 && vW2)); - // else if (floorNW && !floorN && !floorW) replace = PickCorner("corner_nw", preferVoid: (vS2 && vE2)); - - // if (replace == null) continue; - // if (replace.Id == id) continue; - - // layout.Tiles[x, y] = replace.Id; - // if (layout.Walkable != null) layout.Walkable[x, y] = false; - - // // Update existing wall visual if present; otherwise add. - // if (wallDetailIndex.TryGetValue((x, y), out var idx)) - // { - // var d = layout.TileDetails[idx]; - // d.TileDefinitionId = replace.Id; - // d.SheetId = replace.TileMapSheetId; - // d.FrameIndex = replace.FrameIndex; - // layout.TileDetails[idx] = d; - // } - // else - // { - // layout.TileDetails.Add(new PlacedTile - // { - // X = x, - // Y = y, - // TileDefinitionId = replace.Id, - // SheetId = replace.TileMapSheetId, - // FrameIndex = replace.FrameIndex, - // Layer = 2 - // }); - // } - // } - // } - - // Console.WriteLine($"[DungeonGenV2] Generated {wallPositions.Count} auto-walls"); - //} private void GenerateAutoWalls(DungeonLayoutV2 layout) { - if (_tagIndex == null) + // Find wall tiles for each direction + var wallTiles = _tileDefinitions.Values.Where(t => t.Type == TileType.Wall).ToList(); + if (wallTiles.Count == 0) { - Console.WriteLine("[DungeonGenV2] TagIndex missing - skipping auto-wall generation"); + Console.WriteLine("[DungeonGenV2] No wall tiles found - skipping auto-wall generation"); return; } - var biome = DefaultBiome; - - bool InBounds(int x, int y) => x >= 0 && x < MapWidth && y >= 0 && y < MapHeight; - - // TRUE void semantics (your definition): no tile at all (or out of bounds) - bool IsVoidAt(int x, int y) + Console.WriteLine($"[DungeonGenV2] Found {wallTiles.Count} wall tiles for auto-wall generation"); + foreach (var wt in wallTiles) { - if (!InBounds(x, y)) return true; - return layout.Tiles[x, y] == 0; + Console.WriteLine($" - {wt.Id}: {wt.Name} (tags: {wt.Tags ?? "none"})"); } - bool IsWallAt(int x, int y) + // Helper to match tags. + // NOTE: Some of your data has tags separated by commas *and/or* whitespace (e.g. "corner_nw void"), + // so we split on both to be resilient. + bool HasTag(TileDefinition t, string tag) { - if (!InBounds(x, y)) return false; - var id = layout.Tiles[x, y]; - if (id <= 0) return false; - return _tagIndex.IsKind(id, TileKind.Wall); + if (string.IsNullOrWhiteSpace(t.Tags)) return false; + return t.Tags + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Any(s => s.Equals(tag, StringComparison.OrdinalIgnoreCase)); } - bool IsWalkableAt(int x, int y) + // Try to find directional walls by tags, fall back to first wall + var wallNorth = wallTiles.FirstOrDefault(t => HasTag(t, "north")) ?? wallTiles[0]; + var wallSouth = wallTiles.FirstOrDefault(t => HasTag(t, "south")) ?? wallTiles[0]; + var wallEast = wallTiles.FirstOrDefault(t => HasTag(t, "east")) ?? wallTiles[0]; + var wallWest = wallTiles.FirstOrDefault(t => HasTag(t, "west")) ?? wallTiles[0]; + // Corner tiles: + // - corner_* indicates which *quadrant of the tile contains the wall mass* (tile itself lives in void space) + // - many sets also include "void"-tagged corner variants meant specifically for outside/diagonal void corners. + var wallCornerNE = wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne") && !HasTag(t, "void")) + ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne")) + ?? wallTiles[0]; + var wallCornerNW = wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw") && !HasTag(t, "void")) + ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw")) + ?? wallTiles[0]; + var wallCornerSE = wallTiles.FirstOrDefault(t => HasTag(t, "corner_se") && !HasTag(t, "void")) + ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_se")) + ?? wallTiles[0]; + var wallCornerSW = wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw") && !HasTag(t, "void")) + ?? wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw")) + ?? wallTiles[0]; + + // Prefer these when a corner tile borders void on TWO orthogonal sides (classic outside corner). + var wallCornerNEVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_ne") && HasTag(t, "void")); + var wallCornerNWVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_nw") && HasTag(t, "void")); + var wallCornerSEVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_se") && HasTag(t, "void")); + var wallCornerSWVoid = wallTiles.FirstOrDefault(t => HasTag(t, "corner_sw") && HasTag(t, "void")); + + TileDefinition PickCorner(string cornerTag, bool preferVoid) { - if (!InBounds(x, y)) return false; - return layout.IsWalkable(x, y); + // cornerTag is one of: corner_ne, corner_nw, corner_se, corner_sw + return cornerTag.ToLowerInvariant() switch + { + "corner_ne" => (preferVoid ? wallCornerNEVoid : null) ?? wallCornerNE, + "corner_nw" => (preferVoid ? wallCornerNWVoid : null) ?? wallCornerNW, + "corner_se" => (preferVoid ? wallCornerSEVoid : null) ?? wallCornerSE, + "corner_sw" => (preferVoid ? wallCornerSWVoid : null) ?? wallCornerSW, + _ => wallCornerNW + }; } - // Index existing wall visuals so we can update/replace without duplicates + Console.WriteLine($"[DungeonGenV2] Wall tile mapping: N={wallNorth.Id}, S={wallSouth.Id}, E={wallEast.Id}, W={wallWest.Id}"); + + // Track positions that need walls + var wallPositions = new List<(int X, int Y, TileDefinition Tile)>(); + + // Check each void tile to see if it borders a floor + for (int x = 0; x < MapWidth; x++) + { + for (int y = 0; y < MapHeight; y++) + { + // Skip if not void + if (layout.Tiles[x, y] != 0) continue; + + // Check neighbors for floor tiles (walkable) + bool hasFloorNorth = y > 0 && layout.IsWalkable(x, y - 1); + bool hasFloorSouth = y < MapHeight - 1 && layout.IsWalkable(x, y + 1); + bool hasFloorEast = x < MapWidth - 1 && layout.IsWalkable(x + 1, y); + bool hasFloorWest = x > 0 && layout.IsWalkable(x - 1, y); + + // If any adjacent tile is floor, this should be a wall + if (hasFloorNorth || hasFloorSouth || hasFloorEast || hasFloorWest) + { + // Determine wall type based on which direction has floor + TileDefinition wallTile; + + // Check for corners first. + // IMPORTANT: corner_* tags indicate the quadrant where the *wall mass* lives inside this tile. + // That quadrant matches the direction of the interior (walkable) corner. + // Example: if the interior is to the NW (diagonal floor at NW), use corner_nw. + // If this is an *outside* corner (the two orthogonal sides away from the interior are void), + // prefer a corner variant that includes the "void" tag. + bool outsideN = IsOutsideAt(x, y - 1); + + bool outsideS = IsOutsideAt(x, y + 1); + + bool outsideE = IsOutsideAt(x + 1, y); + + bool outsideW = IsOutsideAt(x - 1, y); + + if (hasFloorNorth && hasFloorWest) + wallTile = PickCorner("corner_nw", preferVoid: (outsideS && outsideE)); + else if (hasFloorNorth && hasFloorEast) + wallTile = PickCorner("corner_ne", preferVoid: (outsideS && outsideW)); + else if (hasFloorSouth && hasFloorWest) + wallTile = PickCorner("corner_sw", preferVoid: (outsideN && outsideE)); + else if (hasFloorSouth && hasFloorEast) + wallTile = PickCorner("corner_se", preferVoid: (outsideN && outsideW)); + else if (hasFloorSouth) + wallTile = wallNorth; // Wall is north of floor, so it's a north-facing wall + else if (hasFloorNorth) + wallTile = wallSouth; + else if (hasFloorEast) + wallTile = wallWest; + else + wallTile = wallEast; + + wallPositions.Add((x, y, wallTile)); + } + } + } + + // Place wall tiles + foreach (var (x, y, tile) in wallPositions) + { + layout.Tiles[x, y] = tile.Id; + + // Walls are not walkable + if (layout.Walkable != null) + { + layout.Walkable[x, y] = false; + } + + layout.TileDetails.Add(new PlacedTile + { + X = x, + Y = y, + TileDefinitionId = tile.Id, + SheetId = tile.TileMapSheetId, + FrameIndex = tile.FrameIndex, + Layer = 2 // Walls on layer 2 + }); + } + + // Index existing wall visuals so we can update/replace them without duplicating TileDetails entries. var wallDetailIndex = new Dictionary<(int X, int Y), int>(); for (int i = 0; i < layout.TileDetails.Count; i++) { @@ -1296,27 +1093,47 @@ public class DungeonGeneratorV2 wallDetailIndex[(d.X, d.Y)] = i; } - void PlaceWall(int x, int y, TileDefinition tdef) + // Void-corner fill pass: when corridors/walls are thicker, some corner "caps" end up as void tiles that + // only see walkable space diagonally. Fill those with the appropriate connected corner wall so we don't + // get little void bite-marks at corridor turns and room borders. + bool IsWallAt(int wx, int wy) { - layout.Tiles[x, y] = tdef.Id; - if (layout.Walkable != null) layout.Walkable[x, y] = false; + if (wx < 0 || wx >= MapWidth || wy < 0 || wy >= MapHeight) return false; + int tid = layout.Tiles[wx, wy]; + if (tid == 0) return false; + return _tileDefinitions.TryGetValue(tid, out var td) && td.Type == TileType.Wall; + } - if (wallDetailIndex.TryGetValue((x, y), out var idx)) + + bool IsOutsideAt(int wx, int wy) + { + // "Outside"/"void" in your terminology: a tile that is NOT walkable and NOT a wall. + // This prevents treating floor as "void" while still recognizing unfilled space. + if (wx < 0 || wx >= MapWidth || wy < 0 || wy >= MapHeight) return true; // out of bounds counts as outside + if (layout.IsWalkable(wx, wy)) return false; + return !IsWallAt(wx, wy); + } + + void PlaceWall(int wx, int wy, TileDefinition tdef) + { + layout.Tiles[wx, wy] = tdef.Id; + if (layout.Walkable != null) layout.Walkable[wx, wy] = false; + + if (wallDetailIndex.TryGetValue((wx, wy), out var existingIdx)) { - var d = layout.TileDetails[idx]; + var d = layout.TileDetails[existingIdx]; d.TileDefinitionId = tdef.Id; d.SheetId = tdef.TileMapSheetId; d.FrameIndex = tdef.FrameIndex; - d.Layer = 2; - layout.TileDetails[idx] = d; + layout.TileDetails[existingIdx] = d; } else { - wallDetailIndex[(x, y)] = layout.TileDetails.Count; + wallDetailIndex[(wx, wy)] = layout.TileDetails.Count; layout.TileDetails.Add(new PlacedTile { - X = x, - Y = y, + X = wx, + Y = wy, TileDefinitionId = tdef.Id, SheetId = tdef.TileMapSheetId, FrameIndex = tdef.FrameIndex, @@ -1325,217 +1142,120 @@ public class DungeonGeneratorV2 } } - // --- Selection helpers ---------------------------------------------------- - - // Global forbids for "structural walls only" - string[] ForbidStructural = new[] - { - "door", "doorway", "connector", "decorative", "trim" - }; - - // When we do NOT want void variants, we explicitly forbid them - string[] ForbidVoidBoundary = new[] - { - "void", "empty", "boundary" - }; - - // Basic preference: tweak to your tileset taste - string[] PreferStructural = new[] - { - "solid", "stone" - }; - - // Safe fallback: any wall tile that is not void/boundary/trim/etc - TileDefinition? FallbackSolidWall() - { - // If TagIndex can enumerate, use it; otherwise fall back to tileDefinitions filtered by kind. - // We assume _tileDefinitions includes all defs; we keep it strict. - foreach (var td in _tileDefinitions.Values) - { - if (td.Id <= 0) continue; - if (!_tagIndex.IsKind(td.Id, TileKind.Wall)) continue; - - // Soft-filter using tag text (cheap) - var tags = (td.Tags ?? "").ToLowerInvariant(); - bool bad = - tags.Contains("door") || tags.Contains("doorway") || tags.Contains("connector") || - tags.Contains("decorative") || tags.Contains("trim") || - tags.Contains("void") || tags.Contains("empty") || tags.Contains("boundary"); - - if (!bad) - return td; - } - - // Absolute last resort: *any* wall - foreach (var td in _tileDefinitions.Values) - { - if (td.Id > 0 && _tagIndex.IsKind(td.Id, TileKind.Wall)) - return td; - } - - return null; - } - - TileDefinition FindBestOrFallback( - string[] requiredA, - string[]? requiredB, - bool preferVoidVariant, - string[]? preferExtra = null) - { - // Prefer list: structural + optional extras + (optionally "void" to bias to void variants) - var prefer = PreferStructural - .Concat(preferExtra ?? Array.Empty()) - .Concat(preferVoidVariant ? new[] { "void" } : Array.Empty()) - .ToArray(); - - // Forbid list: always forbid trim/decor/etc, and also forbid void/boundary if not preferring - var forbid = ForbidStructural - .Concat(preferVoidVariant ? Array.Empty() : ForbidVoidBoundary) - .ToArray(); - - // 1) Strict attempt (requiredA) - var t = _tagIndex!.FindBestWall( - requiredA, - prefer: prefer, - forbid: forbid, - allowTrim: false, - allowVoidBoundary: preferVoidVariant, - biome: biome - ); - - if (t != null) return t; - - // 2) Relaxed attempt (requiredB) if provided - if (requiredB != null) - { - t = _tagIndex!.FindBestWall( - requiredB, - prefer: prefer, - forbid: forbid, - allowTrim: false, - allowVoidBoundary: preferVoidVariant, - biome: biome - ); - - if (t != null) return t; - } - - // 3) Fallback to any solid wall (still a wall) - return FallbackSolidWall() ?? _tileDefinitions.Values.First(td => td.Id > 0); - } - - TileDefinition PickEdge(string dir, bool preferVoidVariant) - { - // Many tilesets do NOT tag "edge" consistently. So we try: - // - wall + edge + dir (strict) - // - wall + dir (relaxed) - var requiredStrict = new[] { "wall", "edge", dir }; - var requiredLoose = new[] { "wall", dir }; - - return FindBestOrFallback(requiredStrict, requiredLoose, preferVoidVariant); - } - - TileDefinition PickCorner(string cornerTag, bool preferVoidVariant) - { - // Many tilesets do NOT tag a generic "corner"; try: - // - wall + corner_xx (usually enough) - var required = new[] { "wall", cornerTag }; - return FindBestOrFallback(required, requiredB: null, preferVoidVariant); - } - - // --- MAIN PASS ------------------------------------------------------------ - - int placed = 0; - - for (int x = 0; x < MapWidth; x++) - { - for (int y = 0; y < MapHeight; y++) - { - // Only place into true void (no tile). Also skip walkable (should be redundant). - if (!IsVoidAt(x, y)) continue; - if (IsWalkableAt(x, y)) continue; - - bool floorN = IsWalkableAt(x, y - 1); - bool floorS = IsWalkableAt(x, y + 1); - bool floorE = IsWalkableAt(x + 1, y); - bool floorW = IsWalkableAt(x - 1, y); - - if (!(floorN || floorS || floorE || floorW)) continue; - - // Void checks for preference rule (EXPOSED SIDES of the WALL TILE are void) - // For an interior corner (floorN + floorW), the wall tile's exposed faces are S and E, etc. - bool vN = IsVoidAt(x, y - 1); - bool vS = IsVoidAt(x, y + 1); - bool vE = IsVoidAt(x + 1, y); - bool vW = IsVoidAt(x - 1, y); - - TileDefinition wall; - - // Corners first (interior corner based on two orthogonal floor neighbors) - if (floorN && floorW) wall = PickCorner("corner_nw", preferVoidVariant: (vS && vE)); - else if (floorN && floorE) wall = PickCorner("corner_ne", preferVoidVariant: (vS && vW)); - else if (floorS && floorW) wall = PickCorner("corner_sw", preferVoidVariant: (vN && vE)); - else if (floorS && floorE) wall = PickCorner("corner_se", preferVoidVariant: (vN && vW)); - else - { - // Edges (do NOT use void variants here by default; edges usually shouldn't be void-styled) - // Your earlier note about possible flips still applies; keep your mapping: - if (floorS) wall = PickEdge("north", preferVoidVariant: false); - else if (floorN) wall = PickEdge("south", preferVoidVariant: false); - else if (floorE) wall = PickEdge("west", preferVoidVariant: false); - else wall = PickEdge("east", preferVoidVariant: false); - } - - PlaceWall(x, y, wall); - placed++; - } - } - - // --- DIAGONAL CAP PASS ---------------------------------------------------- - // Fill "bite marks" where a void cell sees walkable diagonally and has adjacent wall support. for (int x = 1; x < MapWidth - 1; x++) { for (int y = 1; y < MapHeight - 1; y++) { - if (!IsVoidAt(x, y)) continue; - if (IsWalkableAt(x, y)) continue; + if (layout.Tiles[x, y] != 0) continue; // only fill void + // Don't overwrite walkable tiles (shouldn't happen because walkable implies non-zero, but safe). + if (layout.IsWalkable(x, y)) continue; - bool floorNE = IsWalkableAt(x + 1, y - 1); - bool floorNW = IsWalkableAt(x - 1, y - 1); - bool floorSE = IsWalkableAt(x + 1, y + 1); - bool floorSW = IsWalkableAt(x - 1, y + 1); + bool floorNE = layout.IsWalkable(x + 1, y - 1); + bool floorNW = layout.IsWalkable(x - 1, y - 1); + bool floorSE = layout.IsWalkable(x + 1, y + 1); + bool floorSW = layout.IsWalkable(x - 1, y + 1); bool wallN = IsWallAt(x, y - 1); bool wallS = IsWallAt(x, y + 1); bool wallE = IsWallAt(x + 1, y); bool wallW = IsWallAt(x - 1, y); - if (!(wallN || wallS || wallE || wallW)) continue; + // Only fill if there's "supporting" wall structure nearby; prevents random diagonal filling. + // The diagonal walkable indicates the interior corner; the adjacent walls indicate we are on the border. + // Diagonal-walkable indicates the interior corner. Choose the corner wall variant that visually + // connects the two bordering wall runs (this tileset uses the opposite naming compared to the + // earlier assumption, so NW<->SE and NE<->SW). + // Outside-void corners: if the two orthogonal tiles away from the interior are void, prefer "void" corner variants. + bool vN = IsOutsideAt(x, y - 1); - bool vN = IsVoidAt(x, y - 1); - bool vS = IsVoidAt(x, y + 1); - bool vE = IsVoidAt(x + 1, y); - bool vW = IsVoidAt(x - 1, y); + bool vS = IsOutsideAt(x, y + 1); - TileDefinition? cap = null; + bool vE = IsOutsideAt(x + 1, y); - // Same preference rule: pick void-variant if the cap tile would have 2 exposed sides void - if (floorSE && (wallS || wallE)) cap = PickCorner("corner_se", preferVoidVariant: (vN && vW)); - else if (floorSW && (wallS || wallW)) cap = PickCorner("corner_sw", preferVoidVariant: (vN && vE)); - else if (floorNE && (wallN || wallE)) cap = PickCorner("corner_ne", preferVoidVariant: (vS && vW)); - else if (floorNW && (wallN || wallW)) cap = PickCorner("corner_nw", preferVoidVariant: (vS && vE)); + bool vW = IsOutsideAt(x - 1, y); - if (cap != null) + if (floorSE && (wallS || wallE)) PlaceWall(x, y, PickCorner("corner_se", preferVoid: (vN && vW))); + else if (floorSW && (wallS || wallW)) PlaceWall(x, y, PickCorner("corner_sw", preferVoid: (vN && vE))); + else if (floorNE && (wallN || wallE)) PlaceWall(x, y, PickCorner("corner_ne", preferVoid: (vS && vW))); + else if (floorNW && (wallN || wallW)) PlaceWall(x, y, PickCorner("corner_nw", preferVoid: (vS && vE))); + } + } + + // Corner fix-up pass: if a wall tile meets void on the outside but is adjacent to walkable + // only diagonally (common with thicker walls / multi-tile corridors), swap in the connected + // corner tile so the silhouette doesn't look "broken". + // + // Example: a boundary wall tile that is NW of a floor tile might not see floor South/East + // directly (those may be other wall tiles), but it *will* see floor at SE diagonally. + for (int x = 1; x < MapWidth - 1; x++) + { + for (int y = 1; y < MapHeight - 1; y++) + { + int id = layout.Tiles[x, y]; + if (id == 0) continue; + if (!_tileDefinitions.TryGetValue(id, out var def)) continue; + if (def.Type != TileType.Wall) continue; + + bool floorN = layout.IsWalkable(x, y - 1); + bool floorS = layout.IsWalkable(x, y + 1); + bool floorE = layout.IsWalkable(x + 1, y); + bool floorW = layout.IsWalkable(x - 1, y); + + bool floorNE = layout.IsWalkable(x + 1, y - 1); + bool floorNW = layout.IsWalkable(x - 1, y - 1); + bool floorSE = layout.IsWalkable(x + 1, y + 1); + bool floorSW = layout.IsWalkable(x - 1, y + 1); + + TileDefinition? replace = null; + + // Choose connected outer corners using diagonal floor hints. + bool vN2 = IsOutsideAt(x, y - 1); + + bool vS2 = IsOutsideAt(x, y + 1); + + bool vE2 = IsOutsideAt(x + 1, y); + + bool vW2 = IsOutsideAt(x - 1, y); + + if (floorSE && !floorS && !floorE) replace = PickCorner("corner_se", preferVoid: (vN2 && vW2)); + else if (floorSW && !floorS && !floorW) replace = PickCorner("corner_sw", preferVoid: (vN2 && vE2)); + else if (floorNE && !floorN && !floorE) replace = PickCorner("corner_ne", preferVoid: (vS2 && vW2)); + else if (floorNW && !floorN && !floorW) replace = PickCorner("corner_nw", preferVoid: (vS2 && vE2)); + + if (replace == null) continue; + if (replace.Id == id) continue; + + layout.Tiles[x, y] = replace.Id; + if (layout.Walkable != null) layout.Walkable[x, y] = false; + + // Update existing wall visual if present; otherwise add. + if (wallDetailIndex.TryGetValue((x, y), out var idx)) { - PlaceWall(x, y, cap); - placed++; + var d = layout.TileDetails[idx]; + d.TileDefinitionId = replace.Id; + d.SheetId = replace.TileMapSheetId; + d.FrameIndex = replace.FrameIndex; + layout.TileDetails[idx] = d; + } + else + { + layout.TileDetails.Add(new PlacedTile + { + X = x, + Y = y, + TileDefinitionId = replace.Id, + SheetId = replace.TileMapSheetId, + FrameIndex = replace.FrameIndex, + Layer = 2 + }); } } } - Console.WriteLine($"[DungeonGenV2] Generated {placed} auto-walls (tag-driven, void-preference fixed)"); + Console.WriteLine($"[DungeonGenV2] Generated {wallPositions.Count} auto-walls"); } + private RoomTemplate? SelectWeightedTemplate(List templates) { if (templates.Count == 0) return null;