Skip to content
Make the player visible with a procedural 3D humanoid132be7c
JavaScript105 lines4.2 KB
import { test } from 'node:test';import assert from 'node:assert/strict';import {  decideBotInput, createBot, createRng, resolveDifficulty, DIFFICULTIES, isAttacking,} from './bot.mjs'; function fighter(over = {}) {  return { x: 0, y: 0, health: 100, maxHealth: 100, facing: 1, attacking: false, onGround: true, ...over };}function state(p1, p2, extra = {}) {  return { fighters: [fighter(p1), fighter(p2)], ...extra };}const always = (v) => () => v; // constant rng test('createRng is deterministic and in [0,1)', () => {  const a = createRng(42), b = createRng(42);  const seqA = Array.from({ length: 5 }, () => a());  const seqB = Array.from({ length: 5 }, () => b());  assert.deepEqual(seqA, seqB);  for (const v of seqA) assert.ok(v >= 0 && v < 1);  assert.notDeepEqual(seqA, Array.from({ length: 5 }, createRng(7)));}); test('resolveDifficulty accepts names, numbers and objects', () => {  assert.equal(resolveDifficulty('hard'), DIFFICULTIES.hard);  assert.equal(resolveDifficulty('bogus'), DIFFICULTIES.medium);  const n = resolveDifficulty(1);  assert.ok(n.aggression > resolveDifficulty(0).aggression);  assert.equal(resolveDifficulty({ range: 500 }).range, 500);}); test('approaches opponent when far away (both directions)', () => {  const r = decideBotInput(state({ x: 100 }, { x: 500 }), 'medium', always(0.99));  assert.equal(r.intent, 'approach');  assert.equal(r.left, true); assert.equal(r.right, false);  const l = decideBotInput(state({ x: 900 }, { x: 500 }), 'medium', always(0.99));  assert.equal(l.right, true); assert.equal(l.left, false);}); test('attacks when in range and aggression roll succeeds', () => {  const r = decideBotInput(state({ x: 500 }, { x: 540 }), 'medium', always(0.1));  assert.equal(r.intent, 'attack');  assert.ok(r.punch || r.kick);  assert.ok(!(r.punch && r.kick));  // kick branch when the move roll is high but aggression succeeds (hard: aggression 0.8)  const k = decideBotInput(state({ x: 500 }, { x: 540 }), 'hard', always(0.7));  assert.equal(k.kick, true);}); test('blocks when opponent attacks nearby', () => {  const r = decideBotInput(state({ x: 500, attacking: true }, { x: 550 }), 'hard', always(0.1));  assert.equal(r.intent, 'block');  assert.equal(r.block, true);  assert.equal(r.punch, false);  assert.equal(isAttacking({ attack: 'kick' }), true);  assert.equal(isAttacking({ state: 'punch' }), true);  assert.equal(isAttacking({ attack: 'none' }), false);}); test('does not block a far-away attack', () => {  const r = decideBotInput(state({ x: 0, attacking: true }, { x: 600 }), 'hard', always(0.1));  assert.equal(r.intent, 'approach');}); test('retreats at low health when opponent is close', () => {  const r = decideBotInput(state({ x: 500 }, { x: 560, health: 10 }), 'medium', always(0.99));  assert.equal(r.intent, 'retreat');  assert.equal(r.right, true); assert.equal(r.left, false);}); test('does not retreat into a wall; fights instead', () => {  const r = decideBotInput(    state({ x: 500 }, { x: 560, health: 10 }, { bounds: { minX: 0, maxX: 565 } }),    'medium', always(0.1));  assert.notEqual(r.intent, 'retreat');  assert.equal(r.intent, 'attack');}); test('is deterministic for the same seed and state', () => {  const s = state({ x: 300 }, { x: 350 });  const a = decideBotInput(s, 'medium', createRng(5));  const b = decideBotInput(s, 'medium', createRng(5));  assert.deepEqual(a, b);}); test('supports controlling fighter 0 and tolerates missing fighters', () => {  const r = decideBotInput(state({ x: 100 }, { x: 500 }), 'easy', always(0.99), 0);  assert.equal(r.right, true);  assert.equal(decideBotInput({ fighters: [] }).intent, 'idle');  assert.equal(decideBotInput(null).intent, 'idle');}); test('createBot thinks at a cadence and is reproducible', () => {  const s = state({ x: 100 }, { x: 500 });  const b1 = createBot({ difficulty: 'easy', seed: 3 });  const b2 = createBot({ difficulty: 'easy', seed: 3 });  assert.ok(b1.thinkInterval > createBot({ difficulty: 'hard' }).thinkInterval);  const out1 = Array.from({ length: 30 }, () => b1.update(s));  const out2 = Array.from({ length: 30 }, () => b2.update(s));  assert.deepEqual(out1, out2);  assert.equal(out1[0].left, true);  b1.reset();  assert.deepEqual(b1.update(s), out1[0]);});