rules.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { describe, expect, it } from 'vitest';
  2. import { loadCharacterStats, loadMap } from '../src/server/assets';
  3. import { Game } from '../src/server/game';
  4. import { MatchStats } from '../src/server/stats';
  5. import { Anim, PLAYABLE_CHARACTER_IDS } from '../src/shared/characters';
  6. import { CHERRY_REGRAB_DELAY, FINAL_RUSH_S, MIN_CAKE_PIXELS, RESPAWN_DELAY_S, SMARTIES_DURATION_S, STUN_GUARD_S, TICK_RATE } from '../src/shared/constants';
  7. import { Button } from '../src/shared/input';
  8. import type { FxEvent } from '../src/shared/protocol';
  9. import { ASSETS, realMaterials } from './helpers';
  10. const materials = realMaterials();
  11. function newRound(duration = 180, seed = 1): Game {
  12. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  13. return new Game({ map: definition, terrain, materials, seed, rules: { duration }, loadCharacter: (id) => loadCharacterStats(ASSETS, id) });
  14. }
  15. function run(game: Game, ticks: number): FxEvent[] {
  16. const fx: FxEvent[] = [];
  17. for (let i = 0; i < ticks; i++) fx.push(...game.step().fx);
  18. return fx;
  19. }
  20. const me = (game: Game, id: number) => game.snapshot().find((p) => p.id === id)!;
  21. describe('custard and the spoon', () => {
  22. it('rises during a round, faster in the final rush, and stays put during the warm-up', () => {
  23. const round = newRound(40);
  24. const start = round.custardLevel;
  25. run(round, 5 * TICK_RATE);
  26. const slow = start - round.custardLevel;
  27. expect(slow).toBeCloseTo(round.map.custard.speed * 5, 0);
  28. run(round, (40 - FINAL_RUSH_S) * TICK_RATE); // into the final rush
  29. const before = round.custardLevel;
  30. run(round, 5 * TICK_RATE);
  31. expect(before - round.custardLevel).toBeCloseTo(round.map.custard.finalRushSpeed * 5, 0);
  32. expect(round.roundState().finalRush).toBe(true);
  33. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  34. const warmup = new Game({ map: definition, terrain, materials, seed: 1, loadCharacter: (id) => loadCharacterStats(ASSETS, id) });
  35. run(warmup, 5 * TICK_RATE);
  36. expect(warmup.custardLevel).toBe(definition.custard.startY);
  37. expect(warmup.roundState()).toMatchObject({ timeLeft: null, cherry: null });
  38. });
  39. it('a player falling in is out for 5 s, then the spoon launches them back from open air', () => {
  40. const game = newRound();
  41. const { id } = game.addPlayer('Plouf', 'generic');
  42. game.teleport(id, 100, 900); // left of the cake: nothing below but custard
  43. const fx = run(game, TICK_RATE);
  44. expect(fx).toContainEqual(expect.objectContaining({ kind: 'splash', id }));
  45. expect(me(game, id).anim & Anim.Dead).toBeTruthy();
  46. expect(game.stats.get(id, 'custard')).toBe(1);
  47. const later = run(game, RESPAWN_DELAY_S * TICK_RATE);
  48. const spoon = later.find((e) => e.kind === 'spoon');
  49. expect(spoon).toBeDefined();
  50. const p = me(game, id);
  51. expect(p.anim & Anim.Dead).toBe(0);
  52. expect(p.y).toBeLessThan(game.custardLevel);
  53. // ...and lands on the cake instead of falling straight back in.
  54. const flight = run(game, 3 * TICK_RATE);
  55. expect(flight.filter((e) => e.kind === 'splash' && e.id === id)).toEqual([]);
  56. expect(me(game, id).grounded).toBe(true);
  57. });
  58. it('the spoon lands players back on the cake whatever spawn it rolls (several seeds)', () => {
  59. for (const seed of [1, 2, 3, 4, 5, 6]) {
  60. const game = newRound(180, seed);
  61. const { id } = game.addPlayer('Plouf', 'generic');
  62. game.teleport(id, 100, 900);
  63. run(game, (RESPAWN_DELAY_S + 0.5) * TICK_RATE);
  64. const fx = run(game, 3 * TICK_RATE);
  65. expect(fx.filter((e) => e.kind === 'splash' && e.id === id), `seed ${seed}`).toEqual([]);
  66. }
  67. });
  68. it('the spoon finds the last little island of cake', () => {
  69. const game = newRound();
  70. // Wipe the cake, plate excluded, except for a 120 px block on the far right.
  71. const { width, height, data } = game.terrain;
  72. for (let y = 0; y < height; y++) {
  73. for (let x = 0; x < width; x++) {
  74. const i = y * width + x;
  75. if (data[i] !== 255 && !(x >= 1600 && x < 1720 && y >= 820)) data[i] = 0;
  76. }
  77. }
  78. game.terrain.markDirty({ x0: 0, y0: 0, x1: width, y1: height });
  79. const { id } = game.addPlayer('Plouf', 'generic');
  80. game.teleport(id, 60, 900); // left of the plate: nothing below but custard
  81. run(game, (RESPAWN_DELAY_S + 0.5) * TICK_RATE);
  82. const fx = run(game, 3 * TICK_RATE);
  83. expect(fx.filter((e) => e.kind === 'splash' && e.id === id)).toEqual([]);
  84. const p = me(game, id);
  85. expect(p.grounded).toBe(true);
  86. expect(p.x).toBeGreaterThan(1590);
  87. expect(p.x).toBeLessThan(1730);
  88. });
  89. it('the round ends when the timer runs out', () => {
  90. const game = newRound(2);
  91. run(game, 2 * TICK_RATE - 1);
  92. expect(game.roundOver).toBe(false);
  93. run(game, 2);
  94. expect(game.roundOver).toBe(true);
  95. expect(game.endReason).toBe('time');
  96. });
  97. it('the round ends early when almost no cake is left above the custard', () => {
  98. const game = newRound();
  99. run(game, 2 * TICK_RATE);
  100. expect(game.roundOver).toBe(false);
  101. const { data } = game.terrain;
  102. let kept = 0;
  103. for (let i = 0; i < data.length; i++) {
  104. if (data[i] === 0 || data[i] === 255) continue;
  105. if (kept < MIN_CAKE_PIXELS / 2) kept++;
  106. else data[i] = 0;
  107. }
  108. run(game, 2 * TICK_RATE);
  109. expect(game.roundOver).toBe(true);
  110. expect(game.endReason).toBe('noCake');
  111. });
  112. });
  113. describe('fair fights', () => {
  114. it('a stun cannot be chained: after one, the player is immune for a moment', () => {
  115. const game = newRound();
  116. const mamie = game.addPlayer('Mamie', 'mamie');
  117. const victim = game.addPlayer('Victime', 'generic');
  118. run(game, 90);
  119. let stunnedTicks = 0;
  120. let longest = 0;
  121. let streak = 0;
  122. // Mamie jabs as fast as she can for 4 s, the victim held right in front of her.
  123. for (let i = 0; i < 4 * TICK_RATE; i++) {
  124. const m = me(game, mamie.id);
  125. game.teleport(victim.id, m.x + 36, m.y);
  126. game.setInput(mamie.id, { buttons: Button.Attack, aimX: Math.round(m.x + 36), aimY: Math.round(m.y) });
  127. game.step();
  128. const stunned = (me(game, victim.id).anim & Anim.Stunned) !== 0;
  129. if (stunned) stunnedTicks++;
  130. streak = stunned ? streak + 1 : 0;
  131. longest = Math.max(longest, streak);
  132. }
  133. const stun = loadCharacterStats(ASSETS, 'mamie').params.needleStun;
  134. expect(longest).toBeLessThanOrEqual(Math.ceil(stun * TICK_RATE) + 1);
  135. // Free for at least the guard time after each stun.
  136. expect(stunnedTicks / (4 * TICK_RATE)).toBeLessThan(stun / (stun + STUN_GUARD_S) + 0.1);
  137. });
  138. it('a stuck player can give up: into the custard, back with the spoon', () => {
  139. const game = newRound();
  140. const { id } = game.addPlayer('Coincé', 'generic');
  141. run(game, 60);
  142. game.setInput(id, { buttons: Button.Suicide, aimX: 0, aimY: 0 });
  143. const fx = run(game, 1);
  144. game.setInput(id, { buttons: 0, aimX: 0, aimY: 0 });
  145. expect(fx).toContainEqual(expect.objectContaining({ kind: 'splash', id }));
  146. expect(me(game, id).anim & Anim.Dead).toBeTruthy();
  147. run(game, (RESPAWN_DELAY_S + 0.5) * TICK_RATE);
  148. expect(me(game, id).anim & Anim.Dead).toBe(0);
  149. });
  150. });
  151. describe('the cherry', () => {
  152. it('is picked up by touching it and scores 1 point per second', () => {
  153. const game = newRound();
  154. const { id } = game.addPlayer('Gourmand', 'generic');
  155. const [cx, cy] = game.map.cherrySpawn;
  156. run(game, 30); // let the cherry settle on the top tier
  157. const cherry = game.roundState().cherry!;
  158. game.teleport(id, cherry.x, cherry.y - 10);
  159. const fx = run(game, 10);
  160. expect(fx).toContainEqual(expect.objectContaining({ kind: 'pickup', id, item: 'cherry' }));
  161. expect(game.roundState().cherry!.holder).toBe(id);
  162. run(game, 3 * TICK_RATE);
  163. expect(me(game, id).score).toBeGreaterThanOrEqual(3);
  164. expect([cx, cy]).toEqual(game.map.cherrySpawn);
  165. });
  166. it('a hit holder drops it, and cannot grab it back right away', () => {
  167. const game = newRound();
  168. const holder = game.addPlayer('Porteur', 'generic');
  169. const biter = game.addPlayer('Castor', 'castor');
  170. run(game, 30);
  171. const cherry = game.roundState().cherry!;
  172. game.teleport(holder.id, cherry.x, cherry.y - 10);
  173. run(game, 10);
  174. expect(game.roundState().cherry!.holder).toBe(holder.id);
  175. const h = me(game, holder.id);
  176. game.teleport(biter.id, h.x - 45, h.y);
  177. game.setInput(biter.id, { buttons: Button.Attack, aimX: Math.round(h.x), aimY: Math.round(h.y) });
  178. const fx = run(game, 2);
  179. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cherryDrop', id: holder.id }));
  180. expect(game.roundState().cherry!.holder).not.toBe(holder.id);
  181. expect(game.stats.get(biter.id, 'hits')).toBeGreaterThanOrEqual(1);
  182. // Still blocked for the holder for a moment.
  183. game.setInput(biter.id, { buttons: 0, aimX: 0, aimY: 0 });
  184. run(game, Math.floor(CHERRY_REGRAB_DELAY * TICK_RATE * 0.5));
  185. expect(game.roundState().cherry!.holder).not.toBe(holder.id);
  186. });
  187. });
  188. describe('items', () => {
  189. it('a candle is picked up, thrown with Use, and explodes: carves the cake and blows players away', () => {
  190. const game = newRound();
  191. const { id } = game.addPlayer('Artificier', 'generic');
  192. run(game, 90);
  193. const p = me(game, id);
  194. game.spawnItem('candle', p.x, p.y - 30);
  195. const fx = run(game, 20);
  196. expect(fx).toContainEqual(expect.objectContaining({ kind: 'pickup', id, item: 'candle' }));
  197. expect(me(game, id).item).toBe('candle');
  198. const eventsBefore = game.events.length;
  199. game.setInput(id, { buttons: Button.Use, aimX: Math.round(p.x + 60), aimY: Math.round(p.y + 80) });
  200. const after = run(game, 2 * TICK_RATE);
  201. expect(me(game, id).item).toBeNull();
  202. expect(after).toContainEqual(expect.objectContaining({ kind: 'explosion' }));
  203. expect(game.events.length).toBeGreaterThan(eventsBefore);
  204. expect(game.stats.get(id, 'candles')).toBe(1);
  205. });
  206. it('smarties boost the speed for a while', () => {
  207. const game = newRound();
  208. const { id } = game.addPlayer('Speedy', 'generic');
  209. run(game, 90);
  210. const p = me(game, id);
  211. game.spawnItem('smarties', p.x, p.y - 30);
  212. run(game, 20);
  213. expect(me(game, id).anim & Anim.Boosted).toBeTruthy();
  214. run(game, SMARTIES_DURATION_S * TICK_RATE);
  215. expect(me(game, id).anim & Anim.Boosted).toBe(0);
  216. });
  217. it('the electric whisk jolts the others and shakes the jelly', () => {
  218. const game = newRound();
  219. const a = game.addPlayer('Fouet', 'generic');
  220. const b = game.addPlayer('Victime', 'generic');
  221. run(game, 90);
  222. const p = me(game, a.id);
  223. const eventsBefore = game.events.length;
  224. game.spawnItem('whisk', p.x, p.y - 30);
  225. let whisked = false;
  226. for (let i = 0; i < 30 && !whisked; i++) whisked = game.step().fx.some((e) => e.kind === 'whisk' && e.id === a.id);
  227. expect(whisked).toBe(true);
  228. // Same tick: the other player is jolted and stunned.
  229. expect(me(game, b.id).anim & Anim.Stunned).toBeTruthy();
  230. run(game, 2);
  231. expect(game.events.length).toBeGreaterThan(eventsBefore + 5);
  232. expect(game.stats.get(a.id, 'eaten')).toBeGreaterThan(0);
  233. });
  234. it('items fall from the sky on their own during a round (seeded)', () => {
  235. const a = newRound(180, 7);
  236. const b = newRound(180, 7);
  237. // The first item drops within map.items.interval[1] seconds; both games drop the same ones.
  238. let seen = 0;
  239. for (let i = 0; i < a.map.items.interval[1] * TICK_RATE + 2; i++) {
  240. a.step();
  241. b.step();
  242. seen = Math.max(seen, a.itemStates().length);
  243. }
  244. expect(seen).toBeGreaterThan(0);
  245. expect(a.itemStates()).toEqual(b.itemStates());
  246. });
  247. });
  248. describe('trophies', () => {
  249. it('go to the best player of each stat, only if they did it', () => {
  250. const stats = new MatchStats();
  251. stats.add(1, 'eaten', 5000);
  252. stats.add(2, 'eaten', 300);
  253. stats.add(2, 'custard', 3);
  254. stats.add(3, 'crushed', 1);
  255. stats.add(3, 'hitTaken', 4);
  256. const t = stats.trophies([1, 2, 3]);
  257. expect(t).toContainEqual({ id: 'glutton', playerId: 1, value: 5000 });
  258. expect(t).toContainEqual({ id: 'kamikaze', playerId: 2, value: 3 });
  259. expect(t).toContainEqual({ id: 'notMyFault', playerId: 3, value: 5 });
  260. expect(t.find((x) => x.id === 'pyromaniac')).toBeUndefined();
  261. });
  262. });
  263. describe('getting up', () => {
  264. /** Holds toward the wall and taps jump, like a player climbing; returns the highest y reached. */
  265. function climb(game: Game, id: number, ticks: number): number {
  266. let best = Infinity;
  267. for (let i = 0; i < ticks; i++) {
  268. const jump = i % 12 < 6 ? Button.Jump : 0;
  269. game.setInput(id, { buttons: Button.Right | jump, aimX: 2000, aimY: 0 });
  270. game.step();
  271. best = Math.min(best, me(game, id).y);
  272. }
  273. return best;
  274. }
  275. it('every character can climb a tier wall by wall-jumping', () => {
  276. for (const character of PLAYABLE_CHARACTER_IDS) {
  277. const game = newRound();
  278. const { id } = game.addPlayer('Grimpeur', character);
  279. game.teleport(id, 470, 670); // bottom tier, against the middle tier's wall (x = 520, 230 px high)
  280. run(game, 30);
  281. expect(climb(game, id, 6 * TICK_RATE), character).toBeLessThan(470);
  282. }
  283. });
  284. it('the cherry is heavy: its holder jumps lower', () => {
  285. const jumpHeight = (holding: boolean): number => {
  286. const game = newRound();
  287. const { id } = game.addPlayer('Sauteur', 'generic');
  288. game.teleport(id, 400, 680);
  289. run(game, 30);
  290. if (holding) {
  291. const cherry = (game as unknown as { cherry: { body: { setPosition(p: { x: number; y: number }): void } } }).cherry;
  292. const p = me(game, id);
  293. cherry.body.setPosition({ x: p.x / 32, y: p.y / 32 });
  294. run(game, 2);
  295. expect(game.roundState().cherry!.holder).toBe(id);
  296. }
  297. const y0 = me(game, id).y;
  298. game.setInput(id, { buttons: Button.Jump, aimX: 0, aimY: 0 });
  299. let top = y0;
  300. for (let i = 0; i < TICK_RATE; i++) {
  301. game.step();
  302. top = Math.min(top, me(game, id).y);
  303. }
  304. return y0 - top;
  305. };
  306. expect(jumpHeight(true)).toBeLessThan(jumpHeight(false) * 0.75);
  307. });
  308. it('a cherry that falls in the custard comes back on the cake, not always at the top', () => {
  309. const tops = new Set<number>();
  310. for (const seed of [1, 2, 3, 4]) {
  311. const game = newRound(180, seed);
  312. const cherry = (game as unknown as { cherry: { body: { setPosition(p: { x: number; y: number }): void } } }).cherry;
  313. cherry.body.setPosition({ x: 100 / 32, y: 1010 / 32 });
  314. run(game, 2);
  315. const c = game.roundState().cherry!;
  316. expect(c.y).toBeLessThan(game.custardLevel);
  317. run(game, TICK_RATE); // it lands on something
  318. expect(game.roundState().cherry!.y).toBeLessThan(game.custardLevel - 10);
  319. tops.add(Math.round(c.y / 100));
  320. }
  321. expect(tops.size).toBeGreaterThan(1);
  322. });
  323. });