/** Each computer's character, fixed by its id (docs/09-game-engine.md §9). */ /** FNV-1a of the parts → [0, 1). Stable: the same bot decides the same way about the same thing. */ export function hash01(...parts: (string | number)[]): number { let h = 2166136261; const s = parts.join('\u0001'); for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619); return (h >>> 0) / 2 ** 32; } /** * Each computer's own pace (0.7 quick … 1.5 careful), fixed for the bot id so "컴퓨터 2" keeps its character for the * whole game, while a new room's 컴퓨터 2 (new id) may be different. */ export function botPace(bot: string): number { let h = 2166136261; for (let i = 0; i < bot.length; i++) h = Math.imul(h ^ bot.charCodeAt(i), 16777619); return 0.7 + ((h >>> 0) / 2 ** 32) * 0.8; } /** How well a computer plays (docs/09-game-engine.md §9.1). */ export type BotLevel = 'easy' | 'normal' | 'hard'; /** Share of computers at each level: 쉬움 35%, 보통 40%, 어려움 25%. */ export const BOT_LEVEL_ODDS: Readonly> = { easy: 0.35, normal: 0.4, hard: 0.25 }; const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; /** * The computer's own die roll in [0, 1), read straight from the random part of its id: the server makes bot ids as * `bot::` with OS secure randomness (`secureRandom`), so the first 6 random characters are 30 uniform bits. * Other ids (tests, hand-made) fall back to a hash. Fixed for the id, so the computer keeps its level after a restart. */ export function botDraw(bot: string): number { const u = bot.split(':')[2] ?? ''; if (!/^[0-9A-HJKMNP-TV-Z]{26}$/.test(u)) return hash01(bot, 'level'); let v = 0; for (const ch of u.slice(10, 16)) v = v * 32 + CROCKFORD.indexOf(ch); return v / 32 ** 6; } export function levelOf(draw: number): BotLevel { if (draw < BOT_LEVEL_ODDS.easy) return 'easy'; return draw < BOT_LEVEL_ODDS.easy + BOT_LEVEL_ODDS.normal ? 'normal' : 'hard'; } export interface BotPersona { /** 쉬움 / 보통 / 어려움: how often it slips in board games (`humanBest`). */ level: BotLevel; /** * 똑똑한 편(근거를 보고 추리, 존댓말) / 허술한 편(느낌과 분위기를 따름, 반말): half of the computers each, by the * same roll as the level — every 쉬움 is 허술, every 어려움 똑똑, 보통 split in two. */ smart: boolean; /** Reaction speed multiplier (`botPace`): 0.7 quick … 1.5 slow. */ pace: number; /** 0.5 … 1: how likely it speaks up. */ chatty: number; } export function botPersona(bot: string): BotPersona { const draw = botDraw(bot); return { level: levelOf(draw), smart: draw >= 0.5, pace: botPace(bot), chatty: 0.5 + hash01(bot, 'chatty') * 0.5 }; }