From 25cc24fa7a66d44b4d18729ab316a7cbc8faba7e Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 30 Dec 2025 14:26:37 -0500 Subject: [PATCH] Attempting GPT Change to dungeon builder --- Game/Dungeon/DungeonGeneratorV2.cs | 207 +++++++++++++++++++++-------- 1 file changed, 150 insertions(+), 57 deletions(-) diff --git a/Game/Dungeon/DungeonGeneratorV2.cs b/Game/Dungeon/DungeonGeneratorV2.cs index 93a6690..7543145 100644 --- a/Game/Dungeon/DungeonGeneratorV2.cs +++ b/Game/Dungeon/DungeonGeneratorV2.cs @@ -1262,11 +1262,17 @@ public class DungeonGeneratorV2 return; } - // We only place *structural* walls here (solid). Add a later pass if you want trim/decor replacement. 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) + { + if (!InBounds(x, y)) return true; + return layout.Tiles[x, y] == 0; + } + bool IsWallAt(int x, int y) { if (!InBounds(x, y)) return false; @@ -1275,12 +1281,10 @@ public class DungeonGeneratorV2 return _tagIndex.IsKind(id, TileKind.Wall); } - bool IsOutsideAt(int x, int y) + bool IsWalkableAt(int x, int y) { - // "Outside" means: out of bounds OR not walkable AND not a wall - if (!InBounds(x, y)) return true; - if (layout.IsWalkable(x, y)) return false; - return !IsWallAt(x, y); + if (!InBounds(x, y)) return false; + return layout.IsWalkable(x, y); } // Index existing wall visuals so we can update/replace without duplicates @@ -1321,57 +1325,149 @@ 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) { - // Require: wall + edge + direction - // Prefer: solid - // Forbid: door/connector/decorative/trim by default - var required = new[] { "wall", "edge", dir }; - var prefer = new[] { "solid", "stone" }; - var forbid = preferVoidVariant - ? new[] { "door", "doorway", "connector", "decorative", "trim" } // allow void/boundary variants if tagged, but don't force it - : new[] { "door", "doorway", "connector", "decorative", "trim", "void", "empty", "boundary" }; + // 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 }; - var t = _tagIndex!.FindBestWall(required, prefer: prefer, forbid: forbid, allowTrim: false, allowVoidBoundary: preferVoidVariant, biome: biome); - return t ?? _tileDefinitions.Values.First(td => td.Id > 0); // should never happen, but keeps you from crashing + return FindBestOrFallback(requiredStrict, requiredLoose, preferVoidVariant); } TileDefinition PickCorner(string cornerTag, bool preferVoidVariant) { - // Require: wall + corner_xx + // Many tilesets do NOT tag a generic "corner"; try: + // - wall + corner_xx (usually enough) var required = new[] { "wall", cornerTag }; - var prefer = new[] { "solid", "stone" }; - var forbid = preferVoidVariant - ? new[] { "door", "doorway", "connector", "decorative", "trim" } - : new[] { "door", "doorway", "connector", "decorative", "trim", "void", "empty", "boundary" }; - - var t = _tagIndex!.FindBestWall(required, prefer: prefer, forbid: forbid, allowTrim: false, allowVoidBoundary: preferVoidVariant, biome: biome); - return t ?? PickEdge("north", preferVoidVariant: false); + return FindBestOrFallback(required, requiredB: null, preferVoidVariant); } - // MAIN PASS: fill void cells adjacent to walkable with edge/corner walls + // --- MAIN PASS ------------------------------------------------------------ + int placed = 0; for (int x = 0; x < MapWidth; x++) { for (int y = 0; y < MapHeight; y++) { - // Only place into empty/void cells - if (layout.Tiles[x, y] != 0) continue; - if (layout.IsWalkable(x, y)) continue; + // 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 = InBounds(x, y - 1) && layout.IsWalkable(x, y - 1); - bool floorS = InBounds(x, y + 1) && layout.IsWalkable(x, y + 1); - bool floorE = InBounds(x + 1, y) && layout.IsWalkable(x + 1, y); - bool floorW = InBounds(x - 1, y) && layout.IsWalkable(x - 1, y); + 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; - // Outside check for corner void 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); + // 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; @@ -1382,13 +1478,8 @@ public class DungeonGeneratorV2 else if (floorS && floorE) wall = PickCorner("corner_se", preferVoidVariant: (vN && vW)); else { - // Edges - // NOTE: Direction tag semantics vary by tileset. This mapping assumes dir tag refers to the wall's "side"/edge. - // If your walls look flipped, swap these: - // - // If floor is NORTH of this void cell, the wall tile is on the SOUTH edge of the floor, and might be tagged "south". - // If floor is SOUTH of this void cell, the wall tile might be tagged "north". - // + // 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); @@ -1400,19 +1491,19 @@ public class DungeonGeneratorV2 } } - // DIAGONAL CAP PASS: - // Fill "bite marks" where void sees walkable only diagonally but has nearby wall support. + // --- 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 (layout.Tiles[x, y] != 0) continue; - if (layout.IsWalkable(x, y)) continue; + if (!IsVoidAt(x, y)) continue; + if (IsWalkableAt(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 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 wallN = IsWallAt(x, y - 1); bool wallS = IsWallAt(x, y + 1); @@ -1421,13 +1512,14 @@ public class DungeonGeneratorV2 if (!(wallN || wallS || wallE || wallW)) continue; - bool vN = IsOutsideAt(x, y - 1); - bool vS = IsOutsideAt(x, y + 1); - bool vE = IsOutsideAt(x + 1, y); - bool vW = IsOutsideAt(x - 1, y); + 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? cap = null; + // 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)); @@ -1441,8 +1533,9 @@ public class DungeonGeneratorV2 } } - Console.WriteLine($"[DungeonGenV2] Generated {placed} auto-walls (tag-driven)"); + Console.WriteLine($"[DungeonGenV2] Generated {placed} auto-walls (tag-driven, void-preference fixed)"); } + private RoomTemplate? SelectWeightedTemplate(List templates) { if (templates.Count == 0) return null;