package brawl import ( "errors" "strconv" ) // Bot profiles: a bot written as a handful of whole-number traits instead of // code, so a player's own bot fits in one short string on chain. The page // learns the traits from how you play; NewProfileBot plays with them. // // The string is eleven letter-digit pairs in this order, for example // "c1t3i1f6j8r6a7b2s5d4k0": // // c class it plays, 0..4 (Hunter, Brawler, Coward, Trickster, Reader) // t ticks between plans, 3..9 (lower re-plans more often) // i chance to stand still when it plans, 5% per step, 0..9 // f chance to chase a rival who carries gold, 11% per step, 0..9 // j chance to go for the jackpot when it is out, 11% per step, 0..9 // r how far it looks for gold, in squares, 1..8 // a chance to swing at a rival in reach, each tick, 11% per step, 0..9 // b chance to block a rival in reach, each tick, 11% per step, 0..9 // s chance to fire its special when it pays, 11% per step, 0..9 // d chance to dodge right after losing gold, 11% per step, 0..9 // k runs from a rival closer than this many squares while ahead, 0..5 (0 never) // // The house levels (Easy, Normal, Hard) are left as they are. // ProfileLen is the length of an encoded profile. const ProfileLen = 22 const profileKeys = "ctifjrabsdk" var profileLo = [11]int{0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0} var profileHi = [11]int{4, 9, 9, 9, 9, 8, 9, 9, 9, 9, 5} type Profile struct { Class int Think int Idle int Chase int Jackpot int Reach int Attack int Block int Special int Dodge int Keep int } func (p *Profile) field(i int) *int { switch i { case 0: return &p.Class case 1: return &p.Think case 2: return &p.Idle case 3: return &p.Chase case 4: return &p.Jackpot case 5: return &p.Reach case 6: return &p.Attack case 7: return &p.Block case 8: return &p.Special case 9: return &p.Dodge } return &p.Keep } // ParseProfile reads an encoded profile, refusing anything out of range. func ParseProfile(s string) (*Profile, error) { if len(s) != ProfileLen { return nil, errors.New("profile: 11 letter-digit pairs, like c1t3i1f6j8r6a7b2s5d4k0") } p := &Profile{} for i := 0; i < 11; i++ { if s[2*i] != profileKeys[i] { return nil, errors.New("profile: expected " + profileKeys[i:i+1] + " at " + strconv.Itoa(2*i+1)) } v := int(s[2*i+1]) - '0' if v < profileLo[i] || v > profileHi[i] { return nil, errors.New("profile: " + profileKeys[i:i+1] + " is " + strconv.Itoa(profileLo[i]) + " to " + strconv.Itoa(profileHi[i])) } *p.field(i) = v } return p, nil } // MustProfile is ParseProfile that panics on a bad profile. func MustProfile(s string) *Profile { p, err := ParseProfile(s) if err != nil { panic(err.Error()) } return p } // String writes the profile back in its encoded form. func (p *Profile) String() string { b := make([]byte, ProfileLen) for i := 0; i < 11; i++ { b[2*i], b[2*i+1] = profileKeys[i], byte('0'+*p.field(i)) } return string(b) } // A ProfileBot plays one side of a match with a profile's traits. It moves // like the house bot (plan, head for gold or the rival, slide round walls) // and rolls its own dice for every choice, so a replay is exact. type ProfileBot struct { P Profile rng uint32 wait int dir int target int rescan int stuck int lastX int lastY int turn int gold int } func NewProfileBot(p *Profile, seed uint32) *ProfileBot { return &ProfileBot{P: *p, rng: seed*2654435761 + 7, dir: Still, target: -1} } func (b *ProfileBot) rand(n int) int { b.rng = b.rng*1103515245 + 12345 return int(b.rng>>8) % n } // Act decides the bot's move and button for this tick, as side s. func (b *ProfileBot) Act(g *Game, s int) (int, int) { p := &b.P me, o := &g.F[s], &g.F[1-s] ex, ey := o.X-me.X, o.Y-me.Y od := ex*ex + ey*ey think := b.wait == 0 if !think { b.wait-- } else { b.wait = p.Think - 1 if b.rand(1000) < p.Idle*50 { b.dir = Still } else { kr := p.Keep * U if me.Gold > o.Gold && od < kr*kr { b.dir = toward(-ex, -ey) // ahead: keep the rival away } else { tx, ty := o.X, o.Y if !(o.Gold > 0 && od < 3000*3000 && b.rand(1000) < p.Chase*111) { if g.Coin[Mid*Size+Mid] == 3 && b.rand(1000) < p.Jackpot*111 { b.target = Mid*Size + Mid } else if b.rescan <= 0 || b.target >= 0 && (g.Coin[b.target] == 0 || b.target == Mid*Size+Mid) { // look again; after an empty look, wait 3 plans (it is the // costly part of a replay when the floor is bare) b.target = g.ringCoin(me, p.Reach) b.rescan = 10 if b.target < 0 { b.rescan = 3 } } b.rescan-- if b.target >= 0 { tx, ty = b.target%Size*U+U/2, b.target/Size*U+U/2 } } b.dir = toward(tx-me.X, ty-me.Y) } if b.stuck > 2 { if b.turn == 0 { b.dir = (b.dir + 4) % 16 } else { b.dir = (b.dir + 12) % 16 } b.turn = 1 - b.turn b.stuck = 0 b.wait = 3 } } } if b.dir != Still && abs(me.X-b.lastX)+abs(me.Y-b.lastY) < 60 { b.stuck++ } else { b.stuck = 0 } b.lastX, b.lastY = me.X, me.Y lost := me.Gold < b.gold b.gold = me.Gold t := g.T near := od <= Melee*Melee act := None if lost && t >= me.DodgeCD && b.rand(1000) < p.Dodge*111 { act = Dodge } else if think && t >= me.CD && specialPays(g, me, o, od, b.dir, b.target) && b.rand(1000) < p.Special*111 { act = Special } else if near && o.Gold > 0 && t >= me.AtkCD && b.rand(1000) < p.Attack*111 { act = Attack } else if near && t >= me.BlockCD && b.rand(1000) < p.Block*111 { act = Block } return b.dir, act } // VersusProfile replays a run (side A's inputs) against a profile bot on // side B, which plays the profile's class. func VersusProfile(seed uint32, classA int, profile, inA string) (int, int) { if classA < Hunter || classA > Reader { panic("brawl: no such class") } if len(inA) > Ticks { panic("brawl: inputs longer than a match") } p := MustProfile(profile) g := New(seed, classA, p.Class) b := NewProfileBot(p, 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 }