Back to Faultline
SOURCE / PINNED RELEASE

Made of little things.

Faultline

Release
da1ffaa5624c…
Author-recorded commit
60f5f03189db…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6932b12c78c

Archive hash verified: 5a2e0492417ec4e7…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/maps.ts
import { office, woodland, resistance, type Material } from './battlefields';
import { groundCrossing, groundHeight, type Terrain } from './terrain';
import { combat } from './combat-tuning';
export type Vec = [number, number, number];
export type Box = {
  x: number;
  y: number;
  z: number;
  w: number;
  h: number;
  d: number;
  color: string;
  solid: boolean;
  roof?: boolean;
  boundary?: boolean;
  shotCost?: number;
  material?: Material;
  pane?: number;
  shape?: 'pine' | 'stone';
};
export type Arena = {
  name: string;
  sector: string;
  type: string;
  description: string;
  size: number;
  boxes: Box[];
  spawns: Vec[];
  terrain?: Terrain;
  intactBoxes?: Box[];
  broken?: number;
  practiceContact: Vec;
  sky: string;
};
export const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
export function buildArena(indoor: boolean): Arena {
  const boxes: Box[] = [];
  // Coordinates describe the centre in X/Z and the bottom in Y.
  function box(
    x: number,
    y: number,
    z: number,
    w: number,
    h: number,
    d: number,
    color: string,
    solid = true,
    roof = false,
    material?: Material,
    shotCost?: number,
  ) {
    boxes.push({ x, y, z, w, h, d, color, solid, roof, material, shotCost });
  }
  const concrete = '#a7987c',
    steel = '#515b56',
    dark = '#424c49',
    trim = '#c8b99b',
    red = '#aa5840',
    ochre = '#b78d51';
  function container(x: number, z: number, vertical: boolean, color: string) {
    const w = vertical ? 3 : 7,
      d = vertical ? 7 : 3;
    box(x, 0, z, w, 2.8, d, color);
    for (let i = -3.1; i <= 3.1; i += 0.64) {
      if (vertical) {
        for (const s of [-1, 1]) box(x + s * 1.51, 0.15, z + i, 0.09, 2.47, 0.13, color, false);
      } else {
        for (const s of [-1, 1]) box(x + i, 0.15, z + s * 1.51, 0.13, 2.47, 0.09, color, false);
      }
    }
    box(x, 2.78, z, w + 0.12, 0.12, d + 0.12, color, false);
    box(x + w / 2 + 0.03, 0.35, z, 0.06, 0.65, Math.min(1.5, d - 0.2), trim, false);
  }
  function crate(x: number, z: number, size = 1.65, y = 0) {
    box(x, y, z, size, size * 0.88, size, '#977d55', true, false, 'plywood', combat.shot.crateCost);
    for (const s of [-1, 1])
      box(x + s * size * 0.31, y, z, 0.11, size * 0.89, size + 0.05, '#565c4d', false);
    box(x, y + 0.15, z, size + 0.04, 0.15, size + 0.04, '#77664c', false);
  }
  function hall(
    x: number,
    z: number,
    w: number,
    d: number,
    h: number,
    color: string,
    openings: 'ends' | 'all' = 'all',
  ) {
    for (const s of [-1, 1]) {
      const wing = (w - 3.4) / 2;
      for (const t of [-1, 1])
        box(x + (t * (w - wing)) / 2, 0, z + (s * d) / 2, wing, h, 0.45, color, true, false, 'drywall');
      box(x, 2.9, z + (s * d) / 2, 3.45, h - 2.9, 0.45, color, true, false, 'brick');
      if (openings === 'ends') box(x + (s * w) / 2, 0, z, 0.45, h, d, color, true, false, 'drywall');
      else {
        const side = (d - 3.6) / 2;
        for (const t of [-1, 1])
          box(
            x + (s * w) / 2,
            0,
            z + (t * (d - side)) / 2,
            0.45,
            h,
            side,
            color,
            true,
            false,
            'drywall',
          );
        box(x + (s * w) / 2, 2.95, z, 0.45, h - 2.95, 3.65, color, true, false, 'brick');
      }
      for (let k = -w / 2 + 0.7; k < w / 2; k += 1.35) {
        if (Math.abs(k) > 1.85)
          box(x + k, h - 0.95, z + s * (d / 2 + 0.24), 0.92, 0.53, 0.06, '#506260', false);
      }
      for (const t of [-1, 1]) box(x + (t * w) / 2, 0, z + (s * d) / 2, 0.62, h + 0.18, 0.62, steel);
    }
    box(x, h, z, w + 0.7, 0.24, d + 0.7, '#718078', true, true);
    for (let k = -w / 2; k < w / 2; k += 1.8)
      box(x + k, h + 0.24, z, 0.13, 0.08, d + 0.48, '#93a096', false, true);
    box(x, 0.005, z, w, 0.025, d, '#888779', false);
    box(x, h - 0.24, z, 2.8, 0.09, 0.32, '#ffeab1', false);
  }
  function gantry(x: number, z: number, width: number) {
    for (const s of [-1, 1]) {
      box(x + (s * width) / 2, 0, z, 0.64, 7.2, 0.64, ochre);
      box(x + (s * width) / 2, 0, z, 1.05, 0.45, 1.05, concrete);
    }
    box(x, 6.7, z, width + 0.8, 0.75, 0.65, ochre);
    for (let i = -width / 2 + 0.7; i < width / 2; i += 1.2)
      box(x + i, 6.84, z - 0.34, 0.55, 0.37, 0.07, dark, false);
    box(x + 1.2, 6.4, z, 1.8, 0.38, 0.95, steel, false);
    box(x + 1.2, 4.7, z, 0.075, 1.7, 0.075, dark, false);
  }
  // Each step is a solid tread. Bridges leave clearance for the lane beneath.
  function stairs(
    x: number,
    lowZ: number,
    direction: number,
    count: number,
    rise: number,
    width: number,
  ) {
    for (let n = 0; n < count; n++)
      box(x, 0, lowZ + n * 0.54 * direction, width, (n + 1) * rise, 0.55, steel);
  }
  const extent = 24;
  box(0, -0.75, 0, 48, 0.75, 48, '#989582');
  for (const s of [-1, 1]) {
    box(s * extent, 0, 0, 0.55, 3, 48, concrete);
    box(0, 0, s * extent, 48, 3, 0.55, concrete);
    for (let i = -23; i < 24; i += 4.5) {
      box(i, 0, s * extent, 0.32, 3.18, 0.72, steel, false);
      box(s * extent, 0, i, 0.72, 3.18, 0.32, steel, false);
    }
  }
  for (let z = -21; z < 24; z += 4) box(0, 0.015, z, 0.16, 0.016, 1.9, '#d3bf83', false);
  for (const x of [-19.5, 19.5]) box(x, 0.012, 0, 0.1, 0.014, 45, '#cfc1a1', false);
  if (indoor) {
    hall(0, 1, 21, 19, 5.3, concrete);
    hall(-15, -13, 9, 8, 3.7, '#8e9b89', 'ends');
    container(14, -12, true, red);
    container(-15, 7, true, steel);
    container(14, 13, false, ochre);
    container(4, 4, true, red);
    crate(-4, -4);
    crate(-4, 4);
    crate(-3, 13);
    gantry(1, -15, 14);
    stairs(-7.1, -6.7, 1, 10, 0.22, 2.1);
    box(-7.1, 2.04, 2.2, 2.1, 0.16, 7.6, steel);
    box(-8.13, 2.2, 2.2, 0.1, 0.7, 7.6, ochre);
    for (const z of [-0.7, 5.4]) box(-7.1, 0, z, 0.28, 2.04, 0.28, dark);
  } else {
    hall(-13, 4, 10, 14, 4.8, '#a0a28c');
    hall(13, -8, 12, 10, 4.3, concrete);
    container(-6, -9, false, red);
    container(7, 5, true, red);
    container(15, 12, false, '#677f76');
    container(-10, 15, false, ochre);
    gantry(-3, 1, 10);
    crate(2, -5);
    crate(3.7, -5);
    crate(2, -5, 1.5, 1.45);
    crate(-4, 8);
    crate(13, -7);
    crate(-14, 3);
    stairs(7, 14.65, -1, 12, 0.235, 2.25);
    box(11.9, 2.65, 5.6, 7.0, 0.17, 2.05, steel);
    box(11.9, 2.82, 6.59, 6.9, 0.6, 0.1, ochre);
    stairs(15, -1.55, 1, 12, 0.235, 2.05);
  }
  // Paint, drains and utility poles are decorative and never block a player.
  for (const x of [-21, 21])
    for (const z of [-17, 0, 17]) {
      box(x, 0, z, 0.17, 5.5, 0.17, dark, false);
      box(x, 5.4, z, 1.7, 0.12, 0.16, dark, false);
      box(x - Math.sign(x) * 0.65, 5.3, z, 0.55, 0.13, 0.3, '#e5d3a1', false);
    }
  for (let k = 0; k < 11; k++) box(-17 + k * 0.7, 0.025, -17, 0.39, 0.02, 1.1, '#b8aa7d', false);
  return {
    name: indoor ? 'Relay Works' : 'Quarry Yard',
    sector: indoor ? 'RW–02' : 'QY–01',
    type: indoor ? 'Interior complex' : 'Industrial yard',
    description: indoor
      ? 'A roofed machine hall with four entrances and a fast perimeter loop.'
      : 'A sunlit freight yard with twin warehouses, cargo lanes and an overhead crane.',
    size: extent,
    boxes,
    sky: indoor ? '#b4bdba' : '#c7c2ac',
    spawns: [
      [-18, -19, 0.55],
      [17, 19, -2.5],
      [18, -19, -0.65],
      [-18, 19, 2.35],
      [0, -21, 0],
      [0, 21, Math.PI],
      [-21, 0, 1.57],
      [21, 0, -1.57],
    ],
    practiceContact: indoor ? [-11, -19, -1.6] : [-12.7, -10.4, -2.55],
  };
}
export const arenas = [buildArena(false), buildArena(true), office(), woodland()];
export function roundArena(base: Arena): Arena {
  return { ...base, intactBoxes: base.boxes, broken: 0 };
}
export function setBroken(map: Arena, mask: number) {
  if (!map.intactBoxes || map.broken === mask) return;
  map.broken = mask;
  map.boxes = map.intactBoxes.filter((b) => !(mask & (b.pane ?? 0)));
}
// The same static index serves character contacts, ragdoll joints and visibility rays.
type Bounds = { min: Vec; max: Vec; cost: number; pane?: number };
const indices = new WeakMap<Box[], { cells: Map<number, Box[]>; bounds: Bounds[] }>();
const cellKey = (x: number, z: number) => x + 128 * z;
function collisionIndex(map: Arena) {
  let index = indices.get(map.boxes);
  if (index) return index;
  index = { cells: new Map(), bounds: [] };
  for (const b of map.boxes) {
    if (!b.solid) continue;
    const min: Vec = [b.x - b.w / 2, b.y, b.z - b.d / 2],
      max: Vec = [b.x + b.w / 2, b.y + b.h, b.z + b.d / 2];
    index.bounds.push({
      min,
      max,
      pane: b.pane,
      cost: b.shotCost ?? (b.material ? resistance[b.material] : Infinity),
    });
    // Include adjacent contacts up to the widest supported player / navigation radius.
    for (let x = Math.floor((min[0] - 0.65) / 4); x <= Math.floor((max[0] + 0.65) / 4); x++)
      for (let z = Math.floor((min[2] - 0.65) / 4); z <= Math.floor((max[2] + 0.65) / 4); z++) {
        const key = cellKey(x, z);
        let cell = index.cells.get(key);
        if (!cell) {
          cell = [];
          index.cells.set(key, cell);
        }
        cell.push(b);
      }
  }
  indices.set(map.boxes, index);
  return index;
}
const empty: Box[] = [];
export function contacts(map: Arena, x: number, z: number) {
  return collisionIndex(map).cells.get(cellKey(Math.floor(x / 4), Math.floor(z / 4))) ?? empty;
}
export function inside(b: Box, x: number, z: number, r = 0) {
  return Math.abs(x - b.x) < b.w / 2 + r && Math.abs(z - b.z) < b.d / 2 + r;
}
// Slab intersection returns distance along a normalized ray, including overhead cover.
export function rayBox(o: Vec, v: Vec, min: Vec, max: Vec, limit: number) {
  let near = 0,
    far = limit;
  for (let k = 0; k < 3; k++) {
    if (Math.abs(v[k]) < 1e-7) {
      if (o[k] < min[k] || o[k] > max[k]) return Infinity;
      continue;
    }
    const a = (min[k] - o[k]) / v[k],
      b = (max[k] - o[k]) / v[k];
    near = Math.max(near, Math.min(a, b));
    far = Math.min(far, Math.max(a, b));
    if (far < near) return Infinity;
  }
  return near;
}
export function wallDistance(map: Arena, o: Vec, v: Vec, limit = 90) {
  let t = Math.min(limit, groundCrossing(map.terrain, o, v, limit));
  for (const b of collisionIndex(map).bounds) t = Math.min(t, rayBox(o, v, b.min, b.max, t));
  return t;
}

