xonecas.ioDon't click! Sean Caetano Martin Sign in

space-browser / commits / 40cee9b

feat: galaxy gen

xonecas5 weeks ago40cee9b543d43f24731cbb54b00c8d1c51a8f5ffparent 97d3d905 files changed

server/galaxy.go+78-0
@@ -0,0 +1,78 @@
1+package main
2+
3+import (
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.
13+const (
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+func (h *hub) galaxy() {
28+ for _, ring := range []struct {
29+ r float64
30+ n int
31+ }{{0, 1}, {midRing, midCount}, {rimRing, rimCount}} {
32+ a0 := rand.Float64() * 2 * math.Pi // every boot turns each ring somewhere else
33+ slot := 2 * math.Pi / float64(ring.n)
34+ for i := 0; i < ring.n; i++ {
35+ // A system may drift a quarter slot off its place, so a ring never reads
36+ // as a drawn circle of dots, and two neighbours can still never touch.
37+ a := a0 + (float64(i)+0.5)*slot + (rand.Float64()-0.5)*slot*0.5
38+ h.system(math.Cos(a)*ring.r, math.Sin(a)*ring.r, ring.r)
39+ }
40+ }
41+}
42+
43+// One star and the planets around it, built as absolute world points: every body
44+// carries the centre of its own system, so nothing else in the game has to know
45+// that systems exist.
46+func (h *hub) system(cx, cy, r float64) {
47+ core := r == 0 // the only system at the very centre is the one a run starts in
48+ star := &body{Kind: "star", R: starMin + rand.Float64()*starVar, cx: cx, cy: cy}
49+ star.ID = len(h.bodies) + 1 // bodies are numbered as they are built
50+ h.bodies = append(h.bodies, star)
51+
52+ n := 2 + rand.Intn(2)
53+ if r < midRing && rand.Intn(2) == 0 {
54+ n++ // the systems around the core are the crowded ones
55+ }
56+ kinds := make([]string, n)
57+ for i := range kinds {
58+ kinds[i] = "barren"
59+ if rand.Float64() < yardCore+(yardRim-yardCore)*(r/rimRing) {
60+ kinds[i] = "shipyard"
61+ }
62+ }
63+ if core {
64+ kinds[0], kinds[1] = "shipyard", "barren"
65+ }
66+ for i, kind := range kinds {
67+ b := &body{
68+ ID: len(h.bodies) + 1, Kind: kind, R: yardMin + rand.Float64()*yardVar, cx: cx, cy: cy,
69+ orbit: orbitMin + float64(i)*orbitGap + rand.Float64()*orbitGap/4,
70+ a0: rand.Float64() * 2 * math.Pi,
71+ speed: 0.03 + rand.Float64()*0.04, // a slow revolution, visible and harmless
72+ }
73+ if kind == "barren" { // the ore bodies of the galaxy: small, rusted, and worth crossing
74+ b.R, b.wait, b.cap = rockMin+rand.Float64()*rockVar, oreWait(), oreHolds+rand.Intn(4)
75+ }
76+ h.bodies = append(h.bodies, b)
77+ }
78+}
server/hull.go+26-6
@@ -2,9 +2,17 @@ package main
22
33 import (
44 "math"
5+ "math/rand"
56 "sort"
67 )
78
9+// A ring point at angle a, as an offset from the centre of the body's system. The
10+// body's own place is folded in, so a ship parked there turns with the body around
11+// the whole star rather than around itself, and systems can sit anywhere.
12+func ring(b *body, a, r float64) (float64, float64) {
13+ return b.X - b.cx + math.Cos(a)*r, b.Y - b.cy + math.Sin(a)*r
14+}
15+
816 // Parking on a body bolts the target to that body's frame: the ring point turns
917 // with the planet around its star, so the ship is carried along.
1018 func (h *hub) anchor(p *player, b *body) {
@@ -13,10 +21,22 @@ func (h *hub) anchor(p *player, b *body) {
1321 gap = starGap
1422 }
1523 a := math.Atan2(p.Y-b.Y, p.X-b.X) // enter the ring on the side we already are on
16- r := b.R + gap
1724 p.orbit, p.anchored = b.ID, h.time
1825 p.chase = 0 // settling into orbit breaks off any approach order
19- p.ox, p.oy = b.X+math.Cos(a)*r, b.Y+math.Sin(a)*r
26+ p.ox, p.oy = ring(b, a, b.R+gap)
27+}
28+
29+// A new run is born on the ring of one of the inner yards, already carried by it:
30+// a ship starts in orbit, not in empty space.
31+func (h *hub) deploy(p *player) {
32+ if len(h.spawns) == 0 {
33+ return // the generator always yards the core, so there is always somewhere to start
34+ }
35+ b := h.body(h.spawns[rand.Intn(len(h.spawns))])
36+ p.ox, p.oy = ring(b, rand.Float64()*2*math.Pi, b.R+standoff)
37+ p.X, p.Y = b.cx+p.ox, b.cy+p.oy
38+ p.TX, p.TY = p.X, p.Y
39+ p.orbit, p.anchored = b.ID, h.time
2040 }
2141
2242 // Torn-off pieces keep the ids they had, so a piece never becomes a new thing by
@@ -107,16 +127,16 @@ func (h *hub) destroy(p *player, id int) {
107127
108128 // Losing the root piece ends that life. Everything still bolted to the hull is left
109129 // where the ship died as loose wreckage, the tow line goes slack, and the callsign
110-// comes back at the spawn in a brand new ship under a new player id, so the client
111-// snaps there instead of sliding out of its own corpse.
130+// comes back in the core yard's orbit in a brand new ship under a new player id, so
131+// the client snaps there instead of sliding out of its own corpse.
112132 func (h *hub) respawn(p *player) *player {
113133 h.corpse(p)
114134 h.nextID++
115- q := &player{ID: h.nextID, Name: p.Name, // the callsign outlives the ship
116- X: spawnX, Y: spawnY, TX: spawnX, TY: spawnY}
135+ q := &player{ID: h.nextID, Name: p.Name} // the callsign outlives the ship
117136 q.Pieces = []piece{h.fitted("triangle", 0)} // every life starts from one triangle
118137 q.FuelMax = q.capacity()
119138 q.Fuel = q.FuelMax
139+ h.deploy(q)
120140 return q
121141 }
122142
server/main.go+9-10
@@ -9,7 +9,6 @@ import (
99 "flag"
1010 "log"
1111 "math"
12- "math/rand"
1312 "net/http"
1413 "strconv"
1514
@@ -32,24 +31,23 @@ func main() {
3231 func newHub() *hub {
3332 h := &hub{clients: map[*client]bool{}, loose: []*loose{}, ore: []*ore{}}
3433 // Empty, not nil: a nil slice marshals as null, and the client walks these.
35- // One hand-tuned system: a star, a shipyard planet and a barren planet, both
36- // revolving. The real galaxy generator replaces this later.
37- h.bodies = []*body{
38- {ID: 1, Kind: "star", R: starR},
39- {ID: 2, Kind: "shipyard", R: 20, orbit: 120, a0: -1.57, speed: 0.06},
40- {ID: 3, Kind: "barren", R: 28, orbit: 215, a0: 0.6, speed: 0.035, wait: oreWait(), cap: oreHolds + rand.Intn(4)},
41- }
34+ h.galaxy() // ten star systems, drawn fresh on every boot: there is no seed
4235 // Place the bodies before anything is born around them, then fill every yard
43- // with its passive AI traffic.
36+ // with its passive AI traffic. A run starts at any yard near the middle, so new
37+ // ships spread over the inner systems instead of stacking on one ring.
4438 for _, b := range h.bodies {
4539 b.place(0)
4640 if b.Kind != "shipyard" {
4741 continue
4842 }
43+ if math.Hypot(b.cx, b.cy) < rimRing {
44+ h.spawns = append(h.spawns, b.ID)
45+ }
4946 for i := 0; i < aiPerYard; i++ {
5047 h.ai = append(h.ai, h.spawnAI(b))
5148 }
5249 }
50+ log.Printf("galaxy: %d systems, %d bodies", 1+midCount+rimCount, len(h.bodies))
5351 return h
5452 }
5553
@@ -60,11 +58,12 @@ func (h *hub) handle(w http.ResponseWriter, r *http.Request) {
6058 }
6159 h.mu.Lock()
6260 h.nextID++
63- c := &client{ws: ws, p: &player{ID: h.nextID, X: spawnX, Y: spawnY, TX: spawnX, TY: spawnY}}
61+ c := &client{ws: ws, p: &player{ID: h.nextID}}
6462 c.p.Name = "P" + strconv.Itoa(c.p.ID)
6563 c.p.Pieces = []piece{h.fitted("triangle", 0)} // every run starts from one triangle
6664 c.p.FuelMax = c.p.capacity()
6765 c.p.Fuel = c.p.FuelMax
66+ h.deploy(c.p)
6867 h.clients[c] = true
6968 h.mu.Unlock()
7069
server/sim.go+4-2
@@ -51,10 +51,12 @@ func (h *hub) step(dt float64) {
5151 h.think(p, dt) // patrol a yard, or run down whoever it is fighting
5252 }
5353 if b := h.body(p.orbit); b != nil {
54+ // The ring point turns with the body around its star, so the ship is
55+ // carried with it even in another system.
5456 d := b.speed * (h.time - p.anchored)
5557 cs, sn := math.Cos(d), math.Sin(d)
56- p.TX = p.ox*cs - p.oy*sn
57- p.TY = p.ox*sn + p.oy*cs
58+ p.TX = b.cx + p.ox*cs - p.oy*sn
59+ p.TY = b.cy + p.ox*sn + p.oy*cs
5860 }
5961 if o := h.player(p.chase); o != nil {
6062 if math.Hypot(o.X-p.X, o.Y-p.Y) > gunRange*approach {
server/world.go+12-10
@@ -11,7 +11,6 @@ const (
1111 tickRate = 20
1212 tickDt = 1.0 / tickRate
1313
14- starR = 30.0
1514 standoff = 26.0 // ring radius beyond a planet's surface
1615 starGap = 34.0 // a star kills on contact, so its ring sits further out
1716 hitR = 1.5 // click radius of a body, as a multiple of its radius
@@ -22,9 +21,7 @@ const (
2221 handR = 2 * spacing
2322 maxSpeed = 90.0
2423 accel = 260.0
25- arrive = 2.2 // slowdown radius factor
26- spawnX = 0.0
27- spawnY = -70.0
24+ arrive = 2.2 // slowdown radius factor
2825 reach = 4000.0 // an order further out than this is ignored
2926
3027 oreGap = 15.0 // closest two ore may sit: also the spacing of the rings
@@ -53,7 +50,7 @@ const (
5350 aiPerYard = 2 // passive AI ships a shipyard keeps in orbit
5451 aiDelay = 25.0 // seconds after an AI death before that yard births another
5552 aiMin = 1 // ship pieces the smallest AI hull is born with
56- aiMax = 2 // and the largest this core test system fields
53+ aiMax = 2 // and the largest a yard fields
5754 patrolSpin = 0.18 // radians a second an AI circles its yard on patrol
5855 patrolLow = 6.0 // the tightest patrol line sits this far beyond the standoff
5956 patrolHigh = 30.0 // and the widest this much further out again
@@ -121,13 +118,17 @@ type ore struct {
121118 }
122119
123120 // A body is a star or a planet. Orbit 0 and speed 0 is the star, which never
124-// moves; planets revolve and their current place is recomputed every tick.
121+// moves; planets revolve around it, and their current place is recomputed every
122+// tick. cx, cy is the centre of the system the body belongs to, so a body is a
123+// world point without anything else having to know about systems.
125124 type body struct {
126125 ID int `json:"id"`
127126 Kind string `json:"kind"`
128127 R float64 `json:"r"`
129128 X float64 `json:"x"`
130129 Y float64 `json:"y"`
130+ cx float64
131+ cy float64
131132 orbit float64
132133 a0 float64
133134 speed float64
@@ -135,11 +136,11 @@ type body struct {
135136 cap int // how many ore this barren planet keeps in orbit
136137 }
137138
138-// A body's place is a pure function of world time: planets revolve, and the star
139-// (orbit 0) never moves.
139+// A body's place is a pure function of world time: planets revolve around their
140+// star, and the star (orbit 0) sits still at its system's centre.
140141 func (b *body) place(t float64) {
141142 a := b.a0 + b.speed*t
142- b.X, b.Y = math.Cos(a)*b.orbit, math.Sin(a)*b.orbit
143+ b.X, b.Y = b.cx+math.Cos(a)*b.orbit, b.cy+math.Sin(a)*b.orbit
143144 }
144145
145146 type player struct {
@@ -165,7 +166,7 @@ type player struct {
165166 cool float64 // seconds until the guns can fire again
166167 vx, vy float64
167168 orbit int // body the ship is anchored to, 0 = none
168- ox, oy float64 // ring point, in world space, at the moment of anchoring
169+ ox, oy float64 // ring point at anchoring, as an offset from the system centre
169170 anchored float64 // world time of anchoring
170171 held int // loose piece being dragged, 0 = none
171172 tow []int // ore on the tow line, in order behind the ship
@@ -188,6 +189,7 @@ type hub struct {
188189 clients map[*client]bool
189190 nextID int
190191 bodies []*body
192+ spawns []int // shipyard bodies a new run may be born in orbit of, never at the rim
191193 loose []*loose
192194 ore []*ore
193195 shots []*shot