xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / server / ore.go 96 lines2.8 kB Raw
1package main
2
3import (
4	"math"
5	"math/rand"
6)
7
8func oreWait() float64 { return oreWaitMin + rand.Float64()*(oreWaitMax-oreWaitMin) }
9
10// A barren planet throws one ore off its surface every 30-60s, aimed straight
11// out, until it holds its full 3-6. The ore coasts out, braking as it goes, and
12// parks in a free slot on one of the planet's rings, so no two ore ever overlap.
13func (h *hub) spawnOre(b *body) {
14	if h.oreCount(b.ID) >= b.cap {
15		return // the planet is holding all the ore it will ever hold
16	}
17	ring, a, ok := h.oreSlot(b)
18	if !ok {
19		return // no room left in orbit, the planet keeps this one
20	}
21	o := &ore{ID: h.pieceID(), planet: b.ID, ring: ring, r: b.R + 2, a: a}
22	o.vr = math.Sqrt(2 * oreBrake * (ring - o.r)) // just enough to reach the ring
23	h.placeOre(o, b)
24	h.ore = append(h.ore, o)
25}
26
27// A free parking slot: a ring and a slot angle on it, clear of every ore already
28// out there. A full planet hands the ore back instead of stacking it.
29func (h *hub) oreSlot(b *body) (float64, float64, bool) {
30	for i := 0; i < 32; i++ {
31		ring := b.R + oreRingMin + float64(rand.Intn(oreRings))*oreGap
32		a := rand.Float64() * 2 * math.Pi
33		if h.oreClear(b.ID, ring, a) {
34			return ring, a, true
35		}
36	}
37	return 0, 0, false
38}
39
40// Ore in orbit around a planet, which is what the planet's cap counts. Ore on a
41// tow line has left the planet, so hauling some away lets the planet make more.
42func (h *hub) oreCount(planet int) int {
43	n := 0
44	for _, o := range h.ore {
45		if o.planet == planet {
46			n++
47		}
48	}
49	return n
50}
51
52func (h *hub) oreClear(planet int, ring, a float64) bool {
53	for _, o := range h.ore {
54		if o.planet != planet || math.Abs(o.ring-ring) > 1 {
55			continue // a different ring is a different radius, so never a clash
56		}
57		if math.Abs(wrap(o.a-a))*ring < oreGap {
58			return false
59		}
60	}
61	return true
62}
63
64func (h *hub) stepOre(o *ore, dt float64) {
65	if o.Towed != 0 {
66		return // its ship places it
67	}
68	b := h.body(o.planet)
69	if b == nil {
70		return // let go in open space, it just drifts where it was dropped
71	}
72	if o.vr > 0 {
73		o.r += o.vr * dt
74		o.vr = math.Max(0, o.vr-oreBrake*dt)
75		if o.r >= o.ring {
76			o.r, o.vr = o.ring, 0 // in orbit now
77		}
78	}
79	h.placeOre(o, b)
80}
81
82// An ore never leaves its slot: the whole ring turns instead, so two ore parked on
83// one ring stay exactly as far apart as they were placed, forever. The planet's own
84// angle is folded in too, so the ring rides around the star with its planet.
85func (h *hub) placeOre(o *ore, b *body) {
86	a := o.a + (oreOrbitV/o.ring)*h.time + b.a0 + b.speed*h.time
87	o.X, o.Y = b.X+math.Cos(a)*o.r, b.Y+math.Sin(a)*o.r
88	o.H = a
89}
90
91// Ore on a tow line trails straight behind the hull, in the order it was hooked.
92func (h *hub) onLine(o *ore, p *player, n float64) {
93	d := towGap * n
94	o.X, o.Y = p.X-math.Cos(p.H)*d, p.Y-math.Sin(p.H)*d
95	o.H = p.H
96}