// Package brawl is the free-movement arena: two fighters run around a 15x15 // floor for one minute grabbing gold and robbing each other. It is written // for the chain, so it uses whole numbers only and runs at 10 ticks a second. // The browser runs an exact copy, so a match played there gives the same // result when the chain replays the inputs. // // Units: 1000 = one square. x grows right, y grows down. Fighter A starts // near (1,1), fighter B near the far corner. // // It is built to be cheap to replay on chain: // - coins and power-ups sit in the middle of a square, and you grab one by // standing on its square, so a tick needs one lookup instead of a loop; // - timers hold the tick they run out on, so nothing counts down; // - wall checks only run next to walls. package brawl const ( U = 1000 Size = 15 SizeU = Size * U Cells = Size * Size Mid = Size / 2 // middle square TPS = 10 Ticks = 60 * TPS // match length RushTicks = 10 * TPS // last 10 s are a gold rush Speed = 420 // per tick Rad = 380 // fighter radius Melee = 1450 Long = 5000 Magnet = 4 // squares StartCoins = 15 MaxCoins = 22 CoinEvery = 30 RushEvery = 9 PowerEvery = 120 FirstPower = 80 AttackCD = 8 BlockCD = 9 BlockT = 5 FeintT = 6 DodgeCD = 13 Decay = 500 // knockback kept per tick, per mille KnockMul = 66 // knockback per point of power, per tick SubStep = 400 // longest move between wall checks ) // Classes. const ( Hunter = iota Brawler Coward Trickster Reader ) // Actions. const ( None = iota Attack Block Special Dodge ) // Still means no movement this tick; 0..15 are directions. const Still = 16 var SpecialCD = [5]int{42, 80, 34, 70, 38} // Arenas are drawn by hand. Each list holds one square of every mirrored // pair as x, y; its twin sits at (14-x, 14-y). var arenaNames = [3]string{"Tavern", "Rooftop", "Scrapyard"} var layouts = [3][3][]int{ { // Tavern: tables everywhere, two kegs that kick you forward {7, 2, 4, 3, 10, 3, 2, 6, 5, 6, 9, 5, 12, 2}, {1, 9, 7, 4}, {}, }, { // Rooftop: open, full of speed pads {7, 3, 3, 7, 4, 10}, {2, 5, 5, 2, 12, 3, 5, 5, 9, 1}, {}, }, { // Scrapyard: crates and traps that knock gold out of you {7, 2, 2, 7, 10, 5, 5, 4}, {7, 4}, {5, 1, 1, 5, 9, 7, 12, 3}, }, } // ArenaName names the arena a seed plays on. func ArenaName(seed uint32) string { return arenaNames[seed%3] } // 16 directions, per mille, starting east and turning towards +y; 16 is still. var dirX = [17]int{1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0, 383, 707, 924, 0} var dirY = [17]int{0, 383, 707, 924, 1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0} // running speed per direction (dir * Speed / 1000), precomputed var runX, runY [17]int func init() { for i := 0; i < 17; i++ { runX[i], runY[i] = dirX[i]*Speed/1000, dirY[i]*Speed/1000 } } // Square kinds. const ( Empty = iota Wall Pad Trap ) // Power-up kinds, stored as kind+1 in Game.Power. const ( Shield = iota Double Recharge ) // Fighter timers (CD .. DodgeCD) hold the tick they run out on. type Fighter struct { Class int X, Y, VX, VY int Gold int Shield bool Double bool CD int // special AtkCD int BlockCD int BlockT int FeintT int InvT int DashT int DashX, DashY int StunT int TrapT int BoostT int PadT int SwingT int DodgeCD int Face int Cell int // square it stands on; things on a square happen when you step onto it } type Game struct { Seed uint32 Arena int T int // ticks played End int // match length in ticks (Ticks unless set with NewLength) F [2]Fighter Move [2]*Recipe // a recipe used in place of the class special, or nil Floor [Cells]byte // Empty, Wall, Pad, Trap Coin [Cells]byte // gold on each square: 0, 1, or 3 for the jackpot Power [Cells]byte // power-up on each square: 0, or kind+1 NCoins int NPower int nearWall [Cells]bool nextCoin int // tick of the next coin drop nextPower int powerKind int jackpot bool rushOn bool rng uint32 } func (g *Game) rand() int { g.rng = g.rng*1103515245 + 12345 return int(g.rng >> 8) // 0 .. 2^24-1 } func abs(v int) int { if v < 0 { return -v } return v } func imax(a, b int) int { if a > b { return a } return b } // isqrt is the whole-number square root, rounded down. func isqrt(n int) int { if n <= 0 { return 0 } r, b := 0, 1<<30 for b > n { b >>= 2 } for b != 0 { if n >= r+b { n -= r + b r = (r >> 1) + b } else { r >>= 1 } b >>= 2 } return r } func (g *Game) floor(x, y int) byte { if x < 0 || y < 0 || x >= Size || y >= Size { return Empty } return g.Floor[y*Size+x] } // New sets up a match. The seed picks the arena and lays out terrain and coins. func New(seed uint32, classA, classB int) *Game { g := &Game{Seed: seed, Arena: int(seed % 3), End: Ticks} g.rng = seed*7919 + 17 for k, list := range layouts[g.Arena] { for i := 0; i+1 < len(list); i += 2 { c := list[i+1]*Size + list[i] g.Floor[c] = byte(k + 1) g.Floor[Cells-1-c] = byte(k + 1) } } for c := 0; c < Cells; c++ { if g.Floor[c] != Wall { continue } x, y := c%Size, c/Size for dy := -1; dy <= 1; dy++ { for dx := -1; dx <= 1; dx++ { if x+dx >= 0 && y+dy >= 0 && x+dx < Size && y+dy < Size { g.nearWall[(y+dy)*Size+x+dx] = true } } } } g.F[0] = Fighter{Class: classA, X: 1200, Y: 1200, Face: 1} g.F[1] = Fighter{Class: classB, X: SizeU - 1200, Y: SizeU - 1200, Face: -1} g.Coin[Mid*Size+Mid] = 1 g.NCoins = 1 for i := 0; i < 200 && g.NCoins < StartCoins; i++ { c := g.rand() * Cells >> 24 x, y := c%Size, c/Size if abs(x-Mid) <= 1 && abs(y-Mid) <= 1 { continue } g.spawnAt(c, false, 1) } g.F[0].Cell, g.F[1].Cell = -1, -1 g.nextCoin = CoinEvery g.nextPower = FirstPower g.powerKind = int(seed % 3) return g } // free reports whether a square can take a new coin or power-up. func (g *Game) free(c int) bool { if g.Floor[c] != Empty || g.Coin[c] != 0 || g.Power[c] != 0 { return false } x, y := c%Size, c/Size if x == 0 || y == 0 || x == Size-1 || y == Size-1 { return false } for i := 0; i < 2; i++ { if g.F[i].X/U == x && g.F[i].Y/U == y { return false } } return true } // spawnAt puts a mirrored pair on square c and its opposite, if both are free. func (g *Game) spawnAt(c int, power bool, val int) bool { m := Cells - 1 - c if c == m || !g.free(c) || !g.free(m) { return false } if power { g.Power[c], g.Power[m] = byte(val+1), byte(val+1) g.NPower += 2 } else { g.Coin[c], g.Coin[m] = byte(val), byte(val) g.NCoins += 2 } return true } func (g *Game) spawnPair(power bool, val int) { for i := 0; i < 40; i++ { if g.spawnAt(g.rand()*Cells>>24, power, val) { return } } } func (g *Game) Over() bool { return g.T >= g.End } // NewLength is New for a longer or shorter match (rooms use it); the jackpot // still drops at half time and the gold rush fills the last 10 seconds. func NewLength(seed uint32, classA, classB, ticks int) *Game { if ticks < 2*RushTicks { panic("brawl: match too short") } g := New(seed, classA, classB) g.End = ticks return g } // Winner is 0 or 1, or -1 for a draw. func (g *Game) Winner() int { a, b := g.F[0].Gold, g.F[1].Gold if a > b { return 0 } if b > a { return 1 } return -1 } // Step runs one tick. move is a direction 0..15 or Still; act is an action. func (g *Game) Step(move0, act0, move1, act1 int) { t := g.T if t >= g.End { return } // actions first; the side that goes first swaps every tick if act0 != None || act1 != None { if t%2 == 0 { g.act(0, move0, act0) g.act(1, move1, act1) } else { g.act(1, move1, act1) g.act(0, move0, act0) } } a, b := &g.F[0], &g.F[1] g.move(a, move0, t) g.move(b, move1, t) dx, dy := b.X-a.X, b.Y-a.Y if dx < 2*Rad && dx > -2*Rad && dy < 2*Rad && dy > -2*Rad { if dd := dx*dx + dy*dy; dd < 4*Rad*Rad && dd > 0 { d := isqrt(dd) push := (2*Rad - d) / 2 nx, ny := dx*push/d, dy*push/d a.X -= nx a.Y -= ny b.X += nx b.Y += ny g.collide(a) g.collide(b) } } t++ g.T = t if c := a.Y/U*Size + a.X/U; c != a.Cell { a.Cell = c g.land(a, t) } if c := b.Y/U*Size + b.X/U; c != b.Cell { b.Cell = c g.land(b, t) } if t == g.End/2 && !g.jackpot { g.jackpot = true c := Mid*Size + Mid if g.Coin[c] == 0 { g.NCoins++ } g.Coin[c] = 3 if a.Cell == c { a.Cell = -1 // grab it next tick } if b.Cell == c { b.Cell = -1 } } if t == g.End-RushTicks && !g.rushOn { g.rushOn = true g.nextCoin = t } if t >= g.nextCoin { g.nextCoin = t + CoinEvery limit := MaxCoins if g.rushOn { g.nextCoin = t + RushEvery limit += 8 } if g.NCoins+2 <= limit { g.spawnPair(false, 1) } } if t >= g.nextPower { g.nextPower = t + PowerEvery if g.NPower == 0 { kind := g.powerKind g.powerKind = (g.powerKind + 1) % 3 g.spawnPair(true, kind) } } } func (g *Game) move(me *Fighter, dir, t int) { if dir == Still && me.VX == 0 && me.VY == 0 && t >= me.DashT { return } var vx, vy int if t < me.DashT { vx, vy = me.DashX, me.DashY } else { if t < me.BoostT || t < me.StunT || t < me.BlockT { sp := Speed if t < me.BoostT { sp = Speed * 18 / 10 } if t < me.StunT { sp /= 4 } if t < me.BlockT { sp /= 2 } vx, vy = dirX[dir]*sp/1000, dirY[dir]*sp/1000 } else { vx, vy = runX[dir], runY[dir] } if me.VX != 0 || me.VY != 0 { vx += me.VX vy += me.VY me.VX = me.VX * Decay / 1000 me.VY = me.VY * Decay / 1000 } } if vx == 0 && vy == 0 { return } if vx > -SubStep && vx < SubStep && vy > -SubStep && vy < SubStep { me.X += vx me.Y += vy g.collide(me) } else { n := (imax(abs(vx), abs(vy)) + SubStep - 1) / SubStep for i := 0; i < n; i++ { me.X += vx*(i+1)/n - vx*i/n me.Y += vy*(i+1)/n - vy*i/n g.collide(me) } } if vx > 30 { me.Face = 1 } else if vx < -30 { me.Face = -1 } } func (g *Game) collide(me *Fighter) { if me.X < Rad { me.X = Rad } else if me.X > SizeU-Rad { me.X = SizeU - Rad } if me.Y < Rad { me.Y = Rad } else if me.Y > SizeU-Rad { me.Y = SizeU - Rad } if !g.nearWall[me.Y/U*Size+me.X/U] { return } x0c, x1c, y0c, y1c := (me.X-Rad)/U, (me.X+Rad)/U, (me.Y-Rad)/U, (me.Y+Rad)/U for wy := y0c; wy <= y1c; wy++ { for wx := x0c; wx <= x1c; wx++ { if wx >= Size || wy >= Size || g.Floor[wy*Size+wx] != Wall { continue } x0, x1, y0, y1 := wx*U+80, wx*U+920, wy*U+80, wy*U+920 px, py := me.X, me.Y if px < x0 { px = x0 } else if px > x1 { px = x1 } if py < y0 { py = y0 } else if py > y1 { py = y1 } dx, dy := me.X-px, me.Y-py dd := dx*dx + dy*dy if dd >= Rad*Rad { continue } if d := isqrt(dd); d > 0 { me.X = px + dx*Rad/d me.Y = py + dy*Rad/d } else { me.X = x0 - Rad } } } } // land handles whatever is on the square a fighter stands on. func (g *Game) land(me *Fighter, t int) { c := me.Y/U*Size + me.X/U if v := g.Coin[c]; v != 0 { g.Coin[c] = 0 g.NCoins-- g.collect(me, int(v)) } if p := g.Power[c]; p != 0 { g.Power[c] = 0 g.NPower-- switch int(p) - 1 { case Shield: me.Shield = true case Double: me.Double = true default: me.CD = 0 } } switch g.Floor[c] { case Trap: if t >= me.TrapT && t >= me.DashT { me.TrapT = t + 12 me.StunT = imax(me.StunT, t+3) if me.Gold > 0 { me.Gold-- } } case Pad: if t >= me.PadT { me.BoostT = t + 9 me.PadT = t + 10 } } } func (g *Game) collect(me *Fighter, val int) { if me.Double { val *= 2 me.Double = false } me.Gold += val } func (g *Game) steal(s, n int) { thief, victim := &g.F[s], &g.F[1-s] if n > 0 && victim.Shield { n-- victim.Shield = false } if n > victim.Gold { n = victim.Gold } if n <= 0 { return } victim.Gold -= n thief.Gold += n } func knock(t, from *Fighter, power int) { dx, dy := t.X-from.X, t.Y-from.Y l := isqrt(dx*dx + dy*dy) if l == 0 { l = 1 } t.VX += dx * power * KnockMul / l t.VY += dy * power * KnockMul / l } func dash(me *Fighter, dx, dy, length, ticks, t int) { l := isqrt(dx*dx + dy*dy) if l == 0 { l = 1 } me.DashT = t + ticks me.DashX = dx * length / l / ticks me.DashY = dy * length / l / ticks } func (g *Game) act(s, dir, action int) { if action == None { return } t := g.T me, o := &g.F[s], &g.F[1-s] mx, my := dirX[dir], dirY[dir] if action == Dodge { // works while dazed: it's your escape if t < me.DodgeCD { return } me.DodgeCD = t + DodgeCD me.StunT = 0 dx, dy := mx, my if dx == 0 && dy == 0 { dx, dy = me.X-o.X, me.Y-o.Y } dash(me, dx, dy, 2400, 2, t) me.InvT = imax(me.InvT, t+4) return } if t < me.StunT { return } dx, dy := me.X-o.X, me.Y-o.Y dd := dx*dx + dy*dy oBlocks := t < o.BlockT || t < o.FeintT oSafe := t < o.InvT switch action { case Attack: if t < me.AtkCD { return } me.AtkCD = t + AttackCD me.SwingT = t + 3 if dd > Melee*Melee || oSafe { return } if oBlocks { g.steal(1-s, 1) me.StunT = t + 5 me.AtkCD = t + 9 } else { g.steal(s, 1) knock(o, me, 3) } case Block: if t < me.BlockCD { return } me.BlockCD = t + BlockCD me.BlockT = t + BlockT case Special: if t < me.CD { return } if r := g.Move[s]; r != nil { me.CD = t + r.CD g.recipe(s, r, mx, my, dd, oBlocks, oSafe) return } me.CD = t + SpecialCD[me.Class] g.special(s, mx, my, dd, oBlocks, oSafe) } } func (g *Game) special(s, mx, my, dd int, oBlocks, oSafe bool) { t := g.T me, o := &g.F[s], &g.F[1-s] switch me.Class { case Hunter: if dd <= Long*Long && !oSafe && g.clear(me, o) { g.steal(s, 1) knock(o, me, 2) } case Brawler: if dd <= (Melee+150)*(Melee+150) && !oSafe { n := 2 if oBlocks { n = 1 } g.steal(s, n) knock(o, me, 9) o.StunT = imax(o.StunT, t+4) } else { dash(me, o.X-me.X, o.Y-me.Y, 1400, 2, t) } case Coward: dx, dy := mx, my if dx == 0 && dy == 0 { if c := g.nearestCoin(me, Size); c >= 0 { dx, dy = c%Size*U+U/2-me.X, c/Size*U+U/2-me.Y } else { dx, dy = me.X-o.X, me.Y-o.Y } } dash(me, dx, dy, 4000, 3, t) me.InvT = t + 7 case Trickster: me.FeintT = t + FeintT if dd <= Melee*Melee && t >= o.SwingT && !oSafe { g.steal(s, 1) } case Reader: for k := 0; k < 2; k++ { c := g.nearestCoin(me, Magnet) if c < 0 { break } v := int(g.Coin[c]) g.Coin[c] = 0 g.NCoins-- g.collect(me, v) } } } // nearestCoin finds the closest square with gold, measured from the fighter // to the middle of the square, within r squares. -1 if none. func (g *Game) nearestCoin(me *Fighter, r int) int { cx, cy := me.X/U, me.Y/U best, bd := -1, 0 lim := (r*U + U/2) * (r*U + U/2) for y := cy - r; y <= cy+r; y++ { if y < 0 || y >= Size { continue } for x := cx - r; x <= cx+r; x++ { 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 d := dx*dx + dy*dy if d <= lim && (best < 0 || d < bd) { best, bd = y*Size+x, d } } } return best } // clear reports whether the straight line between two fighters misses every wall. func (g *Game) clear(a, b *Fighter) bool { dx, dy := b.X-a.X, b.Y-a.Y steps := (isqrt(dx*dx+dy*dy) + 199) / 200 for i := 1; i < steps; i++ { x := a.X + dx*i/steps y := a.Y + dy*i/steps cx, cy := x/U, y/U if g.Floor[cy*Size+cx] == Wall { lx, ly := x-cx*U, y-cy*U if lx > 100 && lx < 900 && ly > 100 && ly < 900 { return false } } } return true }