Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

plan.gno

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