1package main 2 3import "math" 4 5// A neutral ship never opens fire. Once two hulls are hostile, every triangle that 6// has the aimed piece in range fires one round a salvo, and the round has to be 7// flown into: leaving range or dodging sideways is the whole defence. 8func (h *hub) guns(p *player, dt float64) { 9 p.cool = math.Max(0, p.cool-dt) 10 if p.cool > 0 { 11 return 12 } 13 t := h.foeNear(p) 14 if t == nil { 15 return 16 } 17 tx, ty := aim(p, t) 18 fired := false 19 for _, q := range p.Pieces { 20 if q.Kind != "triangle" { 21 continue // only a gun shoots 22 } 23 x, y := hullToWorld(p, q.LX, q.LY) 24 if math.Hypot(tx-x, ty-y) > gunRange { 25 continue 26 } 27 h.shots = append(h.shots, &shot{ID: h.pieceID(), X: x, Y: y, 28 H: math.Atan2(ty-y, tx-x), owner: p.ID, left: gunRange}) 29 fired = true 30 } 31 if fired { 32 p.cool = gunCool 33 } 34} 35 36// The point the guns are pointed at: the piece the click picked, or the nearest 37// piece of the same hull once that one is gone, so a fight never stalls on an aim 38// that no longer describes anything. 39func aim(p, o *player) (float64, float64) { 40 tx, ty, best := 0.0, 0.0, math.MaxFloat64 41 for _, q := range o.Pieces { 42 x, y := hullToWorld(o, q.LX, q.LY) 43 if q.ID == p.Target { 44 return x, y 45 } 46 if d := math.Hypot(x-p.X, y-p.Y); d < best { 47 tx, ty, best = x, y, d 48 } 49 } 50 return tx, ty 51} 52 53// Who shoots whom. A rogue is at war with everything that is not a rogue the moment 54// it can reach it, so its own gun range ring is the whole of its warning. Everyone 55// else is neutral until a fight is declared: a player declares it by clicking a 56// hull, and the police declare it by answering whoever shot them. 57func hostile(a, b *player) bool { 58 if a.Side == sideRogue || b.Side == sideRogue { 59 return a.Side != b.Side 60 } 61 return a.Foe == b.ID || b.Foe == a.ID 62} 63 64// The closest ship this hull is fighting. Rounds hit whatever they touch, so this 65// only decides who the guns are pointed at. 66func (h *hub) foeNear(p *player) *player { 67 var got *player 68 best := math.MaxFloat64 69 for _, o := range h.players() { 70 if o == p || !hostile(p, o) { 71 continue 72 } 73 if d := math.Hypot(o.X-p.X, o.Y-p.Y); d < best { 74 best, got = d, o 75 } 76 } 77 return got 78} 79 80// A round flies in a straight line and lives for one gun range. A shot that has 81// gone that far missed, so it leaves the world instead of crossing the system. 82func (h *hub) stepShots(dt float64) { 83 live := h.shots[:0] 84 for _, s := range h.shots { 85 d := shotSpeed * dt 86 nx, ny := s.X+math.Cos(s.H)*d, s.Y+math.Sin(s.H)*d 87 if p, id := h.shotHit(s, nx, ny); p != nil { 88 h.hurt(p, id, s.owner) 89 continue 90 } 91 s.X, s.Y, s.left = nx, ny, s.left-d 92 if s.left > 0 { 93 live = append(live, s) 94 } 95 } 96 h.shots = live 97} 98 99// A round is checked as the segment it swept this tick against the outlines it 100// crossed, so a fast one can never tunnel through a piece between two ticks. The 101// piece nearest the shot's tail takes it, which is the first one on the path. 102func (h *hub) shotHit(s *shot, nx, ny float64) (*player, int) { 103 var got *player 104 id, best := 0, math.MaxFloat64 105 for _, p := range h.players() { 106 if p.ID == s.owner { 107 continue 108 } 109 for _, q := range p.Pieces { 110 x, y := hullToWorld(p, q.LX, q.LY) 111 if d := math.Hypot(x-s.X, y-s.Y); d < best && pieceCross(q.Kind, x, y, p.H, s.X, s.Y, nx, ny) { 112 best, got, id = d, p, q.ID 113 } 114 } 115 } 116 return got, id 117} 118 119// A round always names the hull that fired it, so a hull that is hit can answer: 120// this is the whole of what makes the police defend themselves instead of sitting 121// there as free kills. A hull on your own side is never answered, so a police round 122// that grazes another police hull does not start a faction war. 123func (h *hub) hurt(p *player, id, by int) { 124 if o := h.player(by); p.AI && o != nil && p.Foe == 0 && o.Side != p.Side { 125 p.Foe = o.ID 126 } 127 for i := range p.Pieces { 128 if p.Pieces[i].ID != id { 129 continue 130 } 131 p.Pieces[i].HP = math.Max(0, p.Pieces[i].HP-shotDmg) 132 if p.Pieces[i].HP == 0 { 133 h.destroy(p, id) // a piece at zero is gone, and takes what hung off it 134 } 135 return 136 } 137}