gitflarePublicPartly agent-ready40
shadow-fight-web-version
No description yet.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
132be7cOct 23, 2026
Make the player visible with a procedural 3D humanoid
132be7c// Deterministic bot controller for Shadow Fight.// Pure: same (state, difficulty, rng) => same inputs. No engine import; works on a// minimal state shape: { fighters: [p1, p2], bounds? } where each fighter has// x, y, health, maxHealth, facing, attacking | attack | state, onGround.// Missing fields fall back to safe defaults. export const EMPTY_INPUT = Object.freeze({ left: false, right: false, jump: false, punch: false, kick: false, block: false,}); /** Mulberry32 seeded RNG: returns a function producing floats in [0, 1). */export function createRng(seed = 1) { let a = seed >>> 0; return function rng() { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };} /** Difficulty presets; a level's botDifficulty may be a name, number 0..1 or object. */export const DIFFICULTIES = Object.freeze({ easy: { reaction: 0.45, aggression: 0.35, blockChance: 0.30, retreatHealth: 0.35, range: 70 }, medium: { reaction: 0.70, aggression: 0.55, blockChance: 0.60, retreatHealth: 0.25, range: 75 }, hard: { reaction: 0.90, aggression: 0.80, blockChance: 0.85, retreatHealth: 0.15, range: 80 },}); export function resolveDifficulty(difficulty) { if (difficulty && typeof difficulty === 'object') { return { ...DIFFICULTIES.medium, ...difficulty }; } if (typeof difficulty === 'number') { const d = Math.min(1, Math.max(0, difficulty)); return { reaction: 0.4 + 0.55 * d, aggression: 0.3 + 0.55 * d, blockChance: 0.25 + 0.65 * d, retreatHealth: 0.35 - 0.2 * d, range: 70 + 10 * d, }; } return DIFFICULTIES[difficulty] ?? DIFFICULTIES.medium;} function num(v, fallback) { return typeof v === 'number' && Number.isFinite(v) ? v : fallback;} export function isAttacking(f) { if (!f) return false; if (f.attacking === true) return true; if (typeof f.attack === 'string' && f.attack !== 'none') return true; if (typeof f.state === 'string' && (f.state === 'punch' || f.state === 'kick')) return true; return false;} /** * Decide inputs for fighter `botIndex` (default 1) in `state`. * @returns {{left,right,jump,punch,kick,block,intent}} input flags (+ debug intent) */export function decideBotInput(state, difficulty = 'medium', rng = createRng(1), botIndex = 1) { const fighters = (state && state.fighters) || []; const me = fighters[botIndex]; const foe = fighters[botIndex === 0 ? 1 : 0]; if (!me || !foe) return { ...EMPTY_INPUT, intent: 'idle' }; const cfg = resolveDifficulty(difficulty); const myX = num(me.x, 0), foeX = num(foe.x, 0); const dx = foeX - myX; const dist = Math.abs(dx); const dir = dx >= 0 ? 1 : -1; const maxHealth = num(me.maxHealth, 100) || 100; const healthRatio = num(me.health, maxHealth) / maxHealth; const inRange = dist <= cfg.range; const foeAttacking = isAttacking(foe); const foeClose = dist <= cfg.range * 1.4; // Draw rolls in a fixed order so decisions stay deterministic per call. const rReact = rng(), rAct = rng(), rMove = rng(), rJump = rng(); const input = { ...EMPTY_INPUT }; // 1. Defend: opponent is attacking nearby and we react in time. if (foeAttacking && foeClose && rReact < cfg.blockChance) { input.block = true; return { ...input, intent: 'block' }; } // 2. Retreat at low health unless cornered against known arena bounds. if (healthRatio <= cfg.retreatHealth && foeClose) { const bounds = state.bounds || state.arena || null; const minX = bounds ? num(bounds.minX ?? bounds.left, -Infinity) : -Infinity; const maxX = bounds ? num(bounds.maxX ?? bounds.right, Infinity) : Infinity; const awayX = myX - dir * 10; const cornered = awayX <= minX || awayX >= maxX; if (!cornered) { if (dir === 1) input.left = true; else input.right = true; if (rJump < 0.25) input.jump = true; return { ...input, intent: 'retreat' }; } } // 3. Attack when in range and the aggression roll succeeds. if (inRange) { if (rAct < cfg.aggression) { if (rMove < 0.6) input.punch = true; else input.kick = true; return { ...input, intent: 'attack' }; } if (rMove < cfg.reaction * 0.5) { input.block = true; return { ...input, intent: 'guard' }; } return { ...input, intent: 'wait' }; } // 4. Approach the opponent; occasionally jump in on harder settings. if (dir === 1) input.right = true; else input.left = true; if (rJump < cfg.aggression * 0.1 && dist < cfg.range * 3) input.jump = true; return { ...input, intent: 'approach' };} /** * Stateful wrapper: keeps the RNG and a reaction cadence so the bot "thinks" * every N frames (fewer on harder difficulty) instead of every frame. */export function createBot({ difficulty = 'medium', seed = 1, botIndex = 1 } = {}) { const cfg = resolveDifficulty(difficulty); const rng = createRng(seed); const thinkInterval = Math.max(1, Math.round(12 - cfg.reaction * 10)); let frame = 0; let last = { ...EMPTY_INPUT, intent: 'idle' }; return { difficulty: cfg, thinkInterval, update(state) { if (frame % thinkInterval === 0) last = decideBotInput(state, cfg, rng, botIndex); frame += 1; return last; }, reset() { frame = 0; last = { ...EMPTY_INPUT, intent: 'idle' }; }, };}