import { expect, test } from "bun:test"; import { AGENTS, agentById, buyableSlots, cast, castable, FLASH_CONE, FLASH_EDGE, flashDuration, 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/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); // Half turned away is a shorter blind than staring straight at it. const glance = flashDuration(1.7, staring, at, facingIt - 0.5, 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; } }); test("the flash cone is the screen: nothing off screen blinds you", () => { // The browser draws FOV wide, so a pop more than half of that off your // facing was never on your screen, and it does nothing. expect(FLASH_CONE).toBeLessThanOrEqual(FOV / 2 + 1e-9); const at = { x: 1000, y: 1000 }; const near = { x: 1100, y: 1000 }; const straight = Math.PI; const deg = (n: number) => (n * Math.PI) / 180; expect(flashDuration(1.7, near, at, straight + deg(15), false, 360)).toBe(1.7); expect(flashDuration(1.7, near, at, straight - deg(15), false, 360)).toBe(1.7); // Used to blind at full strength out to 45° and taper to 100°. expect(flashDuration(1.7, near, at, straight + deg(45), false, 360)).toBe(0); expect(flashDuration(1.7, near, at, straight + deg(70), false, 360)).toBe(0); expect(flashDuration(1.7, near, at, straight + deg(100), false, 360)).toBe(0); // Just inside the screen's edge it is a flicker, not the full pop. const edge = flashDuration(1.7, near, at, straight + FLASH_CONE - deg(2), false, 360); expect(edge).toBeGreaterThan(0); expect(edge).toBeLessThan(0.3); }); 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); } });