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