package brawl
import (
"strings"
"testing"
)
func TestPlanRefused(t *testing.T) {
bad := map[string]string{
"run N": "use run
",
"run N 5; fly 3": `step 2 "fly 3": unknown word "fly"`,
"goto 15,3": "squares are 0 to 14",
"if near 2: jump": "button is attack, block, special or dodge",
"if near 1.456: block": "distance is 0.1 to 15 squares",
"guard": "use guard on or guard off",
"guard up": "use guard on or guard off",
"strike up": "use strike on or strike off",
"ahead 0: attack": "gold gap is 1 to 99",
"ahead 2: behind 1: attack": "one condition per step",
"behind 2 attack": "use behind : ",
"ahead 2: fly 3": `unknown word "fly"`,
"auto 40; wait 5; auto 21": "auto is limited to 60 ticks a plan",
"wait 301": "ticks must be 1 to 300",
"café 3": "plan: plain ASCII only",
strings.Repeat("wait 1;", 72): "plan: at most 500 characters",
}
for text, want := range bad {
p, err := ParsePlan(text)
if p != nil || !strings.Contains(err, want) {
t.Errorf("%q: got %v %q, want an error with %q", text, p != nil, err, want)
}
}
}
func TestPlanWords(t *testing.T) {
for _, text := range []string{
"", "run N 5; wait 3; attack; block; special; dodge", "goto 3,4", "goto (3, 4) 20", "collect 50",
"chase 10\nflee 10", "if near 1.4: attack", "IF NEAR 3 Special", "auto 60", "run 15 2;; ;",
} {
if _, err := ParsePlan(text); err != "" {
t.Errorf("%q: %s", text, err)
}
}
}
// The plain keystroke words play exactly like the inputs rooms.Compile makes.
func TestPlanKeystrokes(t *testing.T) {
p, _ := ParsePlan("run E 3; attack; wait 2; run 14 1; dodge")
g := New(1, Hunter, Hunter)
want := []int{0, None, 0, None, 0, None, 0, Attack, Still, None, Still, None, 14, None, 14, Dodge, Still, None}
for i := 0; i < len(want); i += 2 {
m, a := p.Next(g, 0)
if m != want[i] || a != want[i+1] {
t.Fatalf("tick %d: %d,%d want %d,%d", i/2, m, a, want[i], want[i+1])
}
g.Step(m, a, Still, None)
}
if !p.Done() {
t.Fatal("plan should be done")
}
}
// goto reaches the square, sliding round tables on the way, then ends. On
// the Tavern 2,12 is a table: standing next to it is enough.
func TestPlanGoto(t *testing.T) {
for seed := uint32(0); seed < 3; seed++ {
g := New(seed, Hunter, Hunter)
p, _ := ParsePlan("goto 12,12 200; goto 2,12 200; goto 7,1 200")
for i := 0; i < 600 && !p.Done(); i++ {
m, a := p.Next(g, 0)
g.Step(m, a, Still, None)
}
f := g.F[0]
if !p.Done() || f.X/U != 7 || f.Y/U != 1 {
t.Fatalf("%s: ended on %d,%d at tick %d", ArenaName(seed), f.X/U, f.Y/U, g.T)
}
t.Logf("%s: three gotos in %d ticks", ArenaName(seed), g.T)
}
}
func TestPlanCollectChase(t *testing.T) {
for seed := uint32(0); seed < 3; seed++ {
g := New(seed, Reader, Hunter)
p, _ := ParsePlan("collect 200")
for i := 0; i < 200; i++ {
m, a := p.Next(g, 0)
g.Step(m, a, Still, None)
}
if g.F[0].Gold < 8 {
t.Errorf("%s: collect got %d gold in 20 s", ArenaName(seed), g.F[0].Gold)
}
g = New(seed, Brawler, Hunter)
g.F[1].Gold = 10
p, _ = ParsePlan("chase 80; if near 1.4: attack")
hits := 0
for i := 0; i < 81; i++ {
m, a := p.Next(g, 0)
if a == Attack {
hits++
}
g.Step(m, a, Still, None)
}
if hits != 1 || g.F[1].Gold != 9 {
t.Errorf("%s: chase then hit: %d swings, rival left with %d", ArenaName(seed), hits, g.F[1].Gold)
}
}
}
func TestPlanFleeAuto(t *testing.T) {
g := New(4, Coward, Brawler)
g.F[0].X, g.F[0].Y, g.F[1].X, g.F[1].Y = 7500, 6500, 7500, 8000
p, _ := ParsePlan("flee 30; auto 60")
for i := 0; i < 30; i++ {
m, a := p.Next(g, 0)
g.Step(m, a, Still, None)
}
dx, dy := g.F[0].X-g.F[1].X, g.F[0].Y-g.F[1].Y
if dx*dx+dy*dy < 5000*5000 {
t.Errorf("flee only got %d away", isqrt(dx*dx+dy*dy))
}
for i := 0; i < 60; i++ {
m, a := p.Next(g, 0)
g.Step(m, a, Still, None)
}
if !p.Done() || g.F[0].Gold == 0 {
t.Errorf("auto: done %v, gold %d", p.Done(), g.F[0].Gold)
}
if m, a := p.Next(g, 0); m != Still || a != None {
t.Error("a finished plan stands still")
}
}
// For gas: a 240-tick room round driven by a smart plan on each side.
func TestGasPlanRound(t *testing.T) {
g := NewLength(5, Brawler, Reader, 240)
a, _ := ParsePlan("collect 60; chase 30; if near 1.4: attack; chase 10; if near 1.4: attack; goto 7,7 40; collect 100")
b, _ := ParsePlan("auto 60")
for !g.Over() {
m0, x0 := a.Next(g, 0)
m1, x1 := b.Next(g, 1)
g.Step(m0, x0, m1, x1)
}
t.Logf("round: %d-%d", g.F[0].Gold, g.F[1].Gold)
}
func TestPlanGuard(t *testing.T) {
g := New(7, Reader, Brawler)
me, o := &g.F[0], &g.F[1]
me.X, me.Y, o.X, o.Y = 5000, 5000, 5000+GuardReach-10, 5000
me.Gold = 3
p, _ := ParsePlan("guard on; wait 5; guard off; wait 5")
if _, act := p.Next(g, 0); act != Block {
t.Errorf("rival in reach: want Block, got %d", act)
}
o.X = 5000 + GuardReach + 10
if _, act := p.Next(g, 0); act != None {
t.Errorf("rival out of reach: want None, got %d", act)
}
o.X = 5000 + 1000
o.AtkCD = g.T + 5
if _, act := p.Next(g, 0); act != None {
t.Errorf("rival can't swing yet: want None, got %d", act)
}
o.AtkCD = 0
me.Gold = 0
if _, act := p.Next(g, 0); act != None {
t.Errorf("nothing to lose: want None, got %d", act)
}
me.Gold = 3
p.Next(g, 0) // the last tick of wait 5
if _, act := p.Next(g, 0); act != None {
t.Errorf("after guard off: want None, got %d", act)
}
}
func TestPlanStrikeStance(t *testing.T) {
g := New(7, Brawler, Brawler)
me, o := &g.F[0], &g.F[1]
me.X, me.Y, o.X, o.Y = 5000, 5000, 5000+Melee-10, 5000
o.Gold = 4
p, _ := ParsePlan("strike on; wait 30")
g.T = 1
if _, act := p.Next(g, 0); act != Attack {
t.Errorf("rival in reach with gold: want Attack, got %d", act)
}
o.Gold = 0
if _, act := p.Next(g, 0); act != None {
t.Errorf("rival has nothing to lose: want None, got %d", act)
}
// strike and guard are one stance
q, _ := ParsePlan("guard on; strike on; wait 5")
me.Gold, o.Gold = 3, 3
g.T = 1
if _, act := q.Next(g, 0); act != Attack {
t.Errorf("strike after guard: want Attack, got %d", act)
}
// every third tick of a stance stands still
r, _ := ParsePlan("strike on; run E 30")
o.X = 14000
for tick := 3; tick < 9; tick++ {
g.T = tick
m, _ := r.Next(g, 0)
if (tick%StanceSlow == 0) != (m == Still) {
t.Errorf("tick %d: move %d", tick, m)
}
}
}
func TestPlanConditions(t *testing.T) {
g := New(3, Brawler, Brawler)
me, o := &g.F[0], &g.F[1]
me.Gold, o.Gold = 8, 5
p, _ := ParsePlan("ahead 4: attack; behind 1: block; wait 1")
// ahead 4 fails (3 ahead) and behind 1 fails: both skipped, wait runs
if m, a := p.Next(g, 0); m != Still || a != None {
t.Errorf("both skipped: got %d,%d", m, a)
}
me.Gold = 9
p, _ = ParsePlan("ahead 4: attack")
if _, a := p.Next(g, 0); a != Attack {
t.Errorf("ahead 4 with a 4 lead: want Attack, got %d", a)
}
me.Gold = 2
p, _ = ParsePlan("behind 3: block")
if _, a := p.Next(g, 0); a != Block {
t.Errorf("behind 3 trailing by 3: want Block, got %d", a)
}
}