148 lines
6.0 KiB
Nim
148 lines
6.0 KiB
Nim
## Tier A tests for the CHIMERA virtual-gun tournament
|
|
## (chimera/virtual_guns.nim, spec G8 / M7.5). Pure Nim, no Java.
|
|
## Run: from ChimeraBot_garage, `nimble test` (and once with
|
|
## `-d:chimeraNoVguns` to prove the kill switch compiles).
|
|
##
|
|
## Covers the four stated concerns: EMA scoring, hysteresis, determinism and the
|
|
## virtual-bullet mechanics, plus the kill-switch compile.
|
|
|
|
import std/math
|
|
import chimera/virtual_guns
|
|
|
|
var failed = 0
|
|
proc approx(a, b: float64): bool = abs(a - b) < 1e-9
|
|
proc check(name: string, cond: bool, detail: string = "") =
|
|
if cond: echo " ok ", name
|
|
else: echo " FAIL ", name, " [", detail, "]"; inc failed
|
|
|
|
# The kill switch must track the compile define. This file is also compiled with
|
|
# `-d:chimeraNoVguns` in the nimble task, so both branches are exercised.
|
|
when defined(chimeraNoVguns):
|
|
static: doAssert NoVguns
|
|
else:
|
|
static: doAssert not NoVguns
|
|
|
|
proc driveTo(v: var VirtualGunSet, g: VGun, density: float64, n = 400) =
|
|
## Drive `g`'s EMA toward a hit density in [0,1]. The hits are interleaved
|
|
## (37 is coprime with 100) so the EMA settles on the density rather than on
|
|
## the last run of a clustered pattern.
|
|
let target = int(density * 100.0)
|
|
for i in 0 ..< n:
|
|
v.observeShot(g, ((i * 37) mod 100) < target, 2.0)
|
|
|
|
block:
|
|
echo "vguns 1: EMA scoring (hit/miss)"
|
|
var v = initVirtualGuns()
|
|
check("no shots -> EMA 0", v.emaOf(vgTwin) == 0.0)
|
|
for i in 0 ..< 400: v.observeShot(vgTwin, true, 2.0)
|
|
check("all hits -> EMA near 1", v.emaOf(vgTwin) > 0.95, $v.emaOf(vgTwin))
|
|
for i in 0 ..< 400: v.observeShot(vgTwin, false, 2.0)
|
|
check("switched to misses -> EMA near 0", v.emaOf(vgTwin) < 0.05, $v.emaOf(vgTwin))
|
|
check("shot counter", v.shotsOf(vgTwin) == 800, $v.shotsOf(vgTwin))
|
|
# a hit must raise the EMA, a miss must lower it
|
|
var w = initVirtualGuns()
|
|
driveTo(w, vgElm, 0.5)
|
|
let before = w.emaOf(vgElm)
|
|
w.observeShot(vgElm, true, 2.0)
|
|
let afterHit = w.emaOf(vgElm)
|
|
check("a hit raises the EMA", afterHit > before)
|
|
w.observeShot(vgElm, false, 2.0)
|
|
check("a miss lowers the EMA", w.emaOf(vgElm) < afterHit)
|
|
|
|
block:
|
|
echo "vguns 2: damage-weighted accumulator"
|
|
var v = initVirtualGuns()
|
|
# same hit count, different power: the accumulator must reward damage.
|
|
for i in 0 ..< 50: v.observeShot(vgTwin, true, 3.0)
|
|
for i in 0 ..< 50: v.observeShot(vgElm, true, 0.5)
|
|
check("hit-rate EMA ties (same hit count)", approx(v.emaOf(vgTwin), v.emaOf(vgElm)))
|
|
check("damage accumulator favours the heavier gun",
|
|
v.decayedRate(vgTwin) > v.decayedRate(vgElm),
|
|
$v.decayedRate(vgTwin) & " vs " & $v.decayedRate(vgElm))
|
|
# 3.0 -> 4*3 + 2*2 = 16 damage; 0.5 -> 2.0
|
|
check("accumulator equals damage per shot",
|
|
abs(v.decayedRate(vgTwin) - 16.0) < 1e-9, $v.decayedRate(vgTwin))
|
|
|
|
block:
|
|
echo "vguns 3: hysteresis (0.1 EMA margin)"
|
|
var v = initVirtualGuns()
|
|
driveTo(v, vgTwin, 0.50)
|
|
discard v.selectGun() # make twin the incumbent
|
|
driveTo(v, vgElm, 0.55) # elm now leads, but only by ~0.05
|
|
check("incumbent twin kept while the rival leads by < 0.1",
|
|
v.selectGun() == vgTwin, $v.emaOf(vgTwin) & "/" & $v.emaOf(vgElm))
|
|
var w = initVirtualGuns()
|
|
driveTo(w, vgTwin, 0.50)
|
|
driveTo(w, vgElm, 0.70)
|
|
discard w.selectGun()
|
|
check("rival leading by > 0.1 switches to elm", w.selectGun() == vgElm)
|
|
# and the switch is sticky: once elm, a 0.05 lead does not hand it back
|
|
var u = initVirtualGuns()
|
|
driveTo(u, vgTwin, 0.70)
|
|
driveTo(u, vgElm, 0.75)
|
|
discard u.selectGun()
|
|
# first selection is whichever is best at that moment; assert the margin logic
|
|
let s1 = u.selectGun()
|
|
let s2 = u.selectGun()
|
|
check("selection is stable across repeated calls", s1 == s2)
|
|
|
|
block:
|
|
echo "vguns 4: twin influence gate"
|
|
var v = initVirtualGuns()
|
|
driveTo(v, vgTwin, 0.9)
|
|
driveTo(v, vgElm, 0.1)
|
|
discard v.selectGun() # incumbent twin
|
|
check("twin eligible -> twin chosen", v.selectGun(twinEligible = true) == vgTwin)
|
|
check("twin ineligible -> falls to the best legal gun",
|
|
v.selectGun(twinEligible = false) != vgTwin)
|
|
|
|
block:
|
|
echo "vguns 5: virtual-bullet mechanics"
|
|
var v = initVirtualGuns()
|
|
# head-on east at power 2 -> speed 14. Enemy at (14,0): the first step hits.
|
|
v.launchVirtual(vgHeadOn, 0.0, 0.0, 0.0, 2.0)
|
|
v.vgTick(14.0, 0.0, 20.0, 800.0, 600.0)
|
|
check("a hit resolves and scores", v.shotsOf(vgHeadOn) == 1 and v.emaOf(vgHeadOn) > 0.0)
|
|
check("no live bullets left after a hit", v.bullets.len == 0)
|
|
# a bullet aimed away from the enemy leaves the arena -> a scored miss
|
|
var w = initVirtualGuns()
|
|
w.launchVirtual(vgElm, 0.0, 0.0, PI, 2.0) # west, straight out of the arena
|
|
w.vgTick(500.0, 500.0, 20.0, 800.0, 600.0)
|
|
check("arena exit resolves as a miss", w.shotsOf(vgElm) == 1 and w.emaOf(vgElm) == 0.0)
|
|
# forgetting decays the accumulator but not the accumulated damage average
|
|
var x = initVirtualGuns()
|
|
for i in 0 ..< 10: x.observeShot(vgTwin, true, 2.0)
|
|
let r0 = x.decayedRate(vgTwin)
|
|
for i in 0 ..< 200: x.forgetTick()
|
|
check("forgetting lowers the accumulator weight",
|
|
x.score[vgTwin].weight < 5.0, $x.score[vgTwin].weight)
|
|
|
|
block:
|
|
echo "vguns 6: determinism"
|
|
var a = initVirtualGuns()
|
|
var b = initVirtualGuns()
|
|
for i in 0 ..< 120:
|
|
let hitA = (i mod 3) != 0
|
|
let hitB = (i mod 3) != 0
|
|
a.observeShot(vgTwin, hitA, 1.5)
|
|
b.observeShot(vgTwin, hitB, 1.5)
|
|
a.observeShot(vgElm, (i mod 5) == 0, 2.0)
|
|
b.observeShot(vgElm, (i mod 5) == 0, 2.0)
|
|
check("identical sequences -> identical EMAs",
|
|
a.emaOf(vgTwin) == b.emaOf(vgTwin) and a.emaOf(vgElm) == b.emaOf(vgElm))
|
|
check("identical sequences -> identical selection", a.selectGun() == b.selectGun())
|
|
check("gun names are stable", vgTwin.gunName() == "twin" and
|
|
vgElm.gunName() == "elm" and vgHeadOn.gunName() == "headon")
|
|
|
|
block:
|
|
echo "vguns 7: reset"
|
|
var v = initVirtualGuns()
|
|
for i in 0 ..< 30: v.observeShot(vgTwin, true, 2.0)
|
|
v.resetVguns()
|
|
check("reset clears the scores", v.emaOf(vgTwin) == 0.0 and
|
|
v.shotsOf(vgTwin) == 0)
|
|
|
|
echo ""
|
|
if failed == 0: echo "all virtual-gun tests passed"
|
|
else: echo failed, " virtual-gun test(s) FAILED"; quit(1)
|