fertig-classic-games/src/games/holdem/HoldemTable.js

347 lines
15 KiB
JavaScript

// Texas Hold 'Em table layout — Phaser-free so tools/verifyHoldem.js can test it.
//
// A casino poker-room oval: the dealer sits at the far (top) centre, players
// wrap the other sides, the human at the near (bottom) centre. Uses the shared
// casino perspective (makeProjection) with a milder tilt than the half-moons.
//
// Table space: the oval is a racetrack superellipse centred at (0, 0.5) with
// half-width 1 (u) and half-depth 0.5 (v). rim(φ, k) is the point at angle φ
// (0 = toward the human, +φ toward screen-right) scaled by k of the rim radius.
import { makeProjection, DOCK } from '../../ui/casino/CasinoGeometry.js';
export { DOCK };
export const VIEW = { cx: 960, yFar: 250, yNear: 780, sFar: 0.78, halfW: 760, squash: 0.9 };
export const P = makeProjection(VIEW);
export const CARD_W = 96;
export const CARD_H = 134;
const RAD = Math.PI / 180;
const EXP = 2.4; // superellipse exponent — 2 = ellipse, higher = racetrack
const HALF_U = 1, HALF_V = 0.5, CENTER_V = 0.5;
export const FELT_K = 0.985;
export const WOOD_K = 1.045; // wooden racetrack (drink rail) outer edge
export const RAIL_K = 1.16; // padded rail outer edge
export const RAIL_FACE_PX = 30;
export const RING_K = 0.6; // the printed gold betting line: bets inside, cards + stacks outside
export const CHIP = 44; // chip diameter at perspective scale 1 (stacks + bets)
const RING_MARGIN = 0.02, FELT_MARGIN = 0.022;
export function rimRadius(phiDeg) {
const t = phiDeg * RAD;
const a = Math.abs(Math.sin(t)) / HALF_U, b = Math.abs(Math.cos(t)) / HALF_V;
return 1 / Math.pow(a ** EXP + b ** EXP, 1 / EXP);
}
// Table-space point on the rim at angle φ, scaled by k.
export function rimUV(phiDeg, k = 1) {
const t = phiDeg * RAD, r = rimRadius(phiDeg) * k;
return { u: r * Math.sin(t), v: CENTER_V + r * Math.cos(t) };
}
export function rim(phiDeg, k = 1) {
const { u, v } = rimUV(phiDeg, k);
return P.project(u, v);
}
export function rimPoints(k, steps = 180) {
const pts = [];
for (let i = 0; i < steps; i++) pts.push(rim((360 * i) / steps, k));
return pts;
}
// Is a screen point on the felt (inside the rim)?
export function onTableFelt(pt) {
const { u, v } = P.unproject(pt.x, pt.y);
const du = Math.abs(u) / HALF_U, dv = Math.abs(v - CENTER_V) / HALF_V;
return du ** EXP + dv ** EXP <= FELT_K ** EXP;
}
// Superellipse level of a screen point: 1 = the felt edge, RING_K = the betting line.
export function levelOf(pt) {
const { u, v } = P.unproject(pt.x, pt.y);
return Math.pow((Math.abs(u) / HALF_U) ** EXP + (Math.abs(v - CENTER_V) / HALF_V) ** EXP, 1 / EXP);
}
const inBand = (pt) => { const l = levelOf(pt); return l >= RING_K + RING_MARGIN && l <= FELT_K - FELT_MARGIN; };
const insideRing = (pt) => levelOf(pt) <= RING_K - RING_MARGIN;
// Screen corners of a card pose (container scale (s, s·squash), then rotation).
export function cardCorners(pose) {
const cos = Math.cos(pose.rot), sin = Math.sin(pose.rot);
return [[-1, -1], [1, -1], [1, 1], [-1, 1]].map(([a, b]) => {
const lx = a * (CARD_W / 2) * pose.s, ly = b * (CARD_H / 2) * pose.s * VIEW.squash;
return { x: pose.x + lx * cos - ly * sin, y: pose.y + lx * sin + ly * cos };
});
}
// What a chip stack covers on the felt (up to two columns + the amount pill).
export function stackFootprint(pt, s) {
const size = CHIP * s;
return [
{ x: pt.x - size * 1.1, y: pt.y }, { x: pt.x + size * 1.1, y: pt.y },
{ x: pt.x, y: pt.y - size * 0.3 }, { x: pt.x, y: pt.y + size * 0.95 },
{ x: pt.x - size * 0.9, y: pt.y + size * 0.8 }, { x: pt.x + size * 0.9, y: pt.y + size * 0.8 },
];
}
// What a single bet stack covers (one column + an amount pill up to "$1,250" on its right).
export function betFootprint(pt, s) {
const size = CHIP * s;
return [
{ x: pt.x - size * 0.6, y: pt.y }, { x: pt.x + size * 0.6, y: pt.y },
{ x: pt.x, y: pt.y - size * 0.3 }, { x: pt.x, y: pt.y + size * 0.3 },
{ x: pt.x + size * 2.6, y: pt.y }, { x: pt.x + size * 2.6, y: pt.y + size * 0.25 },
];
}
// Offset a table-space point along the rim tangent at φ (+ = clockwise on screen).
function alongTangent(uv, phiDeg, d) {
const t = phiDeg * RAD;
return { u: uv.u + Math.cos(t) * d, v: uv.v - Math.sin(t) * d };
}
// Rotation whose card "down" points from `from` toward `to` (screen points).
const aimAt = (from, to) => Math.atan2(-(to.x - from.x), to.y - from.y);
// Rotation that lines a card up with the rail: its "down" follows the rail's
// outward normal (so the hand sits squarely in the band outside the betting line).
export function railRotation(phiDeg, k = 0.8) {
const a = rim(phiDeg - 0.5, k), b = rim(phiDeg + 0.5, k);
let nx = -(b.y - a.y), ny = b.x - a.x;
const c = rim(phiDeg, k), o = rim(phiDeg, k + 0.1);
if (nx * (o.x - c.x) + ny * (o.y - c.y) < 0) { nx = -nx; ny = -ny; }
return Math.atan2(-nx, ny);
}
const lerpAngle = (a, b, t) => {
let d = b - a;
while (d > Math.PI) d -= Math.PI * 2;
while (d < -Math.PI) d += Math.PI * 2;
return a + d * t;
};
// ── Seats ────────────────────────────────────────────────────────────────────
// Opponents fill clockwise in logic seat order (seat 1 = just left of the
// human): left side bottom → top, then right side top → bottom.
const SIDE_NEAR = 50, SIDE_FAR = 140;
export function seatAngles(nOpp) {
const L = Math.ceil(nOpp / 2), R = nOpp - L;
const side = (count) => (count === 1 ? [90] : Array.from({ length: count }, (_, i) => SIDE_NEAR + (i * (SIDE_FAR - SIDE_NEAR)) / (count - 1)));
const left = side(L).map(a => -a); // -50 … -140
const right = R ? side(R).reverse() : []; // 140 … 50
return [...left, ...right];
}
export const HUMAN = { maxScale: 1.0, gap: 104, minScale: 0.9 };
export const OPP = { maxScale: 0.9, gap: 72, minScale: 0.78 };
// Fit a hand: both hole cards wholly between the betting line and the rail,
// turned toward the player (`seatPt`, where their portrait sits). The cards
// face the player fully when they fit at a decent size; otherwise they ease
// toward the rail's own direction (which always fits best) and shrink as needed.
function fitAt(phi, cfg, aim) {
for (let scale = cfg.maxScale; scale >= 0.5; scale -= 0.02) {
const fits = [];
for (let k = FELT_K; k >= RING_K; k -= 0.004) {
const c = rim(phi, k);
const rot = aim(c, k);
const s = c.s * scale;
const cos = Math.cos(rot), sin = Math.sin(rot);
const cards = [-1, 1].map(side => {
const lx = side * (cfg.gap / 2) * s;
return { x: c.x + lx * cos, y: c.y + lx * sin, rot, s };
});
if (cards.every(cd => cardCorners(cd).every(inBand))) fits.push({ k, c, cards, scale });
}
if (fits.length) return fits[Math.floor(fits.length / 2)];
}
return null;
}
function fitCards(phi, cfg, seatPt) {
if (phi === 0) return fitAt(phi, cfg, () => 0);
let best = null;
for (let t = 1; t >= -0.001; t -= 0.1) {
const fit = fitAt(phi, cfg, (c, k) => lerpAngle(railRotation(phi, k), aimAt(c, seatPt), t));
if (fit && fit.scale >= cfg.minScale) return { ...fit, toward: t };
if (fit && (!best || fit.scale > best.scale)) best = { ...fit, toward: t };
}
if (best) return best;
const k = (RING_K + FELT_K) / 2, c = rim(phi, k), rot = railRotation(phi, k);
return { k, c, scale: 0.5, toward: 0, cards: [-1, 1].map(side => ({ x: c.x + side * 20, y: c.y, rot, s: c.s * 0.5 })) };
}
// SB / BB marker: beside the bet (left of it first — the amount pill is on the
// right), inside the betting line, clear of the bet, the board and the pot.
function blindSpot(bet) {
const r = 16 * bet.s, dist = CHIP * bet.s * 1.05;
const keepOut = [...[0, 1, 2, 3, 4].map(communityPose), potPose(0)];
for (const deg of [180, 160, 200, 140, 220, 120, 240, 100, 260, 90, 270]) {
const a = (deg * Math.PI) / 180;
const p = { x: bet.x + Math.cos(a) * dist, y: bet.y + Math.sin(a) * dist * VIEW.squash, s: bet.s };
const edge = [[-r, 0], [r, 0], [0, -r], [0, r]].map(([dx, dy]) => ({ x: p.x + dx, y: p.y + dy }));
if (!edge.every(insideRing)) continue;
if (!betFootprint(bet, bet.s).every(q => Math.hypot(q.x - p.x, q.y - p.y) > r + 6)) continue;
if (!keepOut.every(q => Math.hypot(q.x - p.x, q.y - p.y) > 62)) continue;
return p;
}
return { x: bet.x - dist, y: bet.y, s: bet.s };
}
// Deepest spot on the seat line where a bet (and its amount pill) stays inside the line.
function fitBet(phi) {
for (let k = RING_K - 0.05; k >= 0.3; k -= 0.005) {
const p = rim(phi, k);
if (betFootprint(p, p.s).every(insideRing)) return p;
}
return rim(phi, 0.4);
}
// Cards, bet spot, badge and portrait for one seat (stack/button are placed
// table-wide by tableLayout so neighbours never collide).
function baseSeat(seat, nOpp) {
const phi = seat === 0 ? 0 : seatAngles(nOpp)[seat - 1];
const cfg = seat === 0 ? HUMAN : OPP;
let portrait;
if (seat === 0) portrait = { x: 160, y: 1013, r: 50 };
else {
const p = rim(phi, 1.32);
portrait = { x: Math.max(64, Math.min(1856, p.x)), y: Math.min(812, p.y), r: 52 };
}
const fit = fitCards(phi, cfg, portrait);
const centre = fit.c;
const cards = fit.cards;
const rot = cards[0].rot;
const s = cards[0].s;
const bet = fitBet(phi);
const blind = blindSpot(bet);
const badge = rim(phi, RING_K);
return {
seat, phi, cfg, centre, cards, rot, s, portrait, cardK: fit.k, cardScale: fit.scale, toward: fit.toward ?? 1, bet, blind, badge,
};
}
// Does a footprint (polygon-ish point set) stay clear of a card's rectangle?
function clearOfCards(points, cards, pad) {
return cards.every(cd => {
const cos = Math.cos(cd.rot), sin = Math.sin(cd.rot);
const hw = (CARD_W / 2) * cd.s + pad, hh = (CARD_H / 2) * cd.s * VIEW.squash + pad;
return points.every(p => {
const dx = p.x - cd.x, dy = p.y - cd.y;
const lx = dx * cos + dy * sin, ly = -dx * sin + dy * cos;
return Math.abs(lx) > hw || Math.abs(ly) > hh;
});
});
}
// Candidate spots beside a seat's cards, wholly outside the betting line
// (or, for the dealer button only, `inner` also allows a strip just inside it).
function candidates(b, footprint, pad, inner = false) {
const out = [];
const kMin = inner ? RING_K - 0.14 : RING_K + 0.03;
for (let k = FELT_K - 0.03; k >= kMin; k -= 0.02) {
for (let d = 0.06; d <= 0.3; d += 0.02) {
for (const sign of [-1, 1]) {
const uv = alongTangent(rimUV(b.phi, k), b.phi, sign * d);
const pt = P.project(uv.u, uv.v);
const fp = footprint(pt, pt.s);
const placeOk = inner
? fp.every(p => levelOf(p) <= FELT_K - FELT_MARGIN) && !fp.some(p => Math.abs(levelOf(p) - RING_K) < 0.012)
&& betFootprint(b.bet, b.bet.s).every(q => Math.hypot(q.x - pt.x, q.y - pt.y) > 34 * pt.s)
: fp.every(inBand);
if (placeOk && clearOfCards([pt, ...fp], b.cards, pad)) out.push(pt);
}
}
}
return out;
}
const buttonFootprint = (pt, s) => {
const r = 22 * s;
return [{ x: pt.x - r, y: pt.y }, { x: pt.x + r, y: pt.y }, { x: pt.x, y: pt.y - r * 0.9 }, { x: pt.x, y: pt.y + r * 0.9 }];
};
// Pick the candidate that is clear of everything (up to `enough` px counts as
// fully clear), and among those the one nearest the player's own cards.
function bestSpot(cands, avoid, home, enough = 70) {
let best = null, bestScore = -Infinity;
for (const c of cands) {
const clear = Math.min(enough, ...avoid.map(a => Math.hypot(a.x - c.x, a.y - c.y)));
const score = clear * 1000 - Math.hypot(home.x - c.x, home.y - c.y);
if (score > bestScore) { bestScore = score; best = c; }
}
return best;
}
const layoutCache = new Map();
export function tableLayout(nOpp) {
if (layoutCache.has(nOpp)) return layoutCache.get(nOpp);
const seats = Array.from({ length: nOpp + 1 }, (_, seat) => baseSeat(seat, nOpp));
const allCards = seats.flatMap(b => b.cards);
const fixed = [
...seats.flatMap(b => [...b.cards, ...b.cards.flatMap(cardCorners), b.bet, b.badge]),
deckSpot(), muckSpot(), dealerSpot(), potPose(0), potPose(1), potPose(2),
// the dealer's deck and muck pile are card-sized (the muck spreads a little)
...[deckSpot(), muckSpot()].flatMap(d => cardCorners({ ...d, rot: 0, s: d.s * 1.25 })),
...[0, 1, 2, 3, 4].map(communityPose),
];
const placed = [];
const fallback = (b, k, d) => {
const uv = alongTangent(rimUV(b.phi, k), b.phi, d);
return P.project(uv.u, uv.v);
};
// Place the most cramped seats first: the human, then the far corners
// (squeezed between the dealer and their neighbour), working toward the sides.
const order = [...seats].sort((a, b) => (a.seat === 0 ? -1 : b.seat === 0 ? 1 : Math.abs(b.phi) - Math.abs(a.phi)));
for (const b of order) {
const avoid = () => [...fixed, ...placed.flatMap(p => [p, ...stackFootprint(p, p.s)])];
let stackC = candidates({ ...b, cards: allCards }, stackFootprint, 6);
if (b.seat === 0) stackC = stackC.filter(p => p.x < b.centre.x); // the human's chips sit on the left
b.stack = bestSpot(stackC, avoid(), b.centre) ?? fallback(b, (RING_K + FELT_K) / 2, -0.25);
placed.push(b.stack);
let buttonC = candidates({ ...b, cards: allCards }, buttonFootprint, 4, true)
.filter(p => !stackFootprint(b.stack, b.stack.s).some(q => Math.hypot(q.x - p.x, q.y - p.y) < 30 * p.s));
if (b.seat === 0) buttonC = buttonC.filter(p => p.x > b.centre.x);
b.button = bestSpot(buttonC, avoid(), b.centre, 66) ?? fallback(b, RING_K + 0.06, 0.15);
placed.push(b.button);
}
layoutCache.set(nOpp, seats);
return seats;
}
export function seatLayout(seat, nOpp) {
const b = tableLayout(nOpp)[seat];
return { seat: b.seat, phi: b.phi, centre: b.centre, cards: b.cards, stack: b.stack, button: b.button,
bet: b.bet, blind: b.blind, badge: b.badge, portrait: { ...b.portrait }, rot: b.rot, s: b.s };
}
// ── Centre: community cards and pots ─────────────────────────────────────────
export const COMMUNITY_V = 0.5;
export const COMMUNITY_GAP = 106;
export function communityPose(i) {
const c = P.project(0, COMMUNITY_V);
return { x: c.x + (i - 2) * COMMUNITY_GAP * c.s, y: c.y, rot: 0, s: c.s };
}
// Main pot (0) sits between the board and the dealer; side pots fan out beside it.
export const POT_V = 0.335;
const POT_OFFSETS = [0, -1, 1, -2, 2];
export function potPose(i) {
const c = P.project(0, POT_V);
return { x: c.x + POT_OFFSETS[Math.min(i, POT_OFFSETS.length - 1)] * 112 * c.s, y: c.y, s: c.s };
}
// ── Dealer ───────────────────────────────────────────────────────────────────
export const DEALER_V = 0.07;
export function dealerSpot() { return P.project(0, DEALER_V); }
export function deckSpot() { const p = P.project(-0.085, DEALER_V); return { ...p, s: p.s * 0.85 }; }
export function muckSpot() { const p = P.project(0.085, DEALER_V); return { ...p, s: p.s * 0.85 }; }
export const DEALER_PLATE = { x: 960, y: 168 };
// ── HUD ──────────────────────────────────────────────────────────────────────
export const PANEL = { x: 1452, y: 12, w: 456, h: 118 }; // tournament panel (top-right)