import { expect, test } from "bun:test"; import { AGENTS, agentById, buyableSlots, cast, castable, facingFlash, FLASH_CONE, FLASH_EDGE, FLASH_GRACE, flashDuration, flashReach, sightBlocked, SLOTS, type Ability, } from "./abilities.ts"; import { HEIGHT, WIDTH } from "./map.ts"; import { newMatch, startRound, tick } from "./match.ts"; import type { MatchState, Player, Zone } from "./state.ts"; import { FOV } from "../static/engine/raycast.js"; const flashes = (): Ability[] => AGENTS.flatMap((a) => SLOTS.map((s) => a.abilities[s])).filter((a) => a.kind === "flash"); const live = (): MatchState => { const state = newMatch(11); state.phase = "live"; state.clock = 100; return state; }; const attacker = (state: MatchState): Player => state.players.find((p) => p.side === "atk")!; test("every agent has two bought basics, a free signature and an ultimate", () => { expect(AGENTS.length).toBe(5); for (const agent of AGENTS) { for (const slot of SLOTS) expect(agent.abilities[slot].name.length).toBeGreaterThan(0); for (const slot of buyableSlots) expect(agent.abilities[slot].cost).toBeGreaterThan(0); expect(agent.abilities.e.cost).toBe(0); expect(agent.abilities.e.cooldown).toBeGreaterThan(0); expect(agent.abilities.x.points).toBeGreaterThanOrEqual(6); } }); test("a flash blinds by the angle you were facing", () => { const at = { x: 1000, y: 1000 }; const staring = { x: 1100, y: 1000 }; const facingIt = Math.PI; // looking back down the line at the flash const facingAway = 0; expect(flashDuration(1.7, staring, at, facingIt, false, 360)).toBeGreaterThan(1.6); expect(flashDuration(1.7, staring, at, facingAway, false, 360)).toBe(0); // Turned just past the edge of the screen is a shorter blind than staring. const glance = flashDuration(1.7, staring, at, facingIt - 0.8, false, 360); expect(glance).toBeGreaterThan(0); expect(glance).toBeLessThan(1.7); }); test("a wall between you and the flash means no blind at all", () => { const blind = flashDuration(1.7, { x: 1100, y: 1000 }, { x: 1000, y: 1000 }, Math.PI, true, 360); expect(blind).toBe(0); }); test("a flash reaches a room, not the map", () => { // The map is 2400 by 1520. A 900 radius was 70% of it by area and 75% of // its width across: weather, not a thrown object. A site is a few hundred // units across, and that is the scale every flash is kept to. expect(flashes().length).toBeGreaterThan(0); for (const flash of flashes()) { expect(flash.radius).toBeGreaterThanOrEqual(300); expect(flash.radius).toBeLessThanOrEqual(400); expect((Math.PI * flash.radius * flash.radius) / (WIDTH * HEIGHT)).toBeLessThan(0.12); expect((2 * flash.radius) / WIDTH).toBeLessThanOrEqual(0.3); // And it is a thrown object: it lands past its own radius from the thrower. expect(flash.range).toBeGreaterThan(flash.radius); } }); test("distance matters inside the radius: the centre is the full duration, the edge a flicker", () => { const at = { x: 1000, y: 1000 }; const r = 360; const facingIt = Math.PI; const stand = (d: number) => ({ x: 1000 + d, y: 1000 }); expect(flashDuration(1.7, stand(60), at, facingIt, false, r)).toBe(1.7); expect(flashDuration(1.7, stand(0.4 * r), at, facingIt, false, r)).toBe(1.7); const half = flashDuration(1.7, stand(0.7 * r), at, facingIt, false, r); expect(half).toBeGreaterThan(0.3 * 1.7); expect(half).toBeLessThan(0.7 * 1.7); const edge = flashDuration(1.7, stand(r), at, facingIt, false, r); expect(edge).toBeGreaterThan(0); expect(edge).toBeLessThanOrEqual(FLASH_EDGE * 1.7 + 0.01); // Past the radius there is nothing, however hard you were staring. expect(flashDuration(1.7, stand(r + 1), at, facingIt, false, r)).toBe(0); expect(flashDuration(1.7, stand(899), at, facingIt, false, r)).toBe(0); // Closer is never shorter. let last = Infinity; for (let d = 20; d <= r; d += 20) { const now = flashDuration(1.7, stand(d), at, facingIt, false, r); expect(now).toBeLessThanOrEqual(last); last = now; } }); const deg = (n: number) => (n * Math.PI) / 180; test("facing is the gate: on screen is the full duration, behind your back is nothing", () => { // The browser draws FOV wide. Anything inside half of that was on your // screen: you looked at it, you pay all of it. Past the screen's edge a // little grace for turning, gone quickly; beyond that, nothing at all. expect(FLASH_CONE).toBeCloseTo(FOV / 2, 9); expect(FLASH_GRACE).toBeLessThanOrEqual(deg(56)); const at = { x: 1000, y: 1000 }; const near = { x: 1100, y: 1000 }; const straight = Math.PI; for (const off of [0, 15, 30, 37]) { expect(flashDuration(1.7, near, at, straight + deg(off), false, 360)).toBe(1.7); expect(flashDuration(1.7, near, at, straight - deg(off), false, 360)).toBe(1.7); } // The grace falls away quickly: half way through it, well under half is left. const grace = flashDuration(1.7, near, at, straight + deg(46), false, 360); expect(grace).toBeGreaterThan(0); expect(grace).toBeLessThan(0.5 * 1.7); expect(flashDuration(1.7, near, at, straight + deg(53), false, 360)).toBeLessThan(0.1); // Used to blind at full strength out to 45° and taper to 100°. for (const off of [55, 60, 70, 100, 180]) { expect(flashDuration(1.7, near, at, straight + deg(off), false, 360)).toBe(0); expect(facingFlash(near, at, straight + deg(off))).toBe(0); } // Turning further is never a longer blind. let last = Infinity; for (let off = 0; off <= 180; off += 1) { const now = facingFlash(near, at, straight + deg(off)); expect(now).toBeLessThanOrEqual(last); last = now; } }); test("facing decides whether, distance decides how long", () => { const at = { x: 1000, y: 1000 }; const r = 360; const stand = (d: number) => ({ x: 1000 + d, y: 1000 }); // Facing it exactly is never survivable because of a distance table: // every distance the light carries to blinds, and further is shorter. let last = Infinity; for (let d = 10; d <= r; d += 10) { const blind = flashDuration(1.7, stand(d), at, Math.PI, false, r); expect(blind).toBeGreaterThan(0); expect(blind).toBeLessThanOrEqual(last); last = blind; } expect(flashReach(r, r)).toBeCloseTo(FLASH_EDGE, 9); expect(flashReach(r + 1, r)).toBe(0); // And a close flash never blinds a player who had their back to it. for (let d = 10; d <= r; d += 10) { expect(flashDuration(1.7, stand(d), at, 0, false, r)).toBe(0); expect(flashDuration(1.7, stand(d), at, Math.PI / 2, false, r)).toBe(0); expect(flashDuration(1.7, stand(d), at, Math.PI - deg(60), false, r)).toBe(0); } // Behind a wall or a smoke you were not looking at it, whatever your angle says. expect(flashDuration(1.7, stand(10), at, Math.PI, true, r)).toBe(0); }); test("a smoke blocks the sight line for as long as it lives", () => { const smoke: Zone = { id: 1, kind: "smoke", owner: "x", side: "atk", x: 1200, y: 1000, r: 140, life: 8, fuse: 0, beat: 0, pulses: 0, damage: 0, blocks: true, solid: false, }; // Straight down the mid corridor, which is open floor end to end. const from = { x: 1100, y: 1000 }; const to = { x: 1300, y: 1000 }; expect(sightBlocked(from, to, [])).toBe(false); expect(sightBlocked(from, to, [smoke])).toBe(true); // Standing off the line, the smoke is not in the way. expect(sightBlocked({ x: 1100, y: 700 }, { x: 1300, y: 700 }, [smoke])).toBe(false); }); test("a charge is spent on a cast and refused when there is none", () => { const state = live(); const player = attacker(state); player.agent = "veil"; player.charges.c = 1; expect(cast(state, player, "c")).toBe(true); expect(player.charges.c).toBe(0); expect(state.zones.some((z) => z.kind === "smoke")).toBe(true); expect(cast(state, player, "c")).toBe(false); expect(castable(player, "c")).toBe(false); }); test("the signature goes on cooldown and comes back on its own", () => { const state = live(); const player = attacker(state); player.agent = "veil"; const cooldown = agentById("veil").abilities.e.cooldown; expect(cast(state, player, "e")).toBe(true); expect(player.charges.e).toBe(0); expect(player.cooldown.e).toBe(cooldown); for (let i = 0; i < Math.ceil(cooldown * 10) + 4; i++) tick(state, 0.1); expect(player.cooldown.e).toBe(0); expect(castable(player, "e")).toBe(true); }); test("an ultimate costs its points and empties the meter", () => { const state = live(); const player = attacker(state); player.agent = "sable"; player.ult = 3; expect(cast(state, player, "x")).toBe(false); player.ult = 7; expect(cast(state, player, "x")).toBe(true); expect(player.ult).toBe(0); // Hunt reveals every enemy and leaves the caster's own side alone. for (const other of state.players) expect(other.revealed > 0).toBe(other.side !== player.side); }); test("a molly burns whoever stands in it and leaves the thrower's team alone", () => { const state = live(); const player = attacker(state); player.agent = "ember"; player.charges.q = 1; const victim = state.players.find((p) => p.side === "def")!; const mate = state.players.find((p) => p.side === "atk" && p.id !== player.id)!; victim.x = player.x + 120; victim.y = player.y; mate.x = player.x + 120; mate.y = player.y; player.angle = 0; player.x = victim.x - 120; cast(state, player, "q"); for (const zone of state.zones) { zone.x = victim.x; zone.y = victim.y; } for (let i = 0; i < 20; i++) tick(state, 0.1); expect(victim.hp).toBeLessThan(100); expect(mate.hp).toBe(100); }); test("a wall is solid: it blocks the sight line that was open", () => { const state = live(); const player = attacker(state); player.agent = "rook"; // Along the back corridor, which is open from one side of the map to the other. player.x = 400; player.y = 1400; player.angle = 0; const behind = { x: 1000, y: 1400 }; const openBefore = !sightBlocked(player, behind, state.zones); expect(cast(state, player, "e")).toBe(true); expect(openBefore).toBe(true); expect(sightBlocked(player, behind, state.zones)).toBe(true); expect(state.zones.every((z) => z.solid)).toBe(true); }); test("a round starts with the signature ready and no bought charges", () => { const state = newMatch(3); startRound(state, true); for (const player of state.players) { expect(player.charges.c).toBe(0); expect(player.charges.q).toBe(0); expect(player.charges.e).toBe(1); } });