package brawl // House bots: the opponents you fight in the browser, at three levels. They // react to the match every tick, so they are fair rivals, and they are // deterministic, so the chain can replay a match against them exactly. // Bot levels. const ( Easy = iota Normal Hard ) type botLevel struct { think int // ticks between decisions idle int // per mille chance to stand still for a decision fight int // per mille chance to go after the rival and use buttons reach int // how far it looks for gold first, in squares guard int // per mille chance, each tick, to block a rival in reach who can swing } var botLevels = [3]botLevel{ {think: 4, idle: 30, fight: 600, reach: 8, guard: 0}, {think: 3, idle: 80, fight: 900, reach: 8, guard: 150}, {think: 2, idle: 0, fight: 200, reach: 8, guard: 250}, } // BotClass is the class each level plays on each arena. var BotClass = [3][3]int{ {Coward, Trickster, Reader}, {Hunter, Brawler, Reader}, {Brawler, Hunter, Trickster}, } // A Bot plays one side of a match. type Bot struct { Level int rng uint32 wait int dir int target int // square it is heading for, -1 for none rescan int // decisions left before it looks for a better coin stuck int lastX int lastY int turn int } func NewBot(level int, seed uint32) *Bot { return &Bot{Level: level, rng: seed*2654435761 + 1, dir: Still, target: -1} } func (b *Bot) rand(n int) int { b.rng = b.rng*1103515245 + 12345 return int(b.rng>>8) % n } // toward picks one of the 16 directions for (dx, dy) with a few comparisons // (tan 11.25, 33.75, 56.25 and 78.75 degrees); Still if both are 0. func toward(dx, dy int) int { if dx == 0 && dy == 0 { return Still } ax, ay := abs(dx), abs(dy) j := 4 if ay*1000 < ax*199 { j = 0 } else if ay*1000 < ax*668 { j = 1 } else if ay*1000 < ax*1497 { j = 2 } else if ay*1000 < ax*5027 { j = 3 } if dx >= 0 && dy >= 0 { return j } if dx < 0 && dy >= 0 { return 8 - j } if dx < 0 { return 8 + j } return (16 - j) % 16 } // ringCoin looks for gold ring by ring outward from the fighter's square, // up to r squares away, and stops one ring after the first find. -1 if none. func (g *Game) ringCoin(me *Fighter, r int) int { cx, cy := me.X/U, me.Y/U best, bd, found := -1, 0, -1 for k := 0; k <= r; k++ { if found >= 0 && k > found+1 { break } for y := cy - k; y <= cy+k; y++ { if y < 0 || y >= Size { continue } step := 2 * k if y == cy-k || y == cy+k || k == 0 { step = 1 } for x := cx - k; x <= cx+k; x += step { if x < 0 || x >= Size || g.Coin[y*Size+x] == 0 { continue } dx, dy := x*U+U/2-me.X, y*U+U/2-me.Y if d := dx*dx + dy*dy; best < 0 || d < bd { best, bd = y*Size+x, d if found < 0 { found = k } } } } } return best } // looseCoin finds gold on the fighter's square or one of the 8 round it (the // first in ringX order), or -1. func (g *Game) looseCoin(me *Fighter) int { cx, cy := me.X/U, me.Y/U if c := cy*Size + cx; g.Coin[c] != 0 { return c } for k := 0; k < 8; k++ { x, y := cx+ringX[k], cy+ringY[k] if x >= 0 && y >= 0 && x < Size && y < Size && g.Coin[y*Size+x] != 0 { return y*Size + x } } return -1 } // Act decides the bot's move and button for this tick, as side s. func (b *Bot) Act(g *Game, s int) (int, int) { lv := botLevels[b.Level] me, o := &g.F[s], &g.F[1-s] think := b.wait == 0 if !think { b.wait-- } else { b.wait = lv.think - 1 if b.rand(1000) < lv.idle { b.dir = Still } else { tx, ty := o.X, o.Y ex, ey := o.X-me.X, o.Y-me.Y od := ex*ex + ey*ey loose := g.looseCoin(me) // gold right next to it (knocked loose, say) comes first chase := loose < 0 && o.Gold > 0 && od < 2500*2500 && (o.Gold >= me.Gold || od < 1600*1600) && b.rand(1000) < lv.fight if !chase { if loose >= 0 { b.target = loose } else if g.Coin[Mid*Size+Mid] == 3 { b.target = Mid*Size + Mid // the jackpot is worth the trip } else if b.target < 0 || g.Coin[b.target] == 0 || b.rescan <= 0 { b.target = g.ringCoin(me, lv.reach) b.rescan = 10 } 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 { // walking into a wall: slide round it for a moment 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 act := None if lv.fight > 0 { t := g.T ex, ey := me.X-o.X, me.Y-o.Y dd := ex*ex + ey*ey if think && t >= me.CD && b.pays(g, me, o, dd) { act = Special } else if dd <= Melee*Melee && o.Gold > 0 && t >= me.AtkCD { act = Attack } else if lv.guard > 0 && dd <= Melee*Melee && t >= o.AtkCD && t >= me.BlockCD && me.Gold > 0 && b.rand(1000) < lv.guard { act = Block } } return b.dir, act } func (b *Bot) pays(g *Game, me, o *Fighter, dd int) bool { return specialPays(g, me, o, dd, b.dir, b.target) } // specialPays says whether the class special is worth firing now: dir is where the // bot is heading and target the coin square it wants (-1 for none). func specialPays(g *Game, me, o *Fighter, dd, dir, target int) bool { switch me.Class { case Hunter: return dd <= Long*Long && o.Gold > 0 && g.clear(me, o) case Brawler, Trickster: return dd <= Melee*Melee && o.Gold > 0 case Coward: return dd < 2000*2000 && me.Gold > 0 && dir != Still case Reader: return target >= 0 && g.Coin[target] != 0 && abs(target%Size*U+U/2-me.X) <= Magnet*U && abs(target/Size*U+U/2-me.Y) <= Magnet*U } return false } // Versus replays a run (side A's inputs) against a house bot on side B. func Versus(seed uint32, classA, level int, inA 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]) b := NewBot(level, seed) for t := 0; t < Ticks; t++ { m0, a0 := Still, None if t < len(inA) { c := inA[t] if c >= 128 || decMove[c] < 0 { panic("brawl: bad input character") } m0, a0 = decMove[c], decAct[c] } m1, a1 := b.Act(g, 1) g.Step(m0, a0, m1, a1) } return g.F[0].Gold, g.F[1].Gold }