characters.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import { describe, expect, it } from 'vitest';
  2. import { loadCharacterStats, loadMap } from '../src/server/assets';
  3. import { Game } from '../src/server/game';
  4. import { Anim, CHARACTER_IDS } from '../src/shared/characters';
  5. import { MATERIAL_WOOL, TICK_RATE } from '../src/shared/constants';
  6. import { Button } from '../src/shared/input';
  7. import type { FxEvent } from '../src/shared/protocol';
  8. import { ASSETS, realMaterials } from './helpers';
  9. const materials = realMaterials();
  10. /** `params` overrides character tuning values (by character id), e.g. to force a random outcome. */
  11. function newGame(params: Record<string, Record<string, number>> = {}): Game {
  12. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  13. const loadCharacter = (id: string) => {
  14. const stats = loadCharacterStats(ASSETS, id);
  15. return { ...stats, params: { ...stats.params, ...params[id] } };
  16. };
  17. return new Game({ map: definition, terrain, materials, seed: 5, loadCharacter });
  18. }
  19. function run(game: Game, ticks: number): FxEvent[] {
  20. const fx: FxEvent[] = [];
  21. for (let i = 0; i < ticks; i++) fx.push(...game.step().fx);
  22. return fx;
  23. }
  24. const me = (game: Game, id: number) => game.snapshot().find((p) => p.id === id)!;
  25. const countWool = (game: Game): number => game.terrain.data.reduce((n, m) => n + (m === MATERIAL_WOOL ? 1 : 0), 0);
  26. /** Presses `buttons` aimed at (x, y) for one tick, then releases. */
  27. function press(game: Game, id: number, buttons: number, x: number, y: number): FxEvent[] {
  28. game.setInput(id, { buttons, aimX: Math.round(x), aimY: Math.round(y) });
  29. const fx = run(game, 1);
  30. game.setInput(id, { buttons: 0, aimX: Math.round(x), aimY: Math.round(y) });
  31. return fx;
  32. }
  33. describe('roster', () => {
  34. it('every character has data that loads', () => {
  35. expect(CHARACTER_IDS).toEqual(['castor', 'mamie', 'pyromanchot', 'gege', 'pompier', 'prout', 'chat', 'magicien', 'generic']);
  36. for (const id of CHARACTER_IDS) expect(loadCharacterStats(ASSETS, id).id).toBe(id);
  37. });
  38. });
  39. describe('Mamie Tricot', () => {
  40. it('her yarn ball lays a taut wool bridge that holds: nothing falls, nobody is hurt', () => {
  41. const game = newGame();
  42. const { id } = game.addPlayer('Mamie', 'mamie');
  43. game.teleport(id, 640, 440); // middle tier, left of the top tier wall (x ≈ 780)
  44. run(game, 60);
  45. const m = me(game, id);
  46. let detached = 0;
  47. let crushed = 0;
  48. game.setInput(id, { buttons: Button.Special, aimX: 800, aimY: Math.round(m.y - 40) });
  49. for (let i = 0; i < 4 * TICK_RATE; i++) {
  50. const r = game.step();
  51. if (i === 0) game.setInput(id, { buttons: 0, aimX: 800, aimY: Math.round(m.y - 40) });
  52. detached += r.terrain.filter((e) => e.ev.kind === 'detach').length;
  53. crushed += r.crushed.length;
  54. }
  55. expect(countWool(game)).toBeGreaterThan(300);
  56. expect(detached).toBe(0);
  57. expect(crushed).toBe(0);
  58. expect(me(game, id).anim & Anim.Dead).toBe(0);
  59. });
  60. it('the thread follows the ball in flight, tied under her feet', () => {
  61. const game = newGame();
  62. const { id } = game.addPlayer('Mamie', 'mamie');
  63. game.teleport(id, 640, 440);
  64. run(game, 60);
  65. const m = me(game, id);
  66. press(game, id, Button.Special, 800, m.y - 40);
  67. const ball = game.itemStates().find((it) => it.kind === 'yarn')!;
  68. expect(ball.thread).toBeDefined();
  69. const [ax, ay] = ball.thread!;
  70. expect(Math.abs(ax - m.x)).toBeLessThan(20);
  71. expect(ay).toBeGreaterThan(m.y); // on the ground under her
  72. expect(game.terrain.isSolid(Math.round(ax), Math.round(ay))).toBe(true);
  73. });
  74. it('a yarn ball thrown at her own feet never buries or kills her', () => {
  75. const game = newGame();
  76. const { id } = game.addPlayer('Mamie', 'mamie');
  77. game.teleport(id, 640, 440);
  78. run(game, 60);
  79. for (const [ax, ay] of [[0, 60], [0, -200], [40, 20], [-40, 20]]) {
  80. const m = me(game, id);
  81. press(game, id, Button.Special, m.x + ax, m.y + ay);
  82. run(game, 3 * TICK_RATE);
  83. const after = me(game, id);
  84. expect(after.anim & Anim.Dead).toBe(0);
  85. // Not stuck inside wool: no solid pixel at her centre.
  86. expect(game.terrain.isSolid(Math.round(after.x), Math.round(after.y))).toBe(false);
  87. }
  88. });
  89. it('her needles barely push but stun for a long time', () => {
  90. const game = newGame();
  91. const mamie = game.addPlayer('Mamie', 'mamie');
  92. const victim = game.addPlayer('Victime', 'generic');
  93. run(game, 90);
  94. const m = me(game, mamie.id);
  95. game.teleport(victim.id, m.x + 40, m.y);
  96. press(game, mamie.id, Button.Attack, m.x + 40, m.y);
  97. const v = me(game, victim.id);
  98. expect(v.anim & Anim.Stunned).toBeTruthy();
  99. expect(Math.hypot(v.vx, v.vy)).toBeLessThan(400);
  100. run(game, Math.round(0.4 * TICK_RATE));
  101. expect(me(game, victim.id).anim & Anim.Stunned).toBeTruthy();
  102. });
  103. it('her wool burns: an explosion nearby consumes it', () => {
  104. const game = newGame();
  105. const mamie = game.addPlayer('Mamie', 'mamie');
  106. const pyro = game.addPlayer('Pyro', 'pyromanchot');
  107. game.teleport(mamie.id, 640, 440);
  108. run(game, 60);
  109. const m = me(game, mamie.id);
  110. press(game, mamie.id, Button.Special, 800, m.y - 40);
  111. run(game, 2 * TICK_RATE);
  112. const before = countWool(game);
  113. expect(before).toBeGreaterThan(0);
  114. // A firecracker thrown into the wool.
  115. game.teleport(pyro.id, 700, 300);
  116. run(game, 2);
  117. const fx = press(game, pyro.id, Button.Attack, 760, m.y - 30).concat(run(game, TICK_RATE));
  118. expect(fx.some((e) => e.kind === 'explosion')).toBe(true);
  119. run(game, 2);
  120. expect(countWool(game)).toBeLessThan(before);
  121. });
  122. });
  123. describe('Pyromanchot', () => {
  124. it('throws firecrackers far away from him', () => {
  125. const game = newGame({ pyromanchot: { backfireChance: 0 } });
  126. const { id } = game.addPlayer('Pyro', 'pyromanchot');
  127. run(game, 90);
  128. const explosions: { x: number; y: number }[] = [];
  129. for (let shot = 0; shot < 3; shot++) {
  130. const p = me(game, id);
  131. // Aimed up and across: it flies until it lands somewhere on the cake.
  132. const fx = press(game, id, Button.Attack, p.x + 300, p.y - 300).concat(run(game, 2 * TICK_RATE));
  133. for (const e of fx) if (e.kind === 'explosion') explosions.push({ x: e.x - p.x, y: e.y - p.y });
  134. }
  135. expect(explosions).toHaveLength(3);
  136. for (const e of explosions) expect(Math.hypot(e.x, e.y)).toBeGreaterThan(200);
  137. });
  138. it('only some firecrackers backfire', () => {
  139. const game = newGame();
  140. const { id } = game.addPlayer('Pyro', 'pyromanchot');
  141. run(game, 90);
  142. let backfires = 0;
  143. const shots = 40;
  144. for (let shot = 0; shot < shots; shot++) {
  145. const p = me(game, id);
  146. const fx = press(game, id, Button.Attack, p.x + 300, p.y - 300);
  147. if (fx.some((e) => e.kind === 'cue' && e.cue === 'backfire')) backfires++;
  148. run(game, Math.round(0.5 * TICK_RATE));
  149. }
  150. expect(backfires).toBeGreaterThan(0);
  151. expect(backfires).toBeLessThan(shots / 4);
  152. });
  153. it('a backfire stuns him', () => {
  154. const game = newGame({ pyromanchot: { backfireChance: 1 } });
  155. const { id } = game.addPlayer('Pyro', 'pyromanchot');
  156. run(game, 90);
  157. const p = me(game, id);
  158. press(game, id, Button.Attack, p.x + 300, p.y - 100);
  159. expect(me(game, id).anim & Anim.Stunned).toBeTruthy();
  160. });
  161. it('belly slide: fast, then an explosion at the end that spares him', () => {
  162. const game = newGame();
  163. const pyro = game.addPlayer('Pyro', 'pyromanchot');
  164. const victim = game.addPlayer('Victime', 'generic');
  165. game.teleport(pyro.id, 900, 680); // bottom tier, lots of room to the right
  166. game.teleport(victim.id, 1500, 680);
  167. run(game, 60);
  168. const start = me(game, pyro.id);
  169. press(game, pyro.id, Button.Special, start.x + 500, start.y);
  170. expect(me(game, pyro.id).anim & Anim.Special).toBeTruthy();
  171. const fx = run(game, TICK_RATE);
  172. const end = me(game, pyro.id);
  173. expect(end.x - start.x).toBeGreaterThan(200);
  174. expect(fx.some((e) => e.kind === 'explosion')).toBe(true);
  175. expect(end.anim & Anim.Stunned).toBe(0);
  176. });
  177. });
  178. describe('Gégé le Ver', () => {
  179. it('swims down into the cake, then bursts out under a player and sends them flying', () => {
  180. const game = newGame();
  181. const gege = game.addPlayer('Gégé', 'gege');
  182. const victim = game.addPlayer('Victime', 'generic');
  183. game.teleport(gege.id, 600, 420); // middle tier top
  184. game.teleport(victim.id, 730, 420);
  185. run(game, 60);
  186. // Dive.
  187. for (let i = 0; i < 40; i++) {
  188. const g = me(game, gege.id);
  189. game.setInput(gege.id, { buttons: Button.Special, aimX: Math.round(g.x), aimY: Math.round(g.y + 200) });
  190. game.step();
  191. }
  192. expect(me(game, gege.id).anim & Anim.Underground).toBeTruthy();
  193. // Tunnel toward the victim, under them, then let go: he bursts out.
  194. for (let i = 0; i < 200; i++) {
  195. const g = me(game, gege.id);
  196. const v = me(game, victim.id);
  197. const under = Math.abs(g.x - v.x) < 20;
  198. game.setInput(gege.id, { buttons: Button.Special, aimX: Math.round(v.x), aimY: Math.round(under ? v.y : 505) });
  199. game.step();
  200. if (under && g.y - v.y < 70) break;
  201. }
  202. game.setInput(gege.id, { buttons: 0, aimX: 0, aimY: 0 });
  203. const fx = run(game, 2);
  204. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cue', id: gege.id, cue: 'emerge' }));
  205. const v = me(game, victim.id);
  206. expect(v.anim & Anim.Stunned).toBeTruthy();
  207. expect(v.vy).toBeLessThan(-300);
  208. });
  209. });
  210. describe('Bébé Pompier', () => {
  211. it('the water jet pushes a player away and the recoil sends him backwards', () => {
  212. const game = newGame();
  213. const baby = game.addPlayer('Bébé', 'pompier');
  214. const victim = game.addPlayer('Victime', 'generic');
  215. game.teleport(baby.id, 600, 420);
  216. run(game, 60);
  217. const b = me(game, baby.id);
  218. game.teleport(victim.id, b.x + 80, b.y);
  219. run(game, 1);
  220. const fx = press(game, baby.id, Button.Attack, b.x + 200, b.y);
  221. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cue', cue: 'water' }));
  222. expect(me(game, victim.id).vx).toBeGreaterThan(200);
  223. expect(me(game, baby.id).vx).toBeLessThan(-200);
  224. });
  225. it('soaking turns sponge into soaked sponge, which then sags on its own', () => {
  226. const game = newGame();
  227. const baby = game.addPlayer('Bébé', 'pompier');
  228. game.teleport(baby.id, 600, 420);
  229. run(game, 60);
  230. const b = me(game, baby.id);
  231. press(game, baby.id, Button.Special, b.x + 60, b.y + 120);
  232. const soaked = (): number => game.terrain.data.reduce((n, m) => n + (m === 8 ? 1 : 0), 0);
  233. const after = soaked();
  234. expect(after).toBeGreaterThan(1000);
  235. run(game, 4 * TICK_RATE);
  236. expect(soaked()).toBeLessThan(after); // crumbling
  237. });
  238. it('the spray drenches players caught in it: slower for a while', () => {
  239. const game = newGame();
  240. const baby = game.addPlayer('Bébé', 'pompier');
  241. const victim = game.addPlayer('Victime', 'generic');
  242. game.teleport(baby.id, 600, 420);
  243. run(game, 60);
  244. const b = me(game, baby.id);
  245. game.teleport(victim.id, b.x + 100, b.y);
  246. run(game, 1);
  247. press(game, baby.id, Button.Special, b.x + 100, b.y);
  248. expect(me(game, victim.id).anim & Anim.Soaked).toBeTruthy();
  249. expect(me(game, baby.id).anim & Anim.Soaked).toBeFalsy();
  250. });
  251. });
  252. describe('Captain Prout', () => {
  253. it('each thrust lifts him, is heard by all, and leaves a cloud that poisons players inside', () => {
  254. const game = newGame();
  255. const prout = game.addPlayer('Prout', 'prout');
  256. const victim = game.addPlayer('Victime', 'generic');
  257. game.teleport(prout.id, 600, 420);
  258. run(game, 60);
  259. const p0 = me(game, prout.id);
  260. const fx = press(game, prout.id, Button.Special, p0.x, p0.y - 100);
  261. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cue', id: prout.id, cue: 'prout' }));
  262. expect(me(game, prout.id).vy).toBeLessThan(-300);
  263. expect(game.roundState().clouds.length).toBe(1);
  264. const [cx, cy] = game.roundState().clouds[0];
  265. game.teleport(victim.id, cx, cy);
  266. run(game, 2);
  267. expect(me(game, victim.id).anim & Anim.Poisoned).toBeTruthy();
  268. run(game, 1);
  269. game.teleport(prout.id, cx, cy);
  270. run(game, 2);
  271. expect(me(game, prout.id).anim & Anim.Poisoned).toBeFalsy(); // immune to his own clouds
  272. });
  273. it('only a few thrusts before touching the ground again: he hops, he does not fly', () => {
  274. const game = newGame();
  275. const prout = game.addPlayer('Prout', 'prout');
  276. game.teleport(prout.id, 600, 420);
  277. run(game, 60);
  278. let puffs = 0;
  279. // One try per cooldown: he does not land before the fourth one.
  280. for (let i = 0; i < 4; i++) {
  281. const p = me(game, prout.id);
  282. const fx = press(game, prout.id, Button.Special, p.x, p.y - 100);
  283. if (fx.some((e) => e.kind === 'cue' && e.cue === 'prout')) puffs++;
  284. if (i < 3) {
  285. run(game, 24);
  286. expect(me(game, prout.id).grounded).toBe(false);
  287. }
  288. }
  289. expect(puffs).toBe(loadCharacterStats(ASSETS, 'prout').params.airPuffs);
  290. });
  291. });
  292. describe('Le Chat', () => {
  293. it('pounces claws first: digs scratches and sends the player he lands on flying', () => {
  294. const game = newGame();
  295. const cat = game.addPlayer('Chat', 'chat');
  296. const victim = game.addPlayer('Victime', 'generic');
  297. game.teleport(cat.id, 600, 420);
  298. run(game, 60);
  299. const c = me(game, cat.id);
  300. game.teleport(victim.id, c.x + 90, c.y);
  301. run(game, 1);
  302. const before = game.terrain.data.reduce((n, m) => n + (m !== 0 ? 1 : 0), 0);
  303. const fx = press(game, cat.id, Button.Special, c.x + 200, c.y + 30);
  304. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cue', cue: 'pounce' }));
  305. let fastest = 0;
  306. for (let i = 0; i < 20; i++) {
  307. run(game, 1);
  308. const v = me(game, victim.id);
  309. fastest = Math.max(fastest, Math.hypot(v.vx, v.vy));
  310. }
  311. expect(fastest).toBeGreaterThan(300);
  312. expect(game.terrain.data.reduce((n, m) => n + (m !== 0 ? 1 : 0), 0)).toBeLessThan(before);
  313. });
  314. it('shoves a stunned player hard', () => {
  315. const game = newGame();
  316. const cat = game.addPlayer('Chat', 'chat');
  317. const a = game.addPlayer('Assommé', 'generic');
  318. game.teleport(cat.id, 600, 420);
  319. run(game, 60);
  320. const c = me(game, cat.id);
  321. game.teleport(a.id, c.x + 50, c.y);
  322. run(game, 1);
  323. press(game, cat.id, Button.Attack, c.x + 50, c.y); // paw: stuns
  324. expect(me(game, a.id).anim & Anim.Stunned).toBeTruthy();
  325. const now = me(game, cat.id);
  326. game.teleport(a.id, now.x + 55 * now.facing, now.y);
  327. press(game, cat.id, Button.Special, now.x + 200 * now.facing, now.y);
  328. expect(Math.abs(me(game, a.id).vx)).toBeGreaterThan(400);
  329. });
  330. it('stops to lick his paw now and then, ignoring input', () => {
  331. const game = newGame();
  332. const cat = game.addPlayer('Chat', 'chat');
  333. run(game, 60);
  334. let licked = false;
  335. for (let i = 0; i < 26 * TICK_RATE && !licked; i++) licked = game.step().fx.some((e) => e.kind === 'cue' && e.cue === 'lick');
  336. expect(licked).toBe(true);
  337. expect(me(game, cat.id).anim & Anim.Busy).toBeTruthy();
  338. const x0 = me(game, cat.id).x;
  339. game.setInput(cat.id, { buttons: Button.Right, aimX: 0, aimY: 0 });
  340. run(game, 20);
  341. expect(Math.abs(me(game, cat.id).x - x0)).toBeLessThan(5);
  342. });
  343. });
  344. describe('Magicien Raté', () => {
  345. function castMany(leading: boolean, casts: number): Record<string, number> {
  346. const game = newGame();
  347. const mage = game.addPlayer('Mage', 'magicien');
  348. const other = game.addPlayer('Autre', 'generic');
  349. game.teleport(mage.id, 600, 420);
  350. game.teleport(other.id, 1400, 420);
  351. run(game, 60);
  352. // Make him the leader (or last) by giving round points.
  353. const players = (game as unknown as { players: Map<number, { score: number }> }).players;
  354. players.get(mage.id)!.score = leading ? 50 : 0;
  355. players.get(other.id)!.score = leading ? 0 : 50;
  356. const counts: Record<string, number> = {};
  357. for (let i = 0; i < casts; i++) {
  358. const m = me(game, mage.id);
  359. const fx = press(game, mage.id, Button.Special, m.x + 100, m.y - 50);
  360. for (const e of fx) if (e.kind === 'cue' && e.cue.startsWith('spell-')) counts[e.cue] = (counts[e.cue] ?? 0) + 1;
  361. run(game, Math.ceil(2.3 * TICK_RATE));
  362. }
  363. return counts;
  364. }
  365. it('casts every kind of spell, and fizzles far more often when leading', () => {
  366. const last = castMany(false, 40);
  367. expect(Object.keys(last).sort()).toEqual(['spell-jelly', 'spell-pfft', 'spell-rabbit', 'spell-swap']);
  368. const lead = castMany(true, 40);
  369. expect(lead['spell-pfft'] ?? 0).toBeGreaterThan((last['spell-pfft'] ?? 0) * 2);
  370. });
  371. it('the giant rabbit appears in the air and falls (an unanchored sponge chunk)', () => {
  372. const game = newGame();
  373. const mage = game.addPlayer('Mage', 'magicien');
  374. game.teleport(mage.id, 600, 420);
  375. run(game, 60);
  376. for (let i = 0; i < 60; i++) {
  377. const m = me(game, mage.id);
  378. game.setInput(mage.id, { buttons: Button.Special, aimX: Math.round(m.x + 100), aimY: Math.round(m.y - 50) });
  379. const r = game.step();
  380. game.setInput(mage.id, { buttons: 0, aimX: 0, aimY: 0 });
  381. if (!r.fx.some((e) => e.kind === 'cue' && e.cue === 'spell-rabbit')) {
  382. run(game, Math.ceil(2.3 * TICK_RATE));
  383. continue;
  384. }
  385. // The rabbit is filled in this very tick, and comes loose in the same detach pass.
  386. expect(r.terrain.some((e) => e.ev.kind === 'detach' && !e.ev.debris)).toBe(true);
  387. return;
  388. }
  389. throw new Error('no rabbit in 60 casts');
  390. });
  391. });