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

space-browser / commits / b719ece

fix: remove safe zones, fix player spawn

xonecas5 weeks agob719ece8722f6679bf888b4171e12e0a7d861b1dparent 31741eb7 files changed

client/src/config.ts+0-1
@@ -84,7 +84,6 @@ export interface GameConfig {
8484 engageRange: number;
8585 hailRange: number;
8686 spotRange: number;
87- safeRadius: number;
8887 minFleetSize: number;
8988 maxFleetSize: number;
9089 };
server/config.go+1-1
@@ -21,6 +21,7 @@ type Config struct {
2121 SeedFallback uint64 `json:"seedFallback"`
2222 Radius float64 `json:"radius"`
2323 StarCount int `json:"starCount"`
24+ SpawnSystems int `json:"spawnSystems"`
2425 StarSpacing struct {
2526 Min float64 `json:"min"`
2627 Max float64 `json:"max"`
@@ -112,7 +113,6 @@ type Config struct {
112113 EngageRange float64 `json:"engageRange"`
113114 HailRange float64 `json:"hailRange"`
114115 SpotRange float64 `json:"spotRange"`
115- SafeRadius float64 `json:"safeRadius"`
116116 MinFleetSize int `json:"minFleetSize"`
117117 MaxFleetSize int `json:"maxFleetSize"`
118118 } `json:"npc"`
server/config/game.json+1-1
@@ -7,6 +7,7 @@
77 "seedFallback": 11702656774886230293,
88 "radius": 300000,
99 "starCount": 1000,
10+ "spawnSystems": 4,
1011 "starSpacing": {
1112 "min": 5000,
1213 "max": 9000
@@ -102,7 +103,6 @@
102103 "engageRange": 1500,
103104 "hailRange": 5000,
104105 "spotRange": 3500,
105- "safeRadius": 9000,
106106 "minFleetSize": 1,
107107 "maxFleetSize": 6
108108 }
server/galaxy.go+63-39
@@ -64,6 +64,7 @@ type Galaxy struct {
6464 POIs []*POI
6565 byID map[string]*POI
6666 Stars []*POI
67+ Spawns []*POI // round-robin spawn systems, nearest the core first
6768 Home *POI
6869 counts map[string]int
6970 }
@@ -114,13 +115,26 @@ func generateGalaxy(seed uint64) *Galaxy {
114115 r := newRNG(seed ^ 0xA5A5A5A5A5A5A5A5)
115116 g := &Galaxy{Seed: seed, byID: map[string]*POI{}, counts: map[string]int{}}
116117 stars := g.placeStars(r)
117- g.Home = nearestToCore(stars)
118+ g.Spawns = nearestToCore(stars, config.Galaxy.SpawnSystems)
119+ if len(g.Spawns) > 0 {
120+ g.Home = g.Spawns[0]
121+ }
118122 for _, s := range stars {
119123 g.populate(r, s, s == g.Home)
120124 }
121125 return g
122126 }
123127
128+// isSpawn reports whether star is one of the round-robin spawn systems.
129+func (g *Galaxy) isSpawn(star *POI) bool {
130+ for _, s := range g.Spawns {
131+ if s == star {
132+ return true
133+ }
134+ }
135+ return false
136+}
137+
124138 // densityAt is 1 at the core and 0 at the rim; it fades out how rich (and how
125139 // dangerous) a region is.
126140 func densityAt(x, y float64) float64 {
@@ -149,15 +163,17 @@ func systemRadius(s *POI) float64 {
149163 return starSpacing(s.X, s.Y) * config.Galaxy.Shape.SystemRadiusFraction
150164 }
151165
152-func nearestToCore(stars []*POI) *POI {
153- var best *POI
154- bestD := math.Inf(1)
155- for _, s := range stars {
156- if d := math.Hypot(s.X, s.Y); d < bestD {
157- bestD, best = d, s
158- }
166+// nearestToCore returns the n stars closest to the galaxy centre, nearest
167+// first: the shared spawn systems.
168+func nearestToCore(stars []*POI, n int) []*POI {
169+ out := append([]*POI{}, stars...)
170+ sort.Slice(out, func(i, j int) bool {
171+ return math.Hypot(out[i].X, out[i].Y) < math.Hypot(out[j].X, out[j].Y)
172+ })
173+ if n < len(out) {
174+ out = out[:n]
159175 }
160- return best
176+ return out
161177 }
162178
163179 // placeStars scatters stars over the disc model, rejecting any candidate that
@@ -352,20 +368,26 @@ func (g *Galaxy) populate(r *rng, star *POI, home bool) {
352368 }
353369 hosts := append(append([]*POI{}, planets...), star)
354370
355- // fleets never spawn in the home system, and only as friendly within the
356- // spawn safe zone: new players are not thrown straight into combat.
357- safe := home || (g.Home != nil && math.Hypot(star.X-g.Home.X, star.Y-g.Home.Y) <= config.NPC.SafeRadius)
358-
359- // the home system always gets a shipyard and some ore to start on
360- if home {
361- if p := g.satellite(r, star, kindShipyard, hosts, sysR, placed, safe); p != nil {
371+ // every spawn system must contain a shipyard; the home system also gets
372+ // guaranteed ore to start on
373+ spawn := g.isSpawn(star)
374+ if spawn {
375+ for try := 0; try < 8; try++ {
376+ p := g.satellite(r, star, kindShipyard, hosts, sysR, placed)
377+ if p == nil {
378+ continue
379+ }
362380 commit(p)
381+ break
363382 }
364383 }
365384 for _, kind := range poiKinds {
366385 chance := poiChance(kind) * (config.Galaxy.POI.EdgeFade + (1-config.Galaxy.POI.EdgeFade)*density)
386+ if spawn && kind == kindShipyard {
387+ continue
388+ }
367389 if home {
368- if kind == kindShipyard || kind == kindFleet {
390+ if kind == kindFleet {
369391 continue
370392 }
371393 if kind == kindAsteroid {
@@ -375,7 +397,7 @@ func (g *Galaxy) populate(r *rng, star *POI, home bool) {
375397 if !r.Chance(chance) {
376398 continue
377399 }
378- if p := g.satellite(r, star, kind, hosts, sysR, placed, safe); p != nil {
400+ if p := g.satellite(r, star, kind, hosts, sysR, placed); p != nil {
379401 commit(p)
380402 }
381403 }
@@ -439,7 +461,7 @@ func (g *Galaxy) planet(r *rng, star *POI, sysR float64, placed []*POI) *POI {
439461 }
440462
441463 // satellite places a POI in orbit of one of the system's hosts.
442-func (g *Galaxy) satellite(r *rng, star *POI, kind string, hosts []*POI, sysR float64, placed []*POI, safe bool) *POI {
464+func (g *Galaxy) satellite(r *rng, star *POI, kind string, hosts []*POI, sysR float64, placed []*POI) *POI {
443465 gc := config.Galaxy
444466 start := r.Intn(len(hosts))
445467 for i := 0; i < len(hosts); i++ {
@@ -468,8 +490,12 @@ func (g *Galaxy) satellite(r *rng, star *POI, kind string, hosts []*POI, sysR fl
468490 case kindWreck:
469491 p.Wreck = randomWreckPieces(r, config.Cargo.WreckPieces)
470492 case kindFleet:
471- p.Hostile = r.Chance(0.45+0.35*densityAt(star.X, star.Y)) && !safe
472- p.FleetCap = config.NPC.MinFleetSize + r.Intn(config.NPC.MaxFleetSize-config.NPC.MinFleetSize+1)
493+ // fleets are small and weak in the crowded core and grow
494+ // toward the sparse, challenging rim
495+ d := densityAt(star.X, star.Y)
496+ span := config.NPC.MaxFleetSize - config.NPC.MinFleetSize
497+ p.Hostile = r.Chance(0.45 + 0.35*d)
498+ p.FleetCap = config.NPC.MinFleetSize + r.Intn(1+int(float64(span)*(1-d)))
473499 }
474500 p.orbitAround(h)
475501 if fits(p, star, sysR, placed) {
@@ -494,28 +520,26 @@ func fits(p *POI, star *POI, sysR float64, placed []*POI) bool {
494520 return true
495521 }
496522
497-// spawnPos returns a clear starting point in orbit of the home shipyard,
498-// nudged outward until it is not inside a star.
499-func (g *Galaxy) spawnPos() (Vec2, string) {
523+// spawnPos returns a random point inside a spawn system, clear of every star,
524+// plus the id of that system's shipyard.
525+func (g *Galaxy) spawnPos(r *rng, star *POI) (Vec2, string) {
526+ orbit := ""
500527 for _, p := range g.POIs {
501- if p.Kind != kindShipyard || (g.Home != nil && p.Sys != g.Home.ID) {
502- continue
503- }
504- dir := Vec2{p.X - g.Home.X, p.Y - g.Home.Y}
505- if dir.Len() == 0 {
506- dir = Vec2{1, 0}
507- }
508- dir = dir.Norm()
509- pos := Vec2{p.X, p.Y}.Add(dir.Mul(orbitDistance(p.Radius)))
510- for i := 0; i < 40 && g.starAt(pos, 120) != nil; i++ {
511- pos = pos.Add(dir.Mul(120))
528+ if p.Kind == kindShipyard && p.Sys == star.ID {
529+ orbit = p.ID
530+ break
512531 }
513- return pos, p.ID
514532 }
515- if g.Home != nil {
516- return Vec2{g.Home.X + orbitDistance(g.Home.Radius), g.Home.Y}, g.Home.ID
533+ sysR := systemRadius(star)
534+ for try := 0; try < 32; try++ {
535+ a := r.Float64() * 2 * math.Pi
536+ d := sysR * math.Sqrt(r.Float64())
537+ pos := Vec2{star.X + d*math.Cos(a), star.Y + d*math.Sin(a)}
538+ if g.starAt(pos, 120) == nil {
539+ return pos, orbit
540+ }
517541 }
518- return Vec2{}, ""
542+ return Vec2{star.X - sysR, star.Y}, orbit
519543 }
520544
521545 func randomWreckPieces(r *rng, n int) []*Piece {
server/npc.go+2-12
@@ -88,18 +88,11 @@ func (w *World) stepNPC(n *NPC, dt float64) {
8888 n.POI.X, n.POI.Y = n.Pos.X, n.Pos.Y
8989 }
9090
91-// inSafeZone reports whether pos lies inside the spawn safe zone, where
92-// hostile fleets must not engage new players.
93-func (w *World) inSafeZone(pos Vec2) bool {
94- h := w.galaxy.Home
95- return h != nil && pos.Dist(Vec2{h.X, h.Y}) <= config.NPC.SafeRadius
96-}
97-
9891 // npcAcquire picks the nearest player within engage range as a target.
9992 func (w *World) npcAcquire(n *NPC) {
10093 if n.Target != "" {
101- if p, ok := w.players[n.Target]; ok {
102- if tpos, _, ok := w.entityPos(n.Target); ok && n.Pos.Dist(tpos) <= config.NPC.EngageRange*2 && !w.inSafeZone(p.Pos) {
94+ if _, ok := w.players[n.Target]; ok {
95+ if tpos, _, ok := w.entityPos(n.Target); ok && n.Pos.Dist(tpos) <= config.NPC.EngageRange*2 {
10396 return
10497 }
10598 }
@@ -108,9 +101,6 @@ func (w *World) npcAcquire(n *NPC) {
108101 var best *Player
109102 bestD := config.NPC.EngageRange
110103 for _, p := range w.players {
111- if w.inSafeZone(p.Pos) {
112- continue
113- }
114104 if d := n.Pos.Dist(p.Pos); d < bestD {
115105 bestD, best = d, p
116106 }
server/player.go+12-1
@@ -88,6 +88,17 @@ func (w *World) playerByCallsign(callsign string) *Player {
8888 return nil
8989 }
9090
91+// spawnPos places a new player in the next spawn system, round robin, at a
92+// random clear point inside it.
93+func (w *World) spawnPos() (Vec2, string) {
94+ if len(w.galaxy.Spawns) == 0 {
95+ return Vec2{}, ""
96+ }
97+ star := w.galaxy.Spawns[w.spawned%len(w.galaxy.Spawns)]
98+ w.spawned++
99+ return w.galaxy.spawnPos(w.rng, star)
100+}
101+
91102 func (w *World) join(c *Client, m *InMsg) {
92103 callsign := strings.TrimSpace(m.Callsign)
93104 switch {
@@ -112,7 +123,7 @@ func (w *World) join(c *Client, m *InMsg) {
112123 w.sendSelf(existing)
113124 return
114125 }
115- pos, orbit := w.galaxy.spawnPos()
126+ pos, orbit := w.spawnPos()
116127 p := &Player{
117128 ID: w.nextID("p"),
118129 Callsign: callsign,
server/world.go+1-0
@@ -92,6 +92,7 @@ type World struct {
9292 cmd chan func()
9393 t float64
9494 seq int
95+ spawned int
9596 rng *rng
9697 newPOIs []*POI
9798 named []namedEvent