package brawl import ( "errors" "strconv" ) // Move recipes: a special move written as data, so anyone can design one and // both the chain and the browser run it exactly. A fighter with a recipe uses // it instead of its class special; everything else about the match is the same. // // A recipe is written as one short string: // // name/cooldown/effects // // Blink Thief/84/D82S11 dash 4 squares to the nearest gold, then steal 1 in melee // // The name is 1 to 16 letters, digits, spaces, '-' or '\'' (no space at either // end). The cooldown is in ticks (10 a second), MinCD to MaxCD, written without // leading zeros. Then come 1 to 3 effects, three characters each: a letter and // two digits. They fire in order, the moment the move is used: // // Dxm dash x half-squares (1-8) in mode m: 0 the way you steer (away from // the rival if you stand still), 1 at the rival, 2 to the nearest gold, // 3 away from the rival // Snr knock n gold (1-3) loose from the rival within range r (onto the // floor round them, like any hit); a block saves one // Pnr pull up to n coins (1-3) lying within r squares (1-5) into your purse // H00 put up a shield, like the power-up: it saves one gold from the next steal // Ttr stun the rival t ticks (1-9) within range r; a block stops it // It0 turn untouchable for t ticks (1-9) // Kpr knock the rival back with power p (1-9) within range r // // Ranges r are 1-5: 1.45, 2.4, 3.3, 4.2 or 5 squares. Beyond melee (r > 1) a // wall in the way stops the effect, like the Hunter's beam. An untouchable // rival shrugs off steal, stun and knockback. // // Budget: the cooldown must be at least the recipe's Cost, in ticks: // // D x(x+1)/2 (D82: 36) // S n(n+1)/2 * (28+4r) (S11: 32, S15: 48, S21: 96) // P n * (4r+4) (P24: 40, like the Reader's magnet) // H 80 // T t * (2r+4) // I 5t // K (p(r+1)+2)/3 // // plus half the cost of every effect but the dearest, and never under MinCD. // The numbers come from sim/browser/recipes.js: scripted players fight the // Hard bot with each built-in special and with hundreds of recipes at their // cheapest cooldown, and the best recipes only match the best built-ins. // Each letter may appear once. const ( MinCD = 20 MaxCD = 200 MaxEffects = 3 MaxName = 16 ) // Effect letters. const ( FxDash = 'D' FxSteal = 'S' FxPull = 'P' FxShield = 'H' FxStun = 'T' FxGuard = 'I' FxKnock = 'K' ) // Dash modes. const ( DashSteer = iota DashRival DashCoin DashAway ) // reach is the distance for ranges 1..5. var reach = [6]int{0, Melee, 2400, 3300, 4200, Long} type Effect struct { Kind byte A, B int } type Recipe struct { Name string CD int Fx []Effect } // Cost is what one effect adds to the cooldown a recipe needs, in ticks. func (e Effect) Cost() int { a, b := e.A, e.B switch e.Kind { case FxDash: return a * (a + 1) / 2 case FxSteal: return a * (a + 1) / 2 * (28 + 4*b) case FxPull: return a * (4*b + 4) case FxShield: return 80 case FxStun: return a * (2*b + 4) case FxGuard: return 5 * a case FxKnock: return (a*(b+1) + 2) / 3 } return 0 } // Cost is the shortest cooldown the recipe's effects allow: the sum of the // effects' costs, plus half of all but the dearest one, since moves that do // several things at once are worth more than their parts. Never below MinCD. func (r *Recipe) Cost() int { c, top := 0, 0 for _, e := range r.Fx { v := e.Cost() c += v if v > top { top = v } } c += (c - top) / 2 if c < MinCD { c = MinCD } return c } // limits holds the allowed range of each effect's two digits. func limits(k byte) (int, int, int, int, bool) { switch k { case FxDash: return 1, 8, 0, 3, true case FxSteal, FxPull, FxKnock: hi := 3 if k == FxKnock { hi = 9 } return 1, hi, 1, 5, true case FxShield: return 0, 0, 0, 0, true case FxStun: return 1, 9, 1, 5, true case FxGuard: return 1, 9, 0, 0, true } return 0, 0, 0, 0, false } func nameChar(c byte) bool { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == ' ' || c == '-' || c == '\'' } // ParseRecipe reads and checks a recipe string. A valid recipe always writes // back to the same string, so each move has exactly one spelling. func ParseRecipe(s string) (*Recipe, error) { a, b := -1, -1 for i := 0; i < len(s); i++ { if s[i] == '/' { if a < 0 { a = i } else if b < 0 { b = i } else { return nil, errors.New("recipe: name/cooldown/effects") } } } if b < 0 { return nil, errors.New("recipe: name/cooldown/effects") } name, cds, fx := s[:a], s[a+1:b], s[b+1:] if len(name) == 0 || len(name) > MaxName || name[0] == ' ' || name[len(name)-1] == ' ' { return nil, errors.New("recipe: name is 1 to 16 characters, no space at either end") } for i := 0; i < len(name); i++ { if !nameChar(name[i]) { return nil, errors.New("recipe: name uses letters, digits, spaces, - and ' only") } } if len(cds) == 0 || len(cds) > 3 || cds[0] == '0' { return nil, errors.New("recipe: bad cooldown") } cd := 0 for i := 0; i < len(cds); i++ { if cds[i] < '0' || cds[i] > '9' { return nil, errors.New("recipe: bad cooldown") } cd = cd*10 + int(cds[i]-'0') } if cd < MinCD || cd > MaxCD { return nil, errors.New("recipe: cooldown is " + strconv.Itoa(MinCD) + " to " + strconv.Itoa(MaxCD) + " ticks") } if len(fx) == 0 || len(fx)%3 != 0 || len(fx) > 3*MaxEffects { return nil, errors.New("recipe: 1 to 3 effects of 3 characters") } r := &Recipe{Name: name, CD: cd} for i := 0; i < len(fx); i += 3 { k, x, y := fx[i], int(fx[i+1])-'0', int(fx[i+2])-'0' alo, ahi, blo, bhi, ok := limits(k) if !ok { return nil, errors.New("recipe: unknown effect " + fx[i:i+1]) } if x < alo || x > ahi || y < blo || y > bhi { return nil, errors.New("recipe: effect " + fx[i:i+3] + " out of range") } for _, e := range r.Fx { if e.Kind == k { return nil, errors.New("recipe: effect " + fx[i:i+1] + " twice") } } r.Fx = append(r.Fx, Effect{Kind: k, A: x, B: y}) } if c := r.Cost(); cd < c { return nil, errors.New("recipe: these effects need a cooldown of at least " + strconv.Itoa(c)) } return r, nil } // MustRecipe is ParseRecipe that panics on a bad recipe. "" gives nil: the // class's own special. func MustRecipe(s string) *Recipe { if s == "" { return nil } r, err := ParseRecipe(s) if err != nil { panic(err.Error()) } return r } // String writes the recipe back in its one spelling. func (r *Recipe) String() string { b := []byte(r.Name + "/" + strconv.Itoa(r.CD) + "/") for _, e := range r.Fx { b = append(b, e.Kind, byte('0'+e.A), byte('0'+e.B)) } return string(b) } // Equip gives side s a recipe in place of its class special; nil takes it away. func (g *Game) Equip(s int, r *Recipe) { g.Move[s] = r } // hits reports whether a rival-targeting effect with range r lands. func (g *Game) hits(me, o *Fighter, r, dd int, oSafe bool) bool { d := reach[r] return !oSafe && dd <= d*d && (r == 1 || g.clear(me, o)) } // recipe runs side s's recipe; act has already paid the cooldown. func (g *Game) recipe(s int, rec *Recipe, mx, my, dd int, oBlocks, oSafe bool) { t := g.T me, o := &g.F[s], &g.F[1-s] for _, e := range rec.Fx { switch e.Kind { case FxDash: dx, dy := me.X-o.X, me.Y-o.Y // away switch e.B { case DashSteer: if mx != 0 || my != 0 { dx, dy = mx, my } case DashRival: dx, dy = -dx, -dy case DashCoin: if c := g.nearestCoin(me, Size); c >= 0 { dx, dy = c%Size*U+U/2-me.X, c/Size*U+U/2-me.Y } } dash(me, dx, dy, e.A*500, (e.A+2)/3, t) case FxSteal: if g.hits(me, o, e.B, dd, oSafe) { n := e.A if oBlocks { n-- } g.hit(s, n) } case FxPull: for k := 0; k < e.A; k++ { c := g.nearestCoin(me, e.B) if c < 0 { break } v := int(g.Coin[c]) g.Coin[c] = 0 g.NCoins-- g.collect(me, v) } case FxShield: me.Shield = true case FxStun: if !oBlocks && g.hits(me, o, e.B, dd, oSafe) { o.StunT = imax(o.StunT, t+e.A) } case FxGuard: me.InvT = imax(me.InvT, t+e.A) case FxKnock: if g.hits(me, o, e.B, dd, oSafe) { knock(o, me, e.A) } } } } // PlayMoves is Play with a recipe for each side ("" for the class special). func PlayMoves(seed uint32, classA, classB int, inA, inB, moveA, moveB string) (int, int) { return play(seed, classA, classB, inA, inB, false, MustRecipe(moveA), MustRecipe(moveB)) } // DuelMoves is Duel with a recipe for each side ("" for the class special). func DuelMoves(seed uint32, classA, classB int, inA, inB, moveA, moveB string) (int, int) { return play(seed, classA, classB, inA, inB, true, MustRecipe(moveA), MustRecipe(moveB)) } // VersusMove is Versus with a recipe for side A ("" for the class special). // The house bot keeps its class special. func VersusMove(seed uint32, classA, level int, inA, moveA string) (int, int) { if level < Easy || level > Hard { panic("brawl: no such bot level") } if len(inA) > Ticks { panic("brawl: inputs longer than a match") } g := New(seed, classA, BotClass[level][seed%3]) g.Move[0] = MustRecipe(moveA) b := NewBot(level, seed) for t := 0; t < Ticks; t++ { m0, a0 := in(inA, t) m1, a1 := b.Act(g, 1) g.Step(m0, a0, m1, a1) } return g.F[0].Gold, g.F[1].Gold } var dashModes = [4]string{"the way you steer", "at the rival", "to the nearest gold", "away from the rival"} // squares writes a distance in squares: 1450 is "1.45", 2400 is "2.4". func squares(d int) string { s, f := strconv.Itoa(d/U), d%U switch { case f == 0: return s case f%100 == 0: return s + "." + strconv.Itoa(f/100) } return s + "." + strconv.Itoa(f/10) } // Describe says what one effect does, in words. func (e Effect) Describe() string { a, b := strconv.Itoa(e.A), e.B switch e.Kind { case FxDash: return "dash " + squares(e.A*500) + " squares " + dashModes[b] case FxSteal: return "steal " + a + " gold within " + squares(reach[b]) + " squares" case FxPull: if e.A == 1 { return "pull the nearest coin within " + strconv.Itoa(b) + " squares" } return "pull up to " + a + " coins within " + strconv.Itoa(b) + " squares" case FxShield: return "raise a shield" case FxStun: return "stun the rival " + a + " ticks within " + squares(reach[b]) + " squares" case FxGuard: return "untouchable for " + a + " ticks" case FxKnock: return "knock the rival back (power " + a + ") within " + squares(reach[b]) + " squares" } return "?" } // Describe says what the recipe does, in words. func (r *Recipe) Describe() string { s := "" for i, e := range r.Fx { if i > 0 { s += ", then " } s += e.Describe() } return s }