plan.gno
10.85 Kb · 431 lines
1package brawl
2
3import "strconv"
4
5// Smart plans: a side's moves written as plain text, run tick by tick
6// against the live match. Rooms use them so an agent can say "go get that
7// coin" or "chase and hit when close" instead of guessing keystrokes.
8//
9// Steps are separated by ";" or new lines (at most MaxPlan characters, plain
10// ASCII). Words are case-blind. Ticks are 1 to 300 (10 ticks = 1 s).
11//
12// run <dir> <ticks> move that way
13// wait <ticks> stand still
14// attack | block | special | dodge
15// press the button for one tick, still moving the
16// way you were going
17// goto <x>,<y> [ticks] walk to the middle of square (x,y), 0-14, sliding
18// round walls; ends on arrival (next to it, for a
19// table) or after ticks (60)
20// collect <ticks> head for the nearest gold, picking a new coin when
21// that one is gone
22// chase <ticks> run at the rival
23// flee <ticks> run from the rival
24// if near <squares>: <button>
25// one tick: press the button only if the rival is
26// that close (centre to centre, like 1.4 or 3);
27// otherwise just keep moving
28// auto <ticks> let the Normal house bot play for you
29// guard on | guard off from here on, block on its own whenever the
30// rival is within 1.5 squares and can swing (and you
31// have gold and your block is ready), unless the
32// step is already pressing a button. Takes no time.
33//
34// Directions: N, NE, E, SE, S, SW, W, NW (y grows toward S, so A's corner is
35// NW and B's is SE), or a number 0-15 counting clockwise from E in steps of
36// 22.5 degrees. Melee reaches 1.45 squares, the Hunter's beam 5.
37//
38// Everything is whole-number, so the chain and the browser run a plan the
39// same. A tick costs a few comparisons; collect looks for gold ring by ring
40// only when its coin is gone, and at most every 5 ticks while there is none.
41
42const (
43 MaxPlan = 500
44 PlanTicks = 300 // longest step
45 GotoTicks = 60 // goto's default limit
46 // GuardReach is how close the rival must be for guard to block: a little
47 // past melee (1.45), so the block is up before the swing lands.
48 GuardReach = 1500
49)
50
51const (
52 stepRun = iota
53 stepWait
54 stepButton
55 stepGoto
56 stepCollect
57 stepChase
58 stepFlee
59 stepIfNear
60 stepAuto
61 stepGuard
62)
63
64type planStep struct {
65 kind int
66 n int // ticks
67 dir int // run
68 act int // button, if near
69 x, y int // goto
70 near int // if near, in board units
71}
72
73// Plan is a parsed plain-text plan for one side, run tick by tick.
74type Plan struct {
75 steps []planStep
76 i int // current step
77 on bool // the current step has started
78 left int // its ticks left
79 dir int // the last move, kept by buttons
80 target int // collect: the coin it heads for, -1 for none
81 look int // collect: no looking again before this tick
82 stuck int
83 lastX int
84 lastY int
85 turn int // which way it slides round walls
86 hold int // ticks left of a slide
87 slide int
88 bot *Bot // auto
89 guard bool // guard on
90}
91
92var planDirs = map[string]int{"E": 0, "SE": 2, "S": 4, "SW": 6, "W": 8, "NW": 10, "N": 12, "NE": 14}
93var planButtons = map[string]int{"attack": Attack, "block": Block, "special": Special, "dodge": Dodge}
94
95// ParsePlan reads a plan; on a bad step it returns nil and a short error naming the step.
96func ParsePlan(text string) (*Plan, string) {
97 if len(text) > MaxPlan {
98 return nil, "plan: at most " + strconv.Itoa(MaxPlan) + " characters"
99 }
100 for i := 0; i < len(text); i++ {
101 if text[i] >= 128 {
102 return nil, "plan: plain ASCII only"
103 }
104 }
105 p := &Plan{dir: Still, target: -1}
106 start, num := 0, 0
107 for i := 0; i <= len(text); i++ {
108 if i < len(text) && text[i] != ';' && text[i] != '\n' {
109 continue
110 }
111 step := text[start:i]
112 start = i + 1
113 f := planFields(step)
114 if len(f) == 0 {
115 continue
116 }
117 num++
118 st, err := parseStep(f)
119 if err != "" {
120 return nil, "step " + strconv.Itoa(num) + " \"" + planTrim(step) + "\": " + err
121 }
122 p.steps = append(p.steps, st)
123 }
124 return p, ""
125}
126
127func planSpace(c byte) bool {
128 return c == ' ' || c == '\t' || c == '\r' || c == ',' || c == '(' || c == ')'
129}
130
131// planFields splits a step into words on spaces, commas and brackets; a
132// colon is a word of its own.
133func planFields(s string) []string {
134 var f []string
135 w := -1
136 for i := 0; i <= len(s); i++ {
137 if i < len(s) && !planSpace(s[i]) && s[i] != ':' {
138 if w < 0 {
139 w = i
140 }
141 continue
142 }
143 if w >= 0 {
144 f = append(f, s[w:i])
145 w = -1
146 }
147 if i < len(s) && s[i] == ':' {
148 f = append(f, ":")
149 }
150 }
151 return f
152}
153
154func planTrim(s string) string {
155 a, b := 0, len(s)
156 for a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\r') {
157 a++
158 }
159 for b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\r') {
160 b--
161 }
162 return s[a:b]
163}
164
165// planNum reads 1 to 3 digits; -1 if it isn't one.
166func planNum(s string) int {
167 if len(s) == 0 || len(s) > 3 {
168 return -1
169 }
170 v := 0
171 for i := 0; i < len(s); i++ {
172 if s[i] < '0' || s[i] > '9' {
173 return -1
174 }
175 v = v*10 + int(s[i]-'0')
176 }
177 return v
178}
179
180func planLower(s string) string {
181 b := []byte(s)
182 for i := range b {
183 if b[i] >= 'A' && b[i] <= 'Z' {
184 b[i] += 'a' - 'A'
185 }
186 }
187 return string(b)
188}
189
190func planUpper(s string) string {
191 b := []byte(s)
192 for i := range b {
193 if b[i] >= 'a' && b[i] <= 'z' {
194 b[i] -= 'a' - 'A'
195 }
196 }
197 return string(b)
198}
199
200func planTicks(s string) (int, string) {
201 n := planNum(s)
202 if n < 1 || n > PlanTicks {
203 return 0, "ticks must be 1 to " + strconv.Itoa(PlanTicks)
204 }
205 return n, ""
206}
207
208// planNear reads a distance in squares with at most one decimal, like 1.4.
209func planNear(s string) int {
210 dot := -1
211 for i := 0; i < len(s); i++ {
212 if s[i] == '.' {
213 dot = i
214 }
215 }
216 whole, tenth := s, ""
217 if dot >= 0 {
218 whole, tenth = s[:dot], s[dot+1:]
219 if len(tenth) != 1 && len(tenth) != 2 {
220 return -1
221 }
222 }
223 w := planNum(whole)
224 if w < 0 || len(whole) > 2 {
225 return -1
226 }
227 d := w * U
228 if tenth != "" {
229 v := planNum(tenth)
230 if v < 0 {
231 return -1
232 }
233 if len(tenth) == 1 {
234 v *= 10
235 }
236 d += v * 10
237 }
238 if d < 100 || d > Size*U {
239 return -1
240 }
241 return d
242}
243
244func parseStep(f []string) (planStep, string) {
245 word := planLower(f[0])
246 var st planStep
247 var err string
248 if act, ok := planButtons[word]; ok {
249 if len(f) != 1 {
250 return st, "a button takes no number"
251 }
252 return planStep{kind: stepButton, n: 1, act: act}, ""
253 }
254 switch word {
255 case "run":
256 if len(f) != 3 {
257 return st, "use run <dir> <ticks>"
258 }
259 d, ok := planDirs[planUpper(f[1])]
260 if !ok {
261 d = planNum(f[1])
262 if d < 0 || d > 15 {
263 return st, "direction is N, NE, E, SE, S, SW, W, NW or 0-15"
264 }
265 }
266 st = planStep{kind: stepRun, dir: d}
267 st.n, err = planTicks(f[2])
268 case "wait", "collect", "chase", "flee", "auto":
269 if len(f) != 2 {
270 return st, "use " + word + " <ticks>"
271 }
272 st.kind = map[string]int{"wait": stepWait, "collect": stepCollect, "chase": stepChase, "flee": stepFlee, "auto": stepAuto}[word]
273 st.n, err = planTicks(f[1])
274 case "goto":
275 if len(f) != 3 && len(f) != 4 {
276 return st, "use goto <x>,<y> [ticks]"
277 }
278 x, y := planNum(f[1]), planNum(f[2])
279 if x < 0 || x >= Size || y < 0 || y >= Size {
280 return st, "squares are 0 to 14"
281 }
282 st = planStep{kind: stepGoto, x: x, y: y, n: GotoTicks}
283 if len(f) == 4 {
284 st.n, err = planTicks(f[3])
285 }
286 case "guard":
287 if len(f) != 2 || (planLower(f[1]) != "on" && planLower(f[1]) != "off") {
288 return st, "use guard on or guard off"
289 }
290 st = planStep{kind: stepGuard}
291 if planLower(f[1]) == "on" {
292 st.n = 1
293 }
294 case "if":
295 // if near <squares> [:] <button>
296 if len(f) == 5 && f[3] == ":" {
297 f = []string{f[0], f[1], f[2], f[4]}
298 }
299 if len(f) != 4 || planLower(f[1]) != "near" {
300 return st, "use if near <squares>: <button>"
301 }
302 d := planNear(f[2])
303 if d < 0 {
304 return st, "distance is 0.1 to 15 squares"
305 }
306 act, ok := planButtons[planLower(f[3])]
307 if !ok {
308 return st, "button is attack, block, special or dodge"
309 }
310 st = planStep{kind: stepIfNear, n: 1, near: d, act: act}
311 default:
312 return st, "unknown word \"" + f[0] + "\""
313 }
314 return st, err
315}
316
317// Done reports whether the plan has run out.
318func (p *Plan) Done() bool { return p.i >= len(p.steps) }
319
320// Next returns this tick's move and action for side s, reacting to the live game.
321// Once the plan runs out it returns Still, None. Call it once per tick.
322func (p *Plan) Next(g *Game, s int) (move, act int) {
323 me, o := &g.F[s], &g.F[1-s]
324 move, act = Still, None
325 for p.i < len(p.steps) {
326 st := &p.steps[p.i]
327 if !p.on {
328 p.on, p.left, p.target, p.look, p.hold, p.stuck = true, st.n, -1, 0, 0, 0
329 }
330 if st.kind == stepGuard {
331 p.guard = st.n == 1
332 p.i++
333 p.on = false
334 continue
335 }
336 if st.kind == stepGoto && p.arrived(g, me, st) {
337 p.i++
338 p.on = false
339 continue
340 }
341 switch st.kind {
342 case stepRun:
343 p.dir = st.dir
344 case stepWait:
345 p.dir = Still
346 case stepButton:
347 act = st.act
348 case stepIfNear:
349 if dx, dy := o.X-me.X, o.Y-me.Y; dx*dx+dy*dy <= st.near*st.near {
350 act = st.act
351 }
352 case stepGoto:
353 p.steer(st.x*U+U/2-me.X, st.y*U+U/2-me.Y)
354 case stepChase:
355 p.steer(o.X-me.X, o.Y-me.Y)
356 case stepFlee:
357 p.steer(me.X-o.X, me.Y-o.Y)
358 case stepCollect:
359 if (p.target < 0 || g.Coin[p.target] == 0) && g.T >= p.look {
360 p.target = g.ringCoin(me, Size-1)
361 if p.target < 0 {
362 p.look = g.T + 5
363 }
364 }
365 if p.target >= 0 && g.Coin[p.target] != 0 {
366 p.steer(p.target%Size*U+U/2-me.X, p.target/Size*U+U/2-me.Y)
367 } else {
368 p.dir = Still
369 }
370 case stepAuto:
371 if p.bot == nil {
372 p.bot = NewBot(Normal, g.Seed*31+uint32(s)+3)
373 }
374 p.dir, act = p.bot.Act(g, s)
375 }
376 move = p.dir
377 p.left--
378 if p.left <= 0 {
379 p.i++
380 p.on = false
381 }
382 break
383 }
384 if p.guard && act == None && me.Gold > 0 && g.T >= o.AtkCD && g.T >= me.BlockCD {
385 if dx, dy := o.X-me.X, o.Y-me.Y; dx*dx+dy*dy <= GuardReach*GuardReach {
386 act = Block
387 }
388 }
389 if move != Still && abs(me.X-p.lastX)+abs(me.Y-p.lastY) < 60 {
390 p.stuck++
391 } else {
392 p.stuck = 0
393 }
394 p.lastX, p.lastY = me.X, me.Y
395 return move, act
396}
397
398// arrived reports whether a goto is over: the fighter is on the square, or
399// next to it if the square is a wall.
400func (p *Plan) arrived(g *Game, me *Fighter, st *planStep) bool {
401 x, y := me.X/U, me.Y/U
402 if x == st.x && y == st.y {
403 return true
404 }
405 return g.Floor[st.y*Size+st.x] == Wall && abs(x-st.x) <= 1 && abs(y-st.y) <= 1
406}
407
408// steer heads along (dx, dy), sliding round a wall like the house bot: once
409// it has been stuck for 3 ticks it turns a quarter for 4 ticks, and if that
410// way is blocked too it tries the other side.
411func (p *Plan) steer(dx, dy int) {
412 if p.hold > 0 {
413 if p.stuck <= 2 {
414 p.hold--
415 p.dir = p.slide
416 return
417 }
418 p.turn = 1 - p.turn
419 p.hold = 0
420 }
421 d := toward(dx, dy)
422 if p.stuck > 2 && d != Still {
423 if p.turn == 0 {
424 d = (d + 4) % 16
425 } else {
426 d = (d + 12) % 16
427 }
428 p.slide, p.hold, p.stuck = d, 3, 0
429 }
430 p.dir = d
431}