{
 "id": "bsp-dungeon-generation",
 "title": "BSP Dungeon Generation",
 "aliases": [
  "binary space partition dungeon",
  "room-and-corridor generation"
 ],
 "domain": "procgen",
 "subdomain": "layouts",
 "stage": "procedural",
 "realtime": "realtime",
 "platforms": [
  "cpu",
  "python",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 1998,
 "inputs": [],
 "outputs": [
  "layout",
  "graph"
 ],
 "style": "stylized",
 "dynamic": "static",
 "summary": "Recursively split a rectangle into smaller rectangles, place a room in each leaf and connect sibling subtrees with corridors, which gives connected room-and-corridor maps with no overlap.",
 "how_it_works": "Start with the full map rectangle as the root. Recursively split a node horizontally or vertically (biased by aspect ratio) at a random ratio until the leaves reach a minimum size. In each leaf, carve a room with random size and offset that stays inside the leaf with a margin. Walk back up the tree: for each internal node, connect a room from the left subtree to one in the right subtree with an L- or Z-shaped corridor. Since every sibling pair is joined, the whole dungeon is connected, and the tree gives a natural hierarchy for placing keys, stairs and boss rooms. Extra loops can be added by connecting random non-sibling rooms.",
 "when_to_use": "Choose it for roguelikes, level blockout and room layouts where guaranteed connectivity and no overlap are required. Prefer `wave-function-collapse` when you want an example-driven look with local tile adjacency, `markov-junior-rewrite-rules` for organic cave or maze growth, and `map-elites` to explore many parameter settings along descriptors like room count or loops.",
 "cost": {
  "class": "cheap",
  "notes": "Linear in number of leaves; microseconds to milliseconds."
 },
 "failure_modes": [
  "Boxy, grid-aligned look with rooms resembling each other",
  "Pure tree connectivity makes dead-end rooms with no loops, so navigation is linear",
  "Corridors can cross unrelated rooms unless routed with collision checks",
  "Split ratios near 0 or 1 create tiny leaves that cannot fit a room, leaving sparse regions"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation",
   "type": "web",
   "title": "Basic BSP Dungeon generation",
   "authors": [
    "RogueBasin"
   ],
   "year": null,
   "venue": "roguebasin.com",
   "url": "https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261110215435/https://roguebasin.com/index.php/Basic_BSP_Dungeon_generation",
     "timestamp": "20261110215435"
    },
    "page_title": "Basic BSP Dungeon generation - RogueBasin",
    "host": "roguebasin.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "roguebasinbasic"
  }
 ],
 "presets": [
  {
   "id": "bsp-dungeon-generation-specimen",
   "title": "BSP dungeon plate: recursive splits, one room per leaf, L-shaped corridors between siblings",
   "runtime": "glsl-fragment",
   "params": {
    "DEPTH": 4,
    "SEED": 3,
    "CORRIDOR_W": 0.009
   },
   "code": "// BSP dungeon: recursive rectangle split, one room per leaf, corridors join sibling subtrees.\nconst int DEPTH = 4;\nconst float SEED = 3.0;\nconst float CORRIDOR_W = 0.009;\nfloat h(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7)) + SEED) * 43758.5453); }\nvoid splitR(vec2 lo, vec2 hi, int i, out bool v, out float c) {\n    vec2 s = hi - lo; v = s.x > s.y;\n    float r = 0.4 + 0.2 * h(lo * 7.0 + float(i));\n    c = v ? lo.x + s.x * r : lo.y + s.y * r;\n}\nvec2 leafC(vec2 lo, vec2 hi, int i) {\n    for (int k = 0; k < DEPTH; k++) {\n        if (k < i) continue;\n        bool v; float c; splitR(lo, hi, k, v, c);\n        if (v) hi.x = c; else hi.y = c;\n    }\n    return 0.5 * (lo + hi);\n}\nfloat segd(vec2 p, vec2 a, vec2 b) {\n    vec2 pa = p - a, ba = b - a;\n    return length(pa - ba * clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0));\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = fragCoord / iResolution.xy;\n    vec2 lo = vec2(0.03), hi = vec2(0.97);\n    float cor = 1e9, split = 1e9;\n    for (int i = 0; i < DEPTH; i++) {\n        bool v; float c; splitR(lo, hi, i, v, c);\n        vec2 loA = lo, hiA = hi, loB = lo, hiB = hi;\n        if (v) { hiA.x = c; loB.x = c; } else { hiA.y = c; loB.y = c; }\n        vec2 a = leafC(loA, hiA, i + 1), b = leafC(loB, hiB, i + 1);\n        vec2 m = vec2(b.x, a.y);\n        cor = min(cor, min(segd(uv, a, m), segd(uv, m, b)));\n        split = min(split, v ? abs(uv.x - c) : abs(uv.y - c));\n        bool up = v ? uv.x > c : uv.y > c;\n        if (up) { lo = loB; hi = hiB; } else { lo = loA; hi = hiA; }\n    }\n    vec3 col = vec3(0.07, 0.08, 0.11) + 0.05 * h(floor(lo * 40.0));\n    vec2 ctr = 0.5 * (lo + hi), hs = (hi - lo) * (0.28 + 0.14 * h(lo * 3.0 + 1.0));\n    vec2 q = abs(uv - ctr) - hs;\n    float room = max(q.x, q.y);\n    vec3 rc = mix(vec3(0.75, 0.55, 0.30), vec3(0.45, 0.62, 0.70), h(lo * 5.0));\n    col = mix(col, vec3(0.35, 0.37, 0.42), smoothstep(0.004, 0.0, split - 0.002) * 0.6);\n    col = mix(col, vec3(0.55, 0.50, 0.42), smoothstep(CORRIDOR_W, CORRIDOR_W - 0.003, cor));\n    col = mix(col, rc * (0.85 + 0.15 * sin(uv.x * 200.0) * sin(uv.y * 200.0)), smoothstep(0.003, -0.003, room));\n    col = mix(col, vec3(0.1, 0.08, 0.06), smoothstep(0.004, 0.0, abs(room)) * 0.9);\n    fragColor = vec4(pow(col, vec3(0.8)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "bsp-dungeon-generation",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.3483,
      "std": 0.1995,
      "unique_colors": 142
     },
     "image": "/specimens/bsp-dungeon-generation-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "wave-function-collapse",
  "markov-junior-rewrite-rules",
  "map-elites",
  "cga-shape-grammar"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/procgen/bsp-dungeon-generation.json"
 },
 "solves": [
  "generate roguelike dungeon rooms that never overlap",
  "need random room layouts connected by corridors",
  "procedurally partition a map into rooms",
  "want a simple dungeon generator with guaranteed connectivity"
 ],
 "authority": "RogueBasin",
 "url": "https://texelatlas.vercel.app/t/bsp-dungeon-generation/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 7.96,
  "tier": "moderate",
  "p50_ms": 0.2619,
  "p95_ms": 0.2686,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "bsp-dungeon-generation-specimen",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 6.97,
   "tier": "moderate",
   "p50_ms": 0.1108,
   "p95_ms": 0.1112,
   "speedup_x": 2.36,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 5.1,
   "tier": "light",
   "p50_ms": 0.147,
   "p95_ms": 0.1471,
   "speedup_x": 1.78,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}