Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

brawl.gno

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