Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       1601import * as THREE from 'three';2import { PILL } from './config.js';34const STOREY = 3.5;5const LOT = 36; // Same as LOT in city.js (city.js imports this file, so it can't be imported back).67const TRIM = 0xd8d0bf;8const PILASTER = 0x8c8577;9const PIPE = 0x4a4a4a;10const AWNINGS = [0x7a2e2a, 0x2f5a6b, 0x6b5a2a, 0x2e4a2e, 0x4a3b6b];11const PLANTER = 0x3f6b35;12const DOOR = 0x4a3323;13const DOOR_GLASS = 0x1d2a34;14const METAL = 0x8a9097;15const DARK_METAL = 0x3a3f46;16const ROOF_TRIM = 0x55504a;17const PLINTH = 0x6f6a63;18const CORBEL = 0xc4bba6;19const STOOP = 0x8f8a82;20const CHIMNEY = 0x7a4a3a;21const WATER_TANK = 0x6b4a2b;2223const SIDEWALK = 0xa8a59b;24const JOINT = 0x7d7a72;25const CURB = 0x6c6a64;26const INSULATOR = 0x8a6a4a;27const SIGNAL_RED = 0xb32a1f;28const SIGNAL_AMBER = 0xd9a02e;29const SIGNAL_GREEN = 0x2f8a4a;30const LINE = 0xe6e2c8;31const MANHOLE = 0x2a2a2a;32const PATCH = 0x222225;33const TRUNK = 0x4a3520;34const LEAF = [0x3f6e33, 0x4c7f3c, 0x35592b];35const BIN = 0x2f4a33;36const POLE = 0x3a3d42;37const LAMP = 0xfff0b8;38const HYDRANT = 0xb32a1f;39const SIGN_RED = 0xb3261e;40const WOOD = 0x6e4e2e;4142const CAR_GLASS = 0x2a3440;43const CAR_TRIM = 0x1b1b1b;44const CAR_LIGHT = 0xfff2c0;45const CAR_TAIL = 0xa01818;46const TYRE = 0x141414;4748export function detailRng(seed) {49  let a = seed | 0;50  return function () {51    a = (a + 0x6d2b79f5) | 0;52    let t = Math.imul(a ^ (a >>> 15), 1 | a);53    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;54    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;55  };56}5758function vcyl(api, x, y0, z, rBot, rTop, h, hex, seg = 12) {59  api.add(new THREE.CylinderGeometry(rTop, rBot, h, seg).translate(x, y0 + h / 2, z), hex);60}6162function ball(api, x, y, z, r, hex, seg = 8) {63  api.add(new THREE.SphereGeometry(r, seg, Math.max(4, seg - 2)).translate(x, y, z), hex);64}6566function windowCenters(L) {67  const n = Math.max(1, Math.floor(L / 5));68  return Array.from({ length: n }, (_, k) => (L * (k + 1)) / (n + 1));69}7071function facadeSlab(b, side, u0, v0, u1, v1, d0, d1, hex, box) {72  let x0, x1, z0, z1;73  if (side === 'n' || side === 's') {74    x0 = b.x0 + u0;75    x1 = b.x0 + u1;76    if (side === 'n') {77      z0 = b.z1 + d0;78      z1 = b.z1 + d1;79    } else {80      z0 = b.z0 - d1;81      z1 = b.z0 - d0;82    }83  } else {84    z0 = b.z0 + u0;85    z1 = b.z0 + u1;86    if (side === 'e') {87      x0 = b.x1 + d0;88      x1 = b.x1 + d1;89    } else {90      x0 = b.x0 - d1;91      x1 = b.x0 - d0;92    }93  }94  box(x0, v0, z0, x1, v1, z1, hex);95}9697function facadeBox(b, side, u0, v0, u1, v1, depth, hex, box) {98  facadeSlab(b, side, u0, v0, u1, v1, -0.02, depth, hex, box);99}100101// Each building rolls its own traits from its position, so no two look the same.102// Boarded windows are solid, so bullets stop there. Open windows show the room inside.103const FASCIA = [0x2f2b28, 0x3a3f46, 0x5a2e2a, 0x2e4a2e, 0x3b3658, 0x6b5a2a];104const AC_UNIT = 0x9aa0a6;105const AC_GRILLE = 0x4a4f55;106const HATCH = 0x3a3f46;107const WARNING = 0xb32a1f;108const PLYWOOD = 0x8a6d4a;109const PLANK = [0x6e4e2e, 0x5a4026, 0x7a5a36, 0x6a5a48];110const BOARD_CHANCE = [0.55, 0.3]; // ground floor, upper floors111const BRICK = [0x7a4a3a, 0x8a5a44, 0x6a6a68, 0x9a9488, 0x5d4a3f];112113export function buildingSeed(b) {114  return Math.floor((b.x0 + 500) * 7919 + (b.z0 + 500) * 104729);115}116117// Some buildings are half destroyed: the far side of a cut plane loses everything above cutH.118// Walls, floors, the roof and anything else built there are trimmed to the cut (see clipApi in city.js).119export function damageFor(b) {120  if (b.player || b.storeys < 2) return null;121  const r = detailRng(buildingSeed(b) ^ 0x5bd1e995);122  if (r() > 0.3) return null;123  return {124    cutX: b.x0 + (b.x1 - b.x0) * (0.35 + r() * 0.3),125    cutH: STOREY * (1 + Math.floor(r() * (b.storeys - 1))),126  };127}128129export function decorateBuilding(api, b) {130  const rng = detailRng(buildingSeed(b));131  const H = b.storeys * STOREY;132  const traits = {133    fascia: rng() < 0.6,134    lettering: rng() < 0.5,135    bays: rng() < 0.6,136    sash: rng() < 0.6,137    windowBoxes: rng() < 0.35,138    acUnits: rng() < 0.5,139    corbels: rng() < 0.5,140    antenna: rng() < 0.5,141    hatch: rng() < 0.5,142  };143  const roofUnits = Math.floor(rng() * 3);144  const sides = [145    { side: 'n', L: b.x1 - b.x0 },146    { side: 's', L: b.x1 - b.x0 },147    { side: 'e', L: b.z1 - b.z0 },148    { side: 'w', L: b.z1 - b.z0 },149  ];150151  for (const { side, L } of sides) {152    const open = b.open ? b.open[side] : true;153    const bays = windowCenters(L);154    facadeSlab(b, side, -0.1, 0, L + 0.1, 0.9, -0.02, 0.03, PLINTH, api.box);155156    // Ground-floor fascia, sometimes with sign lettering.157    if (traits.fascia && rng() < 0.8) {158      const fTop = Math.min(3.25, H - 0.5);159      facadeBox(b, side, 0.2, 2.78, L - 0.2, fTop, 0.1, FASCIA[Math.floor(rng() * FASCIA.length)], api.box);160      if (traits.lettering) {161        for (let u = 0.9; u < L - 0.9; u += 1.2) facadeBox(b, side, u, 2.88, u + 0.7, 2.98, 0.14, TRIM, api.box);162      }163    }164165    // Pilaster strips between the bays, from the fascia up to the cornice.166    if (traits.bays && H - 0.4 > 3.3) {167      for (let k = 0; k < bays.length - 1; k++) {168        const m = (bays[k] + bays[k + 1]) / 2;169        facadeBox(b, side, m - 0.18, 3.3, m + 0.18, H - 0.4, 0.12, PILASTER, api.box);170      }171    }172173    if (open) {174      for (let s = 0; s < b.storeys; s++) {175        const v0 = s * STOREY + 1.0;176        const v1 = s * STOREY + 2.6;177        const mid = (v0 + v1) / 2;178        bays.forEach((c, k) => {179          facadeSlab(b, side, c - 0.8, v0, c - 0.74, v1, -0.12, 0.03, TRIM, api.box);180          facadeSlab(b, side, c + 0.74, v0, c + 0.8, v1, -0.12, 0.03, TRIM, api.box);181          // Start at the wall face (d0 = 0, not facadeBox's -0.02): the -0.02 overlap182          // put the sill top / lintel underside coplanar with the wall's -> z-fighting.183          facadeSlab(b, side, c - 0.95, v0 - 0.12, c + 0.95, v0, 0, 0.14, TRIM, api.box);184          facadeSlab(b, side, c - 0.95, v1, c + 0.95, v1 + 0.14, 0, 0.1, TRIM, api.box);185          if (traits.sash) {186            facadeSlab(b, side, c - 0.03, v0, c + 0.03, v1, -0.02, 0.05, TRIM, api.box);187            facadeSlab(b, side, c - 0.74, mid - 0.03, c + 0.74, mid + 0.03, -0.02, 0.05, TRIM, api.box);188          }189          if (!b.player && rng() < BOARD_CHANCE[s === 0 ? 0 : 1]) {190            // Boarded: a solid sheet fills the opening, with planks nailed across the front.191            facadeSlab(b, side, c - 0.8, v0, c + 0.8, v1, -0.2, -0.14, PLYWOOD, api.solidBox);192            for (const dv of [0.05, 0.5, 0.95, 1.35]) {193              facadeSlab(b, side, c - 0.9, v0 + dv, c + 0.9, v0 + dv + 0.18, 0.02, 0.09, PLANK[k % PLANK.length], api.box);194            }195          }196          if (traits.windowBoxes && s === 1) facadeBox(b, side, c - 0.9, v0 - 0.22, c + 0.9, v0 - 0.12, 0.3, PLANTER, api.box);197          if (traits.acUnits && s >= 2 && k === 0) {198            facadeSlab(b, side, c - 0.5, v0 - 0.8, c + 0.5, v0 - 0.25, 0, 0.5, AC_UNIT, api.box);199            facadeSlab(b, side, c - 0.42, v0 - 0.72, c + 0.42, v0 - 0.33, 0.5, 0.53, AC_GRILLE, api.box);200          }201        });202      }203    }204205    // Entrance: door with a glass panel, stoop and canopy.206    if (rng() < 0.6) {207      const dc = 1.3;208      facadeBox(b, side, dc - 0.6, 0, dc - 0.5, 2.25, 0.14, TRIM, api.box);209      facadeBox(b, side, dc + 0.5, 0, dc + 0.6, 2.25, 0.14, TRIM, api.box);210      facadeBox(b, side, dc - 0.6, 2.25, dc + 0.6, 2.35, 0.14, TRIM, api.box);211      facadeBox(b, side, dc - 0.5, 0, dc + 0.5, 2.2, 0.05, DOOR, api.box);212      facadeSlab(b, side, dc - 0.3, 1.45, dc + 0.3, 1.95, 0.05, 0.06, DOOR_GLASS, api.box);213      facadeSlab(b, side, dc - 0.8, 0, dc + 0.8, 0.14, -0.02, 0.5, STOOP, api.box);214      facadeBox(b, side, dc - 0.7, 2.4, dc + 1.2, 2.55, 0.6, AWNINGS[Math.floor(rng() * AWNINGS.length)], api.box);215    }216217    for (let s = 1; s < b.storeys; s++) {218      facadeBox(b, side, -0.1, s * STOREY - 0.08, L + 0.1, s * STOREY + 0.02, 0.08, TRIM, api.box);219    }220    facadeBox(b, side, -0.1, H - 0.4, L + 0.1, H, 0.32, TRIM, api.box);221    if (traits.corbels) {222      for (let u = 1.2; u < L - 1; u += 2.4) {223        facadeSlab(b, side, u, H - 0.85, u + 0.2, H - 0.4, 0.05, 0.3, CORBEL, api.box);224      }225    }226    facadeBox(b, side, 0, 0, 0.4, H, 0.12, PILASTER, api.box);227    facadeBox(b, side, L - 0.4, 0, L, H, 0.12, PILASTER, api.box);228    if (rng() < 0.5) facadeBox(b, side, L - 0.62, 0, L - 0.48, H, 0.1, PIPE, api.box);229  }230231  const x0 = b.x0 - 0.2;232  const x1 = b.x1 + 0.2;233  const z0 = b.z0 - 0.2;234  const z1 = b.z1 + 0.2;235  const base = H + 0.3;236  const ph = 0.7;237  const pt = 0.22;238  api.box(x0, base, z0, x1, base + ph, z0 + pt, ROOF_TRIM);239  api.box(x0, base, z1 - pt, x1, base + ph, z1, ROOF_TRIM);240  api.box(x0, base, z0, x0 + pt, base + ph, z1, ROOF_TRIM);241  api.box(x1 - pt, base, z0, x1, base + ph, z1, ROOF_TRIM);242243  for (let i = 0; i < roofUnits; i++) {244    const fx = [0.3, 0.65][i];245    const fz = [0.35, 0.6][i];246    const ux = x0 + 1.5 + fx * (x1 - x0 - 4.2);247    const uz = z0 + 1.5 + fz * (z1 - z0 - 4);248    api.box(ux, base, uz, ux + 1.3, base + 0.9, uz + 1.0, METAL);249    api.box(ux + 0.1, base + 0.9, uz + 0.1, ux + 1.2, base + 0.95, uz + 0.9, DARK_METAL);250  }251  if (traits.hatch) {252    const hx = x1 - 2.3;253    const hz = z0 + 1.0;254    api.box(hx, base, hz, hx + 1.2, base + 0.25, hz + 1.2, HATCH);255    api.box(hx + 0.12, base + 0.25, hz + 0.12, hx + 1.08, base + 0.28, hz + 1.08, DOOR_GLASS);256  }257  if (rng() < 0.35) {258    const hx = x0 + 1.2 + rng() * (x1 - x0 - 2.4);259    const hz = z0 + 1.2 + rng() * (z1 - z0 - 2.4);260    vcyl(api, hx, base, hz, 0.22, 0.2, 1.4, CHIMNEY, 10);261    api.box(hx - 0.28, base + 1.4, hz - 0.28, hx + 0.28, base + 1.5, hz + 0.28, DARK_METAL);262  }263  if (traits.antenna) {264    const ax = x0 + 0.9;265    const az = z0 + 0.9;266    vcyl(api, ax, base, az, 0.06, 0.03, 3.2, DARK_METAL, 6);267    api.box(ax - 0.4, base + 2.4, az - 0.03, ax + 0.4, base + 2.46, az + 0.03, DARK_METAL);268    api.box(ax - 0.3, base + 1.6, az - 0.03, ax + 0.3, base + 1.66, az + 0.03, DARK_METAL);269    ball(api, ax, base + 3.25, az, 0.08, WARNING, 6);270  }271  if (b.storeys >= 4 && rng() < 0.35) {272    const tx = x0 + 3 + rng() * (x1 - x0 - 7);273    const tz = z0 + 3 + rng() * (z1 - z0 - 7);274    for (const [dx, dz] of [[0, 0], [1.4, 0], [0, 1.4], [1.4, 1.4]]) {275      api.box(tx + dx, base, tz + dz, tx + dx + 0.12, base + 2.6, tz + dz + 0.12, DARK_METAL);276    }277    vcyl(api, tx + 0.7, base + 2.6, tz + 0.7, 0.95, 0.95, 1.7, WATER_TANK, 14);278    api.add(new THREE.ConeGeometry(1.05, 0.5, 14).translate(tx + 0.7, base + 4.55, tz + 0.7), DARK_METAL);279  }280}281282const pick = (arr, rng) => arr[Math.floor(rng() * arr.length)];283284// Furniture is laid out in the room's own frame: u runs along the piece, v across it, y up from the floor.285function furnish(api, floorY, cx, cz, rot) {286  const at = (u, v) => (rot ? [cx + v, cz + u] : [cx + u, cz + v]);287  return (u0, y0, v0, u1, y1, v1, hex) => {288    const [ax, az] = at(u0, v0);289    const [bx, bz] = at(u1, v1);290    api.box(Math.min(ax, bx), floorY + y0, Math.min(az, bz), Math.max(ax, bx), floorY + y1, Math.max(az, bz), hex);291  };292}293294const FURN = {295  frame: 0x5a4632,296  sheets: [0x9aa3ad, 0xb8a58e, 0x7d8c9a],297  pillow: 0xe8e4da,298  fabric: [0x5d6b4a, 0x6b3a3a, 0x3a4a5e, 0x7a6a4a],299  wood: 0x7a5a38,300  darkWood: 0x4a3520,301  books: [0x8c2a2a, 0x2a4a7a, 0xc9a94a, 0x3d6b3a, 0x6a3a6a],302  fridge: 0xdfe3e6,303  cabinet: 0xb8b2a6,304  counter: 0x3a3a3a,305  screen: 0x2a4a6a,306  stand: 0x222222,307  rugs: [0x7a3a3a, 0x3a5a6a, 0x8a7a4a],308  lamp: 0xfff0b8,309};310311// Each piece takes the local box helper f(u0, y0, v0, u1, y1, v1, hex) and the RNG.312const PIECES = [313  (f, rng) => {314    f(-1.0, 0, -0.5, 1.0, 0.5, 0.5, FURN.frame);315    f(-0.95, 0.5, -0.45, 0.95, 0.68, 0.45, pick(FURN.sheets, rng));316    f(0.45, 0.68, -0.35, 0.9, 0.8, 0.35, FURN.pillow);317  },318  (f, rng) => {319    const c = pick(FURN.fabric, rng);320    f(-1.0, 0, -0.4, 1.0, 0.45, 0.4, c);321    f(-1.0, 0.45, -0.4, 1.0, 0.9, -0.28, c);322    f(-1.0, 0.45, -0.4, -0.88, 0.65, 0.4, c);323    f(0.88, 0.45, -0.4, 1.0, 0.65, 0.4, c);324  },325  (f) => {326    f(-0.6, 0.7, -0.45, 0.6, 0.76, 0.45, FURN.wood);327    for (const u of [-0.52, 0.5]) for (const v of [-0.37, 0.35]) f(u, 0, v, u + 0.06, 0.7, v + 0.06, FURN.darkWood);328  },329  (f) => {330    f(-0.5, 0, -0.28, 0.5, 2.0, 0.28, FURN.darkWood);331    f(-0.44, 0.05, 0.28, 0.44, 1.95, 0.3, FURN.wood);332  },333  (f, rng) => {334    f(-0.5, 0, -0.17, 0.5, 2.1, 0.17, FURN.darkWood);335    for (const y of [0.5, 1.1, 1.7]) f(-0.42, y, -0.14, 0.42, y + 0.45, 0.14, pick(FURN.books, rng));336  },337  (f) => {338    f(-0.35, 0, -0.35, 0.35, 1.7, 0.35, FURN.fridge);339    f(0.3, 0.9, -0.38, 0.33, 1.3, -0.33, FURN.stand);340  },341  (f) => {342    f(-1.1, 0, -0.3, 1.1, 0.9, 0.3, FURN.cabinet);343    f(-1.12, 0.9, -0.32, 1.12, 0.95, 0.32, FURN.counter);344  },345  (f) => {346    f(-0.5, 0.35, -0.1, 0.5, 0.45, 0.1, FURN.stand);347    f(-0.45, 0.45, -0.11, 0.45, 0.95, -0.09, FURN.screen);348  },349  (f) => {350    f(-0.7, 0.75, -0.35, 0.7, 0.8, 0.35, FURN.wood);351    f(-0.2, 0.8, -0.05, 0.2, 1.2, 0.05, FURN.stand);352    f(-0.18, 0.9, -0.07, 0.18, 1.2, -0.06, FURN.screen);353  },354  (f, rng) => {355    f(-1.0, 0, -0.7, 1.0, 0.02, 0.7, pick(FURN.rugs, rng));356  },357];358359// Furniture inside the building, visible through the open windows. Nothing here is solid.360export function decorateRooms(api, b) {361  const rng = detailRng(buildingSeed(b) ^ 0x2f6e2b1);362  for (let s = 0; s < b.storeys; s++) {363    const floorY = s === 0 ? 0.1 : s * STOREY;364    const cordTop = (s + 1) * STOREY - 0.15;365    const n = 3 + Math.floor(rng() * 3);366    for (let i = 0; i < n; i++) {367      const x = b.x0 + 1.5 + rng() * (b.x1 - b.x0 - 3);368      const z = b.z0 + 1.5 + rng() * (b.z1 - b.z0 - 3);369      const rot = rng() < 0.5;370      pick(PIECES, rng)(furnish(api, floorY, x, z, rot), rng);371    }372    const lx = (b.x0 + b.x1) / 2 + (rng() - 0.5) * 2;373    const lz = (b.z0 + b.z1) / 2 + (rng() - 0.5) * 2;374    api.box(lx - 0.01, cordTop - 0.5, lz - 0.01, lx + 0.01, cordTop, lz + 0.01, DARK_METAL);375    vcyl(api, lx, cordTop - 0.75, lz, 0.2, 0.1, 0.25, FURN.lamp, 10);376  }377}378379// Rubble on the floor and out on the pavement beside a half destroyed building.380export function decorateRubble(api, b) {381  const d = b.damage;382  const rng = detailRng(buildingSeed(b) + 11);383  for (let i = 0; i < 28; i++) {384    const x = d.cutX + rng() * (b.x1 + 1.5 - d.cutX);385    const z = b.z0 - 0.5 + rng() * (b.z1 - b.z0 + 1);386    const s = 0.3 + rng() * 0.7;387    const h = 0.1 + rng() * 0.35;388    api.box(x, 0.05, z, x + s, 0.05 + h, z + s * (0.6 + rng() * 0.6), pick(BRICK, rng));389  }390}391392export function decorateStreets(api, lots) {393  const rng = detailRng(99991);394  const edge = Math.max(...lots.map((l) => l.cx));395  for (const { cx, cz } of lots) {396    api.box(cx + 12, 0, cz - 14, cx + 14, 0.14, cz + 14, SIDEWALK);397    api.box(cx - 14, 0, cz - 14, cx - 12, 0.14, cz + 14, SIDEWALK);398    api.box(cx - 12, 0, cz + 12, cx + 12, 0.14, cz + 14, SIDEWALK);399    api.box(cx - 12, 0, cz - 14, cx + 12, 0.14, cz - 12, SIDEWALK);400401    // Paving joints every 4 m along each pavement strip.402    for (let k = -10; k <= 10; k += 4) {403      api.box(cx + 12, 0.14, cz + k, cx + 14, 0.145, cz + k + 0.06, JOINT);404      api.box(cx - 14, 0.14, cz + k, cx - 12, 0.145, cz + k + 0.06, JOINT);405      api.box(cx + k, 0.14, cz + 12, cx + k + 0.06, 0.145, cz + 14, JOINT);406      api.box(cx + k, 0.14, cz - 14, cx + k + 0.06, 0.145, cz - 12, JOINT);407    }408409    api.box(cx + 14, 0, cz - 14, cx + 14.2, 0.2, cz + 14, CURB);410    api.box(cx - 14.2, 0, cz - 14, cx - 14, 0.2, cz + 14, CURB);411    api.box(cx - 14, 0, cz + 14, cx + 14, 0.2, cz + 14.2, CURB);412    api.box(cx - 14, 0, cz - 14.2, cx + 14, 0.2, cz - 14, CURB);413414    for (const d of [-10, -2, 6]) {415      api.box(cx + 17.94, 0, cz + d, cx + 18.06, 0.02, cz + d + 4, LINE);416      api.box(cx + d, 0, cz + 17.94, cx + d + 4, 0.02, cz + 18.06, LINE);417    }418419    for (let k = 0; k < 7; k++) {420      api.box(cx + 14.7 + k, 0, cz - 13.5, cx + 15.2 + k, 0.03, cz - 10.5, LINE);421      api.box(cx + 10.7, 0, cz + 14.7 + k, cx + 13.7, 0.03, cz + 15.2 + k, LINE);422    }423424    if (rng() < 0.3) api.box(cx + 17.5, 0, cz - 7.5, cx + 18.5, 0.03, cz - 6.5, MANHOLE);425    if (rng() < 0.25) {426      const px = cx + 15 + rng() * 6;427      const pz = cz - 10 + rng() * 20;428      api.box(px, 0, pz, px + 2, 0.012, pz + 2, PATCH);429    }430431    vcyl(api, cx + 12.8, 0.14, cz - 13.5, 0.27, 0.25, 0.9, BIN, 10);432    vcyl(api, cx + 12.8, 1.04, cz - 13.5, 0.3, 0.3, 0.06, DARK_METAL, 10);433434    if (rng() < 0.6) {435      const z0 = cz + 3;436      const z1 = cz + 5.2;437      api.box(cx + 12.3, 0.42, z0, cx + 13.5, 0.5, z1, WOOD);438      api.box(cx + 12.3, 0.5, z0, cx + 12.4, 0.95, z1, WOOD);439      for (const x of [cx + 12.4, cx + 13.35]) {440        for (const z of [z0 + 0.15, z1 - 0.15]) {441          api.box(x - 0.04, 0.14, z - 0.04, x + 0.04, 0.42, z + 0.04, DARK_METAL);442        }443      }444    }445446    if (rng() < 0.7) {447      const hx = cx - 13;448      const hz = cz - 5.5;449      vcyl(api, hx, 0.14, hz, 0.12, 0.11, 0.55, HYDRANT, 10);450      ball(api, hx, 0.69, hz, 0.12, HYDRANT, 8);451      api.box(hx - 0.22, 0.3, hz - 0.04, hx - 0.1, 0.38, hz + 0.04, HYDRANT);452    }453454    // A tree in a grate on every block corner, same on every block.455    const tx = cx - 13;456    const tz = cz + 13;457    api.box(tx - 0.45, 0.14, tz - 0.45, tx + 0.45, 0.16, tz + 0.45, DARK_METAL);458    vcyl(api, tx, 0.14, tz, 0.13, 0.1, 2.5, TRUNK, 8);459    ball(api, tx, 3.1, tz, 1.0, LEAF[Math.floor(rng() * LEAF.length)], 10);460    ball(api, tx + 0.45, 3.6, tz - 0.35, 0.7, LEAF[Math.floor(rng() * LEAF.length)], 8);461    ball(api, tx - 0.5, 2.9, tz + 0.3, 0.65, LEAF[Math.floor(rng() * LEAF.length)], 8);462463    const lx = cx + 13.2;464    const lz = cz + 13.2;465    vcyl(api, lx, 0.14, lz, 0.2, 0.16, 0.5, POLE, 10);466    vcyl(api, lx, 0.64, lz, 0.08, 0.06, 5.5, POLE, 10);467    api.box(lx, 6.0, lz - 0.06, lx + 2.0, 6.12, lz + 0.06, POLE);468    api.box(lx + 1.7, 5.9, lz - 0.2, lx + 2.2, 6.0, lz + 0.2, POLE);469    api.box(lx + 1.75, 5.84, lz - 0.16, lx + 2.15, 5.9, lz + 0.16, LAMP);470471    // Traffic signal on the south-east corner. Every intersection gets exactly one.472    const sx = cx + 14.6;473    const sz = cz + 14.6;474    vcyl(api, sx, 0.14, sz, 0.09, 0.07, 4.2, POLE, 8);475    api.box(sx, 4.0, sz - 0.05, sx + 2.2, 4.12, sz + 0.05, POLE);476    api.box(sx + 1.4, 3.05, sz - 0.16, sx + 1.7, 3.95, sz + 0.16, DARK_METAL);477    for (const [dy, col] of [[0.72, SIGNAL_RED], [0.45, SIGNAL_AMBER], [0.18, SIGNAL_GREEN]]) {478      api.box(sx + 1.7, 3.05 + dy, sz - 0.1, sx + 1.76, 3.05 + dy + 0.16, sz + 0.1, col);479    }480481    // Utility pole on the north-west corner, with two power lines running east to the next block.482    const px = cx - 14.6;483    const pz = cz - 14.6;484    vcyl(api, px, 0.14, pz, 0.11, 0.08, 7.4, POLE, 8);485    api.box(px - 0.06, 6.9, pz - 0.9, px + 0.06, 7.05, pz + 0.9, POLE);486    for (const dz of [-0.85, 0.85]) {487      ball(api, px, 7.12, pz + dz, 0.07, INSULATOR, 6);488      if (cx < edge) api.box(px, 6.92, pz + dz - 0.015, px + LOT, 6.95, pz + dz + 0.015, POLE);489    }490491    if (rng() < 0.5) {492      const sx = cx - 13.4;493      const sz = cz - 13.4;494      vcyl(api, sx, 0.14, sz, 0.035, 0.035, 2.3, POLE, 6);495      api.add(new THREE.CylinderGeometry(0.3, 0.3, 0.03, 8).rotateZ(Math.PI / 2).translate(sx, 2.25, sz), SIGN_RED);496    }497  }498}499500export function decorateCar(api, x, z, along, hex) {501  const at = (u, v) => (along ? [x + u, z + v] : [x + v, z + u]);502  const bx = (u0, y0, v0, u1, y1, v1, col) => {503    const [ax, az] = at(u0, v0);504    const [cx2, cz2] = at(u1, v1);505    api.box(Math.min(ax, cx2), y0, Math.min(az, cz2), Math.max(ax, cx2), y1, Math.max(az, cz2), col);506  };507508  bx(-1.26, 0.8, -0.57, 1.26, 1.4, 0.57, CAR_GLASS);509  bx(-1.05, 1.4, -0.52, 1.05, 1.46, 0.52, hex);510  bx(-2.16, 0.22, -0.9, -2.08, 0.5, 0.9, CAR_TRIM);511  bx(2.08, 0.22, -0.9, 2.16, 0.5, 0.9, CAR_TRIM);512  bx(2.09, 0.55, 0.45, 2.12, 0.68, 0.8, CAR_LIGHT);513  bx(2.09, 0.55, -0.8, 2.12, 0.68, -0.45, CAR_LIGHT);514  bx(-2.12, 0.55, 0.5, -2.09, 0.66, 0.85, CAR_TAIL);515  bx(-2.12, 0.55, -0.85, -2.09, 0.66, -0.5, CAR_TAIL);516517  for (const u of [-1.35, 1.35]) {518    for (const v of [-0.95, 0.95]) {519      const [wx, wz] = at(u, v);520      const g = new THREE.CylinderGeometry(0.34, 0.34, 0.24, 12);521      if (along) g.rotateX(Math.PI / 2);522      else g.rotateZ(Math.PI / 2);523      api.add(g.translate(wx, 0.34, wz), TYRE);524    }525  }526}527528// Open roof deck (third floor): crates, planters, a picnic table and a lantern.529// Everything the player can bump into is solid so it doesn't clip.530export function decorateRoof(api, b, solidBox, y) {531  const CRATE = 0x7a5a36;532  const CRATE_TOP = 0x8f6d44;533  const TUB = 0x5a4a3a;534  const LEAF_TOP = [0x3f6e33, 0x4c7f3c, 0x35592b];535536  const crate = (x, z, ly) => solidBox(x, y + ly, z, x + 1, y + ly + 0.9, z + 1, CRATE);537  crate(-6, -4, 0);538  crate(-4.8, -4, 0);539  crate(-5.4, -4, 0.9);540  crate(-6, -2.6, 0);541542  const planter = (x, z, i) => {543    solidBox(x, y, z, x + 1.4, y + 0.6, z + 0.6, TUB);544    ball(api, x + 0.7, y + 1.05, z + 0.3, 0.5, LEAF_TOP[i % LEAF_TOP.length]);545  };546  planter(-8.2, 2.5, 0);547  planter(-8.2, 4.2, 1);548  planter(6.4, -6.0, 2);549550  solidBox(-1.0, y + 0.75, -0.6, 1.0, y + 0.85, 0.6, WOOD);551  solidBox(-0.9, y, -0.5, -0.8, y + 0.75, 0.5, DARK_METAL);552  solidBox(0.8, y, -0.5, 0.9, y + 0.75, 0.5, DARK_METAL);553554  solidBox(2.6, y, -3.4, 2.7, y + 2.4, -3.3, DARK_METAL);555  ball(api, 2.65, y + 2.5, -3.35, 0.2, LAMP, 10);556}557558export function decorateInterior(api, b, solidBox) {559  const rng = detailRng(4242);560  const T = 2 * STOREY;561  const plank = [0x8a6a48, 0x7d5f3f, 0x947452, 0x735636];562  let i = 0;563  for (let x = b.x0 + 0.3; x < b.x1 - 0.3; x += 0.32, i++) {564    api.box(x, T, b.z0 + 0.3, Math.min(x + 0.3, b.x1 - 0.3), T + 0.02, b.z1 - 0.3, plank[i % plank.length]);565  }566  const BASE = 0x2a2420;567  api.box(b.x0 + 0.3, T, b.z0 + 0.3, b.x0 + 0.36, T + 0.12, b.z1 - 0.3, BASE);568  api.box(b.x1 - 0.36, T, b.z0 + 0.3, b.x1 - 0.3, T + 0.12, b.z1 - 0.3, BASE);569  api.box(b.x0 + 0.3, T, b.z1 - 0.36, b.x1 - 0.3, T + 0.12, b.z1 - 0.3, BASE);570  api.box(b.x0 + 0.3, T, b.z0 + 0.3, b.x1 - 0.3, T + 0.12, b.z0 + 0.36, BASE);571572  const PANEL = 0x4a3a2a;573  const RAIL = 0x5a4632;574  const walls = [575    [b.x0 + 0.3, b.z0 + 0.3, b.x0 + 0.33, b.z1 - 0.3],576    [b.x1 - 0.33, b.z0 + 0.3, b.x1 - 0.3, b.z1 - 0.3],577    [b.x0 + 0.3, b.z0 + 0.3, b.x1 - 0.3, b.z0 + 0.33],578    [b.x0 + 0.3, b.z1 - 0.33, b.x1 - 0.3, b.z1 - 0.3],579  ];580  for (const [a0, c0, a1, c1] of walls) {581    api.box(a0, T + 0.12, c0, a1, T + 0.85, c1, PANEL);582    api.box(a0, T + 0.85, c0, a1, T + 0.9, c1, RAIL);583  }584585  const beam = 0x4a3a2a;586  for (const z of [-4, 0, 4]) api.box(b.x0 + 0.3, STOREY * 3 - 0.4, z - 0.12, b.x1 - 0.3, STOREY * 3, z + 0.12, beam);587588  const sill = 0x8a6a48;589  const sillTop = T + 1.0;590  // Inner edge stops exactly at the wall face (WALL_T = 0.3); going past it591  // makes the sill top coplanar with the wall's window-bottom face -> z-fighting.592  for (const c of windowCenters(b.x1 - b.x0)) {593    facadeSlab(b, 'n', c - 0.9, sillTop - 0.07, c + 0.9, sillTop, -0.46, -0.3, sill, api.box);594    facadeSlab(b, 's', c - 0.9, sillTop - 0.07, c + 0.9, sillTop, -0.46, -0.3, sill, api.box);595  }596  for (const c of windowCenters(b.z1 - b.z0)) {597    facadeSlab(b, 'e', c - 0.9, sillTop - 0.07, c + 0.9, sillTop, -0.46, -0.3, sill, api.box);598    facadeSlab(b, 'w', c - 0.9, sillTop - 0.07, c + 0.9, sillTop, -0.46, -0.3, sill, api.box);599  }600601  const pendant = (x, z) => {602    api.box(x - 0.01, 9.67, z - 0.01, x + 0.01, STOREY * 3, z + 0.01, 0x222222);603    vcyl(api, x, 9.45, z, 0.28, 0.1, 0.22, 0xe8dcae, 14);604  };605  pendant(3, 0);606  pendant(-6.5, -4);607  pendant(4.5, 5.5);608609  api.box(-0.5, T, -3.5, 3.5, T + 0.03, 3.5, 0x6b2f2a);610611  // Small side table with the pill on it (see PILL in config.js).612  solidBox(0.9, T, -2.0, 1.9, T + 0.9, -1.0, 0x5d4a35);613  // The pill is two capsule halves, red and white.614  api.box(PILL.x - 0.05, T + 0.9, PILL.z - 0.02, PILL.x, T + 0.95, PILL.z + 0.02, 0xc83a3a);615  api.box(PILL.x, T + 0.9, PILL.z - 0.02, PILL.x + 0.05, T + 0.95, PILL.z + 0.02, 0xf2f0ea);616617  api.box(-0.9, 8.1, -4.7, -0.84, 9.5, -2.8, 0x3a2a1a);618  api.box(-0.84, 8.2, -4.6, -0.82, 9.4, -2.9, 0xd8cfb0);619620  const bench = 0x6e4e2e;621  const steel = 0x2d3033;622  const top = T + 0.9;623  api.box(-7.6, T, -5.1, -5.5, top, -2.9, 0x5a3f25);624  api.box(-7.6, top, -5.1, -5.4, top + 0.08, -2.9, bench);625  for (const [z0, z1] of [[-4.9, -4.0], [-3.9, -3.0]]) {626    for (const [y0, y1] of [[T + 0.08, T + 0.4], [T + 0.45, T + 0.75]]) {627      api.box(-5.5, y0, z0, -5.44, y1, z1, 0x7a5a38);628      api.box(-5.44, (y0 + y1) / 2 - 0.03, (z0 + z1) / 2 - 0.1, -5.4, (y0 + y1) / 2 + 0.03, (z0 + z1) / 2 + 0.1, 0x2a2a2a);629    }630  }631  api.box(-7.4, top + 0.08, -4.9, -6.9, top + 0.34, -4.4, steel);632  api.box(-7.4, top + 0.34, -4.9, -7.35, top + 0.5, -4.4, steel);633  api.box(-6.0, top + 0.08, -3.9, -5.6, top + 0.34, -3.4, steel);634  api.box(-5.85, top + 0.34, -3.8, -5.75, top + 0.7, -3.5, steel);635  api.box(-6.6, top + 0.08, -4.3, -6.52, top + 0.12, -4.25, 0x222222);636  api.box(-6.9, top + 0.08, -3.1, -6.2, top + 0.11, -3.0, 0x6a6e74);637  for (let k = 0; k < 8; k++) {638    const sx = -7.3 + rng() * 1.8;639    const sz = -4.8 + rng() * 1.6;640    api.box(sx, top + 0.08, sz, sx + 0.04, top + 0.1, sz + 0.02, 0xc0a04a);641  }642  api.box(-7.2, top + 0.08, -6.6, -7.0, top + 0.13, -6.5, 0x9a4a2a);643  api.box(-6.8, top + 0.08, -6.6, -6.4, top + 0.25, -6.5, 0x5a5f3a);644645  api.box(-7.4, top + 1.1, -6.68, -5.6, top + 1.8, -6.62, 0x9a7d55);646  api.box(-7.0, top + 1.3, -6.6, -6.9, top + 1.5, -6.55, DARK_METAL);647  api.box(-6.2, top + 1.3, -6.6, -6.1, top + 1.5, -6.55, DARK_METAL);648  api.box(-6.4, top + 1.45, -6.6, -6.2, top + 1.7, -6.52, 0x222222);649650  api.box(-6.4, top + 1.9, -6.6, -6.2, top + 2.1, -6.3, DARK_METAL);651  api.box(-6.4, top + 2.2, -6.4, -6.2, top + 2.4, -6.0, DARK_METAL);652  api.box(-6.8, top + 2.0, -6.0, -5.8, top + 2.25, -5.4, 0x2b3a2b);653654  vcyl(api, -4.3, T, -2.3, 0.22, 0.22, 0.04, 0x222222, 12);655  vcyl(api, -4.3, T + 0.04, -2.3, 0.035, 0.035, 0.55, steel, 8);656  vcyl(api, -4.3, T + 0.59, -2.3, 0.2, 0.2, 0.07, bench, 12);657658  api.box(-4.5, T + 0.44, -4.4, -3.9, T + 0.5, -3.9, 0x3a2e22);659  for (const [sx, sz] of [[-4.45, -4.35], [-3.95, -4.35], [-4.45, -3.95], [-3.95, -3.95]]) {660    api.box(sx - 0.03, T, sz - 0.03, sx + 0.03, T + 0.44, sz + 0.03, 0x2a2a2a);661  }662663  const cab = 0x35404a;664  api.box(-7.4, T + 0.05, 3.6, -7.36, T + 1.95, 4.6, 0x2a3138);665  api.box(-7.36, T + 0.95, 4.02, -7.34, T + 1.0, 4.08, 0x111111);666  api.box(-7.36, T + 1.0, 4.02, -7.34, T + 1.04, 4.08, 0x111111);667  for (let r = 0; r < 3; r++) {668    api.box(-8.5, T + 2.02, 3.7 + r * 0.25, -7.5, T + 2.1, 3.78 + r * 0.25, 0x222a22);669  }670  api.box(-8.4, T, 2.2, -7.9, T + 0.45, 2.8, 0x5a5f3a);671  api.box(-8.4, T + 0.45, 2.2, -7.9, T + 0.8, 2.8, 0x5a5f3a);672  api.box(-7.7, T, 2.3, -7.2, T + 0.35, 2.9, 0x6a5a3a);673674  const table = 0x4a3b2c;675  api.box(3.5, T + 0.9, 4.9, 5.5, T + 0.96, 6.1, 0x5d4a35);676  api.box(3.7, T + 0.96, 5.1, 4.1, T + 1.08, 5.45, 0xa8925e);677  api.box(4.15, T + 0.96, 5.1, 4.55, T + 1.08, 5.45, 0xa8925e);678  api.box(3.9, T + 1.08, 5.1, 4.35, T + 1.2, 5.3, 0xa8925e);679  api.box(4.6, T + 0.96, 5.6, 5.1, T + 1.16, 5.85, 0x5a5f3a);680  api.box(4.8, T + 0.96, 5.1, 4.82, T + 1.3, 5.12, 0x222222);681682  api.box(3.6, T + 0.45, 3.9, 4.5, T + 0.5, 4.6, 0x3a2e22);683  api.box(3.6, T + 0.5, 4.55, 4.5, T + 0.95, 4.6, 0x3a2e22);684685  solidBox(8.2, T, -6.0, 8.7, T + 1.2, -2.0, 0x4a4a48);686  for (let k = 0; k < 6; k++) {687    api.box(8.3 + (k % 3) * 0.12, T + 1.2, -5.6 + Math.floor(k / 3) * 0.6 + k * 0.1, 8.4 + (k % 3) * 0.12, T + 1.36, -5.5 + Math.floor(k / 3) * 0.6 + k * 0.1, 0x9aa0a6);688  }689690  api.box(4.6, T, -4.2, 5.4, T + 0.6, -3.4, 0x6b5238);691  api.box(4.7, T + 0.6, -4.1, 5.3, T + 1.2, -3.5, 0x5d4a35);692693  vcyl(api, 7.4, T, -5.6, 0.3, 0.3, 0.9, 0x3d4a3a, 12);694  vcyl(api, 7.4, T + 0.15, -5.6, 0.31, 0.31, 0.05, 0x222222, 12);695  vcyl(api, 7.4, T + 0.7, -5.6, 0.31, 0.31, 0.05, 0x222222, 12);696  vcyl(api, 7.4, T + 0.9, -5.6, 0.27, 0.27, 0.03, 0x2a2a2a, 12);697698  vcyl(api, 7.7, T, 6.0, 0.2, 0.17, 0.45, 0x8a4a2a, 10);699  ball(api, 7.7, T + 0.85, 6.0, 0.38, PLANTER, 8);700  ball(api, 7.45, T + 0.7, 6.2, 0.25, 0x4a7d3c, 8);701}