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