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/effects.ts
import { groundHeight } from './terrain';
import { combat } from './combat-tuning';
import { contacts, inside, type Arena, type Vec } from './maps';
import type { Effect, Player } from './simulation';

export const bones = [
  [0, 1],
  [1, 2],
  [1, 3],
  [3, 4],
  [4, 5],
  [1, 6],
  [6, 7],
  [7, 8],
  [0, 9],
  [9, 10],
  [10, 11],
  [0, 12],
  [12, 13],
  [13, 14],
];
const braces = [
  [3, 6],
  [9, 12],
  [3, 0],
  [6, 0],
  [1, 9],
  [1, 12],
  [3, 2],
  [6, 2],
];
export function skeleton(x: number, y: number, z: number, yaw: number, phase = 0, duck = false, pitch = 0): Vec[] {
  const walk = Math.sin(phase) * 0.24,
    low = duck ? 0.38 : 0;
  const joints: Vec[] = [
    [0, 0.93 - low, 0],
    [0, 1.44 - low, 0],
    [0, 1.67 - low, 0],
    [-0.22, 1.43 - low, 0],
    [-0.28, 1.2 - low, 0.13],
    [-0.14, 1.29 - low, 0.44],
    [0.22, 1.43 - low, 0],
    [0.29, 1.17 - low, -0.01],
    [0.1, 1.29 - low, 0.25],
    [-0.125, 0.86 - low, 0],
    [-0.14, 0.47 - low * 0.5, 0.08 + (duck ? 0.23 : 0) + walk],
    [-0.15, 0.11, walk],
    [0.125, 0.86 - low, 0],
    [0.14, 0.47 - low * 0.5, 0.08 + (duck ? 0.23 : 0) - walk],
    [0.15, 0.11, -walk],
  ];
  // Aim from the shoulders. A smaller neck rotation keeps the helmet close to
  // the existing hit silhouette. The pelvis, knees and feet stay independent.
  for (const index of [2, 4, 5, 7, 8]) {
    const joint = joints[index];
    const pivot = joints[index === 2 ? 1 : index < 6 ? 3 : 6];
    const angle = index === 2 ? pitch * 0.65 : pitch;
    const dy = joint[1] - pivot[1], dz = joint[2] - pivot[2];
    joint[1] = pivot[1] + dy * Math.cos(angle) + dz * Math.sin(angle);
    joint[2] = pivot[2] - dy * Math.sin(angle) + dz * Math.cos(angle);
  }
  const c = Math.cos(yaw),
    s = Math.sin(yaw);
  return joints.map(([a, b, d]) => [x + a * c + d * s, y + b, z + d * c - a * s]);
}
type Particle = { p: Vec; old: Vec; r: number };
type Corpse = {
  j: Particle[];
  links: { a: number; b: number; length: number }[];
  bends: { a: number; b: number; min: number; max: number }[];
  age: number;
  team: number;
  sleep: number;
};
type Spark = { p: Vec; v: Vec; t: number; total: number; size: number; color: string };
export class Effects {
  bodies: Corpse[] = [];
  sparks: Spark[] = [];
  marks: { o: Vec; n: Vec; size: number; color: string; seed: number }[] = [];
  muzzles = new Map<number, number>();
  damageBearing = 0;
  blasts: { o: Vec; age: number }[] = [];
  flash = 0;
  kick = 0;
  hit = 0;
  hurt = 0;
  shake = 0;
  shotCount = 0;
  reset() {
    this.blasts = [];
    this.bodies = [];
    this.sparks = [];
    this.marks = [];
    this.muzzles.clear();
    this.flash = this.kick = this.hit = this.hurt = this.shake = this.shotCount = 0;
  }
  localShot(kick: number) {
    this.flash = 0.075;
    this.kick = Math.min(0.15, this.kick + kick);
    this.shotCount++;
  }
  receive(e: Effect, localId: number, players: Player[]) {
    if (e.kind === 'shot') {
      this.muzzles.set(e.who, 0.075);
    } else if (e.kind === 'impact' || e.kind === 'blood') {
      const blood = e.kind === 'blood';
      this.marks.push({
        o: e.o,
        n: e.v,
        size: blood ? 0.16 : 0.034,
        color: blood ? '#742a25' : '#383a35',
        seed: e.id,
      });
      if (this.marks.length > combat.shot.markLimit) this.marks.shift();
      if (!blood) this.burst(e.o, 3, 1.4, false);
    } else if (e.kind === 'glass') {
      const start = this.sparks.length;
      this.burst(e.o, 14, 2.8, false);
      for (const shard of this.sparks.slice(Math.min(start, this.sparks.length - 14)))
        shard.color = '#abd2d5';
    } else if (e.kind === 'death') {
      const p = players.find((p) => p.id === e.other);
      const j = skeleton(...e.o, p?.yaw ?? 0, Math.random() * 5, Boolean(p && p.buttons & 4), p?.pitch ?? 0).map(
        (v, i) => ({
          p: v,
          old: v.map(
            (n, k) => n - (e.v[k] * (i < 3 ? 1 : 0.64) + (Math.random() - 0.5) * 1.7) / 60,
          ) as Vec,
          r: i === 2 ? 0.16 : 0.095,
        }),
      );
      const links = [...bones, ...braces].map(([a, b]) => ({
        a,
        b,
        length: Math.hypot(...j[a].p.map((n, k) => n - j[b].p[k])),
      }));
      const bends = [
        [3, 4, 5],
        [6, 7, 8],
        [9, 10, 11],
        [12, 13, 14],
      ].map(([a, pivot, b]) => {
        const segment = (i: number, k: number) => Math.hypot(...j[i].p.map((v, d) => v - j[k].p[d]));
        const span = segment(a, pivot) + segment(pivot, b);
        return { a, b, min: span * (a < 9 ? 0.31 : 0.39), max: span * 0.996 };
      });
      this.bodies.push({ j, links, bends, age: 0, team: e.other, sleep: 0 });
      if (this.bodies.length > 10) this.bodies.shift();
    } else if (e.kind === 'hit') {
      if (e.who === localId) this.hit = 0.18;
      if (e.other === localId) {
        this.hurt = 0.48;
        this.damageBearing = Math.atan2(-e.v[0], -e.v[2]);
      }
    } else {
      this.blasts.push({ o: e.o, age: 0 });
      this.burst(e.o, combat.feel.particles, combat.feel.particleSpeed, true);
      const me = players.find((p) => p.id === localId);
      if (me)
        this.shake = Math.max(
          this.shake,
          combat.feel.cameraKick * Math.max(0, 1 - Math.hypot(me.x - e.o[0], me.z - e.o[2]) / 18),
        );
      for (const b of this.bodies)
        for (const n of b.j) {
          const d = Math.hypot(...n.p.map((x, k) => x - e.o[k]));
          if (d < combat.blast.radius) {
            b.sleep = 0;
            for (let k = 0; k < 3; k++)
              n.old[k] -=
                (((n.p[k] - e.o[k]) / (d + 0.3)) *
                  (1 - d / combat.blast.radius) *
                  combat.blast.impulse) /
                60;
          }
        }
    }
  }
  private burst(o: Vec, count: number, speed: number, blast: boolean) {
    for (let i = 0; i < count; i++) {
      const smoke = blast && i % 3 === 0,
        life = smoke ? 0.65 + Math.random() * 0.55 : 0.1 + Math.random() * (blast ? 0.65 : 0.14);
      this.sparks.push({
        p: [...o],
        v: [
          (Math.random() - 0.5) * speed * 2,
          Math.random() * speed + 1,
          (Math.random() - 0.5) * speed * 2,
        ],
        t: life,
        total: life,
        size: smoke ? 0.23 + Math.random() * 0.3 : 0.04 + Math.random() * 0.075,
        color: smoke ? '#716d5e' : i % 2 ? '#fec15e' : '#e58337',
      });
    }
    if (this.sparks.length > 240) this.sparks.splice(0, this.sparks.length - 240);
  }
  step(map: Arena, dt: number) {
    this.blasts = this.blasts.filter((b) => (b.age += dt) < combat.feel.duration);
    this.flash = Math.max(0, this.flash - dt);
    this.kick *= Math.exp(-dt * 12);
    this.hit = Math.max(0, this.hit - dt);
    this.hurt = Math.max(0, this.hurt - dt);
    this.shake *= Math.exp(-dt * combat.feel.shakeDecay);
    for (const [id, t] of this.muzzles) {
      if (t <= dt) this.muzzles.delete(id);
      else this.muzzles.set(id, t - dt);
    }
    this.sparks = this.sparks.filter((p) => {
      p.t -= dt;
      p.v[1] -= dt * (p.color === '#716d5e' ? 3 : 12);
      for (let k = 0; k < 3; k++) p.p[k] += p.v[k] * dt;
      const ground = groundHeight(map.terrain, p.p[0], p.p[2]) + 0.03;
      if (p.p[1] < ground) {
        p.p[1] = ground;
        p.v[1] *= -0.3;
      }
      return p.t > 0;
    });
    this.bodies = this.bodies.filter((b) => (b.age += dt) < 9);
    for (const body of this.bodies) {
      if (body.sleep > 0.6) continue;
      let speed = 0;
      for (const n of body.j) {
        const next = n.p.map((x, k) => x + (x - n.old[k]) * 0.991 - (k === 1 ? 14 * dt * dt : 0)) as Vec;
        n.old = n.p;
        n.p = next;
      }
      for (let pass = 0; pass < 5; pass++) {
        for (const { a, b, length } of body.links) {
          const p = body.j[a].p,
            q = body.j[b].p,
            dx = q[0] - p[0],
            dy = q[1] - p[1],
            dz = q[2] - p[2];
          const fix = (1 - length / Math.max(0.0001, Math.hypot(dx, dy, dz))) * 0.5;
          p[0] += dx * fix;
          p[1] += dy * fix;
          p[2] += dz * fix;
          q[0] -= dx * fix;
          q[1] -= dy * fix;
          q[2] -= dz * fix;
        }
        for (const bend of body.bends) {
          const a = body.j[bend.a].p,
            b = body.j[bend.b].p;
          const dx = b[0] - a[0],
            dy = b[1] - a[1],
            dz = b[2] - a[2],
            d = Math.hypot(dx, dy, dz);
          if (d >= bend.min && d <= bend.max) continue;
          const fix = 0.5 * (1 - (d < bend.min ? bend.min : bend.max) / Math.max(0.0001, d));
          a[0] += dx * fix;
          a[1] += dy * fix;
          a[2] += dz * fix;
          b[0] -= dx * fix;
          b[1] -= dy * fix;
          b[2] -= dz * fix;
        }
        for (const n of body.j) {
          n.p[1] = Math.max(n.p[1], groundHeight(map.terrain, n.p[0], n.p[2]) + n.r);
          for (const b of contacts(map, n.p[0], n.p[2])) {
            if (!inside(b, n.p[0], n.p[2], n.r) || n.p[1] + n.r <= b.y || n.p[1] - n.r >= b.y + b.h)
              continue;
            const deltas = [
              b.w / 2 + n.r - Math.abs(n.p[0] - b.x),
              b.h / 2 + n.r - Math.abs(n.p[1] - b.y - b.h / 2),
              b.d / 2 + n.r - Math.abs(n.p[2] - b.z),
            ];
            const k = deltas.indexOf(Math.min(...deltas)),
              center = [b.x, b.y + b.h / 2, b.z][k];
            n.p[k] += (n.p[k] >= center ? 1 : -1) * deltas[k];
            if (k === 1 && pass === 4) {
              n.old[0] += (n.p[0] - n.old[0]) * 0.42;
              n.old[2] += (n.p[2] - n.old[2]) * 0.42;
            }
          }
        }
      }
      for (const n of body.j) speed += Math.hypot(...n.p.map((x, k) => x - n.old[k]));
      body.sleep = speed < 0.023 ? body.sleep + dt : 0;
    }
  }
}