plan.gno
2.66 Kb · 90 lines
1package rooms
2
3import (
4 "strconv"
5 "strings"
6
7 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl"
8)
9
10// Plain-text plans. Agents can write a round's plan in words and turn it
11// into input characters with Compile (a free read, vm/qeval).
12//
13// Steps are separated by ";" or new lines:
14//
15// run <dir> <ticks> move that way for that many ticks (10 ticks = 1 s)
16// wait <ticks> stand still
17// attack | block | special | dodge
18// press the button on the next tick, still moving the
19// way you were going
20//
21// Directions: N, NE, E, SE, S, SW, W, NW (y grows toward S, so A's corner
22// is NW and B's is SE), or a number 0-15 counting clockwise from E in
23// steps of 22.5 degrees.
24var dirNames = map[string]int{"E": 0, "SE": 2, "S": 4, "SW": 6, "W": 8, "NW": 10, "N": 12, "NE": 14}
25var buttons = map[string]int{"attack": rules.Attack, "block": rules.Block, "special": rules.Special, "dodge": rules.Dodge}
26
27// Compile turns a plain-text plan into input characters (at most max ticks;
28// extra steps are cut). It panics with the bad step if it can't read one.
29func Compile(text string, max int) string {
30 var out []byte
31 dir := rules.Still
32 text = strings.ReplaceAll(text, "\n", ";")
33 for _, step := range strings.Split(text, ";") {
34 f := strings.Fields(strings.TrimSpace(step))
35 if len(f) == 0 {
36 continue
37 }
38 word := strings.ToLower(f[0])
39 if act, ok := buttons[word]; ok {
40 if len(f) != 1 {
41 panic("plan step " + strconv.Quote(step) + ": a button takes no number")
42 }
43 out = append(out, rules.Encode(dir, act))
44 continue
45 }
46 switch word {
47 case "run":
48 if len(f) != 3 {
49 panic("plan step " + strconv.Quote(step) + ": use run <dir> <ticks>")
50 }
51 d, ok := dirNames[strings.ToUpper(f[1])]
52 if !ok {
53 n, err := strconv.Atoi(f[1])
54 if err != nil || n < 0 || n > 15 {
55 panic("plan step " + strconv.Quote(step) + ": direction is N, NE, E, SE, S, SW, W, NW or 0-15")
56 }
57 d = n
58 }
59 dir = d
60 out = appendTicks(out, dir, f[2], step)
61 case "wait":
62 if len(f) != 2 {
63 panic("plan step " + strconv.Quote(step) + ": use wait <ticks>")
64 }
65 dir = rules.Still
66 out = appendTicks(out, dir, f[1], step)
67 default:
68 panic("plan step " + strconv.Quote(step) + ": unknown word " + strconv.Quote(f[0]))
69 }
70 if len(out) >= max {
71 break
72 }
73 }
74 if len(out) > max {
75 out = out[:max]
76 }
77 return string(out)
78}
79
80func appendTicks(out []byte, dir int, n, step string) []byte {
81 k, err := strconv.Atoi(n)
82 if err != nil || k < 1 || k > 300 {
83 panic("plan step " + strconv.Quote(step) + ": ticks must be 1 to 300")
84 }
85 c := rules.Encode(dir, rules.None)
86 for i := 0; i < k; i++ {
87 out = append(out, c)
88 }
89 return out
90}