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.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import ts from 'typescript';
const tmp = await mkdtemp(join(tmpdir(), 'faultline-rules-'));
try {
for (const name of [
'combat-tuning',
'terrain',
'battlefields',
'maps',
'weapons',
'simulation',
'playout',
'effects',
'snapshot',
]) {
const source = await readFile(new URL('../src/' + name + '.ts', import.meta.url), 'utf8');
const js = ts
.transpile(source, { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 })
.replace(/from '(.\/[\w-]+)'/g, "from '$1.mjs'");
await writeFile(join(tmp, name + '.mjs'), js);
}
const { groundHeight, groundCrossing } = await import(pathToFileURL(join(tmp, 'terrain.mjs')));
const { arenas, setBroken } = await import(pathToFileURL(join(tmp, 'maps.mjs')));
const {
World,
spawn,
stepPlayer,
blocked,
validInput,
FIRE,
THROW,
RELOAD,
SECONDARY,
PRIMARY,
DUCK,
LEAN_LEFT,
LEAN_RIGHT,
eyeOrigin,
spreadFor,
recoilFor,
AIM,
equipLoadout,
} = await import(pathToFileURL(join(tmp, 'simulation.mjs')));
const { encodePlayers, decodePlayers } = await import(pathToFileURL(join(tmp, 'snapshot.mjs')));
const { Effects, skeleton } = await import(pathToFileURL(join(tmp, 'effects.mjs')));
const { combat } = await import(pathToFileURL(join(tmp, 'combat-tuning.mjs')));
const { guns, RPG } = await import(pathToFileURL(join(tmp, 'weapons.mjs')));
const map = arenas[0],
open = { ...map, boxes: [map.boxes[0]] };
const { nextViewTick, queueCommand } = await import(pathToFileURL(join(tmp, 'playout.mjs')));
test('replicated pitch tips the head and hands while crouching bends the knees with planted feet', () => {
const p = spawn(arenas[0], 0, 0, 1);
const pose = (pitch, duck) => {
const [remote] = decodePlayers(encodePlayers([{ ...p, pitch, buttons: duck ? DUCK : 0 }]));
return skeleton(0, 0, 0, remote.yaw, 0, !!(remote.buttons & DUCK), remote.pitch);
};
const up = pose(1, false), down = pose(-1, false), low = pose(1, true);
assert.ok(up[8][1] - down[8][1] > 0.35);
assert.ok(up[5][1] - down[5][1] > 0.65);
assert.ok(Math.hypot(up[2][0] - down[2][0], up[2][2] - down[2][2]) > 0.2);
assert.ok(up[2][1] - low[2][1] > 0.35);
assert.ok(Math.hypot(up[10][0] - low[10][0], up[10][2] - low[10][2]) > 0.2);
for (const i of [11, 14]) assert.deepEqual(up[i], low[i]);
});
test('late snapshots never rewind the render clock', () => {
let previous = 0;
let latest = 0;
let arrival = 0;
// 20 Hz packets with a late arrival and two burst deliveries.
const packets = [[60, 3], [113, 6], [222, 9], [228, 12], [255, 15], [385, 18], [387, 21]];
for (let time = 0; time < 500; time += 8) {
while (packets.length && packets[0][0] <= time) [arrival, latest] = packets.shift();
const t = nextViewTick(previous, latest, time - arrival, 8);
assert.ok(t >= previous && t <= latest);
previous = t;
}
assert.equal(previous, 21);
});
test('burst input stays bounded, keeps a queued press/release and ignores duplicate sequences', () => {
const queue = [];
for (let n = 1; n <= 60; n++) {
const cmd = [n, 1, 0, 0, 0, n === 27 ? FIRE : 0, 1, n];
queueCommand(queue, cmd, 0, 0);
queueCommand(queue, cmd, 0, 0);
assert.ok(queue.length <= 9);
}
assert.equal(queue.filter(cmd => cmd[5] === FIRE).length, 1);
assert.equal(queue.at(-1)[0], 60);
assert.ok(queue.some(cmd => cmd[0] > 27 && cmd[5] === 0));
});
test('host resolves a delayed shot at the observed pose while the same target has since moved', () => {
const world = new World(open);
Object.assign(world.add(0, 0), { x: 0, z: 0, protect: 0 });
const target = Object.assign(world.add(1, 0), { x: 0, z: 4, protect: 0 });
for (let tick = 1; tick <= 20; tick++) {
if (tick === 8) target.x = 2;
world.advance(new Map());
}
const shot = input(FIRE | AIM, Math.atan2(1.1 - 1.59, 4));
shot[7] = 7;
world.advance(new Map([[0, shot]]));
assert.ok(target.hp < 100);
});
test('all map spawns and both sides of each warehouse entrance are reachable', () => {
for (const a of arenas) {
const origin = a.spawns[0].slice(0, 2);
const queue = [origin],
seen = new Set([origin.join(',')]);
for (let i = 0; i < queue.length; i++) {
const [x, z] = queue[i];
for (const [dx, dz] of [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
]) {
const k = [x + dx, z + dz],
id = k.join(',');
if (
seen.has(id) ||
Math.abs(k[0]) >= a.size ||
Math.abs(k[1]) >= a.size ||
blocked(a, k[0], groundHeight(a.terrain, k[0], k[1]), k[1], 1.76)
)
continue;
seen.add(id);
queue.push(k);
}
}
for (const [x, z] of a.spawns)
assert.ok(seen.has([x, z].join(',')), a.name + ' spawn ' + x + ',' + z);
for (const roof of a.boxes.filter((b) => b.roof && b.solid && b.w < 40)) {
for (const dz of [-1, 1])
for (const side of [-1, 1]) {
const z = Math.round(roof.z + dz * (roof.d / 2 + side));
assert.ok(seen.has([roof.x, z].join(',')), a.name + ' door');
}
}
}
});
test('predicted movement converges after snapshot and replay; walls stop movement', () => {
const p = spawn(map, 0, 0),
prediction = { ...p },
history = [];
for (let n = 1; n <= 160; n++) history.push([n, 1, 0.25, -Math.PI / 2, 0, 0, 1, -1]);
for (const i of history) stepPlayer(map, prediction, i);
for (const i of history.slice(0, 100)) stepPlayer(map, p, i);
const [snapshot] = decodePlayers(encodePlayers([p]));
for (const i of history.slice(100)) stepPlayer(map, snapshot, i);
assert.ok(Math.abs(snapshot.x - prediction.x) < 0.012);
assert.ok(prediction.x >= -23.405 && prediction.x < -23.2);
assert.equal(validInput([1, 0, 0, 0, 0, Infinity, 1, 0]), false);
assert.equal(decodePlayers([0, NaN]), null);
assert.equal(decodePlayers(encodePlayers([p, p])), null);
assert.equal(
Buffer.from(encodePlayers(Array.from({ length: 8 }, (_, i) => spawn(map, i, 0))), 'base64').length,
8 * 40,
);
});
test('steps reach the overhead walkway and leave a playable lane underneath', () => {
const p = spawn(map, 0, 0);
Object.assign(p, { x: 7, z: 16.5, yaw: Math.PI });
for (let n = 1; n < 120; n++) stepPlayer(map, p, [n, 1, 0, Math.PI, 0, 0, 1, -1]);
assert.ok(p.y > 2.7 && p.z < 9);
assert.equal(blocked(map, 11, 0, 5.6, 1.76), false);
});
test('confirmed shots score once, dead players respawn, cover blocks damage', () => {
const w = new World(open),
p = w.add(0, 3),
q = w.add(1, 0);
Object.assign(p, { x: 0, z: 0, yaw: 0, protect: 0 });
Object.assign(q, { x: 0, z: 5, protect: 0 });
for (let n = 1; n <= 25; n++)
w.advance(new Map([[0, [n, 0, 0, 0, 0, n === 1 || n === 21 ? FIRE : 0, 1, -1]]]));
assert.equal(q.hp, 0);
assert.equal(p.kills, 1);
assert.equal(w.effects.filter((e) => e.kind === 'death').length, 1);
for (let n = 0; n < 151; n++) w.advance(new Map());
assert.equal(w.players.get(1).life, 2);
const barricade = { x: 0, z: 3, y: 0, w: 3, h: 3, d: 1, color: '#aaa', solid: true };
const guarded = new World({ ...open, boxes: [...open.boxes, barricade] });
Object.assign(guarded.add(0, 3), { x: 0, z: 0 });
Object.assign(guarded.add(1, 0), { x: 0, z: 5, protect: 0 });
guarded.advance(new Map([[0, [1, 0, 0, 0, 0, FIRE, 1, -1]]]));
assert.equal(guarded.players.get(1).hp, 100);
});
test('profiles have distinct cadence, magazine and reload behavior', () => {
for (const [id, profile] of guns.entries()) {
const p = spawn(open, 0, id),
i = [1, 0, 0, 0, 0, FIRE, 1, -1];
assert.ok(stepPlayer(open, p, i) & FIRE);
assert.equal(p.ammo, profile.mag - 1);
assert.equal(p.cool, profile.delay);
assert.equal(stepPlayer(open, p, [2, 0, 0, 0, 0, 0, 1, -1]) & FIRE, 0);
stepPlayer(open, p, [2, 0, 0, 0, 0, RELOAD, 1, -1]);
assert.equal(p.reload, profile.reload);
for (let n = 0; n < profile.reload; n++) stepPlayer(open, p, [3 + n, 0, 0, 0, 0, 0, 1, -1]);
assert.equal(p.ammo, profile.mag);
}
});
const input = (bits, pitch = 0) => [1, 0, 0, 0, pitch, bits, 1, -1];
const wall = (z, d, shotCost) => ({
x: 0,
y: 0,
z,
w: 6,
h: 4,
d,
color: '#888',
solid: true,
shotCost,
});
function shoot({ height = 1.1, targetX = 0, duck = false, boxes = [], second = false } = {}) {
const w = new World({ ...open, boxes: [...open.boxes, ...boxes] });
Object.assign(w.add(0, 0), { x: 0, z: 0 });
const target = Object.assign(w.add(1, 0), {
x: targetX,
z: 4,
protect: 0,
buttons: duck ? DUCK : 0,
});
if (second) Object.assign(w.add(2, 0), { x: 0, z: 5.5, protect: 0 });
w.advance(
new Map([
[0, input(FIRE | AIM, Math.atan2(height - 1.59, 4))],
[1, input(duck ? DUCK : 0)],
]),
);
return { w, target };
}
test('hit regions follow the standing and crouched silhouette', () => {
for (const duck of [false, true]) {
const low = duck ? 0.38 : 0;
for (const [height, weight] of [
[1.66 - low, combat.hit.head],
[1.1 - low, combat.hit.torso],
[0.2, combat.hit.legs],
]) {
const { target } = shoot({ height, duck });
assert.equal(100 - target.hp, Math.round(guns[0].damage * weight));
}
assert.equal(shoot({ height: 1.67 - low, targetX: 0.23, duck }).target.hp, 100);
}
});
test('shots pass through bodies and thin walls with diminishing damage and directional decals', () => {
const { w, target } = shoot({
boxes: [wall(2, 0.3, combat.shot.wallCost), wall(7, 1)],
second: true,
});
assert.ok(target.hp > 100 - guns[0].damage && target.hp < 100);
assert.ok(w.players.get(2).hp > target.hp && w.players.get(2).hp < 100);
const marks = w.effects.filter((e) => e.kind === 'impact');
assert.deepEqual(
marks.map((e) => Math.round(e.o[2] * 100)),
[185, 215, 650],
);
assert.deepEqual(
marks.map((e) => e.v[2]),
[-1, 1, -1],
);
const blood = w.effects.filter((e) => e.kind === 'blood');
assert.equal(blood.length, 1);
assert.deepEqual(blood[0].o, marks[2].o);
assert.equal(shoot({ boxes: [wall(2, 2, combat.shot.wallCost)] }).target.hp, 100);
assert.equal(shoot({ boxes: [wall(2, 0.1)] }).target.hp, 100);
assert.equal(
shoot({ boxes: [wall(15, 0.5)] }).w.effects.some((e) => e.kind === 'blood'),
false,
);
const stopped = shoot({ boxes: [wall(2, 1.1, combat.shot.wallCost), wall(6, 0.2)] });
assert.ok(stopped.target.hp < 100);
assert.equal(stopped.w.effects.filter((e) => e.kind === 'impact').length, 2);
assert.equal(stopped.w.effects.filter((e) => e.kind === 'blood').length, 1);
const fx = new Effects();
fx.receive(
w.effects.find((e) => e.kind === 'hit' && e.other === 1),
1,
[],
);
assert.ok(fx.hurt > 0);
assert.ok(Math.cos(fx.damageBearing) < -0.99);
for (let i = 0; i < combat.shot.markLimit + 5; i++) fx.receive(marks[0], 0, []);
assert.equal(fx.marks.length, combat.shot.markLimit);
});
test('a confirmed hit briefly increases movement speed and survives a snapshot', () => {
const { target } = shoot();
const [copy] = decodePlayers(encodePlayers([target]));
assert.equal(copy.boost, target.boost);
assert.ok(target.boost > 0);
copy.boost = 0;
const move = [1, 1, 0, 0, 0, 0, 1, -1];
for (let i = 0; i < combat.hit.speedTicks; i++) {
stepPlayer(open, target, move);
stepPlayer(open, copy, move);
}
assert.ok(target.z > copy.z + 0.1);
assert.equal(target.boost, 0);
});
test('slot switching preserves both magazines and a changed loadout must reload', () => {
const p = spawn(open, 0, 0);
stepPlayer(open, p, input(FIRE));
stepPlayer(open, p, input(SECONDARY));
assert.equal(p.gun, 5);
assert.equal(p.spareAmmo, 29);
for (let n = 0; n < 10; n++) stepPlayer(open, p, input(0));
stepPlayer(open, p, input(FIRE | AIM));
assert.equal(p.ammo, 13);
assert.deepEqual(decodePlayers(encodePlayers([p]))[0], p);
stepPlayer(open, p, input(PRIMARY));
assert.equal(p.gun, 0);
assert.equal(p.ammo, 29);
equipLoadout(p, 0, RPG);
assert.equal(p.ammo, 29);
assert.equal(p.spareAmmo, 0);
stepPlayer(open, p, input(SECONDARY));
assert.equal(p.gun, RPG);
assert.ok(p.reload > 0);
equipLoadout(p, 1, RPG);
assert.equal(p.spareAmmo, 0);
});
test('the frag fuse continues through input gaps, release, overcook and a dropped live frag', () => {
const w = new World(open),
p = Object.assign(w.add(0, 0), { x: 0, z: 0 });
w.advance(new Map([[0, input(THROW)]]));
for (let i = 1; i < 55; i++) w.advance(new Map());
assert.equal(p.cook, combat.grenade.fuse - 55);
assert.equal(w.grenades.length, 0);
w.advance(new Map([[0, input(0)]]));
assert.equal(w.grenades[0].fuse, combat.grenade.fuse - 56);
assert.ok(w.grenades[0].vz > 18);
while (w.tick < combat.grenade.fuse - 1) w.advance(new Map());
assert.equal(w.grenades.length, 1);
w.advance(new Map());
assert.equal(w.grenades.length, 0);
const held = new World(open);
Object.assign(held.add(0, 0), { x: 0, z: 0 });
for (let i = 0; i < combat.grenade.fuse; i++) held.advance(new Map([[0, input(THROW)]]));
assert.equal(held.players.get(0).hp, 0);
assert.equal(held.players.get(0).frags, 1);
const blast = held.effects.find((e) => e.kind === 'blast');
assert.equal(blast.o[2], 0);
const dropped = new World(open);
Object.assign(dropped.add(0, 0), { x: 0, z: 0 });
dropped.advance(new Map([[0, input(THROW)]]));
dropped.grenades.push({ x: 0, y: 1, z: 0, vx: 0, vy: 0, vz: 0, owner: 0, fuse: 1 });
dropped.advance(new Map());
assert.equal(dropped.players.get(0).hp, 0);
assert.equal(dropped.grenades.length, 1);
assert.ok(dropped.grenades[0].fuse > 1);
dropped.grenades[0].y = 0.14;
dropped.grenades[0].vy = -8;
dropped.advance(new Map());
assert.ok(dropped.grenades[0].vy > 3 && dropped.grenades[0].vy < 4);
const rail = new World({ ...open, boxes: [...open.boxes, wall(0.175, 0.02)] });
rail.grenades.push({ x: 0, y: 1, z: 0, vx: 0, vy: 0, vz: 20, owner: 0, fuse: 10 });
rail.advance(new Map());
assert.ok(rail.grenades[0].z < 0.175 && rail.grenades[0].vz < -8 && rail.grenades[0].vz > -9);
});
test('lean, shot origin and stance stay consistent on both sides of a snapshot', () => {
const p = Object.assign(spawn(open, 0, 0), { x: 0, z: 0, yaw: 0 });
for (let n = 0; n < 8; n++) stepPlayer(open, p, input(LEAN_LEFT));
assert.equal(p.lean, -100);
assert.ok(eyeOrigin(p)[0] < -0.4);
assert.equal(decodePlayers(encodePlayers([p]))[0].lean, -100);
const cover = { ...open, boxes: [...open.boxes, { ...wall(0, 8), x: 0.8, w: 0.6 }] };
for (let n = 0; n < 20; n++) stepPlayer(cover, p, input(LEAN_RIGHT));
assert.ok(eyeOrigin(p)[0] > 0.2 && eyeOrigin(p)[0] < 0.36);
for (let n = 0; n < 8; n++) stepPlayer(open, p, input(0));
assert.equal(p.lean, 0);
const standing = spreadFor(p);
stepPlayer(open, p, input(DUCK));
assert.ok(spreadFor(p) < standing);
assert.ok(recoilFor(p.gun, DUCK) < recoilFor(p.gun, 0));
for (const [shooterX, expectedDamage] of [
[0, 0],
[0.46, Math.round(guns[0].damage * combat.hit.head)],
]) {
const w = new World(open);
Object.assign(w.add(0, 0), { x: shooterX, z: 0 });
const leaning = Object.assign(w.add(1, 0), { x: 0, z: 4, yaw: 0, lean: 100, protect: 0 });
w.advance(new Map([[0, input(FIRE | AIM)]]));
assert.equal(100 - leaning.hp, expectedDamage);
}
});
test('office glazing opens a passage per round and the three wall materials resist shots differently', () => {
const office = arenas[2],
pane = office.boxes.find((b) => b.pane),
w = new World(office);
Object.assign(w.add(0, 0), { x: pane.x, z: pane.z - 2, yaw: 0 });
assert.ok(blocked(w.map, pane.x, 0, pane.z, 1.7));
w.advance(new Map([[0, input(FIRE)]]));
assert.equal(w.map.broken & pane.pane, pane.pane);
assert.ok(w.effects.some((e) => e.kind === 'glass'));
assert.ok(!blocked(w.map, pane.x, 0, pane.z, 1.7));
assert.ok(blocked(new World(office).map, pane.x, 0, pane.z, 1.7));
const health = ['drywall', 'plywood', 'brick'].map(
(material) => shoot({ boxes: [{ ...wall(2, 0.45), material }] }).target.hp,
);
assert.ok(health[0] < health[1] && health[1] < health[2]);
assert.equal(health[2], 100);
});
test('woodland hills stop sightlines and players outside the old map bounds replicate', () => {
const a = arenas[3];
assert.ok(a.size > 2 * arenas[0].size);
const p = spawn(a, 0, 0);
assert.equal(p.y, groundHeight(a.terrain, p.x, p.z));
assert.ok(decodePlayers(encodePlayers([p]), a.size));
assert.ok(groundCrossing(a.terrain, [-40, 4, 9], [1, 0, 0], 65) < 40);
const h = groundHeight(a.terrain, 24, -18);
assert.ok(h > 7);
assert.ok(Math.abs(groundCrossing(a.terrain, [24, 12, -18], [0, -1, 0], 20) - (12 - h)) < 0.001);
});
test('team scoring counts opposing eliminations and protects teammates', () => {
const w = new World(open, true),
p = Object.assign(w.add(0, 3), { x: 0, z: 0, protect: 0 });
const friend = Object.assign(w.add(2, 0), { x: 0, z: 2, protect: 0 }),
enemy = Object.assign(w.add(1, 0), { x: 0, z: 4, protect: 0, hp: 1 });
w.advance(new Map([[0, input(FIRE)]]));
assert.equal(friend.hp, 100);
assert.equal(enemy.hp, 0);
assert.equal(p.kills, 1);
assert.deepEqual(w.scores, [1, 0]);
w.players.delete(0);
assert.equal(w.scores[0], 1);
});
test('the rocket travels and explodes once on its first contact', () => {
const w = new World({ ...open, boxes: [...open.boxes, wall(4, 0.08)] });
Object.assign(w.add(0, RPG), { x: 0, z: 0 });
w.advance(new Map([[0, input(FIRE)]]));
assert.ok(w.grenades[0].rocket);
assert.ok(w.grenades[0].z > 0);
for (let n = 0; n < 12; n++) w.advance(new Map());
const blasts = w.effects.filter((e) => e.kind === 'blast');
assert.equal(blasts.length, 1);
assert.ok(blasts[0].o[2] > 3.8 && blasts[0].o[2] < 4);
assert.equal(w.grenades.length, 0);
});
test('grenades detonate once, respect cover and push articulated bodies that settle on the floor', () => {
const w = new World(open),
p = w.add(0, 0);
w.advance(new Map([[0, [1, 0, 0, 0, 0, THROW, 1, -1]]]));
assert.equal(w.grenades.length, 0);
w.advance(new Map([[0, [2, 0, 0, 0, 0, 0, 1, -1]]]));
assert.equal(w.grenades.length, 1);
assert.ok(Math.hypot(w.grenades[0].x - p.x, w.grenades[0].z - p.z) > 0.4);
assert.equal(p.frags, 1);
for (let n = 0; n < combat.grenade.fuse; n++) w.advance(new Map());
assert.equal(w.effects.filter((e) => e.kind === 'blast').length, 1);
assert.equal(w.grenades.length, 0);
const wall = { x: 0, y: 0, z: 0, w: 4, h: 4, d: 0.5, color: '#aaa', solid: true };
const sheltered = new World({ ...open, boxes: [...open.boxes, wall] });
Object.assign(sheltered.add(0, 0), { x: 0, z: -1, protect: 0 });
Object.assign(sheltered.add(1, 0), { x: 0, z: 1, protect: 0 });
sheltered.grenades.push({ x: 0, y: 0.3, z: -1, vx: 0, vy: 0, vz: 0, owner: 0, fuse: 1 });
sheltered.advance(new Map());
assert.equal(sheltered.players.get(0).hp, 0);
assert.equal(sheltered.players.get(1).hp, 100);
const fx = new Effects();
fx.receive(
sheltered.effects.find((e) => e.kind === 'death'),
0,
[],
);
for (let n = 0; n < 220; n++) fx.step(open, 1 / 60);
const body = fx.bodies[0];
assert.equal(body.j.length, 15);
for (const j of body.j)
assert.ok(j.p.every(Number.isFinite) && j.p[1] >= j.r - 0.015 && j.p[1] < 0.9);
for (const { a, b, length } of body.links)
assert.ok(Math.abs(Math.hypot(...body.j[a].p.map((v, k) => v - body.j[b].p[k])) - length) < 0.06);
});
} finally {
await rm(tmp, { recursive: true, force: true });
}
