Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

plan_test.gno

4.14 Kb · 136 lines
  1package brawl
  2
  3import (
  4	"strings"
  5	"testing"
  6)
  7
  8func TestPlanRefused(t *testing.T) {
  9	bad := map[string]string{
 10		"run N":                       "use run <dir> <ticks>",
 11		"run N 5; fly 3":              `step 2 "fly 3": unknown word "fly"`,
 12		"goto 15,3":                   "squares are 0 to 14",
 13		"if near 2: jump":             "button is attack, block, special or dodge",
 14		"if near 1.45: block":         "distance is 0.1 to 15 squares",
 15		"wait 301":                    "ticks must be 1 to 300",
 16		"café 3":                      "plan: plain ASCII only",
 17		strings.Repeat("wait 1;", 72): "plan: at most 500 characters",
 18	}
 19	for text, want := range bad {
 20		p, err := ParsePlan(text)
 21		if p != nil || !strings.Contains(err, want) {
 22			t.Errorf("%q: got %v %q, want an error with %q", text, p != nil, err, want)
 23		}
 24	}
 25}
 26
 27func TestPlanWords(t *testing.T) {
 28	for _, text := range []string{
 29		"", "run N 5; wait 3; attack; block; special; dodge", "goto 3,4", "goto (3, 4) 20", "collect 50",
 30		"chase 10\nflee 10", "if near 1.4: attack", "IF NEAR 3 Special", "auto 300", "run 15 2;;  ;",
 31	} {
 32		if _, err := ParsePlan(text); err != "" {
 33			t.Errorf("%q: %s", text, err)
 34		}
 35	}
 36}
 37
 38// The plain keystroke words play exactly like the inputs rooms.Compile makes.
 39func TestPlanKeystrokes(t *testing.T) {
 40	p, _ := ParsePlan("run E 3; attack; wait 2; run 14 1; dodge")
 41	g := New(1, Hunter, Hunter)
 42	want := []int{0, None, 0, None, 0, None, 0, Attack, Still, None, Still, None, 14, None, 14, Dodge, Still, None}
 43	for i := 0; i < len(want); i += 2 {
 44		m, a := p.Next(g, 0)
 45		if m != want[i] || a != want[i+1] {
 46			t.Fatalf("tick %d: %d,%d want %d,%d", i/2, m, a, want[i], want[i+1])
 47		}
 48		g.Step(m, a, Still, None)
 49	}
 50	if !p.Done() {
 51		t.Fatal("plan should be done")
 52	}
 53}
 54
 55// goto reaches the square, sliding round tables on the way, then ends. On
 56// the Tavern 2,12 is a table: standing next to it is enough.
 57func TestPlanGoto(t *testing.T) {
 58	for seed := uint32(0); seed < 3; seed++ {
 59		g := New(seed, Hunter, Hunter)
 60		p, _ := ParsePlan("goto 12,12 200; goto 2,12 200; goto 7,1 200")
 61		for i := 0; i < 600 && !p.Done(); i++ {
 62			m, a := p.Next(g, 0)
 63			g.Step(m, a, Still, None)
 64		}
 65		f := g.F[0]
 66		if !p.Done() || f.X/U != 7 || f.Y/U != 1 {
 67			t.Fatalf("%s: ended on %d,%d at tick %d", ArenaName(seed), f.X/U, f.Y/U, g.T)
 68		}
 69		t.Logf("%s: three gotos in %d ticks", ArenaName(seed), g.T)
 70	}
 71}
 72
 73func TestPlanCollectChase(t *testing.T) {
 74	for seed := uint32(0); seed < 3; seed++ {
 75		g := New(seed, Reader, Hunter)
 76		p, _ := ParsePlan("collect 200")
 77		for i := 0; i < 200; i++ {
 78			m, a := p.Next(g, 0)
 79			g.Step(m, a, Still, None)
 80		}
 81		if g.F[0].Gold < 8 {
 82			t.Errorf("%s: collect got %d gold in 20 s", ArenaName(seed), g.F[0].Gold)
 83		}
 84		g = New(seed, Brawler, Hunter)
 85		g.F[1].Gold = 10
 86		p, _ = ParsePlan("chase 80; if near 1.4: attack")
 87		hits := 0
 88		for i := 0; i < 81; i++ {
 89			m, a := p.Next(g, 0)
 90			if a == Attack {
 91				hits++
 92			}
 93			g.Step(m, a, Still, None)
 94		}
 95		if hits != 1 || g.F[1].Gold != 9 {
 96			t.Errorf("%s: chase then hit: %d swings, rival left with %d", ArenaName(seed), hits, g.F[1].Gold)
 97		}
 98	}
 99}
100
101func TestPlanFleeAuto(t *testing.T) {
102	g := New(4, Coward, Brawler)
103	g.F[0].X, g.F[0].Y, g.F[1].X, g.F[1].Y = 7500, 6500, 7500, 8000
104	p, _ := ParsePlan("flee 30; auto 150")
105	for i := 0; i < 30; i++ {
106		m, a := p.Next(g, 0)
107		g.Step(m, a, Still, None)
108	}
109	dx, dy := g.F[0].X-g.F[1].X, g.F[0].Y-g.F[1].Y
110	if dx*dx+dy*dy < 5000*5000 {
111		t.Errorf("flee only got %d away", isqrt(dx*dx+dy*dy))
112	}
113	for i := 0; i < 150; i++ {
114		m, a := p.Next(g, 0)
115		g.Step(m, a, Still, None)
116	}
117	if !p.Done() || g.F[0].Gold == 0 {
118		t.Errorf("auto: done %v, gold %d", p.Done(), g.F[0].Gold)
119	}
120	if m, a := p.Next(g, 0); m != Still || a != None {
121		t.Error("a finished plan stands still")
122	}
123}
124
125// For gas: a 240-tick room round driven by a smart plan on each side.
126func TestGasPlanRound(t *testing.T) {
127	g := NewLength(5, Brawler, Reader, 240)
128	a, _ := ParsePlan("collect 60; chase 30; if near 1.4: attack; chase 10; if near 1.4: attack; goto 7,7 40; collect 100")
129	b, _ := ParsePlan("auto 240")
130	for !g.Over() {
131		m0, x0 := a.Next(g, 0)
132		m1, x1 := b.Next(g, 1)
133		g.Step(m0, x0, m1, x1)
134	}
135	t.Logf("round: %d-%d", g.F[0].Gold, g.F[1].Gold)
136}