brawl.gno
15.44 Kb · 750 lines
1// Package brawl is the free-movement arena: two fighters run around a 15x15
2// floor for one minute grabbing gold and robbing each other. It is written
3// for the chain, so it uses whole numbers only and runs at 10 ticks a second.
4// The browser runs an exact copy, so a match played there gives the same
5// result when the chain replays the inputs.
6//
7// Units: 1000 = one square. x grows right, y grows down. Fighter A starts
8// near (1,1), fighter B near the far corner.
9//
10// It is built to be cheap to replay on chain:
11// - coins and power-ups sit in the middle of a square, and you grab one by
12// standing on its square, so a tick needs one lookup instead of a loop;
13// - timers hold the tick they run out on, so nothing counts down;
14// - wall checks only run next to walls.
15package brawl
16
17const (
18 U = 1000
19 Size = 15
20 SizeU = Size * U
21 Cells = Size * Size
22 Mid = Size / 2 // middle square
23
24 TPS = 10
25 Ticks = 60 * TPS // match length
26 RushTicks = 10 * TPS // last 10 s are a gold rush
27
28 Speed = 420 // per tick
29 Rad = 380 // fighter radius
30 Melee = 1450
31 Long = 5000
32 Magnet = 4 // squares
33
34 StartCoins = 15
35 MaxCoins = 22
36 CoinEvery = 30
37 RushEvery = 9
38 PowerEvery = 120
39 FirstPower = 80
40
41 AttackCD = 8
42 BlockCD = 9
43 BlockT = 5
44 FeintT = 6
45 DodgeCD = 13
46
47 Decay = 500 // knockback kept per tick, per mille
48 KnockMul = 66 // knockback per point of power, per tick
49 SubStep = 400 // longest move between wall checks
50)
51
52// Classes.
53const (
54 Hunter = iota
55 Brawler
56 Coward
57 Trickster
58 Reader
59)
60
61// Actions.
62const (
63 None = iota
64 Attack
65 Block
66 Special
67 Dodge
68)
69
70// Still means no movement this tick; 0..15 are directions.
71const Still = 16
72
73var SpecialCD = [5]int{42, 80, 34, 70, 38}
74
75// Arenas are drawn by hand. Each list holds one square of every mirrored
76// pair as x, y; its twin sits at (14-x, 14-y).
77var arenaNames = [3]string{"Tavern", "Rooftop", "Scrapyard"}
78
79var layouts = [3][3][]int{
80 { // Tavern: tables everywhere, two kegs that kick you forward
81 {7, 2, 4, 3, 10, 3, 2, 6, 5, 6, 9, 5, 12, 2},
82 {1, 9, 7, 4},
83 {},
84 },
85 { // Rooftop: open, full of speed pads
86 {7, 3, 3, 7, 4, 10},
87 {2, 5, 5, 2, 12, 3, 5, 5, 9, 1},
88 {},
89 },
90 { // Scrapyard: crates and traps that knock gold out of you
91 {7, 2, 2, 7, 10, 5, 5, 4},
92 {7, 4},
93 {5, 1, 1, 5, 9, 7, 12, 3},
94 },
95}
96
97// ArenaName names the arena a seed plays on.
98func ArenaName(seed uint32) string { return arenaNames[seed%3] }
99
100// 16 directions, per mille, starting east and turning towards +y; 16 is still.
101var dirX = [17]int{1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0, 383, 707, 924, 0}
102var dirY = [17]int{0, 383, 707, 924, 1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0}
103
104// running speed per direction (dir * Speed / 1000), precomputed
105var runX, runY [17]int
106
107func init() {
108 for i := 0; i < 17; i++ {
109 runX[i], runY[i] = dirX[i]*Speed/1000, dirY[i]*Speed/1000
110 }
111}
112
113// Square kinds.
114const (
115 Empty = iota
116 Wall
117 Pad
118 Trap
119)
120
121// Power-up kinds, stored as kind+1 in Game.Power.
122const (
123 Shield = iota
124 Double
125 Recharge
126)
127
128// Fighter timers (CD .. DodgeCD) hold the tick they run out on.
129type Fighter struct {
130 Class int
131 X, Y, VX, VY int
132 Gold int
133 Shield bool
134 Double bool
135 CD int // special
136 AtkCD int
137 BlockCD int
138 BlockT int
139 FeintT int
140 InvT int
141 DashT int
142 DashX, DashY int
143 StunT int
144 TrapT int
145 BoostT int
146 PadT int
147 SwingT int
148 DodgeCD int
149 Face int
150 Cell int // square it stands on; things on a square happen when you step onto it
151}
152
153type Game struct {
154 Seed uint32
155 Arena int
156 T int // ticks played
157 End int // match length in ticks (Ticks unless set with NewLength)
158 F [2]Fighter
159 Move [2]*Recipe // a recipe used in place of the class special, or nil
160 Floor [Cells]byte // Empty, Wall, Pad, Trap
161 Coin [Cells]byte // gold on each square: 0, 1, or 3 for the jackpot
162 Power [Cells]byte // power-up on each square: 0, or kind+1
163 NCoins int
164 NPower int
165 nearWall [Cells]bool
166 nextCoin int // tick of the next coin drop
167 nextPower int
168 powerKind int
169 jackpot bool
170 rushOn bool
171 rng uint32
172}
173
174func (g *Game) rand() int {
175 g.rng = g.rng*1103515245 + 12345
176 return int(g.rng >> 8) // 0 .. 2^24-1
177}
178
179func abs(v int) int {
180 if v < 0 {
181 return -v
182 }
183 return v
184}
185
186func imax(a, b int) int {
187 if a > b {
188 return a
189 }
190 return b
191}
192
193// isqrt is the whole-number square root, rounded down.
194func isqrt(n int) int {
195 if n <= 0 {
196 return 0
197 }
198 r, b := 0, 1<<30
199 for b > n {
200 b >>= 2
201 }
202 for b != 0 {
203 if n >= r+b {
204 n -= r + b
205 r = (r >> 1) + b
206 } else {
207 r >>= 1
208 }
209 b >>= 2
210 }
211 return r
212}
213
214func (g *Game) floor(x, y int) byte {
215 if x < 0 || y < 0 || x >= Size || y >= Size {
216 return Empty
217 }
218 return g.Floor[y*Size+x]
219}
220
221// New sets up a match. The seed picks the arena and lays out terrain and coins.
222func New(seed uint32, classA, classB int) *Game {
223 g := &Game{Seed: seed, Arena: int(seed % 3), End: Ticks}
224 g.rng = seed*7919 + 17
225 for k, list := range layouts[g.Arena] {
226 for i := 0; i+1 < len(list); i += 2 {
227 c := list[i+1]*Size + list[i]
228 g.Floor[c] = byte(k + 1)
229 g.Floor[Cells-1-c] = byte(k + 1)
230 }
231 }
232 for c := 0; c < Cells; c++ {
233 if g.Floor[c] != Wall {
234 continue
235 }
236 x, y := c%Size, c/Size
237 for dy := -1; dy <= 1; dy++ {
238 for dx := -1; dx <= 1; dx++ {
239 if x+dx >= 0 && y+dy >= 0 && x+dx < Size && y+dy < Size {
240 g.nearWall[(y+dy)*Size+x+dx] = true
241 }
242 }
243 }
244 }
245
246 g.F[0] = Fighter{Class: classA, X: 1200, Y: 1200, Face: 1}
247 g.F[1] = Fighter{Class: classB, X: SizeU - 1200, Y: SizeU - 1200, Face: -1}
248 g.Coin[Mid*Size+Mid] = 1
249 g.NCoins = 1
250 for i := 0; i < 200 && g.NCoins < StartCoins; i++ {
251 c := g.rand() * Cells >> 24
252 x, y := c%Size, c/Size
253 if abs(x-Mid) <= 1 && abs(y-Mid) <= 1 {
254 continue
255 }
256 g.spawnAt(c, false, 1)
257 }
258 g.F[0].Cell, g.F[1].Cell = -1, -1
259 g.nextCoin = CoinEvery
260 g.nextPower = FirstPower
261 g.powerKind = int(seed % 3)
262 return g
263}
264
265// free reports whether a square can take a new coin or power-up.
266func (g *Game) free(c int) bool {
267 if g.Floor[c] != Empty || g.Coin[c] != 0 || g.Power[c] != 0 {
268 return false
269 }
270 x, y := c%Size, c/Size
271 if x == 0 || y == 0 || x == Size-1 || y == Size-1 {
272 return false
273 }
274 for i := 0; i < 2; i++ {
275 if g.F[i].X/U == x && g.F[i].Y/U == y {
276 return false
277 }
278 }
279 return true
280}
281
282// spawnAt puts a mirrored pair on square c and its opposite, if both are free.
283func (g *Game) spawnAt(c int, power bool, val int) bool {
284 m := Cells - 1 - c
285 if c == m || !g.free(c) || !g.free(m) {
286 return false
287 }
288 if power {
289 g.Power[c], g.Power[m] = byte(val+1), byte(val+1)
290 g.NPower += 2
291 } else {
292 g.Coin[c], g.Coin[m] = byte(val), byte(val)
293 g.NCoins += 2
294 }
295 return true
296}
297
298func (g *Game) spawnPair(power bool, val int) {
299 for i := 0; i < 40; i++ {
300 if g.spawnAt(g.rand()*Cells>>24, power, val) {
301 return
302 }
303 }
304}
305
306func (g *Game) Over() bool { return g.T >= g.End }
307
308// NewLength is New for a longer or shorter match (rooms use it); the jackpot
309// still drops at half time and the gold rush fills the last 10 seconds.
310func NewLength(seed uint32, classA, classB, ticks int) *Game {
311 if ticks < 2*RushTicks {
312 panic("brawl: match too short")
313 }
314 g := New(seed, classA, classB)
315 g.End = ticks
316 return g
317}
318
319// Winner is 0 or 1, or -1 for a draw.
320func (g *Game) Winner() int {
321 a, b := g.F[0].Gold, g.F[1].Gold
322 if a > b {
323 return 0
324 }
325 if b > a {
326 return 1
327 }
328 return -1
329}
330
331// Step runs one tick. move is a direction 0..15 or Still; act is an action.
332func (g *Game) Step(move0, act0, move1, act1 int) {
333 t := g.T
334 if t >= g.End {
335 return
336 }
337 // actions first; the side that goes first swaps every tick
338 if act0 != None || act1 != None {
339 if t%2 == 0 {
340 g.act(0, move0, act0)
341 g.act(1, move1, act1)
342 } else {
343 g.act(1, move1, act1)
344 g.act(0, move0, act0)
345 }
346 }
347 a, b := &g.F[0], &g.F[1]
348 g.move(a, move0, t)
349 g.move(b, move1, t)
350 dx, dy := b.X-a.X, b.Y-a.Y
351 if dx < 2*Rad && dx > -2*Rad && dy < 2*Rad && dy > -2*Rad {
352 if dd := dx*dx + dy*dy; dd < 4*Rad*Rad && dd > 0 {
353 d := isqrt(dd)
354 push := (2*Rad - d) / 2
355 nx, ny := dx*push/d, dy*push/d
356 a.X -= nx
357 a.Y -= ny
358 b.X += nx
359 b.Y += ny
360 g.collide(a)
361 g.collide(b)
362 }
363 }
364 t++
365 g.T = t
366 if c := a.Y/U*Size + a.X/U; c != a.Cell {
367 a.Cell = c
368 g.land(a, t)
369 }
370 if c := b.Y/U*Size + b.X/U; c != b.Cell {
371 b.Cell = c
372 g.land(b, t)
373 }
374 if t == g.End/2 && !g.jackpot {
375 g.jackpot = true
376 c := Mid*Size + Mid
377 if g.Coin[c] == 0 {
378 g.NCoins++
379 }
380 g.Coin[c] = 3
381 if a.Cell == c {
382 a.Cell = -1 // grab it next tick
383 }
384 if b.Cell == c {
385 b.Cell = -1
386 }
387 }
388 if t == g.End-RushTicks && !g.rushOn {
389 g.rushOn = true
390 g.nextCoin = t
391 }
392 if t >= g.nextCoin {
393 g.nextCoin = t + CoinEvery
394 limit := MaxCoins
395 if g.rushOn {
396 g.nextCoin = t + RushEvery
397 limit += 8
398 }
399 if g.NCoins+2 <= limit {
400 g.spawnPair(false, 1)
401 }
402 }
403 if t >= g.nextPower {
404 g.nextPower = t + PowerEvery
405 if g.NPower == 0 {
406 kind := g.powerKind
407 g.powerKind = (g.powerKind + 1) % 3
408 g.spawnPair(true, kind)
409 }
410 }
411}
412
413func (g *Game) move(me *Fighter, dir, t int) {
414 if dir == Still && me.VX == 0 && me.VY == 0 && t >= me.DashT {
415 return
416 }
417 var vx, vy int
418 if t < me.DashT {
419 vx, vy = me.DashX, me.DashY
420 } else {
421 if t < me.BoostT || t < me.StunT || t < me.BlockT {
422 sp := Speed
423 if t < me.BoostT {
424 sp = Speed * 18 / 10
425 }
426 if t < me.StunT {
427 sp /= 4
428 }
429 if t < me.BlockT {
430 sp /= 2
431 }
432 vx, vy = dirX[dir]*sp/1000, dirY[dir]*sp/1000
433 } else {
434 vx, vy = runX[dir], runY[dir]
435 }
436 if me.VX != 0 || me.VY != 0 {
437 vx += me.VX
438 vy += me.VY
439 me.VX = me.VX * Decay / 1000
440 me.VY = me.VY * Decay / 1000
441 }
442 }
443 if vx == 0 && vy == 0 {
444 return
445 }
446 if vx > -SubStep && vx < SubStep && vy > -SubStep && vy < SubStep {
447 me.X += vx
448 me.Y += vy
449 g.collide(me)
450 } else {
451 n := (imax(abs(vx), abs(vy)) + SubStep - 1) / SubStep
452 for i := 0; i < n; i++ {
453 me.X += vx*(i+1)/n - vx*i/n
454 me.Y += vy*(i+1)/n - vy*i/n
455 g.collide(me)
456 }
457 }
458 if vx > 30 {
459 me.Face = 1
460 } else if vx < -30 {
461 me.Face = -1
462 }
463}
464
465func (g *Game) collide(me *Fighter) {
466 if me.X < Rad {
467 me.X = Rad
468 } else if me.X > SizeU-Rad {
469 me.X = SizeU - Rad
470 }
471 if me.Y < Rad {
472 me.Y = Rad
473 } else if me.Y > SizeU-Rad {
474 me.Y = SizeU - Rad
475 }
476 if !g.nearWall[me.Y/U*Size+me.X/U] {
477 return
478 }
479 x0c, x1c, y0c, y1c := (me.X-Rad)/U, (me.X+Rad)/U, (me.Y-Rad)/U, (me.Y+Rad)/U
480 for wy := y0c; wy <= y1c; wy++ {
481 for wx := x0c; wx <= x1c; wx++ {
482 if wx >= Size || wy >= Size || g.Floor[wy*Size+wx] != Wall {
483 continue
484 }
485 x0, x1, y0, y1 := wx*U+80, wx*U+920, wy*U+80, wy*U+920
486 px, py := me.X, me.Y
487 if px < x0 {
488 px = x0
489 } else if px > x1 {
490 px = x1
491 }
492 if py < y0 {
493 py = y0
494 } else if py > y1 {
495 py = y1
496 }
497 dx, dy := me.X-px, me.Y-py
498 dd := dx*dx + dy*dy
499 if dd >= Rad*Rad {
500 continue
501 }
502 if d := isqrt(dd); d > 0 {
503 me.X = px + dx*Rad/d
504 me.Y = py + dy*Rad/d
505 } else {
506 me.X = x0 - Rad
507 }
508 }
509 }
510}
511
512// land handles whatever is on the square a fighter stands on.
513func (g *Game) land(me *Fighter, t int) {
514 c := me.Y/U*Size + me.X/U
515 if v := g.Coin[c]; v != 0 {
516 g.Coin[c] = 0
517 g.NCoins--
518 g.collect(me, int(v))
519 }
520 if p := g.Power[c]; p != 0 {
521 g.Power[c] = 0
522 g.NPower--
523 switch int(p) - 1 {
524 case Shield:
525 me.Shield = true
526 case Double:
527 me.Double = true
528 default:
529 me.CD = 0
530 }
531 }
532 switch g.Floor[c] {
533 case Trap:
534 if t >= me.TrapT && t >= me.DashT {
535 me.TrapT = t + 12
536 me.StunT = imax(me.StunT, t+3)
537 if me.Gold > 0 {
538 me.Gold--
539 }
540 }
541 case Pad:
542 if t >= me.PadT {
543 me.BoostT = t + 9
544 me.PadT = t + 10
545 }
546 }
547}
548
549func (g *Game) collect(me *Fighter, val int) {
550 if me.Double {
551 val *= 2
552 me.Double = false
553 }
554 me.Gold += val
555}
556
557func (g *Game) steal(s, n int) {
558 thief, victim := &g.F[s], &g.F[1-s]
559 if n > 0 && victim.Shield {
560 n--
561 victim.Shield = false
562 }
563 if n > victim.Gold {
564 n = victim.Gold
565 }
566 if n <= 0 {
567 return
568 }
569 victim.Gold -= n
570 thief.Gold += n
571}
572
573func knock(t, from *Fighter, power int) {
574 dx, dy := t.X-from.X, t.Y-from.Y
575 l := isqrt(dx*dx + dy*dy)
576 if l == 0 {
577 l = 1
578 }
579 t.VX += dx * power * KnockMul / l
580 t.VY += dy * power * KnockMul / l
581}
582
583func dash(me *Fighter, dx, dy, length, ticks, t int) {
584 l := isqrt(dx*dx + dy*dy)
585 if l == 0 {
586 l = 1
587 }
588 me.DashT = t + ticks
589 me.DashX = dx * length / l / ticks
590 me.DashY = dy * length / l / ticks
591}
592
593func (g *Game) act(s, dir, action int) {
594 if action == None {
595 return
596 }
597 t := g.T
598 me, o := &g.F[s], &g.F[1-s]
599 mx, my := dirX[dir], dirY[dir]
600 if action == Dodge { // works while dazed: it's your escape
601 if t < me.DodgeCD {
602 return
603 }
604 me.DodgeCD = t + DodgeCD
605 me.StunT = 0
606 dx, dy := mx, my
607 if dx == 0 && dy == 0 {
608 dx, dy = me.X-o.X, me.Y-o.Y
609 }
610 dash(me, dx, dy, 2400, 2, t)
611 me.InvT = imax(me.InvT, t+4)
612 return
613 }
614 if t < me.StunT {
615 return
616 }
617 dx, dy := me.X-o.X, me.Y-o.Y
618 dd := dx*dx + dy*dy
619 oBlocks := t < o.BlockT || t < o.FeintT
620 oSafe := t < o.InvT
621 switch action {
622 case Attack:
623 if t < me.AtkCD {
624 return
625 }
626 me.AtkCD = t + AttackCD
627 me.SwingT = t + 3
628 if dd > Melee*Melee || oSafe {
629 return
630 }
631 if oBlocks {
632 g.steal(1-s, 1)
633 me.StunT = t + 5
634 me.AtkCD = t + 9
635 } else {
636 g.steal(s, 1)
637 knock(o, me, 3)
638 }
639 case Block:
640 if t < me.BlockCD {
641 return
642 }
643 me.BlockCD = t + BlockCD
644 me.BlockT = t + BlockT
645 case Special:
646 if t < me.CD {
647 return
648 }
649 if r := g.Move[s]; r != nil {
650 me.CD = t + r.CD
651 g.recipe(s, r, mx, my, dd, oBlocks, oSafe)
652 return
653 }
654 me.CD = t + SpecialCD[me.Class]
655 g.special(s, mx, my, dd, oBlocks, oSafe)
656 }
657}
658
659func (g *Game) special(s, mx, my, dd int, oBlocks, oSafe bool) {
660 t := g.T
661 me, o := &g.F[s], &g.F[1-s]
662 switch me.Class {
663 case Hunter:
664 if dd <= Long*Long && !oSafe && g.clear(me, o) {
665 g.steal(s, 1)
666 knock(o, me, 2)
667 }
668 case Brawler:
669 if dd <= (Melee+150)*(Melee+150) && !oSafe {
670 n := 2
671 if oBlocks {
672 n = 1
673 }
674 g.steal(s, n)
675 knock(o, me, 9)
676 o.StunT = imax(o.StunT, t+4)
677 } else {
678 dash(me, o.X-me.X, o.Y-me.Y, 1400, 2, t)
679 }
680 case Coward:
681 dx, dy := mx, my
682 if dx == 0 && dy == 0 {
683 if c := g.nearestCoin(me, Size); c >= 0 {
684 dx, dy = c%Size*U+U/2-me.X, c/Size*U+U/2-me.Y
685 } else {
686 dx, dy = me.X-o.X, me.Y-o.Y
687 }
688 }
689 dash(me, dx, dy, 4000, 3, t)
690 me.InvT = t + 7
691 case Trickster:
692 me.FeintT = t + FeintT
693 if dd <= Melee*Melee && t >= o.SwingT && !oSafe {
694 g.steal(s, 1)
695 }
696 case Reader:
697 for k := 0; k < 2; k++ {
698 c := g.nearestCoin(me, Magnet)
699 if c < 0 {
700 break
701 }
702 v := int(g.Coin[c])
703 g.Coin[c] = 0
704 g.NCoins--
705 g.collect(me, v)
706 }
707 }
708}
709
710// nearestCoin finds the closest square with gold, measured from the fighter
711// to the middle of the square, within r squares. -1 if none.
712func (g *Game) nearestCoin(me *Fighter, r int) int {
713 cx, cy := me.X/U, me.Y/U
714 best, bd := -1, 0
715 lim := (r*U + U/2) * (r*U + U/2)
716 for y := cy - r; y <= cy+r; y++ {
717 if y < 0 || y >= Size {
718 continue
719 }
720 for x := cx - r; x <= cx+r; x++ {
721 if x < 0 || x >= Size || g.Coin[y*Size+x] == 0 {
722 continue
723 }
724 dx, dy := x*U+U/2-me.X, y*U+U/2-me.Y
725 d := dx*dx + dy*dy
726 if d <= lim && (best < 0 || d < bd) {
727 best, bd = y*Size+x, d
728 }
729 }
730 }
731 return best
732}
733
734// clear reports whether the straight line between two fighters misses every wall.
735func (g *Game) clear(a, b *Fighter) bool {
736 dx, dy := b.X-a.X, b.Y-a.Y
737 steps := (isqrt(dx*dx+dy*dy) + 199) / 200
738 for i := 1; i < steps; i++ {
739 x := a.X + dx*i/steps
740 y := a.Y + dy*i/steps
741 cx, cy := x/U, y/U
742 if g.Floor[cy*Size+cx] == Wall {
743 lx, ly := x-cx*U, y-cy*U
744 if lx > 100 && lx < 900 && ly > 100 && ly < 900 {
745 return false
746 }
747 }
748 }
749 return true
750}