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