package main

import "math"

// A neutral ship never opens fire. Once two hulls are hostile, every triangle that
// has the aimed piece in range fires one round a salvo, and the round has to be
// flown into: leaving range or dodging sideways is the whole defence.
func (h *hub) guns(p *player, dt float64) {
	p.cool = math.Max(0, p.cool-dt)
	if p.cool > 0 {
		return
	}
	t := h.foeNear(p)
	if t == nil {
		return
	}
	tx, ty := aim(p, t)
	fired := false
	for _, q := range p.Pieces {
		if q.Kind != "triangle" {
			continue // only a gun shoots
		}
		x, y := hullToWorld(p, q.LX, q.LY)
		if math.Hypot(tx-x, ty-y) > gunRange {
			continue
		}
		h.shots = append(h.shots, &shot{ID: h.pieceID(), X: x, Y: y,
			H: math.Atan2(ty-y, tx-x), owner: p.ID, left: gunRange})
		fired = true
	}
	if fired {
		p.cool = gunCool
	}
}

// The point the guns are pointed at: the piece the click picked, or the nearest
// piece of the same hull once that one is gone, so a fight never stalls on an aim
// that no longer describes anything.
func aim(p, o *player) (float64, float64) {
	tx, ty, best := 0.0, 0.0, math.MaxFloat64
	for _, q := range o.Pieces {
		x, y := hullToWorld(o, q.LX, q.LY)
		if q.ID == p.Target {
			return x, y
		}
		if d := math.Hypot(x-p.X, y-p.Y); d < best {
			tx, ty, best = x, y, d
		}
	}
	return tx, ty
}

// Who shoots whom. A rogue is at war with everything that is not a rogue the moment
// it can reach it, so its own gun range ring is the whole of its warning. Everyone
// else is neutral until a fight is declared: a player declares it by clicking a
// hull, and the police declare it by answering whoever shot them.
func hostile(a, b *player) bool {
	if a.Side == sideRogue || b.Side == sideRogue {
		return a.Side != b.Side
	}
	return a.Foe == b.ID || b.Foe == a.ID
}

// The closest ship this hull is fighting. Rounds hit whatever they touch, so this
// only decides who the guns are pointed at.
func (h *hub) foeNear(p *player) *player {
	var got *player
	best := math.MaxFloat64
	for _, o := range h.players() {
		if o == p || !hostile(p, o) {
			continue
		}
		if d := math.Hypot(o.X-p.X, o.Y-p.Y); d < best {
			best, got = d, o
		}
	}
	return got
}

// A round flies in a straight line and lives for one gun range. A shot that has
// gone that far missed, so it leaves the world instead of crossing the system.
func (h *hub) stepShots(dt float64) {
	live := h.shots[:0]
	for _, s := range h.shots {
		d := shotSpeed * dt
		nx, ny := s.X+math.Cos(s.H)*d, s.Y+math.Sin(s.H)*d
		if p, id := h.shotHit(s, nx, ny); p != nil {
			h.hurt(p, id, s.owner)
			continue
		}
		s.X, s.Y, s.left = nx, ny, s.left-d
		if s.left > 0 {
			live = append(live, s)
		}
	}
	h.shots = live
}

// A round is checked as the segment it swept this tick against the outlines it
// crossed, so a fast one can never tunnel through a piece between two ticks. The
// piece nearest the shot's tail takes it, which is the first one on the path.
func (h *hub) shotHit(s *shot, nx, ny float64) (*player, int) {
	var got *player
	id, best := 0, math.MaxFloat64
	for _, p := range h.players() {
		if p.ID == s.owner {
			continue
		}
		for _, q := range p.Pieces {
			x, y := hullToWorld(p, q.LX, q.LY)
			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) {
				best, got, id = d, p, q.ID
			}
		}
	}
	return got, id
}

// A round always names the hull that fired it, so a hull that is hit can answer:
// this is the whole of what makes the police defend themselves instead of sitting
// there as free kills. A hull on your own side is never answered, so a police round
// that grazes another police hull does not start a faction war.
func (h *hub) hurt(p *player, id, by int) {
	if o := h.player(by); p.AI && o != nil && p.Foe == 0 && o.Side != p.Side {
		p.Foe = o.ID
	}
	for i := range p.Pieces {
		if p.Pieces[i].ID != id {
			continue
		}
		p.Pieces[i].HP = math.Max(0, p.Pieces[i].HP-shotDmg)
		if p.Pieces[i].HP == 0 {
			h.destroy(p, id) // a piece at zero is gone, and takes what hung off it
		}
		return
	}
}
