1package main 2 3import ( 4 "math" 5 "math/rand" 6) 7 8// The galaxy is small and compact on purpose: one protected core system, a middle 9// ring, and a sparse rim. Distance from the centre is the only dial, so shipyards 10// thin out and the gaps between systems open up as you fly outward, and nothing 11// sits further than a tank or two from the middle. Every boot draws a new layout: 12// there is no seed and no two runs share a galaxy. 13const ( 14 midRing = 1150.0 // stars sit on three rings: the centre, this one, and the rim 15 rimRing = 2250.0 16 midCount = 3 // systems on the middle ring 17 rimCount = 6 // and on the rim, ten in all with the core 18 starMin, starVar = 30.0, 6.0 // a star is the biggest thing in a system: 30-36 19 yardMin, yardVar = 23.0, 5.0 // a shipyard world comes next: 23-28 20 rockMin, rockVar = 15.0, 6.0 // and a barren ore world is the smallest: 15-21 21 orbitMin = 175.0 // the first ring sits clear of the star and the patrol lines 22 orbitGap = 85.0 // each next planet sits this much further out 23 yardCore = 0.85 // chance a core planet is a shipyard 24 yardRim = 0.12 // and the same chance out at the rim 25) 26 27// A star system is the point its bodies circle. The map draws one mark per 28// system, so that centre is the whole of what it needs to know. 29type system struct { 30 ID int `json:"id"` 31 X float64 `json:"x"` 32 Y float64 `json:"y"` 33} 34 35func (h *hub) galaxy() { 36 for _, ring := range []struct { 37 r float64 38 n int 39 }{{0, 1}, {midRing, midCount}, {rimRing, rimCount}} { 40 a0 := rand.Float64() * 2 * math.Pi // every boot turns each ring somewhere else 41 slot := 2 * math.Pi / float64(ring.n) 42 for i := 0; i < ring.n; i++ { 43 // A system may drift a quarter slot off its place, so a ring never reads 44 // as a drawn circle of dots, and two neighbours can still never touch. 45 a := a0 + (float64(i)+0.5)*slot + (rand.Float64()-0.5)*slot*0.5 46 h.system(math.Cos(a)*ring.r, math.Sin(a)*ring.r, ring.r) 47 } 48 } 49} 50 51// One star and the planets around it, built as absolute world points: every body 52// carries the centre of its own system, so nothing else in the game has to know 53// that systems exist. 54func (h *hub) system(cx, cy, r float64) { 55 core := r == 0 // the only system at the very centre is the one a run starts in 56 h.systems = append(h.systems, &system{ID: len(h.systems) + 1, X: cx, Y: cy}) 57 star := &body{Kind: "star", R: starMin + rand.Float64()*starVar, cx: cx, cy: cy} 58 star.ID = len(h.bodies) + 1 // bodies are numbered as they are built 59 h.bodies = append(h.bodies, star) 60 61 n := 2 + rand.Intn(2) 62 if r < midRing && rand.Intn(2) == 0 { 63 n++ // the systems around the core are the crowded ones 64 } 65 kinds := make([]string, n) 66 for i := range kinds { 67 kinds[i] = "barren" 68 if rand.Float64() < yardCore+(yardRim-yardCore)*(r/rimRing) { 69 kinds[i] = "shipyard" 70 } 71 } 72 if core { 73 kinds[0], kinds[1] = "shipyard", "barren" 74 } 75 for i, kind := range kinds { 76 b := &body{ 77 ID: len(h.bodies) + 1, Kind: kind, R: yardMin + rand.Float64()*yardVar, cx: cx, cy: cy, 78 orbit: orbitMin + float64(i)*orbitGap + rand.Float64()*orbitGap/4, 79 a0: rand.Float64() * 2 * math.Pi, 80 speed: 0.03 + rand.Float64()*0.04, // a slow revolution, visible and harmless 81 } 82 if kind == "barren" { // the ore bodies of the galaxy: small, rusted, and worth crossing 83 b.R, b.wait, b.cap = rockMin+rand.Float64()*rockVar, oreWait(), oreHolds+rand.Intn(4) 84 } 85 h.bodies = append(h.bodies, b) 86 } 87} 88 89// The rogue points of the galaxy: the gaps between the stars on the two outer 90// rings, never in orbit of anything. A patrol posted there is far from every 91// shipyard, so police traffic and rogue traffic only ever meet if a rogue is drawn 92// out to the edge of its leash, and the core stays the safe end of the galaxy. 93func (h *hub) rogues() { 94 for _, r := range []struct { 95 r float64 96 n int 97 }{{midRing, rogueMidN}, {rimRing, rogueRimN}} { 98 for i := 0; i < r.n; i++ { 99 x, y, ok := h.gap(r.r) 100 if !ok { 101 continue // this ring is too crowded to post another patrol on 102 } 103 h.slots = append(h.slots, &slot{x: x, y: y, ring: r.r}) 104 h.ai = append(h.ai, h.spawnRogue(len(h.slots)-1)) 105 } 106 } 107} 108 109// A point on a galaxy ring that is clear of every star system. Thirty-two tries is 110// plenty: the gaps between two stars are far wider than the clearance. 111func (h *hub) gap(rad float64) (float64, float64, bool) { 112 for i := 0; i < 32; i++ { 113 a := rand.Float64() * 2 * math.Pi 114 x, y := math.Cos(a)*rad, math.Sin(a)*rad 115 clear := true 116 for _, s := range h.systems { 117 if math.Hypot(s.X-x, s.Y-y) < rogueClear { 118 clear = false 119 break 120 } 121 } 122 if clear { 123 return x, y, true 124 } 125 } 126 return 0, 0, false 127}