package main

import (
	"math"
	"math/rand"
)

// The galaxy is small and compact on purpose: one protected core system, a middle
// ring, and a sparse rim. Distance from the centre is the only dial, so shipyards
// thin out and the gaps between systems open up as you fly outward, and nothing
// sits further than a tank or two from the middle. Every boot draws a new layout:
// there is no seed and no two runs share a galaxy.
const (
	midRing          = 1150.0 // stars sit on three rings: the centre, this one, and the rim
	rimRing          = 2250.0
	midCount         = 3         // systems on the middle ring
	rimCount         = 6         // and on the rim, ten in all with the core
	starMin, starVar = 30.0, 6.0 // a star is the biggest thing in a system: 30-36
	yardMin, yardVar = 23.0, 5.0 // a shipyard world comes next: 23-28
	rockMin, rockVar = 15.0, 6.0 // and a barren ore world is the smallest: 15-21
	orbitMin         = 175.0     // the first ring sits clear of the star and the patrol lines
	orbitGap         = 85.0      // each next planet sits this much further out
	yardCore         = 0.85      // chance a core planet is a shipyard
	yardRim          = 0.12      // and the same chance out at the rim
)

// A star system is the point its bodies circle. The map draws one mark per
// system, so that centre is the whole of what it needs to know.
type system struct {
	ID int     `json:"id"`
	X  float64 `json:"x"`
	Y  float64 `json:"y"`
}

func (h *hub) galaxy() {
	for _, ring := range []struct {
		r float64
		n int
	}{{0, 1}, {midRing, midCount}, {rimRing, rimCount}} {
		a0 := rand.Float64() * 2 * math.Pi // every boot turns each ring somewhere else
		slot := 2 * math.Pi / float64(ring.n)
		for i := 0; i < ring.n; i++ {
			// A system may drift a quarter slot off its place, so a ring never reads
			// as a drawn circle of dots, and two neighbours can still never touch.
			a := a0 + (float64(i)+0.5)*slot + (rand.Float64()-0.5)*slot*0.5
			h.system(math.Cos(a)*ring.r, math.Sin(a)*ring.r, ring.r)
		}
	}
}

// One star and the planets around it, built as absolute world points: every body
// carries the centre of its own system, so nothing else in the game has to know
// that systems exist.
func (h *hub) system(cx, cy, r float64) {
	core := r == 0 // the only system at the very centre is the one a run starts in
	h.systems = append(h.systems, &system{ID: len(h.systems) + 1, X: cx, Y: cy})
	star := &body{Kind: "star", R: starMin + rand.Float64()*starVar, cx: cx, cy: cy}
	star.ID = len(h.bodies) + 1 // bodies are numbered as they are built
	h.bodies = append(h.bodies, star)

	n := 2 + rand.Intn(2)
	if r < midRing && rand.Intn(2) == 0 {
		n++ // the systems around the core are the crowded ones
	}
	kinds := make([]string, n)
	for i := range kinds {
		kinds[i] = "barren"
		if rand.Float64() < yardCore+(yardRim-yardCore)*(r/rimRing) {
			kinds[i] = "shipyard"
		}
	}
	if core {
		kinds[0], kinds[1] = "shipyard", "barren"
	}
	for i, kind := range kinds {
		b := &body{
			ID: len(h.bodies) + 1, Kind: kind, R: yardMin + rand.Float64()*yardVar, cx: cx, cy: cy,
			orbit: orbitMin + float64(i)*orbitGap + rand.Float64()*orbitGap/4,
			a0:    rand.Float64() * 2 * math.Pi,
			speed: 0.03 + rand.Float64()*0.04, // a slow revolution, visible and harmless
		}
		if kind == "barren" { // the ore bodies of the galaxy: small, rusted, and worth crossing
			b.R, b.wait, b.cap = rockMin+rand.Float64()*rockVar, oreWait(), oreHolds+rand.Intn(4)
		}
		h.bodies = append(h.bodies, b)
	}
}

// The rogue points of the galaxy: the gaps between the stars on the two outer
// rings, never in orbit of anything. A patrol posted there is far from every
// shipyard, so police traffic and rogue traffic only ever meet if a rogue is drawn
// out to the edge of its leash, and the core stays the safe end of the galaxy.
func (h *hub) rogues() {
	for _, r := range []struct {
		r float64
		n int
	}{{midRing, rogueMidN}, {rimRing, rogueRimN}} {
		for i := 0; i < r.n; i++ {
			x, y, ok := h.gap(r.r)
			if !ok {
				continue // this ring is too crowded to post another patrol on
			}
			h.slots = append(h.slots, &slot{x: x, y: y, ring: r.r})
			h.ai = append(h.ai, h.spawnRogue(len(h.slots)-1))
		}
	}
}

// A point on a galaxy ring that is clear of every star system. Thirty-two tries is
// plenty: the gaps between two stars are far wider than the clearance.
func (h *hub) gap(rad float64) (float64, float64, bool) {
	for i := 0; i < 32; i++ {
		a := rand.Float64() * 2 * math.Pi
		x, y := math.Cos(a)*rad, math.Sin(a)*rad
		clear := true
		for _, s := range h.systems {
			if math.Hypot(s.X-x, s.Y-y) < rogueClear {
				clear = false
				break
			}
		}
		if clear {
			return x, y, true
		}
	}
	return 0, 0, false
}
