fable/val Public Public: anyone can read this; nobody can change it.

forked from benjivers/val

workbenchthis machine
Files a9ddab6ef893cac593648df9ff6b4d2d3d14b7bc
1
import { expect, test } from "bun:test";
2
import {
3
  AGENTS,
4
  agentById,
5
  buyableSlots,
6
  cast,
7
  castable,
8
  FLASH_CONE,
9
  FLASH_EDGE,
10
  flashDuration,
11
  sightBlocked,
12
  SLOTS,
13
  type Ability,
14
} from "./abilities.ts";
15
import { HEIGHT, WIDTH } from "./map.ts";
16
import { newMatch, startRound, tick } from "./match.ts";
17
import type { MatchState, Player, Zone } from "./state.ts";
18
import { FOV } from "../static/raycast.js";
19
20
const flashes = (): Ability[] => AGENTS.flatMap((a) => SLOTS.map((s) => a.abilities[s])).filter((a) => a.kind === "flash");
21
22
const live = (): MatchState => {
23
  const state = newMatch(11);
24
  state.phase = "live";
25
  state.clock = 100;
26
  return state;
27
};
28
const attacker = (state: MatchState): Player => state.players.find((p) => p.side === "atk")!;
29
30
test("every agent has two bought basics, a free signature and an ultimate", () => {
31
  expect(AGENTS.length).toBe(5);
32
  for (const agent of AGENTS) {
33
    for (const slot of SLOTS) expect(agent.abilities[slot].name.length).toBeGreaterThan(0);
34
    for (const slot of buyableSlots) expect(agent.abilities[slot].cost).toBeGreaterThan(0);
35
    expect(agent.abilities.e.cost).toBe(0);
36
    expect(agent.abilities.e.cooldown).toBeGreaterThan(0);
37
    expect(agent.abilities.x.points).toBeGreaterThanOrEqual(6);
38
  }
39
});
40
41
test("a flash blinds by the angle you were facing", () => {
42
  const at = { x: 1000, y: 1000 };
43
  const staring = { x: 1100, y: 1000 };
44
  const facingIt = Math.PI; // looking back down the line at the flash
45
  const facingAway = 0;
46
  expect(flashDuration(1.7, staring, at, facingIt, false, 360)).toBeGreaterThan(1.6);
47
  expect(flashDuration(1.7, staring, at, facingAway, false, 360)).toBe(0);
48
  // Half turned away is a shorter blind than staring straight at it.
49
  const glance = flashDuration(1.7, staring, at, facingIt - 0.5, false, 360);
50
  expect(glance).toBeGreaterThan(0);
51
  expect(glance).toBeLessThan(1.7);
52
});
53
54
test("a wall between you and the flash means no blind at all", () => {
55
  const blind = flashDuration(1.7, { x: 1100, y: 1000 }, { x: 1000, y: 1000 }, Math.PI, true, 360);
56
  expect(blind).toBe(0);
57
});
58
59
test("a flash reaches a room, not the map", () => {
60
  // The map is 2400 by 1520. A 900 radius was 70% of it by area and 75% of
61
  // its width across: weather, not a thrown object. A site is a few hundred
62
  // units across, and that is the scale every flash is kept to.
63
  expect(flashes().length).toBeGreaterThan(0);
64
  for (const flash of flashes()) {
65
    expect(flash.radius).toBeGreaterThanOrEqual(300);
66
    expect(flash.radius).toBeLessThanOrEqual(400);
67
    expect((Math.PI * flash.radius * flash.radius) / (WIDTH * HEIGHT)).toBeLessThan(0.12);
68
    expect((2 * flash.radius) / WIDTH).toBeLessThanOrEqual(0.3);
69
    // And it is a thrown object: it lands past its own radius from the thrower.
70
    expect(flash.range).toBeGreaterThan(flash.radius);
71
  }
72
});
73
74
test("distance matters inside the radius: the centre is the full duration, the edge a flicker", () => {
75
  const at = { x: 1000, y: 1000 };
76
  const r = 360;
77
  const facingIt = Math.PI;
78
  const stand = (d: number) => ({ x: 1000 + d, y: 1000 });
79
  expect(flashDuration(1.7, stand(60), at, facingIt, false, r)).toBe(1.7);
80
  expect(flashDuration(1.7, stand(0.4 * r), at, facingIt, false, r)).toBe(1.7);
81
  const half = flashDuration(1.7, stand(0.7 * r), at, facingIt, false, r);
82
  expect(half).toBeGreaterThan(0.3 * 1.7);
83
  expect(half).toBeLessThan(0.7 * 1.7);
84
  const edge = flashDuration(1.7, stand(r), at, facingIt, false, r);
85
  expect(edge).toBeGreaterThan(0);
86
  expect(edge).toBeLessThanOrEqual(FLASH_EDGE * 1.7 + 0.01);
87
  // Past the radius there is nothing, however hard you were staring.
88
  expect(flashDuration(1.7, stand(r + 1), at, facingIt, false, r)).toBe(0);
89
  expect(flashDuration(1.7, stand(899), at, facingIt, false, r)).toBe(0);
90
  // Closer is never shorter.
91
  let last = Infinity;
92
  for (let d = 20; d <= r; d += 20) {
93
    const now = flashDuration(1.7, stand(d), at, facingIt, false, r);
94
    expect(now).toBeLessThanOrEqual(last);
95
    last = now;
96
  }
97
});
98
99
test("the flash cone is the screen: nothing off screen blinds you", () => {
100
  // The browser draws FOV wide, so a pop more than half of that off your
101
  // facing was never on your screen, and it does nothing.
102
  expect(FLASH_CONE).toBeLessThanOrEqual(FOV / 2 + 1e-9);
103
  const at = { x: 1000, y: 1000 };
104
  const near = { x: 1100, y: 1000 };
105
  const straight = Math.PI;
106
  const deg = (n: number) => (n * Math.PI) / 180;
107
  expect(flashDuration(1.7, near, at, straight + deg(15), false, 360)).toBe(1.7);
108
  expect(flashDuration(1.7, near, at, straight - deg(15), false, 360)).toBe(1.7);
109
  // Used to blind at full strength out to 45° and taper to 100°.
110
  expect(flashDuration(1.7, near, at, straight + deg(45), false, 360)).toBe(0);
111
  expect(flashDuration(1.7, near, at, straight + deg(70), false, 360)).toBe(0);
112
  expect(flashDuration(1.7, near, at, straight + deg(100), false, 360)).toBe(0);
113
  // Just inside the screen's edge it is a flicker, not the full pop.
114
  const edge = flashDuration(1.7, near, at, straight + FLASH_CONE - deg(2), false, 360);
115
  expect(edge).toBeGreaterThan(0);
116
  expect(edge).toBeLessThan(0.3);
117
});
118
119
test("a smoke blocks the sight line for as long as it lives", () => {
120
  const smoke: Zone = {
121
    id: 1, kind: "smoke", owner: "x", side: "atk", x: 1200, y: 1000, r: 140,
122
    life: 8, fuse: 0, beat: 0, pulses: 0, damage: 0, blocks: true, solid: false,
123
  };
124
  // Straight down the mid corridor, which is open floor end to end.
125
  const from = { x: 1100, y: 1000 };
126
  const to = { x: 1300, y: 1000 };
127
  expect(sightBlocked(from, to, [])).toBe(false);
128
  expect(sightBlocked(from, to, [smoke])).toBe(true);
129
  // Standing off the line, the smoke is not in the way.
130
  expect(sightBlocked({ x: 1100, y: 700 }, { x: 1300, y: 700 }, [smoke])).toBe(false);
131
});
132
133
test("a charge is spent on a cast and refused when there is none", () => {
134
  const state = live();
135
  const player = attacker(state);
136
  player.agent = "veil";
137
  player.charges.c = 1;
138
  expect(cast(state, player, "c")).toBe(true);
139
  expect(player.charges.c).toBe(0);
140
  expect(state.zones.some((z) => z.kind === "smoke")).toBe(true);
141
  expect(cast(state, player, "c")).toBe(false);
142
  expect(castable(player, "c")).toBe(false);
143
});
144
145
test("the signature goes on cooldown and comes back on its own", () => {
146
  const state = live();
147
  const player = attacker(state);
148
  player.agent = "veil";
149
  const cooldown = agentById("veil").abilities.e.cooldown;
150
  expect(cast(state, player, "e")).toBe(true);
151
  expect(player.charges.e).toBe(0);
152
  expect(player.cooldown.e).toBe(cooldown);
153
  for (let i = 0; i < Math.ceil(cooldown * 10) + 4; i++) tick(state, 0.1);
154
  expect(player.cooldown.e).toBe(0);
155
  expect(castable(player, "e")).toBe(true);
156
});
157
158
test("an ultimate costs its points and empties the meter", () => {
159
  const state = live();
160
  const player = attacker(state);
161
  player.agent = "sable";
162
  player.ult = 3;
163
  expect(cast(state, player, "x")).toBe(false);
164
  player.ult = 7;
165
  expect(cast(state, player, "x")).toBe(true);
166
  expect(player.ult).toBe(0);
167
  // Hunt reveals every enemy and leaves the caster's own side alone.
168
  for (const other of state.players) expect(other.revealed > 0).toBe(other.side !== player.side);
169
});
170
171
test("a molly burns whoever stands in it and leaves the thrower's team alone", () => {
172
  const state = live();
173
  const player = attacker(state);
174
  player.agent = "ember";
175
  player.charges.q = 1;
176
  const victim = state.players.find((p) => p.side === "def")!;
177
  const mate = state.players.find((p) => p.side === "atk" && p.id !== player.id)!;
178
  victim.x = player.x + 120;
179
  victim.y = player.y;
180
  mate.x = player.x + 120;
181
  mate.y = player.y;
182
  player.angle = 0;
183
  player.x = victim.x - 120;
184
  cast(state, player, "q");
185
  for (const zone of state.zones) {
186
    zone.x = victim.x;
187
    zone.y = victim.y;
188
  }
189
  for (let i = 0; i < 20; i++) tick(state, 0.1);
190
  expect(victim.hp).toBeLessThan(100);
191
  expect(mate.hp).toBe(100);
192
});
193
194
test("a wall is solid: it blocks the sight line that was open", () => {
195
  const state = live();
196
  const player = attacker(state);
197
  player.agent = "rook";
198
  // Along the back corridor, which is open from one side of the map to the other.
199
  player.x = 400;
200
  player.y = 1400;
201
  player.angle = 0;
202
  const behind = { x: 1000, y: 1400 };
203
  const openBefore = !sightBlocked(player, behind, state.zones);
204
  expect(cast(state, player, "e")).toBe(true);
205
  expect(openBefore).toBe(true);
206
  expect(sightBlocked(player, behind, state.zones)).toBe(true);
207
  expect(state.zones.every((z) => z.solid)).toBe(true);
208
});
209
210
test("a round starts with the signature ready and no bought charges", () => {
211
  const state = newMatch(3);
212
  startRound(state, true);
213
  for (const player of state.players) {
214
    expect(player.charges.c).toBe(0);
215
    expect(player.charges.q).toBe(0);
216
    expect(player.charges.e).toBe(1);
217
  }
218
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts