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.45: block": "distance is 0.1 to 15 squares",
"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 300", "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 150")
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 < 150; 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 240")
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)
}