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

space-browser / commits / 8ce30a3

fix: one single exploration mechanic

xonecas5 weeks ago8ce30a336b1586a915fabf269cd9c0fa1f0f0f14parent e23fd6512 files changed

client/src/config.ts+0-1
@@ -83,7 +83,6 @@ export interface GameConfig {
8383 npc: {
8484 engageRange: number;
8585 hailRange: number;
86- spotRange: number;
8786 minFleetSize: number;
8887 maxFleetSize: number;
8988 };
client/src/engine.ts+3-3
@@ -543,8 +543,8 @@ export class Engine {
543543 this.fogTex.needsUpdate = true;
544544 }
545545
546- // revealFog clears a soft disc of the shared fog: the server owns what has
547- // been explored, every client just paints the discs it announces.
546+ // revealFog clears a disc of the shared fog: the server owns what has been
547+ // explored, every client just paints the discs it announces.
548548 revealFog(x: number, y: number, worldRadius: number) {
549549 this.clearFog(x, y, worldRadius);
550550 this.fogTex.needsUpdate = true;
@@ -562,7 +562,7 @@ export class Engine {
562562 for (let y = y0; y <= y1; y++) {
563563 for (let x = x0; x <= x1; x++) {
564564 const d = Math.hypot(x + 0.5 - cx, y + 0.5 - cy) / r;
565- const v = 255 * Math.min(1, Math.max(0, (d - 0.55) / 0.45));
565+ const v = 255 * Math.min(1, Math.max(0, (d - 1) / 0.5));
566566 const k = (y * FOG_TEXELS + x) * 4;
567567 if (v < this.fogData[k]) this.fogData[k] = v;
568568 }
client/src/protocol.ts+0-1
@@ -58,7 +58,6 @@ export interface POI {
5858 angle?: number;
5959 name?: string;
6060 by?: string;
61- explored?: boolean;
6261 ore?: number;
6362 wreck?: Piece[];
6463 hostile?: boolean;
server/config.go+0-1
@@ -112,7 +112,6 @@ type Config struct {
112112 NPC struct {
113113 EngageRange float64 `json:"engageRange"`
114114 HailRange float64 `json:"hailRange"`
115- SpotRange float64 `json:"spotRange"`
116115 MinFleetSize int `json:"minFleetSize"`
117116 MaxFleetSize int `json:"maxFleetSize"`
118117 } `json:"npc"`
server/config/game.json+0-1
@@ -102,7 +102,6 @@
102102 "npc": {
103103 "engageRange": 1500,
104104 "hailRange": 5000,
105- "spotRange": 3500,
106105 "minFleetSize": 1,
107106 "maxFleetSize": 6
108107 }
server/fog.go+18-5
@@ -9,8 +9,9 @@ import (
99 const fogTexels = 1024
1010
1111 // fogMask is the shared record of explored space: 0 where space has been seen,
12-// 255 where it is still unknown, one byte per texel. Players paint it as they
13-// fly and every reveal is broadcast, so exploration is shared by everyone.
12+// 255 where it is still unknown, one byte per texel. Players clear it as they
13+// fly and every reveal is broadcast, so exploration is shared by everyone and
14+// never returns.
1415 type fogMask []uint8
1516
1617 func newFogMask() fogMask {
@@ -21,8 +22,20 @@ func newFogMask() fogMask {
2122 return m
2223 }
2324
24-// reveal clears a soft disc around a world point and reports whether it
25-// changed anything.
25+// fogIndex maps a world point to its texel.
26+func fogIndex(x, y float64) int {
27+ span := 2 * config.Galaxy.Radius
28+ tx := min(fogTexels-1, max(0, int((x+config.Galaxy.Radius)/span*fogTexels)))
29+ ty := min(fogTexels-1, max(0, int((y+config.Galaxy.Radius)/span*fogTexels)))
30+ return ty*fogTexels + tx
31+}
32+
33+// known reports whether space at a world point has been explored. This is the
34+// only rule that decides what players can see.
35+func (m fogMask) known(p Vec2) bool { return m[fogIndex(p.X, p.Y)] == 0 }
36+
37+// reveal clears a disc around a world point, with a soft rim just outside it,
38+// and reports whether it changed anything.
2639 func (m fogMask) reveal(x, y, worldRadius float64) bool {
2740 span := 2 * config.Galaxy.Radius
2841 cx := (x + config.Galaxy.Radius) / span * fogTexels
@@ -36,7 +49,7 @@ func (m fogMask) reveal(x, y, worldRadius float64) bool {
3649 for ty := y0; ty <= y1; ty++ {
3750 for tx := x0; tx <= x1; tx++ {
3851 d := math.Hypot(float64(tx)+0.5-cx, float64(ty)+0.5-cy) / r
39- v := uint8(255 * math.Min(1, math.Max(0, (d-0.55)/0.45)))
52+ v := uint8(255 * math.Min(1, math.Max(0, (d-1)/0.5)))
4053 k := ty*fogTexels + tx
4154 if v < m[k] {
4255 m[k] = v
server/galaxy.go+12-12
@@ -19,18 +19,18 @@ const (
1919 // POI is a place of interest: a star, a planet, or something in orbit of
2020 // either. Sys names the star that owns it, Star its direct parent.
2121 type POI struct {
22- ID string `json:"id"`
23- Kind string `json:"kind"`
24- Sys string `json:"sys,omitempty"` // owning star
25- X float64 `json:"x"`
26- Y float64 `json:"y"`
27- Radius float64 `json:"r"`
28- Star string `json:"star,omitempty"`
29- Orbit float64 `json:"orbit,omitempty"`
30- Angle float64 `json:"angle,omitempty"`
31- Name string `json:"name,omitempty"`
32- NamedBy string `json:"by,omitempty"`
33- Explored bool `json:"explored"`
22+ ID string `json:"id"`
23+ Kind string `json:"kind"`
24+ Sys string `json:"sys,omitempty"` // owning star
25+ X float64 `json:"x"`
26+ Y float64 `json:"y"`
27+ Radius float64 `json:"r"`
28+ Star string `json:"star,omitempty"`
29+ Orbit float64 `json:"orbit,omitempty"`
30+ Angle float64 `json:"angle,omitempty"`
31+ Name string `json:"name,omitempty"`
32+ NamedBy string `json:"by,omitempty"`
33+ sent bool // already announced to clients (fog decides visibility)
3434
3535 Ore int `json:"ore,omitempty"`
3636 Wreck []*Piece `json:"wreck,omitempty"`
server/main.go+4-13
@@ -40,23 +40,14 @@ func main() {
4040 log.Fatalf("store: %v", err)
4141 }
4242 if rows, err := store.loadPOIs(); err == nil {
43- explored := 0
4443 for id, row := range rows {
45- poi := galaxy.get(id)
46- if poi == nil {
47- continue
48- }
49- poi.Name = row.Name
50- poi.NamedBy = row.NamedBy
51- poi.Explored = row.Explored
52- if row.Explored {
53- explored++
44+ if poi := galaxy.get(id); poi != nil {
45+ poi.Name = row.Name
46+ poi.NamedBy = row.NamedBy
5447 }
5548 }
56- log.Printf("galaxy seed=%d stars=%d pois=%d explored=%d", seed, len(galaxy.Stars), len(galaxy.POIs), explored)
57- } else {
58- log.Printf("galaxy seed=%d stars=%d pois=%d", seed, len(galaxy.Stars), len(galaxy.POIs))
5949 }
50+ log.Printf("galaxy seed=%d stars=%d pois=%d", seed, len(galaxy.Stars), len(galaxy.POIs))
6051
6152 w = newWorld(store, galaxy)
6253 w.initNPCs()
server/persist.go+8-15
@@ -15,9 +15,8 @@ type Store struct {
1515 }
1616
1717 type poiRow struct {
18- Name string
19- NamedBy string
20- Explored bool
18+ Name string
19+ NamedBy string
2120 }
2221
2322 func openStore(path string) (*Store, error) {
@@ -29,7 +28,7 @@ func openStore(path string) (*Store, error) {
2928 s := &Store{db: db}
3029 stmts := []string{
3130 `CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)`,
32- `CREATE TABLE IF NOT EXISTS poi (id TEXT PRIMARY KEY, name TEXT NOT NULL DEFAULT '', named_by TEXT NOT NULL DEFAULT '', explored INTEGER NOT NULL DEFAULT 0)`,
31+ `CREATE TABLE IF NOT EXISTS poi (id TEXT PRIMARY KEY, name TEXT NOT NULL DEFAULT '', named_by TEXT NOT NULL DEFAULT '')`,
3332 `CREATE TABLE IF NOT EXISTS fog (id INTEGER PRIMARY KEY, mask BLOB NOT NULL)`,
3433 }
3534 for _, q := range stmts {
@@ -87,7 +86,7 @@ func (s *Store) checkLayout() error {
8786 }
8887
8988 func (s *Store) loadPOIs() (map[string]poiRow, error) {
90- rows, err := s.db.Query(`SELECT id, name, named_by, explored FROM poi`)
89+ rows, err := s.db.Query(`SELECT id, name, named_by FROM poi`)
9190 if err != nil {
9291 return nil, err
9392 }
@@ -96,11 +95,9 @@ func (s *Store) loadPOIs() (map[string]poiRow, error) {
9695 for rows.Next() {
9796 var id string
9897 var r poiRow
99- var explored int
100- if err := rows.Scan(&id, &r.Name, &r.NamedBy, &explored); err != nil {
98+ if err := rows.Scan(&id, &r.Name, &r.NamedBy); err != nil {
10199 return nil, err
102100 }
103- r.Explored = explored != 0
104101 out[id] = r
105102 }
106103 return out, rows.Err()
@@ -120,13 +117,9 @@ func (s *Store) saveFog(m []byte) error {
120117 }
121118
122119 func (s *Store) savePOI(p *POI) error {
123- explored := 0
124- if p.Explored {
125- explored = 1
126- }
127- _, err := s.db.Exec(`INSERT INTO poi(id, name, named_by, explored) VALUES(?, ?, ?, ?)
128- ON CONFLICT(id) DO UPDATE SET name = excluded.name, named_by = excluded.named_by, explored = excluded.explored`,
129- p.ID, p.Name, p.NamedBy, explored)
120+ _, err := s.db.Exec(`INSERT INTO poi(id, name, named_by) VALUES(?, ?, ?)
121+ ON CONFLICT(id) DO UPDATE SET name = excluded.name, named_by = excluded.named_by`,
122+ p.ID, p.Name, p.NamedBy)
130123 if err != nil {
131124 return fmt.Errorf("save poi %s: %w", p.ID, err)
132125 }
server/player.go+8-9
@@ -257,17 +257,16 @@ func (w *World) killPlayer(p *Player, reason string) {
257257
258258 func (w *World) spawnWreck(p *Player) {
259259 poi := &POI{
260- ID: w.nextID("wreck:"),
261- Kind: kindWreck,
262- X: p.Pos.X,
263- Y: p.Pos.Y,
264- Radius: 160,
265- Name: p.Callsign + "'s wreck",
266- Explored: true,
267- Wreck: p.Ship.piecesSlice(),
260+ ID: w.nextID("wreck:"),
261+ Kind: kindWreck,
262+ X: p.Pos.X,
263+ Y: p.Pos.Y,
264+ Radius: 160,
265+ Name: p.Callsign + "'s wreck",
266+ Wreck: p.Ship.piecesSlice(),
268267 }
269268 w.galaxy.add(poi)
270- w.newPOIs = append(w.newPOIs, poi)
269+ w.clearFog(poi.Pos(), poi.Radius)
271270 }
272271
273272 // ---- ship editing ---------------------------------------------------------
server/protocol.go+2-2
@@ -68,7 +68,7 @@ func (w *World) posMessage() map[string]any {
6868 }
6969 npcs := []any{}
7070 for _, n := range w.npcs {
71- if !n.POI.Explored {
71+ if !w.fog.known(n.Pos) {
7272 continue
7373 }
7474 npcs = append(npcs, map[string]any{
@@ -150,7 +150,7 @@ func (w *World) broadcastDots() {
150150 func (w *World) sendDots(c *Client) {
151151 pois := []any{}
152152 for _, poi := range w.galaxy.POIs {
153- if poi.Explored {
153+ if w.fog.known(poi.Pos()) {
154154 pois = append(pois, poi)
155155 }
156156 }
server/world.go+13-27
@@ -137,11 +137,10 @@ func newWorld(store *Store, g *Galaxy) *World {
137137 w.fog.load(blob)
138138 }
139139 }
140- // bodies discovered since the last mask flush still need their island
140+ // bodies already uncovered by the persisted mask were announced to clients
141+ // before the restart, so they are not announced again
141142 for _, poi := range g.POIs {
142- if poi.Explored {
143- w.fog.reveal(poi.Pos().X, poi.Pos().Y, config.Galaxy.DiscoverRange*2)
144- }
143+ poi.sent = w.fog.known(poi.Pos())
145144 }
146145 return w
147146 }
@@ -204,12 +203,19 @@ func (w *World) step(dt float64) {
204203 w.reapPlayers()
205204 }
206205
207-// clearFog lifts the shared fog mask and queues the disc for broadcast.
206+// clearFog lifts the shared fog of war: the disc is cleared, queued for
207+// broadcast, and every body it uncovers is announced.
208208 func (w *World) clearFog(pos Vec2, radius float64) {
209209 if w.fog.reveal(pos.X, pos.Y, radius) {
210210 w.fogOut = append(w.fogOut, fogReveal{pos.X, pos.Y, radius})
211211 w.fogDirty = true
212212 }
213+ for _, poi := range w.galaxy.nearby(pos, radius) {
214+ if !poi.sent && w.fog.known(poi.Pos()) {
215+ poi.sent = true
216+ w.newPOIs = append(w.newPOIs, poi)
217+ }
218+ }
213219 }
214220
215221 // flushFog writes the shared mask back to sqlite, so exploration survives a
@@ -227,30 +233,10 @@ func (w *World) flushFog() {
227233 w.fogDirty = false
228234 }
229235
230-// discovery lifts the shared fog of war: any POI a player gets near becomes
231-// explored for everyone. Fleets are spotted from further out than the bodies
232-// they patrol, so a rogue fleet shows up before it starts shooting.
236+// discovery paints the shared fog of war where players fly, and links the
237+// antennas they pass.
233238 func (w *World) discovery() {
234- reach := math.Max(config.Galaxy.DiscoverRange, config.NPC.SpotRange)
235239 for _, p := range w.players {
236- for _, poi := range w.galaxy.nearby(p.Pos, reach) {
237- if poi.Explored {
238- continue
239- }
240- r := config.Galaxy.DiscoverRange
241- if poi.Kind == kindFleet {
242- r = config.NPC.SpotRange
243- }
244- if poi.Pos().Dist(p.Pos) > r {
245- continue
246- }
247- poi.Explored = true
248- w.newPOIs = append(w.newPOIs, poi)
249- w.clearFog(poi.Pos(), config.Galaxy.DiscoverRange*2)
250- if w.store != nil {
251- _ = w.store.savePOI(poi)
252- }
253- }
254240 // flight paints the fog too: every player clears the space it flies
255241 // through, and everyone sees it.
256242 if !p.fogInit || p.Pos.Dist(p.fogAt) >= config.Galaxy.DiscoverRange/5 {