plan_test.gno
7.05 Kb · 228 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 "strike up": "use strike on or strike off",
18 "ahead 0: attack": "gold gap is 1 to 99",
19 "ahead 2: behind 1: attack": "one condition per step",
20 "behind 2 attack": "use behind <gold>: <step>",
21 "ahead 2: fly 3": `unknown word "fly"`,
22 "auto 40; wait 5; auto 21": "auto is limited to 60 ticks a plan",
23 "wait 301": "ticks must be 1 to 300",
24 "café 3": "plan: plain ASCII only",
25 strings.Repeat("wait 1;", 72): "plan: at most 500 characters",
26 }
27 for text, want := range bad {
28 p, err := ParsePlan(text)
29 if p != nil || !strings.Contains(err, want) {
30 t.Errorf("%q: got %v %q, want an error with %q", text, p != nil, err, want)
31 }
32 }
33}
34
35func TestPlanWords(t *testing.T) {
36 for _, text := range []string{
37 "", "run N 5; wait 3; attack; block; special; dodge", "goto 3,4", "goto (3, 4) 20", "collect 50",
38 "chase 10\nflee 10", "if near 1.4: attack", "IF NEAR 3 Special", "auto 60", "run 15 2;; ;",
39 } {
40 if _, err := ParsePlan(text); err != "" {
41 t.Errorf("%q: %s", text, err)
42 }
43 }
44}
45
46// The plain keystroke words play exactly like the inputs rooms.Compile makes.
47func TestPlanKeystrokes(t *testing.T) {
48 p, _ := ParsePlan("run E 3; attack; wait 2; run 14 1; dodge")
49 g := New(1, Hunter, Hunter)
50 want := []int{0, None, 0, None, 0, None, 0, Attack, Still, None, Still, None, 14, None, 14, Dodge, Still, None}
51 for i := 0; i < len(want); i += 2 {
52 m, a := p.Next(g, 0)
53 if m != want[i] || a != want[i+1] {
54 t.Fatalf("tick %d: %d,%d want %d,%d", i/2, m, a, want[i], want[i+1])
55 }
56 g.Step(m, a, Still, None)
57 }
58 if !p.Done() {
59 t.Fatal("plan should be done")
60 }
61}
62
63// goto reaches the square, sliding round tables on the way, then ends. On
64// the Tavern 2,12 is a table: standing next to it is enough.
65func TestPlanGoto(t *testing.T) {
66 for seed := uint32(0); seed < 3; seed++ {
67 g := New(seed, Hunter, Hunter)
68 p, _ := ParsePlan("goto 12,12 200; goto 2,12 200; goto 7,1 200")
69 for i := 0; i < 600 && !p.Done(); i++ {
70 m, a := p.Next(g, 0)
71 g.Step(m, a, Still, None)
72 }
73 f := g.F[0]
74 if !p.Done() || f.X/U != 7 || f.Y/U != 1 {
75 t.Fatalf("%s: ended on %d,%d at tick %d", ArenaName(seed), f.X/U, f.Y/U, g.T)
76 }
77 t.Logf("%s: three gotos in %d ticks", ArenaName(seed), g.T)
78 }
79}
80
81func TestPlanCollectChase(t *testing.T) {
82 for seed := uint32(0); seed < 3; seed++ {
83 g := New(seed, Reader, Hunter)
84 p, _ := ParsePlan("collect 200")
85 for i := 0; i < 200; i++ {
86 m, a := p.Next(g, 0)
87 g.Step(m, a, Still, None)
88 }
89 if g.F[0].Gold < 8 {
90 t.Errorf("%s: collect got %d gold in 20 s", ArenaName(seed), g.F[0].Gold)
91 }
92 g = New(seed, Brawler, Hunter)
93 g.F[1].Gold = 10
94 p, _ = ParsePlan("chase 80; if near 1.4: attack")
95 hits := 0
96 for i := 0; i < 81; i++ {
97 m, a := p.Next(g, 0)
98 if a == Attack {
99 hits++
100 }
101 g.Step(m, a, Still, None)
102 }
103 if hits != 1 || g.F[1].Gold != 9 {
104 t.Errorf("%s: chase then hit: %d swings, rival left with %d", ArenaName(seed), hits, g.F[1].Gold)
105 }
106 }
107}
108
109func TestPlanFleeAuto(t *testing.T) {
110 g := New(4, Coward, Brawler)
111 g.F[0].X, g.F[0].Y, g.F[1].X, g.F[1].Y = 7500, 6500, 7500, 8000
112 p, _ := ParsePlan("flee 30; auto 60")
113 for i := 0; i < 30; i++ {
114 m, a := p.Next(g, 0)
115 g.Step(m, a, Still, None)
116 }
117 dx, dy := g.F[0].X-g.F[1].X, g.F[0].Y-g.F[1].Y
118 if dx*dx+dy*dy < 5000*5000 {
119 t.Errorf("flee only got %d away", isqrt(dx*dx+dy*dy))
120 }
121 for i := 0; i < 60; i++ {
122 m, a := p.Next(g, 0)
123 g.Step(m, a, Still, None)
124 }
125 if !p.Done() || g.F[0].Gold == 0 {
126 t.Errorf("auto: done %v, gold %d", p.Done(), g.F[0].Gold)
127 }
128 if m, a := p.Next(g, 0); m != Still || a != None {
129 t.Error("a finished plan stands still")
130 }
131}
132
133// For gas: a 240-tick room round driven by a smart plan on each side.
134func TestGasPlanRound(t *testing.T) {
135 g := NewLength(5, Brawler, Reader, 240)
136 a, _ := ParsePlan("collect 60; chase 30; if near 1.4: attack; chase 10; if near 1.4: attack; goto 7,7 40; collect 100")
137 b, _ := ParsePlan("auto 60")
138 for !g.Over() {
139 m0, x0 := a.Next(g, 0)
140 m1, x1 := b.Next(g, 1)
141 g.Step(m0, x0, m1, x1)
142 }
143 t.Logf("round: %d-%d", g.F[0].Gold, g.F[1].Gold)
144}
145
146func TestPlanGuard(t *testing.T) {
147 g := New(7, Reader, Brawler)
148 me, o := &g.F[0], &g.F[1]
149 me.X, me.Y, o.X, o.Y = 5000, 5000, 5000+GuardReach-10, 5000
150 me.Gold = 3
151 p, _ := ParsePlan("guard on; wait 5; guard off; wait 5")
152 if _, act := p.Next(g, 0); act != Block {
153 t.Errorf("rival in reach: want Block, got %d", act)
154 }
155 o.X = 5000 + GuardReach + 10
156 if _, act := p.Next(g, 0); act != None {
157 t.Errorf("rival out of reach: want None, got %d", act)
158 }
159 o.X = 5000 + 1000
160 o.AtkCD = g.T + 5
161 if _, act := p.Next(g, 0); act != None {
162 t.Errorf("rival can't swing yet: want None, got %d", act)
163 }
164 o.AtkCD = 0
165 me.Gold = 0
166 if _, act := p.Next(g, 0); act != None {
167 t.Errorf("nothing to lose: want None, got %d", act)
168 }
169 me.Gold = 3
170 p.Next(g, 0) // the last tick of wait 5
171 if _, act := p.Next(g, 0); act != None {
172 t.Errorf("after guard off: want None, got %d", act)
173 }
174}
175
176func TestPlanStrikeStance(t *testing.T) {
177 g := New(7, Brawler, Brawler)
178 me, o := &g.F[0], &g.F[1]
179 me.X, me.Y, o.X, o.Y = 5000, 5000, 5000+Melee-10, 5000
180 o.Gold = 4
181 p, _ := ParsePlan("strike on; wait 30")
182 g.T = 1
183 if _, act := p.Next(g, 0); act != Attack {
184 t.Errorf("rival in reach with gold: want Attack, got %d", act)
185 }
186 o.Gold = 0
187 if _, act := p.Next(g, 0); act != None {
188 t.Errorf("rival has nothing to lose: want None, got %d", act)
189 }
190 // strike and guard are one stance
191 q, _ := ParsePlan("guard on; strike on; wait 5")
192 me.Gold, o.Gold = 3, 3
193 g.T = 1
194 if _, act := q.Next(g, 0); act != Attack {
195 t.Errorf("strike after guard: want Attack, got %d", act)
196 }
197 // every third tick of a stance stands still
198 r, _ := ParsePlan("strike on; run E 30")
199 o.X = 14000
200 for tick := 3; tick < 9; tick++ {
201 g.T = tick
202 m, _ := r.Next(g, 0)
203 if (tick%StanceSlow == 0) != (m == Still) {
204 t.Errorf("tick %d: move %d", tick, m)
205 }
206 }
207}
208
209func TestPlanConditions(t *testing.T) {
210 g := New(3, Brawler, Brawler)
211 me, o := &g.F[0], &g.F[1]
212 me.Gold, o.Gold = 8, 5
213 p, _ := ParsePlan("ahead 4: attack; behind 1: block; wait 1")
214 // ahead 4 fails (3 ahead) and behind 1 fails: both skipped, wait runs
215 if m, a := p.Next(g, 0); m != Still || a != None {
216 t.Errorf("both skipped: got %d,%d", m, a)
217 }
218 me.Gold = 9
219 p, _ = ParsePlan("ahead 4: attack")
220 if _, a := p.Next(g, 0); a != Attack {
221 t.Errorf("ahead 4 with a 4 lead: want Attack, got %d", a)
222 }
223 me.Gold = 2
224 p, _ = ParsePlan("behind 3: block")
225 if _, a := p.Next(g, 0); a != Block {
226 t.Errorf("behind 3 trailing by 3: want Block, got %d", a)
227 }
228}