export type SurfaceCrossing = {
  distance: number;
  normal: Vec;
  entering: boolean;
  cost: number;
  pane?: number;
};
export function shotSurfaces(map: Arena, o: Vec, v: Vec, limit: number) {
  const result: SurfaceCrossing[] = [];
  const soil = groundCrossing(map.terrain, o, v, limit);
  if (Number.isFinite(soil)) {
    const x = o[0] + soil * v[0],
      z = o[2] + soil * v[2],
      h = groundHeight(map.terrain, x, z),
      dx = (h - groundHeight(map.terrain, x + 0.01, z)) * 100,
      dz = (h - groundHeight(map.terrain, x, z + 0.01)) * 100,
      l = Math.hypot(dx, 1, dz);
    result.push({ distance: soil, entering: true, cost: Infinity, normal: [dx / l, 1 / l, dz / l] });
  }
  for (const b of collisionIndex(map).bounds) {
    const entry = rayBox(o, v, b.min, b.max, limit);
    if (!Number.isFinite(entry)) continue;
    let end = Infinity,
      entryAxis = 0,
      exitAxis = 0,
      nearest = -Infinity;
    for (let k = 0; k < 3; k++) {
      if (Math.abs(v[k]) < 1e-7) continue;
      const a = (b.min[k] - o[k]) / v[k],
        c = (b.max[k] - o[k]) / v[k];
      if (Math.min(a, c) > nearest) {
        nearest = Math.min(a, c);
        entryAxis = k;
      }
      if (Math.max(a, c) < end) {
        end = Math.max(a, c);
        exitAxis = k;
      }
    }
    if (end <= entry + 1e-6) continue;
    const front: Vec = [0, 0, 0],
      back: Vec = [0, 0, 0];
    front[entryAxis] = -Math.sign(v[entryAxis]);
    back[exitAxis] = Math.sign(v[exitAxis]);
    result.push({ distance: entry, normal: front, entering: true, cost: b.cost, pane: b.pane });
    if (end <= limit)
      result.push({ distance: end, normal: back, entering: false, cost: b.cost, pane: b.pane });
  }
  return result;
}