{
 "id": "cellular-automata-cave-generation",
 "title": "Cellular automata cave generation",
 "aliases": [
  "CA caves",
  "cave smoothing rule",
  "4-5 rule"
 ],
 "domain": "procgen",
 "subdomain": "cave and dungeon layout",
 "stage": "procedural",
 "realtime": "realtime",
 "platforms": [
  "cpu",
  "webgl2",
  "python",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 2010,
 "inputs": [
  "layout"
 ],
 "outputs": [
  "layout",
  "voxels"
 ],
 "style": "stylized",
 "dynamic": "static",
 "summary": "Fills a grid with random walls, then repeatedly replaces each cell by a majority-style rule over its neighbours, which erodes noise into organic cave chambers and tunnels.",
 "how_it_works": "Initialise a 2D or 3D grid with each cell wall with probability around 45 percent. Run a few smoothing iterations where every cell counts its wall neighbours in the 8-cell or 26-cell neighbourhood: a cell becomes a wall if it has at least a threshold number of wall neighbours and otherwise open (a common choice is the 4-5 rule, with an early-stage variant that also fills isolated pockets). Noise collapses into blobs and the blobs merge into cave-like caverns. Afterwards, flood-fill to label connected open regions, drop tiny ones, and connect the survivors with tunnels or keep only the largest. Border cells are forced solid. Convert to a mesh with `marching-cubes` or `surface-nets` for smooth cave walls, or to blocks for voxel games. An infinite variant processes chunks with a window of neighbours and seed-hash initialisation.",
 "when_to_use": "Choose it for quick organic caves and island-like shapes in grids where you want few knobs and no authored layout. Prefer `bsp-dungeon-generation` for rooms and corridors, `wave-function-collapse` or `model-synthesis` when adjacency rules and authored tiles matter, `markov-junior-rewrite-rules` for rule-based tile rewriting, and `worley-noise` or `fractal-brownian-motion` thresholds when you need continuous, unbounded fields.",
 "cost": {
  "class": "cheap",
  "notes": "A few passes over the grid; trivial on the CPU for 2D, moderate for 3D volumes and chunked infinite worlds."
 },
 "failure_modes": [
  "Disconnected caverns unless a flood-fill and tunnel-carving pass joins them",
  "Result looks uniformly blobby with no large-scale structure or guaranteed path length",
  "Chunked generation shows seams at borders because each chunk smooths without the neighbours' cells",
  "Parameters are brittle: a small change to the initial wall density flips between solid rock and one open void"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/1814256.1814266",
   "type": "paper",
   "title": "Cellular automata for real-time generation of infinite cave levels",
   "authors": [
    "Lawrence Johnson",
    "Georgios N. Yannakakis",
    "Julian Togelius"
   ],
   "year": 2010,
   "venue": "Proceedings of the 2010 Workshop on Procedural Content Generation in Games",
   "url": "https://doi.org/10.1145/1814256.1814266",
   "ids": {
    "doi": "10.1145/1814256.1814266"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1814256.1814266",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1814256.1814266",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1814256.1814266",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1814256.1814266",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1814256.1814266",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "johnson2010cellular"
  }
 ],
 "presets": [
  {
   "id": "cellular-automata-cave-generation-live",
   "title": "Cellular automata caves — B5678/S45678 Moore-neighborhood cavern carving",
   "runtime": "glsl-fragment",
   "params": {
    "P_FILL_DENSITY": 0.48,
    "P_SMOOTHING": 1,
    "P_CELL_SCALE": 26,
    "P_WATER_LEVEL": 0.42
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_FILL_DENSITY = 0.48; // P Fill Density [0.25, 0.72, 0.01]\nconst float P_SMOOTHING = 1.0; // P CA Smoothing [0.0, 2.0, 0.05]\nconst float P_CELL_SCALE = 26.0; // P Cell Scale [10.0, 48.0, 1.0]\nconst float P_WATER_LEVEL = 0.42; // P Aquifer Glow [0.0, 1.0, 0.02]\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(123.34, 456.21));\n  p += dot(p, p + 45.32);\n  return fract(p.x * p.y);\n}\n\n// Initial random rock fill with gentle macro cavern bias\nfloat initCell(vec2 cell, float fill) {\n  float h = hash21(cell);\n  // Subtle macro room seed so connected caverns emerge reliably\n  float macro = 0.5 + 0.5 * sin(cell.x * 0.22 + 0.7) * cos(cell.y * 0.21 - 0.4);\n  float thresh = clamp(fill + (macro - 0.5) * 0.18, 0.15, 0.85);\n  return step(1.0 - thresh, h);\n}\n\n// Pass 1: 3x3 Moore neighborhood count (birth/survival >= 5 of 9 cells)\nfloat caPass1(vec2 c, float fill) {\n  float sum = 0.0;\n  for (int dy = -1; dy <= 1; dy++) {\n    for (int dx = -1; dx <= 1; dx++) {\n      sum += initCell(c + vec2(float(dx), float(dy)), fill);\n    }\n  }\n  return smoothstep(4.2, 5.2, sum);\n}\n\n// Pass 2: second smoothing generation over Pass 1\nfloat caPass2(vec2 c, float fill) {\n  float sum = 0.0;\n  for (int dy = -1; dy <= 1; dy++) {\n    for (int dx = -1; dx <= 1; dx++) {\n      sum += caPass1(c + vec2(float(dx), float(dy)), fill);\n    }\n  }\n  return sum / 9.0;\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float scale = clamp(P_CELL_SCALE, 8.0, 56.0);\n  vec2 pan = vec2(iTime * 0.45, sin(iTime * 0.22) * 1.4);\n  vec2 gPos = uv * scale + pan;\n  vec2 cell = floor(gPos);\n  vec2 f = fract(gPos);\n\n  float fill = clamp(P_FILL_DENSITY, 0.2, 0.8);\n  float sm = clamp(P_SMOOTHING, 0.0, 2.0);\n\n  // Bilinearly sample smoothed CA field across 4 neighboring cells for crisp organic walls\n  vec2 w = f * f * (3.0 - 2.0 * f);\n  float s00 = mix(caPass1(cell, fill), caPass2(cell, fill), clamp(sm, 0.0, 1.0));\n  float s10 = mix(caPass1(cell + vec2(1.0, 0.0), fill), caPass2(cell + vec2(1.0, 0.0), fill), clamp(sm, 0.0, 1.0));\n  float s01 = mix(caPass1(cell + vec2(0.0, 1.0), fill), caPass2(cell + vec2(0.0, 1.0), fill), clamp(sm, 0.0, 1.0));\n  float s11 = mix(caPass1(cell + vec2(1.0, 1.0), fill), caPass2(cell + vec2(1.0, 1.0), fill), clamp(sm, 0.0, 1.0));\n  float field = mix(mix(s00, s10, w.x), mix(s01, s11, w.x), w.y);\n\n  // Blend between crisp tile-level CA and interpolated smooth cave wall\n  float rawCell = caPass2(cell, fill);\n  float caveVal = mix(rawCell, field, clamp(sm * 0.55, 0.0, 1.0));\n\n  // Gradient for rim bevel & top-left torch lighting\n  vec2 grad = vec2(\n    mix(s10 - s00, s11 - s01, w.y),\n    mix(s01 - s00, s11 - s10, w.x)\n  );\n  vec3 nrm = normalize(vec3(-grad * 4.5, 1.0));\n  vec2 torchPos = vec2(0.28 * cos(iTime * 0.6), 0.22 * sin(iTime * 0.48));\n  vec3 lDir = normalize(vec3(torchPos - uv, 0.35));\n  float torchAtt = 1.0 / (1.0 + 3.8 * dot(torchPos - uv, torchPos - uv));\n\n  // Wall vs open floor mask\n  float isWall = smoothstep(0.46, 0.54, caveVal);\n  float wallRim = smoothstep(0.12, 0.0, abs(caveVal - 0.5));\n\n  // Stratified cavern limestone coloring\n  float strata = 0.5 + 0.5 * sin(gPos.y * 1.4 + sin(gPos.x * 0.45) * 1.6);\n  float grain = hash21(floor(gPos * 4.0));\n  vec3 rockCol = mix(vec3(0.14, 0.12, 0.11), vec3(0.32, 0.27, 0.23), strata * 0.65 + 0.35 * grain);\n  float ndl = max(dot(nrm, lDir), 0.0);\n  rockCol *= (0.35 + 1.15 * ndl * torchAtt);\n  rockCol += vec3(0.55, 0.42, 0.28) * wallRim * (0.4 + 0.9 * torchAtt);\n\n  // Open cavern floor with subtle pebble texture and glowing turquoise aquifer pools\n  float depth = clamp((0.48 - caveVal) * 2.5, 0.0, 1.0);\n  vec3 floorCol = mix(vec3(0.08, 0.085, 0.095), vec3(0.04, 0.055, 0.07), depth);\n  floorCol *= (0.5 + 1.4 * torchAtt);\n\n  float waterMask = smoothstep(0.58 - 0.42 * P_WATER_LEVEL, 0.78 - 0.42 * P_WATER_LEVEL, depth);\n  float ripple = 0.5 + 0.5 * sin(gPos.x * 2.6 + iTime * 2.2) * cos(gPos.y * 2.4 - iTime * 1.8);\n  vec3 waterCol = vec3(0.06, 0.46, 0.58) * (0.75 + 0.45 * ripple) + vec3(0.18, 0.85, 0.92) * pow(ripple, 5.0) * 0.45;\n  floorCol = mix(floorCol, waterCol, waterMask * clamp(P_WATER_LEVEL * 1.2, 0.0, 1.0));\n\n  // Subtle tile-grid overlay to reveal underlying cellular lattice\n  vec2 cellEdge = abs(f - 0.5);\n  float tileLine = smoothstep(0.45, 0.5, max(cellEdge.x, cellEdge.y));\n  vec3 col = mix(floorCol, rockCol, isWall);\n  col = mix(col, col * 0.72 + vec3(0.03, 0.04, 0.05), tileLine * 0.35);\n\n  // Warm moving explorer torch aura\n  col += vec3(1.0, 0.68, 0.32) * exp(-length(uv - torchPos) * 7.5) * 0.35;\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "cellular-automata-cave-generation",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5711,
      "std": 0.1206,
      "unique_colors": 451
     },
     "image": "/specimens/cellular-automata-cave-generation-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "bsp-dungeon-generation",
  "wave-function-collapse",
  "markov-junior-rewrite-rules",
  "marching-cubes",
  "surface-nets"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "surface-nets",
   "why": "Treating the wall mask as a scalar field and meshing it with surface nets gives smooth organic cave walls from the blocky automaton output."
  }
 ],
 "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/cellular-automata-cave-generation.json"
 },
 "solves": [
  "want organic cave layouts from simple rules",
  "random noise grid to cave chambers",
  "roguelike map generator with few parameters",
  "generated caves are disconnected rooms"
 ],
 "authority": "Johnson et al., 2010",
 "url": "https://texelatlas.vercel.app/t/cellular-automata-cave-generation/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 71.56,
  "tier": "extreme",
  "p50_ms": 2.3542,
  "p95_ms": 2.4307,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "cellular-automata-cave-generation-live",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 70.81,
   "tier": "extreme",
   "p50_ms": 1.1259,
   "p95_ms": 1.1341,
   "speedup_x": 2.09,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 52.46,
   "tier": "heavy",
   "p50_ms": 1.5109,
   "p95_ms": 1.5114,
   "speedup_x": 1.56,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}