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