anvilsign in

collin/mahjong

1import type { NetThrow } from '../game/ctl';
2import type { GameState } from '../game/engine';
3import { makeRng, type Tile } from '../game/tiles';
4import type { SeatId } from '../game/types';
5import { SEAT_ROT } from '../ui/rotation';
6import {
7 aimAt,
8 canThrow,
9 canThrowFrom,
10 centreOf,
11 measure,
12 spanThrough,
13 type Point,
14 type TableGeometry,
15} from './geometry';
16import {
17 addBody,
18 advance as advanceWorld,
19 createWorld,
20 liftFor,
21 reshape,
22 settled,
23 stir,
24 SLIDE_TIME,
25 type Body,
26 type Impact,
27 type Rect,
28 type World,
29} from './physics';
30
31const SEATS: SeatId[] = [0, 1, 2, 3];
32
33/**
34 * The tiles in the middle, kept honest against the game state.
35 *
36 * This *diffs* rather than listening for events, and that is the whole design.
37 * A discard is identified by which seat threw it and how many it had thrown
38 * before — and those keys never shift, because discards are only ever pushed and
39 * `engine.executeClaim` only ever pops the one on top. So every way the pile can
40 * change is the same code path: a key that has appeared is a tile to throw in, a
41 * key that has gone is a tile to take out. Undo, 下一局, a claim, and picking up
42 * a saved game all fall out of that without the engine knowing this exists.
43 *
44 * Nothing here is stored in the game state, so there is no save format to
45 * change. A pile is rebuilt from the discards it can see, seeded off the hand
46 * number and each tile's place in it, which is why a refresh mid-hand comes back
47 * to the pile it had rather than a freshly scattered one.
48 */
49
50/** How long a placed tile is in the air. Long enough to arc over the wall. */
51const PLACE_FLIGHT = 0.62;
52/** A flicked tile hops rather than lofts — across the table, not over a wall. */
53const THROW_LIFT = 0.18;
54/** Fastest flick we will honour, px/s. Beyond this it is a slip, not a throw. */
55export const MAX_THROW_SPEED = 3600;
56/** How long a claimed tile takes to leave the table for the seat taking it. */
57const TAKE_FLIGHT = 0.34;
58
59/** A tile on its way off the table, no longer part of the pile. */
60interface Leaving {
61 body: Body;
62 tx: number;
63 ty: number;
64 /** 0 to 1. */
65 t: number;
66}
67
68/**
69 * Whether a tile turns over on its way in.
70 *
71 * Off for now. Like `LOB_WHEN_WALLED` below, the tumble is kept whole — the
72 * flip is a real axis in `physics.ts` and the renderer already squashes the
73 * tile along it — and this is the only switch that reaches it, so putting it
74 * back is one word rather than a rewrite.
75 */
76const TUMBLE: boolean = false;
77
78/**
79 * Whether a wall standing in the way turns a discard into a lob over the top
80 * rather than a flick across the table.
81 *
82 * Back on now that the square is a square again. It was off while the wall ran
83 * along the edges of the middle, where there was nothing much to get over and
84 * everything read better slid; a square standing in the middle with the pool
85 * inside it is the other case, and a tile slid flat at it simply bounces off
86 * and ends up lying outside — which is not where a discard goes. As the square
87 * is eaten the gaps open and the slide comes back on its own, seat by seat.
88 */
89const LOB_WHEN_WALLED: boolean = true;
90
91/** A tile lifted out of a hand and carried about. Drawn, but not simulated. */
92export interface Held {
93 seat: SeatId;
94 tile: Tile;
95 /** Where it came from in that seat's hand, so the caller knows what to play. */
96 index: number;
97 x: number;
98 y: number;
99 angle: number;
100 /** The size it was in the hand — it shrinks to pool size once thrown. */
101 w: number;
102 h: number;
103}
104
105/** A throw that has been let go of, waiting for the discard it belongs to. */
106interface Release {
107 seat: SeatId;
108 x: number;
109 y: number;
110 angle: number;
111 w: number;
112 h: number;
113 vx: number;
114 vy: number;
115 spin: number;
116}
117
118export class TablePool {
119 world: World | null = null;
120 geo: TableGeometry | null = null;
121 /** The tile in somebody's fingers, if any. */
122 held: Held | null = null;
123 /**
124 * Poke the render loop. Set by ui/Pool.tsx while it is mounted; a gesture
125 * calls it so a carried tile follows the finger without waiting on a re-render.
126 */
127 wake: () => void = () => {};
128
129 private table: HTMLElement | null = null;
130 /** Bodies by `seat:index` into that seat's discards. */
131 private entries = new Map<string, Body>();
132 private release: Release | null = null;
133 /** When the throw was let go of. Online its discard takes a round-trip to
134 * come back, so a release has to be able to outlive a few frames. */
135 private releasedAt = 0;
136 /** What the wall looked like when we last measured, so we know to re-measure. */
137 private measuredAt = '';
138 private hand = -1;
139 /** The tile currently on the table to be claimed, so it can be marked. */
140 private newestKey: string | null = null;
141 /** How many melds each seat had last time, to spot who took a tile. */
142 private melds = [0, 0, 0, 0];
143 /** Tiles on their way off the table. Drawn, but out of the physics. */
144 private leaving: Leaving[] = [];
145 /** When the table will have stopped talking. See `heckle`. */
146 private quietAt = 0;
147
148 // Which seats can throw, as a string, so the seats can re-render when it
149 // changes. Read through useSyncExternalStore, the same way the game is.
150 private openness = '----';
151 private listeners = new Set<() => void>();
152
153 attach(table: HTMLElement) {
154 this.table = table;
155 this.measuredAt = '';
156 }
157
158 detach() {
159 this.table = null;
160 this.world = null;
161 this.geo = null;
162 this.held = null;
163 this.release = null;
164 this.entries.clear();
165 this.leaving = [];
166 this.measuredAt = '';
167 }
168
169 subscribe = (cb: () => void) => {
170 this.listeners.add(cb);
171 return () => this.listeners.delete(cb);
172 };
173
174 /** Which seats have a clear line in, as a string — cheap to compare. */
175 getSnapshot = () => this.openness;
176
177 /** Force a re-measure — the window resized, or the layout changed under us. */
178 invalidate() {
179 this.measuredAt = '';
180 }
181
182 /**
183 * Whether this seat can flick a tile in, or has to lift it over the wall.
184 * Unmeasured (or compact, where there is no square) means no.
185 */
186 canThrow(seat: SeatId): boolean {
187 return this.openness[seat] === 'y';
188 }
189
190 /** Where the middle of the table is, for a gesture working out its aim. */
191 get poolCentre(): Point | null {
192 return this.geo ? centreOf(this.geo.pool) : null;
193 }
194
195 /** Whether a point is already inside the square — a tile dropped there stays. */
196 inPool(x: number, y: number): boolean {
197 const p = this.geo?.pool;
198 if (!p) return false;
199 return x >= p.x && x <= p.x + p.w && y >= p.y && y <= p.y + p.h;
200 }
201
202 /** Whether a tile let go of *here* has a clear line in. Not the same question
203 * as `canThrow(seat)`: a carried tile is wherever the player took it. */
204 canThrowFrom(x: number, y: number): boolean {
205 return !!this.geo && canThrowFrom(this.geo, { x, y });
206 }
207
208 /** Where the pool sits on the table, for the patch of it you can reach into. */
209 get poolRect(): Rect | null {
210 return this.geo?.pool ?? null;
211 }
212
213 /**
214 * Push the tiles in the middle about — washing them, the way everybody does
215 * to a pool of discards while waiting. Purely cosmetic: where a discard is
216 * lying is not part of the game.
217 */
218 stir(x: number, y: number, vx: number, vy: number) {
219 if (!this.world || !this.geo) return;
220 // A finger's worth: about a tile across.
221 stir(this.world, x, y, vx, vy, this.geo.tile.w * 0.6);
222 }
223
224 // ---- the tile in your fingers -----------------------------------------
225
226 /** Lifted out of a hand. From here the canvas draws it, not the DOM. */
227 pickUp(held: Held) {
228 this.held = held;
229 }
230
231 moveHeld(x: number, y: number, angle?: number) {
232 if (!this.held) return;
233 this.held.x = x;
234 this.held.y = y;
235 if (angle !== undefined) this.held.angle = angle;
236 }
237
238 /** Put it back in the hand — the throw was not committed to. */
239 cancelHeld() {
240 this.held = null;
241 }
242
243 /**
244 * Let go of it. The body is not made here: the reconciler owns every tile in
245 * the pool, so this leaves the throw where the next `sync` will find it and
246 * the caller goes on to call `game.discard`. The held tile stays drawn until
247 * that happens, so there is no frame with nothing on screen.
248 */
249 throwHeld(vx: number, vy: number, spin: number) {
250 const h = this.held;
251 if (!h) return;
252 const speed = Math.hypot(vx, vy);
253 const scale = speed > MAX_THROW_SPEED ? MAX_THROW_SPEED / speed : 1;
254 this.release = {
255 seat: h.seat,
256 x: h.x,
257 y: h.y,
258 angle: h.angle,
259 w: h.w,
260 h: h.h,
261 vx: vx * scale,
262 vy: vy * scale,
263 spin,
264 };
265 this.releasedAt = Date.now();
266 }
267
268 /**
269 * A throw that happened on somebody's phone, arriving over the wire in their
270 * own frame: `u` along their edge, `vy` into the table. It is stood at their
271 * side of this screen and turned into the table's coordinates, so the tile
272 * flies in from off their edge — from where they are sitting, as a thrown
273 * tile should. The discard it belongs to arrives through the state as usual;
274 * this only decides how it enters.
275 */
276 throwFromNet(seat: SeatId, t: NetThrow) {
277 const geo = this.geo;
278 if (!geo) return;
279 const f = geo.felt;
280 // Just beyond the felt, where the thrower's strip is.
281 const m = geo.tile.h * 1.4;
282 const u = Math.min(1, Math.max(0, t.u));
283 let x: number;
284 let y: number;
285 let vx: number;
286 let vy: number;
287 // Each seat's frame: x runs to the player's right along their edge, y into
288 // the table. The screen rotation for each seat is what `ROT` says it is.
289 switch (seat) {
290 case 0:
291 x = f.x + u * f.w;
292 y = f.y + f.h + m;
293 vx = t.vx;
294 vy = -t.vy;
295 break;
296 case 1:
297 x = f.x + f.w + m;
298 y = f.y + (1 - u) * f.h;
299 vx = -t.vy;
300 vy = -t.vx;
301 break;
302 case 2:
303 x = f.x + (1 - u) * f.w;
304 y = f.y - m;
305 vx = -t.vx;
306 vy = t.vy;
307 break;
308 default:
309 x = f.x - m;
310 y = f.y + u * f.h;
311 vx = t.vy;
312 vy = t.vx;
313 break;
314 }
315 const speed = Math.hypot(vx, vy);
316 const scale = speed > MAX_THROW_SPEED ? MAX_THROW_SPEED / speed : 1;
317 this.release = {
318 seat,
319 x,
320 y,
321 // Facing its thrower, the way a tile lifted from their hand would be.
322 angle: (SEAT_ROT[seat] * Math.PI) / 180,
323 w: geo.tile.w,
324 h: geo.tile.h,
325 vx: vx * scale,
326 vy: vy * scale,
327 spin: t.spin,
328 };
329 this.releasedAt = Date.now();
330 }
331
332 // ---- keeping up with the game ----------------------------------------
333
334 /** Bring the pile in line with the state. Cheap when nothing has changed. */
335 sync(state: GameState) {
336 this.remeasure(state);
337 const geo = this.geo;
338 const world = this.world;
339 if (!geo || !world) return;
340
341 // A new hand is a new pile, and nothing should fly anywhere.
342 const fresh = state.handNumber !== this.hand;
343 this.hand = state.handNumber;
344
345 const want = new Set<string>();
346 const added: { key: string; seat: SeatId; index: number; tile: Tile }[] = [];
347 for (const seat of SEATS) {
348 const discards = state.players[seat].discards;
349 for (let i = 0; i < discards.length; i++) {
350 const key = `${seat}:${i}`;
351 want.add(key);
352 if (!this.entries.has(key)) added.push({ key, seat, index: i, tile: discards[i] });
353 }
354 }
355
356 const taken: Body[] = [];
357 for (const [key, body] of this.entries) {
358 if (want.has(key)) continue;
359 const at = world.bodies.indexOf(body);
360 if (at >= 0) world.bodies.splice(at, 1);
361 this.entries.delete(key);
362 taken.push(body);
363 }
364
365 // Exactly one tile off the table is somebody taking it — a 碰, a 吃, a 槓,
366 // or a discard being taken back. It goes to whoever now has a meld they did
367 // not have a moment ago, and failing that back to the seat that threw it,
368 // which is what an undo is. Several at once is the hand ending, and those
369 // simply go.
370 const claimer = SEATS.find((q) => state.players[q].melds.length > this.melds[q]);
371 this.melds = SEATS.map((q) => state.players[q].melds.length);
372 if (taken.length === 1 && !fresh) {
373 const to = geo.launch[claimer ?? taken[0].seat];
374 if (to) this.leaving.push({ body: taken[0], tx: to.x, ty: to.y, t: 0 });
375 }
376
377 // One tile at a time is somebody playing, and it gets thrown. Several at
378 // once is a pile being rebuilt — a save picked up, an undo, a fresh deal —
379 // and those tiles were thrown long ago, so they are simply already there.
380 const rebuilding = fresh || added.length > 1;
381 for (const a of added) {
382 this.entries.set(a.key, rebuilding ? this.settle(geo, state, a) : this.launch(geo, state, a));
383 }
384
385 // A throw is good for exactly one discard. At a local table the discard is
386 // in the state before the next frame, so the tile has either just arrived
387 // or is never going to; online it is out on a round-trip, so the release
388 // waits — but not forever, in case the host turned the move down.
389 if (added.length || (this.release && Date.now() - this.releasedAt > 2500)) {
390 this.release = null;
391 this.held = null;
392 }
393
394 const ld = state.lastDiscard;
395 this.newestKey = ld ? `${ld.from}:${state.players[ld.from].discards.length - 1}` : null;
396 }
397
398 /** Run the physics on. Returns impacts worth a sound. */
399 advance(seconds: number): Impact[] {
400 this.carryOff(seconds);
401 return this.world ? advanceWorld(this.world, seconds) : [];
402 }
403
404 /** Tiles leaving the table, lifted and carried off to the seat taking them. */
405 private carryOff(seconds: number) {
406 if (this.leaving.length === 0) return;
407 for (const l of this.leaving) {
408 l.t = Math.min(1, l.t + seconds / TAKE_FLIGHT);
409 // Ease out, so it leaves quickly and arrives gently.
410 const k = 1 - (1 - l.t) * (1 - l.t);
411 l.body.x += (l.tx - l.body.x) * k * 0.5;
412 l.body.y += (l.ty - l.body.y) * k * 0.5;
413 // Up off the table on the way, the way a tile picked up would be.
414 l.body.z = Math.sin(l.t * Math.PI) * 40;
415 l.body.angle += seconds * 2.2;
416 }
417 this.leaving = this.leaving.filter((l) => l.t < 1);
418 }
419
420 /** What is on its way off, and how far gone, for the renderer to fade. */
421 get departing(): { body: Body; fade: number }[] {
422 return this.leaving.map((l) => ({ body: l.body, fade: 1 - l.t }));
423 }
424
425 get bodies(): Body[] {
426 return this.world?.bodies ?? [];
427 }
428
429 /** The tile on the table, if there is one — drawn lit, as a lifted tile is. */
430 get newest(): Body | null {
431 return (this.newestKey && this.entries.get(this.newestKey)) || null;
432 }
433
434 /**
435 * A tile is still on its way to the middle. The computer players hold off
436 * while this is true — nobody at a real table reaches across for a tile that
437 * has not landed, and a 碰 called over one still turning over in the air looks
438 * like it was snatched out of flight.
439 */
440 get throwInFlight(): boolean {
441 const b = this.newest;
442 return !!b && !b.resting;
443 }
444
445 /**
446 * Somebody is being told off for where they put that tile. Held for as long
447 * as the line takes to say, told to us by whoever is saying it.
448 */
449 heckle(seconds: number) {
450 this.quietAt = Math.max(this.quietAt, Date.now() + seconds * 1000);
451 }
452
453 /**
454 * Whether the table is mid-something and a computer player should wait: a
455 * tile still in the air, or a player still complaining about the last one.
456 * Reaching for a tile over the top of somebody objecting to it lands the
457 * objection on nobody.
458 */
459 get busy(): boolean {
460 return this.throwInFlight || Date.now() < this.quietAt;
461 }
462
463 /** Nothing is moving, so the render loop can stop until something changes. */
464 get settled(): boolean {
465 return !this.held && this.leaving.length === 0 && (!this.world || settled(this.world));
466 }
467
468 // ---- measuring ---------------------------------------------------------
469
470 /**
471 * Re-read the table when the wall's shape has changed or the window has. The
472 * pile keeps its positions across a re-measure: the wall shrinking does not
473 * move the tiles already on the felt.
474 */
475 private remeasure(state: GameState) {
476 const table = this.table;
477 if (!table) return;
478 // Every tile on the table is a collider now, so the shape of each hand and
479 // each set laid down is part of what has to be re-read — not just the wall.
480 const hands = state.players.map((p) => `${p.hand.length}.${p.melds.length}.${p.flowers.length}`);
481 const stamp = `${state.drawnFront}/${state.drawnBack}/${hands.join(',')}/${Math.round(
482 table.clientWidth,
483 )}x${Math.round(table.clientHeight)}`;
484 if (stamp === this.measuredAt) return;
485
486 const geo = measure(table, state);
487 this.measuredAt = geo ? stamp : '';
488 this.geo = geo;
489 if (!geo) {
490 this.world = null;
491 this.entries.clear();
492 this.setOpenness('----');
493 return;
494 }
495 // What keeps a tile in is the tiles: the stacks still standing, the sets
496 // laid down, and the hands beyond them. `felt` is the table's own edge and
497 // is only there so a tile that got past all of that is still on the table.
498 if (!this.world) this.world = createWorld(geo.felt, geo.barriers);
499 else reshape(this.world, geo.felt, geo.barriers);
500 // Only here, never per frame: a hundred-odd ray casts is nothing once a
501 // draw, and a great deal every sixtieth of a second.
502 this.setOpenness(SEATS.map((s) => (canThrow(geo, s) ? 'y' : '-')).join(''));
503 }
504
505 private setOpenness(next: string) {
506 if (next === this.openness) return;
507 this.openness = next;
508 for (const cb of this.listeners) cb();
509 }
510
511 // ---- putting tiles in -------------------------------------------------
512
513 /** A tile that is simply there already — a rebuilt pile. */
514 private settle(
515 geo: TableGeometry,
516 state: GameState,
517 a: { seat: SeatId; index: number; tile: Tile },
518 ): Body {
519 const rng = this.seeded(state, a);
520 const { pool } = geo;
521 // Bunched towards the middle rather than spread evenly to the edges, which
522 // is what a pool thrown into from four sides actually looks like.
523 const spread = 0.3;
524 return addBody(this.world!, {
525 tile: a.tile,
526 seat: a.seat,
527 x: pool.x + pool.w * (0.5 + (rng() - 0.5) * 2 * spread),
528 y: pool.y + pool.h * (0.5 + (rng() - 0.5) * 2 * spread),
529 vx: 0,
530 vy: 0,
531 z: 0,
532 vz: 0,
533 angle: (rng() - 0.5) * Math.PI,
534 spin: 0,
535 w: geo.tile.w,
536 h: geo.tile.h,
537 atRest: true,
538 });
539 }
540
541 /** A tile arriving now: thrown if somebody threw it, placed over the wall if
542 * it was tapped, and flat and quick if a bot's wall happens to be open. */
543 private launch(
544 geo: TableGeometry,
545 state: GameState,
546 a: { seat: SeatId; index: number; tile: Tile },
547 ): Body {
548 const rng = this.seeded(state, a);
549 const thrown = this.release?.seat === a.seat ? this.release : null;
550
551 if (thrown) {
552 // Which of the two ways a tile goes in, decided by what is in its way.
553 //
554 // A clear line to the square means it can be *slid* — flat across the
555 // cloth, at exactly the speed it was let go at, which is what a flick
556 // wants to be. A standing wall means it has to be *thrown* instead: lifted
557 // over the top and dropped in, and then the flick's speed decides how far
558 // across the pool it lands rather than how fast it skates.
559 // Sliding is the preferred way in, and the question is asked from where
560 // the tile actually is — a player who carried it out over the square has a
561 // clear line even though the seat they came from does not. Only a tile
562 // with nothing but standing wall in front of it has to be lobbed.
563 const set = this.inPool(thrown.x, thrown.y);
564 const slide = set || !LOB_WHEN_WALLED || this.canThrowFrom(thrown.x, thrown.y);
565 let { vx, vy } = thrown;
566 let vz = 0;
567 const speed = Math.hypot(vx, vy);
568 let flipTurns = 0;
569 let flipOver = 0;
570
571 if (!slide) {
572 // Lofted over a standing wall, and it has to come *down* inside the
573 // square: a lob solved for where it would come to rest touches down
574 // short, on the wrong side of the wall, and stops dead against it.
575 //
576 // So the flick picks a landing spot along its own line, between just
577 // inside the near edge of the square and just short of the far one. A
578 // limp one drops in over the near wall, a hard one carries to the far
579 // side and barges through whatever is lying there, and neither of them
580 // can leave the tile outside.
581 const dir = speed > 0 ? { x: vx / speed, y: vy / speed } : { x: 0, y: -1 };
582 const span = spanThrough(geo.pool, thrown, dir);
583 const centre = centreOf(geo.pool);
584 let carry = speed * PLACE_FLIGHT;
585 if (span) {
586 const near = span.near + geo.tile.h;
587 carry = Math.min(Math.max(near, carry), Math.max(near, span.far - geo.tile.h));
588 } else {
589 // Flicked somewhere that is not the square at all — put it in anyway.
590 carry = Math.hypot(centre.x - thrown.x, centre.y - thrown.y);
591 dir.x = (centre.x - thrown.x) / (carry || 1);
592 dir.y = (centre.y - thrown.y) / (carry || 1);
593 }
594 vx = (dir.x * carry) / PLACE_FLIGHT;
595 vy = (dir.y * carry) / PLACE_FLIGHT;
596 vz = liftFor(PLACE_FLIGHT);
597 // Long enough in the air to turn over properly, and harder means more.
598 flipOver = PLACE_FLIGHT;
599 flipTurns = speed > 1300 ? 2 : 1;
600 } else if (!set) {
601 // A skated tile leaves the fingers off the cloth, and turns over on its
602 // way down — harder means longer in the air and more turns in it.
603 const hop = THROW_LIFT + Math.min(0.22, (speed / MAX_THROW_SPEED) * 0.28);
604 vz = liftFor(hop);
605 flipOver = hop;
606 flipTurns = speed > 2200 ? 2 : speed > 600 ? 1 : 0;
607 }
608 if (!TUMBLE) flipTurns = 0;
609
610 return addBody(this.world!, {
611 tile: a.tile,
612 seat: a.seat,
613 x: thrown.x,
614 y: thrown.y,
615 vx,
616 vy,
617 z: 0,
618 vz,
619 angle: thrown.angle,
620 spin: thrown.spin,
621 flipTurns,
622 flipOver,
623 // It tumbles across its line of travel, the way a thrown tile does.
624 flipAxis: Math.atan2(vy, vx),
625 w: geo.tile.w,
626 h: geo.tile.h,
627 // It was drawn at hand size a moment ago; it shrinks on the way down.
628 scale: thrown.w / geo.tile.w,
629 });
630 }
631
632 // Off the end of the hand, not the middle of it — a tile is thrown from the
633 // hand holding it, which is the one at the right-hand end of the row.
634 const from = geo.throwFrom[a.seat] ?? geo.launch[a.seat] ?? centreOf(geo.pool);
635 const open = !LOB_WHEN_WALLED || this.canThrow(a.seat);
636 const target = this.aimFor(geo, a.seat, rng);
637 const flight = open ? 0.34 + rng() * 0.1 : PLACE_FLIGHT;
638 // A slide is aimed at where it comes to *rest*, since it keeps going after
639 // it lands and SLIDE_TIME is roughly how much further. A lob is aimed at
640 // where it touches *down*, because it has to come down on the inside of the
641 // wall — solving for the resting place lands it short, against the outside.
642 const reach = open ? flight + SLIDE_TIME : flight;
643
644 const vx = (target.x - from.x) / reach;
645 const vy = (target.y - from.y) / reach;
646 return addBody(this.world!, {
647 tile: a.tile,
648 seat: a.seat,
649 x: from.x,
650 y: from.y,
651 vx,
652 vy,
653 z: 0,
654 vz: liftFor(flight),
655 angle: (rng() - 0.5) * Math.PI,
656 spin: (rng() - 0.5) * (open ? 7 : 2.5),
657 // It turns over on its way in, whichever way it is going.
658 flipTurns: TUMBLE ? 1 : 0,
659 flipOver: flight,
660 flipAxis: Math.atan2(vy, vx),
661 w: geo.tile.w,
662 h: geo.tile.h,
663 });
664 }
665
666 /** Where a tile from this seat is headed, with a little scatter. */
667 private aimFor(geo: TableGeometry, seat: SeatId, rng: () => number): Point {
668 const base = aimAt(geo, seat) ?? centreOf(geo.pool);
669 const jitter = Math.min(geo.pool.w, geo.pool.h) * 0.22;
670 return { x: base.x + (rng() - 0.5) * jitter, y: base.y + (rng() - 0.5) * jitter };
671 }
672
673 /**
674 * A tile's own random stream. Keyed on the hand and its place in that seat's
675 * discards, so the same tile scatters the same way every time it is rebuilt —
676 * a refresh mid-hand puts the pile back exactly as it was.
677 */
678 private seeded(state: GameState, a: { seat: SeatId; index: number; tile: Tile }) {
679 return makeRng(state.handNumber * 7919 + a.seat * 977 + a.index * 31 + a.tile + 1);
680 }
681}