ChimeraBot_garage: M7.5 — virtual-gun tournament, Dodger-v2, discriminating bench
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
## Construction test for the Dodger archetype (tests/archetypes/Dodger).
|
||||
## The bot's run loop contains no decision logic: it calls `dodgeDecision` and
|
||||
## applies the result, so pinning the spec function's outputs on synthetic
|
||||
## inputs pins the bot's behaviour by construction. Pure Nim, no Java.
|
||||
## Construction + behaviour tests for the Dodger archetype
|
||||
## (tests/archetypes/Dodger; M7.5/G8).
|
||||
##
|
||||
## The bot's run loop contains no decision logic: it calls `dodgeDecision` /
|
||||
## `gunDecision` and applies the result, so pinning the spec functions' outputs on
|
||||
## synthetic inputs pins the bot's behaviour by construction. The last two blocks
|
||||
## go further:
|
||||
##
|
||||
## * a **dodge-rate matrix** runs the documented decision through the physics
|
||||
## mirror and fires head-on-gun shots at the resulting trajectory, asserting
|
||||
## a dodge rate >= 60% for every (shooter, start) pair;
|
||||
## * a **fire-back** check asserts the head-on gun fires when aligned and cold.
|
||||
##
|
||||
## Pure Nim, no Java.
|
||||
|
||||
import std/[math]
|
||||
import std/[math, strutils]
|
||||
import chimera/physics
|
||||
import dodger_spec
|
||||
|
||||
var failed = 0
|
||||
@@ -24,19 +35,20 @@ block:
|
||||
waveETA(w, 160.0, 0.0, 20) < 0.0)
|
||||
|
||||
block:
|
||||
echo "dodger 2: far wave -> patrol"
|
||||
echo "dodger 2: far wave -> centre patrol (never charge the gun line)"
|
||||
let w = DodgeWave(originX: 0.0, originY: 0.0, fireTick: 0, bulletSpeed: 16.0)
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 1600.0, y: 0.0, heading: 0.0), 0,
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 1600.0, y: 0.0, heading: 0.0), 7,
|
||||
800.0, 600.0, 1.0)
|
||||
check("not dodging", not c.dodging)
|
||||
check("patrol speed", approx(c.targetSpeed, PatrolSpeed))
|
||||
check("no turn", approx(c.turnRate, 0.0))
|
||||
check("turns toward the centre (not straight)", abs(c.turnRate) > 0.0,
|
||||
$c.turnRate)
|
||||
check("strafe sign carried", approx(c.strafeSign, 1.0))
|
||||
|
||||
block:
|
||||
echo "dodger 3: spent wave -> patrol"
|
||||
echo "dodger 3: spent wave -> centre patrol, sign retained"
|
||||
let w = DodgeWave(originX: 0.0, originY: 0.0, fireTick: 100, bulletSpeed: 16.0)
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 160.0, y: 0.0, heading: 0.0), 0,
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 160.0, y: 0.0, heading: 0.0), 7,
|
||||
800.0, 600.0, -1.0)
|
||||
check("not dodging", not c.dodging)
|
||||
check("strafe sign retained", approx(c.strafeSign, -1.0))
|
||||
@@ -44,32 +56,36 @@ block:
|
||||
block:
|
||||
echo "dodger 4: close wave -> strafe perpendicular"
|
||||
let w = DodgeWave(originX: 0.0, originY: 300.0, fireTick: 0, bulletSpeed: 16.0)
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 160.0, y: 300.0, heading: 0.0), 0,
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 160.0, y: 300.0, heading: 0.0), 7,
|
||||
800.0, 600.0, 1.0)
|
||||
check("dodging", c.dodging)
|
||||
check("full throttle", approx(c.targetSpeed, StrafeSpeed))
|
||||
check("turns toward +90 bearing (clamped to 10)", approx(c.turnRate, 10.0),
|
||||
$c.turnRate)
|
||||
check("strafe sign +1", approx(c.strafeSign, 1.0))
|
||||
let c2 = dodgeDecision(w, DodgeOwn(x: 160.0, y: 300.0, heading: 90.0), 0,
|
||||
let c2 = dodgeDecision(w, DodgeOwn(x: 160.0, y: 300.0, heading: 90.0), 7,
|
||||
800.0, 600.0, 1.0)
|
||||
check("already perpendicular -> no turn", approx(c2.turnRate, 0.0), $c2.turnRate)
|
||||
check("already perpendicular: centre-pull turns back toward the centre",
|
||||
approx(c2.turnRate, -10.0), $c2.turnRate)
|
||||
|
||||
block:
|
||||
echo "dodger 5: corner flip near a wall"
|
||||
let w = DodgeWave(originX: 0.0, originY: 0.0, fireTick: 0, bulletSpeed: 16.0)
|
||||
let w = DodgeWave(originX: 0.0, originY: 0.0, fireTick: 5, bulletSpeed: 16.0)
|
||||
check("nearWall true inside the band", nearWall(50.0, 300.0, 800.0, 600.0))
|
||||
check("nearWall false in the open", not nearWall(400.0, 300.0, 800.0, 600.0))
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 50.0, y: 0.0, heading: 0.0), 0,
|
||||
800.0, 600.0, 1.0)
|
||||
check("strafe sign flipped", approx(c.strafeSign, -1.0), $c.strafeSign)
|
||||
check("turns toward -90 bearing", approx(c.turnRate, -10.0), $c.turnRate)
|
||||
let c2 = dodgeDecision(w, DodgeOwn(x: 50.0, y: 0.0, heading: 0.0), 0,
|
||||
800.0, 600.0, -1.0)
|
||||
check("flip is an involution on the sign", approx(c2.strafeSign, 1.0))
|
||||
# strafing SOUTH (into the bottom wall we are near) is flipped to NORTH
|
||||
let c = dodgeDecision(w, DodgeOwn(x: 50.0, y: 0.0, heading: 0.0), 7,
|
||||
800.0, 600.0, -1.0)
|
||||
check("wall-ward strafe sign flipped", approx(c.strafeSign, 1.0), $c.strafeSign)
|
||||
check("turns toward +90 (away from the wall)", approx(c.turnRate, 10.0),
|
||||
$c.turnRate)
|
||||
# strafing NORTH (away from the wall) needs no flip
|
||||
let c2 = dodgeDecision(w, DodgeOwn(x: 50.0, y: 0.0, heading: 0.0), 7,
|
||||
800.0, 600.0, 1.0)
|
||||
check("away-from-wall strafe is kept", approx(c2.strafeSign, 1.0))
|
||||
|
||||
block:
|
||||
echo "dodger 6: determinism"
|
||||
echo "dodger 6: determinism + documented seed"
|
||||
let w = DodgeWave(originX: 10.0, originY: 20.0, fireTick: 3, bulletSpeed: 17.0)
|
||||
let a = dodgeDecision(w, DodgeOwn(x: 120.0, y: 40.0, heading: 33.0), 7,
|
||||
800.0, 600.0, 1.0)
|
||||
@@ -79,6 +95,99 @@ block:
|
||||
a.turnRate == b.turnRate and a.targetSpeed == b.targetSpeed and
|
||||
a.strafeSign == b.strafeSign and a.dodging == b.dodging)
|
||||
check("reaction window is 14 ticks", ReactionTicks == 14.0)
|
||||
check("seed is documented", DefaultSeed == 0x5EED1307'u64)
|
||||
check("seededBit is deterministic",
|
||||
seededBit(DefaultSeed, 123) == seededBit(DefaultSeed, 123))
|
||||
# the seed must actually vary across reversal periods, else it is pointless
|
||||
var varied = false
|
||||
for i in 0 ..< 8:
|
||||
if seededBit(DefaultSeed, i) != seededBit(DefaultSeed, i + 1): varied = true
|
||||
check("seededBit varies across periods", varied)
|
||||
|
||||
# ── offline dodge-rate matrix ───────────────────────────────────────────────
|
||||
# Fire head-on-gun shots (no lead) at the Dodger's trajectory; the dodge rate is
|
||||
# the fraction that miss (the bullet never comes within 20 units of the bot).
|
||||
# Reproduces the archetype by running `dodgeDecision` + the physics mirror.
|
||||
|
||||
proc dodgeRate(shooterX, shooterY, startX, startY: float64,
|
||||
ticks = 1400, stride = 4): float64 =
|
||||
const W = 800.0
|
||||
const H = 600.0
|
||||
const HitR = 20.0
|
||||
var ex = startX
|
||||
var ey = startY
|
||||
var ehead = 0.0
|
||||
var espeed = 0.0
|
||||
var sign = 1.0
|
||||
var wave = DodgeWave(originX: shooterX, originY: shooterY, fireTick: -1000,
|
||||
bulletSpeed: 14.0)
|
||||
var pos: seq[tuple[x, y: float64]]
|
||||
for tick in 1 .. ticks:
|
||||
if tick mod 12 == 0:
|
||||
wave = DodgeWave(originX: shooterX, originY: shooterY, fireTick: tick,
|
||||
bulletSpeed: 14.0)
|
||||
let cmd = dodgeDecision(wave, DodgeOwn(x: ex, y: ey, heading: ehead), tick,
|
||||
W, H, sign)
|
||||
sign = cmd.strafeSign
|
||||
espeed = stepSpeed(espeed, cmd.targetSpeed)
|
||||
ehead = wrap180(ehead + limitTurnRate(cmd.turnRate, espeed))
|
||||
ex = clamp(ex + espeed * cos(degToRad(ehead)), 0.0, W)
|
||||
ey = clamp(ey + espeed * sin(degToRad(ehead)), 0.0, H)
|
||||
pos.add (ex, ey)
|
||||
var hits = 0
|
||||
var n = 0
|
||||
for i in countup(0, ticks - 1, stride):
|
||||
let e = pos[i]
|
||||
let bearing = arctan2(e.y - shooterY, e.x - shooterX)
|
||||
var bx = shooterX
|
||||
var by = shooterY
|
||||
var didHit = false
|
||||
for h in 1 .. 80:
|
||||
bx += 14.0 * cos(bearing)
|
||||
by += 14.0 * sin(bearing)
|
||||
if bx < 0.0 or bx > W or by < 0.0 or by > H: break
|
||||
let k = i + h
|
||||
if k < pos.len and hypot(bx - pos[k].x, by - pos[k].y) <= HitR:
|
||||
didHit = true
|
||||
break
|
||||
if didHit: inc hits
|
||||
inc n
|
||||
100.0 - 100.0 * float(hits) / float(n)
|
||||
|
||||
block:
|
||||
echo "dodger 7: offline dodge-rate matrix (bar >= 60% vs head-on)"
|
||||
let cases = [
|
||||
(shooterX: 600.0, shooterY: 300.0, startX: 200.0, startY: 300.0),
|
||||
(shooterX: 400.0, shooterY: 300.0, startX: 700.0, startY: 150.0),
|
||||
(shooterX: 200.0, shooterY: 300.0, startX: 600.0, startY: 300.0),
|
||||
(shooterX: 400.0, shooterY: 400.0, startX: 100.0, startY: 100.0),
|
||||
(shooterX: 500.0, shooterY: 200.0, startX: 150.0, startY: 450.0),
|
||||
]
|
||||
for c in cases:
|
||||
let d = dodgeRate(c.shooterX, c.shooterY, c.startX, c.startY)
|
||||
check("dodge >= 60% (shooter " & $c.shooterX & "," & $c.shooterY & ")",
|
||||
d >= 60.0, formatFloat(d, ffDecimal, 1) & "%")
|
||||
echo " shooter(", c.shooterX, ",", c.shooterY, ") start(",
|
||||
c.startX, ",", c.startY, ") dodge=", formatFloat(d, ffDecimal, 1), "%"
|
||||
|
||||
block:
|
||||
echo "dodger 8: fires back (head-on gun)"
|
||||
let aligned = gunDecision(ex = 100.0, ey = 0.0, sx = 0.0, sy = 0.0,
|
||||
gunHeading = 0.0, gunHeat = 0.0)
|
||||
check("aligned + cold -> fire", aligned.fire > 0.0, $aligned.fire)
|
||||
check("aimed flag set", aligned.aimed)
|
||||
let hot = gunDecision(100.0, 0.0, 0.0, 0.0, 0.0, 1.0)
|
||||
check("aligned but hot -> no fire", hot.fire == 0.0)
|
||||
let mis = gunDecision(0.0, 100.0, 0.0, 0.0, 0.0, 0.0)
|
||||
check("90 deg off -> no fire, slews", mis.fire == 0.0 and mis.turnRate > 0.0)
|
||||
# a short offline exchange: the gun fires repeatedly when it stays aligned
|
||||
var fired = 0
|
||||
var ghead = 0.0
|
||||
for tick in 1 .. 60:
|
||||
let g = gunDecision(100.0, 0.0, 0.0, 0.0, ghead, 0.0)
|
||||
ghead = wrap180(ghead + limitTurnRate(g.turnRate, 0.0))
|
||||
if g.fire > 0.0: inc fired
|
||||
check("fires more than once in a 60-tick exchange", fired >= 2, $fired)
|
||||
|
||||
echo ""
|
||||
if failed == 0: echo "all dodger tests passed"
|
||||
|
||||
Reference in New Issue
Block a user