// Block Fighter AI — placement search over the headless engine. // Skill 1-10 controls lookahead, blunder rate, evaluation richness, and how // fast the AI physically executes its plan (one input per movePeriodMs). // Personality reshapes the evaluation so fighters play visibly differently: // balanced the tuned all-rounder // builder hoards giant power gems, holds crash gems back // rusher pops anything it can, constant small pressure // chainer sets up cascades and rewards chain depth // turtle keeps the stack low, purges counters early import { WIDTH, HEIGHT, SPAWN_COL, KIND, SUPER_COST, legalPlacements, simulatePlacement, makeRng, peekPiece, canUseSuper, colorGroups, } from './BlockFighterLogic.js'; export const PERSONALITIES = ['balanced', 'builder', 'rusher', 'chainer', 'turtle']; // Multipliers / additions applied over the skill profile's weights. const PERSONALITY_MODS = { balanced: {}, builder: { power: 1.9, cluster: 1.3, crash: 0.5, smallClear: 30 }, rusher: { attack: 1.3, cleared: 1.8, power: 0.4, cluster: 0.8, smallClear: -10 }, chainer: { crash: 1.3, cluster: 1.15, chainBonus: 28 }, turtle: { height: 1.6, spawnCol: 1.6, counters: 2.2, bumpy: 1.5, attack: 0.9 }, }; // skill → knobs (interpolated linearly between anchor rows) const SKILL_ANCHORS = [ { skill: 1, lookahead: 1, blunder: 0.45, movePeriodMs: 620, profile: 'basic' }, { skill: 3, lookahead: 1, blunder: 0.25, movePeriodMs: 460, profile: 'cluster' }, { skill: 5, lookahead: 1, blunder: 0.10, movePeriodMs: 330, profile: 'full' }, { skill: 7, lookahead: 2, blunder: 0.05, movePeriodMs: 230, profile: 'full' }, { skill: 9, lookahead: 2, blunder: 0.01, movePeriodMs: 150, profile: 'aggressive' }, { skill: 10, lookahead: 2, blunder: 0.00, movePeriodMs: 110, profile: 'aggressive' }, ]; function knobsFor(skill) { const s = Math.max(1, Math.min(10, skill)); let lo = SKILL_ANCHORS[0], hi = SKILL_ANCHORS[SKILL_ANCHORS.length - 1]; for (let i = 0; i < SKILL_ANCHORS.length - 1; i++) { if (s >= SKILL_ANCHORS[i].skill && s <= SKILL_ANCHORS[i + 1].skill) { lo = SKILL_ANCHORS[i]; hi = SKILL_ANCHORS[i + 1]; break; } } const t = hi.skill === lo.skill ? 0 : (s - lo.skill) / (hi.skill - lo.skill); return { lookahead: t < 0.5 ? lo.lookahead : hi.lookahead, blunder: lo.blunder + (hi.blunder - lo.blunder) * t, movePeriodMs: Math.round(lo.movePeriodMs + (hi.movePeriodMs - lo.movePeriodMs) * t), profile: t < 0.5 ? lo.profile : hi.profile, }; } const EVAL_WEIGHTS = { basic: { attack: 40, cleared: 6, cluster: 0, crash: 0, power: 0, height: 1.0, spawnCol: 2, counters: 0, bumpy: 0 }, cluster: { attack: 40, cleared: 6, cluster: 2.5, crash: 4, power: 0, height: 1.0, spawnCol: 2, counters: 0.5, bumpy: 0.5 }, full: { attack: 45, cleared: 5, cluster: 3, crash: 6, power: 5, height: 1.1, spawnCol: 3, counters: 1, bumpy: 1 }, aggressive: { attack: 60, cleared: 4, cluster: 3.5, crash: 8, power: 7, height: 1.2, spawnCol: 4, counters: 1.5, bumpy: 1 }, }; function weightsFor(profile, personality) { const base = { ...EVAL_WEIGHTS[profile], smallClear: 0, chainBonus: 0 }; const mods = PERSONALITY_MODS[personality] ?? {}; for (const [k, v] of Object.entries(mods)) { if (k === 'smallClear' || k === 'chainBonus') base[k] = v; else base[k] *= v; } return base; } export function createAI({ skill = 5, speed = 1, seed = 1, personality = 'balanced', useSupers = true } = {}) { const knobs = knobsFor(skill); // higher level speed also quickens the AI's hands a bit knobs.movePeriodMs = Math.round(knobs.movePeriodMs * (1 - 0.08 * (speed - 1))); const pers = PERSONALITY_MODS[personality] ? personality : 'balanced'; return { skill, knobs, personality: pers, weights: weightsFor(knobs.profile, pers), rng: makeRng(seed), plan: null, // { col, orient } nextActionAt: 0, fresh: false, // plan not yet started (for hesitation) // low skill fighters pause to "think" before moving a new piece hesitateMs: skill <= 5 ? (6 - skill) * 90 : 0, useSoftDrop: skill >= 4, useHardDrop: skill >= 7, useSupers: useSupers && skill >= 3, }; } // ── Board evaluation ───────────────────────────────────────────────────────── function evaluateBoard(player, w) { const { board } = player; let score = 0; const heights = new Array(WIDTH).fill(0); for (let c = 0; c < WIDTH; c++) { for (let r = 0; r < HEIGHT; r++) { if (board[r][c]) { heights[c] = HEIGHT - r; break; } } } for (let c = 0; c < WIDTH; c++) { const weight = c === SPAWN_COL ? w.spawnCol : w.height; score -= weight * heights[c] * heights[c] * 0.25; } for (let c = 0; c < WIDTH - 1; c++) score -= w.bumpy * Math.abs(heights[c] - heights[c + 1]); let cluster = 0, counters = 0, crashPotential = 0; for (let r = 0; r < HEIGHT; r++) { for (let c = 0; c < WIDTH; c++) { const cell = board[r][c]; if (!cell) continue; if (cell.kind === KIND.COUNTER) { counters += 1; continue; } const right = c + 1 < WIDTH ? board[r][c + 1] : null; const down = r + 1 < HEIGHT ? board[r + 1][c] : null; if (right && right.kind !== KIND.COUNTER && right.color === cell.color) cluster += 1; if (down && down.kind !== KIND.COUNTER && down.color === cell.color) cluster += 1; if (cell.kind === KIND.CRASH) { // size of the same-color group this crash gem touches let touching = 0; for (const [dr, dc] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) { const n = board[r + dr]?.[c + dc]; if (n && n.color === cell.color && n.kind !== KIND.COUNTER) touching += 1; } crashPotential += touching; } } } score += w.cluster * cluster + w.crash * crashPotential - w.counters * counters; let power = 0; for (const gem of player.powerGems.values()) power += Math.pow(gem.w * gem.h, 1.3); score += w.power * power; return score; } function scorePlacement(result, w) { if (!result) return -Infinity; if (result.died) return -1e9; let s = w.attack * result.attack + w.cleared * result.cleared + evaluateBoard(result.player, w); if (result.cleared > 0 && result.cleared < 5 && !result.powerCells) s -= w.smallClear; if (result.chain > 1) s += w.chainBonus * (result.chain - 1) * (result.chain - 1); return s; } // ── Planning ───────────────────────────────────────────────────────────────── export function planPlacement(ai, match, pIdx) { const player = match.players[pIdx]; if (!player.piece) return null; const w = ai.weights ?? EVAL_WEIGHTS[ai.knobs.profile]; const options = []; for (const { col, orient } of legalPlacements(player)) { const result = simulatePlacement(match, pIdx, col, orient); if (!result) continue; let score = scorePlacement(result, w); if (ai.knobs.lookahead >= 2 && score > -1e8) { // probe the next shared piece on the resulting board const nextProto = peekPiece(match, player.pieceIndex); if (nextProto) { const ghost = { players: pIdx === 0 ? [result.player, match.players[1]] : [match.players[0], result.player], over: false, winner: null, headless: true, }; result.player.piece = { a: { ...nextProto.a }, b: { ...nextProto.b }, row: 1, col: SPAWN_COL, orient: 0 }; let best2 = -Infinity; for (const p2 of legalPlacements(result.player)) { const r2 = simulatePlacement(ghost, pIdx, p2.col, p2.orient); if (!r2) continue; const s2 = scorePlacement(r2, w); if (s2 > best2) best2 = s2; } result.player.piece = null; if (best2 > -Infinity) score = score * 0.6 + best2 * 0.4; } } options.push({ col, orient, score }); } if (!options.length) { ai.plan = null; return null; } options.sort((a, b) => b.score - a.score); let pick = options[0]; if (ai.rng() < ai.knobs.blunder) { pick = options[Math.floor(ai.rng() * Math.min(options.length, 5))]; } ai.plan = { col: pick.col, orient: pick.orient }; ai.nextActionAt = 0; ai.fresh = true; return ai.plan; } // ── Super moves ────────────────────────────────────────────────────────────── // Returns the super tier to fire now, or null. Call between inputs; after an // Overdrive the scene should re-plan (the falling piece changed). export function decideSuper(ai, match, pIdx) { if (!ai.useSupers) return null; const player = match.players[pIdx]; if (!player.piece) return null; let counters = 0; let maxH = 0; for (let c = 0; c < WIDTH; c++) { for (let r = 0; r < HEIGHT; r++) if (player.board[r][c]) { maxH = Math.max(maxH, HEIGHT - r); break; } } for (const row of player.board) for (const cell of row) if (cell?.kind === KIND.COUNTER) counters += 1; const turtle = ai.personality === 'turtle'; if (canUseSuper(match, pIdx, 1)) { const needCounters = turtle ? 3 : 6; const needHeight = turtle ? 5 : 8; // hoard toward Overdrive unless under real pressure const saving = !turtle && player.super >= SUPER_COST[3] - 40 && maxH < 10; if (counters >= needCounters && maxH >= needHeight && !saving) return 1; } if (canUseSuper(match, pIdx, 3)) { const biggest = colorGroups(player.board)[0]?.size ?? 0; const need = ai.personality === 'rusher' ? 6 : ai.personality === 'builder' ? 12 : 8; if (biggest >= need) return 3; } return null; } // ── Execution: one input per movePeriodMs ──────────────────────────────────── export function nextAction(ai, match, pIdx, nowMs) { const player = match.players[pIdx]; if (!player.piece || !ai.plan) return null; if (ai.fresh) { ai.fresh = false; if (ai.hesitateMs) { ai.nextActionAt = nowMs + ai.hesitateMs * (0.4 + ai.rng() * 1.2); return null; } } if (nowMs < ai.nextActionAt) return null; const piece = player.piece; let action = null; if (piece.orient !== ai.plan.orient) { const cwDist = (ai.plan.orient - piece.orient + 4) % 4; action = cwDist <= 2 ? 'rotateCW' : 'rotateCCW'; } else if (piece.col > ai.plan.col) { action = 'left'; } else if (piece.col < ai.plan.col) { action = 'right'; } else if (ai.useHardDrop) { action = 'hardDrop'; } else if (ai.useSoftDrop) { action = 'softDrop'; } if (action) ai.nextActionAt = nowMs + ai.knobs.movePeriodMs * (0.8 + ai.rng() * 0.4); return action; }