plan.gno
3.93 Kb · 139 lines
1package rooms
2
3import (
4 "strconv"
5 "strings"
6
7 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v2"
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
80// A runner plays a revealed plan tick by tick: either raw input characters
81// (keys:...) or a smart plan from the rules package (goto, collect, chase,
82// flee, if near, auto, and the keystroke words), which looks at the game
83// each tick.
84type runner struct {
85 keys string // input characters
86 i int
87 plan *rules.Plan // or a smart plan
88}
89
90func newRunner(plan string) *runner {
91 if strings.HasPrefix(plan, keysPrefix) {
92 return &runner{keys: plan[len(keysPrefix):]}
93 }
94 p, err := rules.ParsePlan(plan)
95 if err != "" {
96 panic(err)
97 }
98 return &runner{plan: p}
99}
100
101func (p *runner) next(g *rules.Game, s int) (int, int) {
102 if p.plan != nil {
103 return p.plan.Next(g, s)
104 }
105 if p.i >= len(p.keys) {
106 return rules.Still, rules.None
107 }
108 m, a, _ := rules.Decode(p.keys[p.i])
109 p.i++
110 return m, a
111}
112
113// checkPlan panics with a useful message if a plan can't be played.
114func checkPlan(plan string, roundTicks int) {
115 if len(plan) > MaxPlan {
116 panic("plan: " + strconv.Itoa(MaxPlan) + " characters at most")
117 }
118 if strings.HasPrefix(plan, keysPrefix) {
119 if k := plan[len(keysPrefix):]; len(k) > roundTicks || !rules.Valid(k) {
120 panic("plan: after keys:, at most " + strconv.Itoa(roundTicks) + " input characters")
121 }
122 return
123 }
124 if _, err := rules.ParsePlan(plan); err != "" {
125 panic(err)
126 }
127}
128
129func appendTicks(out []byte, dir int, n, step string) []byte {
130 k, err := strconv.Atoi(n)
131 if err != nil || k < 1 || k > 300 {
132 panic("plan step " + strconv.Quote(step) + ": ticks must be 1 to 300")
133 }
134 c := rules.Encode(dir, rules.None)
135 for i := 0; i < k; i++ {
136 out = append(out, c)
137 }
138 return out
139}