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/snapshot.ts
// Fixed-width pose records keep full, independently decodable snapshots cheap at 20 Hz.
// Positions and speeds use centimetres. Angles are signed fractions of half a turn.
import { combat } from './combat-tuning';
import type { Player } from './simulation';
import { guns, isPrimary } from './weapons';
const fractions = ['x', 'y', 'z', 'vx', 'vy', 'vz', 'yaw', 'pitch'] as const;
const bytes = [
  'id',
  'hp',
  'gun',
  'ammo',
  'cool',
  'reload',
  'frags',
  'kills',
  'deaths',
  'respawn',
  'buttons',
  'ground',
  'protect',
  'holster',
  'spareAmmo',
  'cook',
  'boost',
  'lean',
] as const;
const stride = fractions.length * 2 + bytes.length + 6;
export function encodePlayers(players: Iterable<Player>) {
  const list = [...players],
    buffer = new Uint8Array(list.length * stride),
    view = new DataView(buffer.buffer);
  let at = 0;
  for (const p of list) {
    for (const k of fractions) {
      view.setInt16(at, Math.round(p[k] * (k === 'yaw' || k === 'pitch' ? 10000 : 100)), true);
      at += 2;
    }
    for (const k of bytes) buffer[at++] = p[k];
    view.setUint16(at, p.life, true);
    at += 2;
    view.setUint32(at, p.ack, true);
    at += 4;
  }
  return btoa(String.fromCharCode(...buffer));
}
export function decodePlayers(raw: unknown, extent = 24): Player[] | null {
  if (typeof raw !== 'string' || !raw.length || raw.length > Math.ceil((8 * stride) / 3) * 4)
    return null;
  let text: string;
  try {
    text = atob(raw);
  } catch {
    return null;
  }
  if (!text.length || text.length % stride || text.length > stride * 8) return null;
  const data = Uint8Array.from(text, (c) => c.charCodeAt(0)),
    view = new DataView(data.buffer),
    players: Player[] = [],
    ids = new Set<number>();
  let at = 0;
  while (at < data.length) {
    const p = {} as Player;
    for (const k of fractions) {
      p[k] = view.getInt16(at, true) / (k === 'yaw' || k === 'pitch' ? 10000 : 100);
      at += 2;
    }
    for (const k of bytes) p[k] = data[at++];
    if (p.lean > 127) p.lean -= 256;
    p.life = view.getUint16(at, true);
    at += 2;
    p.ack = view.getUint32(at, true);
    at += 4;
    if (
      p.id > 7 ||
      ids.has(p.id) ||
      !guns[p.gun] ||
      !guns[p.holster] ||
      isPrimary(p.gun) === isPrimary(p.holster) ||
      p.spareAmmo > guns[p.holster].mag ||
      Math.abs(p.lean) > 100 ||
      p.hp > 100 ||
      p.cook > combat.grenade.fuse ||
      p.boost > combat.hit.speedTicks ||
      !p.life ||
      p.ammo > guns[p.gun].mag ||
      p.buttons >= 128 ||
      Math.abs(p.x) > extent + 2 ||
      Math.abs(p.z) > extent + 2 ||
      p.y < -0.02 ||
      p.y > 26 ||
      Math.abs(p.pitch) > 1.39 ||
      Math.abs(p.yaw) > Math.PI + 0.001
    )
      return null;
    ids.add(p.id);
    players.push(p);
  }
  return players;
}