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.

recipe.gno

10.50 Kb · 392 lines
  1package brawl
  2
  3import (
  4	"errors"
  5	"strconv"
  6)
  7
  8// Move recipes: a special move written as data, so anyone can design one and
  9// both the chain and the browser run it exactly. A fighter with a recipe uses
 10// it instead of its class special; everything else about the match is the same.
 11//
 12// A recipe is written as one short string:
 13//
 14//	name/cooldown/effects
 15//
 16//	Blink Thief/84/D82S11   dash 4 squares to the nearest gold, then steal 1 in melee
 17//
 18// The name is 1 to 16 letters, digits, spaces, '-' or '\'' (no space at either
 19// end). The cooldown is in ticks (10 a second), MinCD to MaxCD, written without
 20// leading zeros. Then come 1 to 3 effects, three characters each: a letter and
 21// two digits. They fire in order, the moment the move is used:
 22//
 23//	Dxm  dash x half-squares (1-8) in mode m: 0 the way you steer (away from
 24//	     the rival if you stand still), 1 at the rival, 2 to the nearest gold,
 25//	     3 away from the rival
 26//	Snr  knock n gold (1-3) loose from the rival within range r (onto the
 27//	     floor round them, like any hit); a block saves one
 28//	Pnr  pull up to n coins (1-3) lying within r squares (1-5) into your purse
 29//	H00  put up a shield, like the power-up: it saves one gold from the next steal
 30//	Ttr  stun the rival t ticks (1-9) within range r; a block stops it
 31//	It0  turn untouchable for t ticks (1-9)
 32//	Kpr  knock the rival back with power p (1-9) within range r
 33//
 34// Ranges r are 1-5: 1.45, 2.4, 3.3, 4.2 or 5 squares. Beyond melee (r > 1) a
 35// wall in the way stops the effect, like the Hunter's beam. An untouchable
 36// rival shrugs off steal, stun and knockback.
 37//
 38// Budget: the cooldown must be at least the recipe's Cost, in ticks:
 39//
 40//	D  x(x+1)/2           (D82: 36)
 41//	S  n(n+1)/2 * (28+4r) (S11: 32, S15: 48, S21: 96)
 42//	P  n * (4r+4)         (P24: 40, like the Reader's magnet)
 43//	H  80
 44//	T  t * (2r+4)
 45//	I  5t
 46//	K  (p(r+1)+2)/3
 47//
 48// plus half the cost of every effect but the dearest, and never under MinCD.
 49// The numbers come from sim/browser/recipes.js: scripted players fight the
 50// Hard bot with each built-in special and with hundreds of recipes at their
 51// cheapest cooldown, and the best recipes only match the best built-ins.
 52// Each letter may appear once.
 53
 54const (
 55	MinCD      = 20
 56	MaxCD      = 200
 57	MaxEffects = 3
 58	MaxName    = 16
 59)
 60
 61// Effect letters.
 62const (
 63	FxDash   = 'D'
 64	FxSteal  = 'S'
 65	FxPull   = 'P'
 66	FxShield = 'H'
 67	FxStun   = 'T'
 68	FxGuard  = 'I'
 69	FxKnock  = 'K'
 70)
 71
 72// Dash modes.
 73const (
 74	DashSteer = iota
 75	DashRival
 76	DashCoin
 77	DashAway
 78)
 79
 80// reach is the distance for ranges 1..5.
 81var reach = [6]int{0, Melee, 2400, 3300, 4200, Long}
 82
 83type Effect struct {
 84	Kind byte
 85	A, B int
 86}
 87
 88type Recipe struct {
 89	Name string
 90	CD   int
 91	Fx   []Effect
 92}
 93
 94// Cost is what one effect adds to the cooldown a recipe needs, in ticks.
 95func (e Effect) Cost() int {
 96	a, b := e.A, e.B
 97	switch e.Kind {
 98	case FxDash:
 99		return a * (a + 1) / 2
100	case FxSteal:
101		return a * (a + 1) / 2 * (28 + 4*b)
102	case FxPull:
103		return a * (4*b + 4)
104	case FxShield:
105		return 80
106	case FxStun:
107		return a * (2*b + 4)
108	case FxGuard:
109		return 5 * a
110	case FxKnock:
111		return (a*(b+1) + 2) / 3
112	}
113	return 0
114}
115
116// Cost is the shortest cooldown the recipe's effects allow: the sum of the
117// effects' costs, plus half of all but the dearest one, since moves that do
118// several things at once are worth more than their parts. Never below MinCD.
119func (r *Recipe) Cost() int {
120	c, top := 0, 0
121	for _, e := range r.Fx {
122		v := e.Cost()
123		c += v
124		if v > top {
125			top = v
126		}
127	}
128	c += (c - top) / 2
129	if c < MinCD {
130		c = MinCD
131	}
132	return c
133}
134
135// limits holds the allowed range of each effect's two digits.
136func limits(k byte) (int, int, int, int, bool) {
137	switch k {
138	case FxDash:
139		return 1, 8, 0, 3, true
140	case FxSteal, FxPull, FxKnock:
141		hi := 3
142		if k == FxKnock {
143			hi = 9
144		}
145		return 1, hi, 1, 5, true
146	case FxShield:
147		return 0, 0, 0, 0, true
148	case FxStun:
149		return 1, 9, 1, 5, true
150	case FxGuard:
151		return 1, 9, 0, 0, true
152	}
153	return 0, 0, 0, 0, false
154}
155
156func nameChar(c byte) bool {
157	return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == ' ' || c == '-' || c == '\''
158}
159
160// ParseRecipe reads and checks a recipe string. A valid recipe always writes
161// back to the same string, so each move has exactly one spelling.
162func ParseRecipe(s string) (*Recipe, error) {
163	a, b := -1, -1
164	for i := 0; i < len(s); i++ {
165		if s[i] == '/' {
166			if a < 0 {
167				a = i
168			} else if b < 0 {
169				b = i
170			} else {
171				return nil, errors.New("recipe: name/cooldown/effects")
172			}
173		}
174	}
175	if b < 0 {
176		return nil, errors.New("recipe: name/cooldown/effects")
177	}
178	name, cds, fx := s[:a], s[a+1:b], s[b+1:]
179	if len(name) == 0 || len(name) > MaxName || name[0] == ' ' || name[len(name)-1] == ' ' {
180		return nil, errors.New("recipe: name is 1 to 16 characters, no space at either end")
181	}
182	for i := 0; i < len(name); i++ {
183		if !nameChar(name[i]) {
184			return nil, errors.New("recipe: name uses letters, digits, spaces, - and ' only")
185		}
186	}
187	if len(cds) == 0 || len(cds) > 3 || cds[0] == '0' {
188		return nil, errors.New("recipe: bad cooldown")
189	}
190	cd := 0
191	for i := 0; i < len(cds); i++ {
192		if cds[i] < '0' || cds[i] > '9' {
193			return nil, errors.New("recipe: bad cooldown")
194		}
195		cd = cd*10 + int(cds[i]-'0')
196	}
197	if cd < MinCD || cd > MaxCD {
198		return nil, errors.New("recipe: cooldown is " + strconv.Itoa(MinCD) + " to " + strconv.Itoa(MaxCD) + " ticks")
199	}
200	if len(fx) == 0 || len(fx)%3 != 0 || len(fx) > 3*MaxEffects {
201		return nil, errors.New("recipe: 1 to 3 effects of 3 characters")
202	}
203	r := &Recipe{Name: name, CD: cd}
204	for i := 0; i < len(fx); i += 3 {
205		k, x, y := fx[i], int(fx[i+1])-'0', int(fx[i+2])-'0'
206		alo, ahi, blo, bhi, ok := limits(k)
207		if !ok {
208			return nil, errors.New("recipe: unknown effect " + fx[i:i+1])
209		}
210		if x < alo || x > ahi || y < blo || y > bhi {
211			return nil, errors.New("recipe: effect " + fx[i:i+3] + " out of range")
212		}
213		for _, e := range r.Fx {
214			if e.Kind == k {
215				return nil, errors.New("recipe: effect " + fx[i:i+1] + " twice")
216			}
217		}
218		r.Fx = append(r.Fx, Effect{Kind: k, A: x, B: y})
219	}
220	if c := r.Cost(); cd < c {
221		return nil, errors.New("recipe: these effects need a cooldown of at least " + strconv.Itoa(c))
222	}
223	return r, nil
224}
225
226// MustRecipe is ParseRecipe that panics on a bad recipe. "" gives nil: the
227// class's own special.
228func MustRecipe(s string) *Recipe {
229	if s == "" {
230		return nil
231	}
232	r, err := ParseRecipe(s)
233	if err != nil {
234		panic(err.Error())
235	}
236	return r
237}
238
239// String writes the recipe back in its one spelling.
240func (r *Recipe) String() string {
241	b := []byte(r.Name + "/" + strconv.Itoa(r.CD) + "/")
242	for _, e := range r.Fx {
243		b = append(b, e.Kind, byte('0'+e.A), byte('0'+e.B))
244	}
245	return string(b)
246}
247
248// Equip gives side s a recipe in place of its class special; nil takes it away.
249func (g *Game) Equip(s int, r *Recipe) { g.Move[s] = r }
250
251// hits reports whether a rival-targeting effect with range r lands.
252func (g *Game) hits(me, o *Fighter, r, dd int, oSafe bool) bool {
253	d := reach[r]
254	return !oSafe && dd <= d*d && (r == 1 || g.clear(me, o))
255}
256
257// recipe runs side s's recipe; act has already paid the cooldown.
258func (g *Game) recipe(s int, rec *Recipe, mx, my, dd int, oBlocks, oSafe bool) {
259	t := g.T
260	me, o := &g.F[s], &g.F[1-s]
261	for _, e := range rec.Fx {
262		switch e.Kind {
263		case FxDash:
264			dx, dy := me.X-o.X, me.Y-o.Y // away
265			switch e.B {
266			case DashSteer:
267				if mx != 0 || my != 0 {
268					dx, dy = mx, my
269				}
270			case DashRival:
271				dx, dy = -dx, -dy
272			case DashCoin:
273				if c := g.nearestCoin(me, Size); c >= 0 {
274					dx, dy = c%Size*U+U/2-me.X, c/Size*U+U/2-me.Y
275				}
276			}
277			dash(me, dx, dy, e.A*500, (e.A+2)/3, t)
278		case FxSteal:
279			if g.hits(me, o, e.B, dd, oSafe) {
280				n := e.A
281				if oBlocks {
282					n--
283				}
284				g.hit(s, n)
285			}
286		case FxPull:
287			for k := 0; k < e.A; k++ {
288				c := g.nearestCoin(me, e.B)
289				if c < 0 {
290					break
291				}
292				v := int(g.Coin[c])
293				g.Coin[c] = 0
294				g.NCoins--
295				g.collect(me, v)
296			}
297		case FxShield:
298			me.Shield = true
299		case FxStun:
300			if !oBlocks && g.hits(me, o, e.B, dd, oSafe) {
301				o.StunT = imax(o.StunT, t+e.A)
302			}
303		case FxGuard:
304			me.InvT = imax(me.InvT, t+e.A)
305		case FxKnock:
306			if g.hits(me, o, e.B, dd, oSafe) {
307				knock(o, me, e.A)
308			}
309		}
310	}
311}
312
313// PlayMoves is Play with a recipe for each side ("" for the class special).
314func PlayMoves(seed uint32, classA, classB int, inA, inB, moveA, moveB string) (int, int) {
315	return play(seed, classA, classB, inA, inB, false, MustRecipe(moveA), MustRecipe(moveB))
316}
317
318// DuelMoves is Duel with a recipe for each side ("" for the class special).
319func DuelMoves(seed uint32, classA, classB int, inA, inB, moveA, moveB string) (int, int) {
320	return play(seed, classA, classB, inA, inB, true, MustRecipe(moveA), MustRecipe(moveB))
321}
322
323// VersusMove is Versus with a recipe for side A ("" for the class special).
324// The house bot keeps its class special.
325func VersusMove(seed uint32, classA, level int, inA, moveA string) (int, int) {
326	if level < Easy || level > Hard {
327		panic("brawl: no such bot level")
328	}
329	if len(inA) > Ticks {
330		panic("brawl: inputs longer than a match")
331	}
332	g := New(seed, classA, BotClass[level][seed%3])
333	g.Move[0] = MustRecipe(moveA)
334	b := NewBot(level, seed)
335	for t := 0; t < Ticks; t++ {
336		m0, a0 := in(inA, t)
337		m1, a1 := b.Act(g, 1)
338		g.Step(m0, a0, m1, a1)
339	}
340	return g.F[0].Gold, g.F[1].Gold
341}
342
343var dashModes = [4]string{"the way you steer", "at the rival", "to the nearest gold", "away from the rival"}
344
345// squares writes a distance in squares: 1450 is "1.45", 2400 is "2.4".
346func squares(d int) string {
347	s, f := strconv.Itoa(d/U), d%U
348	switch {
349	case f == 0:
350		return s
351	case f%100 == 0:
352		return s + "." + strconv.Itoa(f/100)
353	}
354	return s + "." + strconv.Itoa(f/10)
355}
356
357// Describe says what one effect does, in words.
358func (e Effect) Describe() string {
359	a, b := strconv.Itoa(e.A), e.B
360	switch e.Kind {
361	case FxDash:
362		return "dash " + squares(e.A*500) + " squares " + dashModes[b]
363	case FxSteal:
364		return "steal " + a + " gold within " + squares(reach[b]) + " squares"
365	case FxPull:
366		if e.A == 1 {
367			return "pull the nearest coin within " + strconv.Itoa(b) + " squares"
368		}
369		return "pull up to " + a + " coins within " + strconv.Itoa(b) + " squares"
370	case FxShield:
371		return "raise a shield"
372	case FxStun:
373		return "stun the rival " + a + " ticks within " + squares(reach[b]) + " squares"
374	case FxGuard:
375		return "untouchable for " + a + " ticks"
376	case FxKnock:
377		return "knock the rival back (power " + a + ") within " + squares(reach[b]) + " squares"
378	}
379	return "?"
380}
381
382// Describe says what the recipe does, in words.
383func (r *Recipe) Describe() string {
384	s := ""
385	for i, e := range r.Fx {
386		if i > 0 {
387			s += ", then "
388		}
389		s += e.Describe()
390	}
391	return s
392}