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

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