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

space-browser / commits / d7fed82

fix: minor fixes all over.

xonecas5 weeks agod7fed8201689cee91efb6c4e2f031cfd7fabd126parent e7ace756 files changed

.gitignore+1-0
@@ -1 +1,2 @@
11 /.dev/
2+/server/server
client/src/main.ts+34-23
@@ -6,18 +6,20 @@ const canvas = document.getElementById('view') as HTMLCanvasElement;
66 const view = new View(canvas);
77 addEventListener('resize', () => view.resize());
88
9+const cam: Camera & { follow: boolean } = { x: 0, y: 0, zoom: ZOOM_MIN, follow: true };
10+
911 // Everything drawn comes from the server. We only send intents: pick up what is
1012 // under the cursor, report the drag, drop it and let the server decide.
1113 let state: State | null = null;
1214 const net = connect((s) => {
13- const first = state === null;
15+ const was = state?.you;
1416 state = s;
15- if (first) {
16- const me = s.players.find((p) => p.id === s.you);
17- if (me) {
18- cam.x = me.x;
19- cam.y = me.y;
20- }
17+ const me = s.players.find((p) => p.id === s.you);
18+ // A ship we have never followed (the first, or the one we come back as) puts the
19+ // camera on it at once, so a death never slides the view across the system.
20+ if (me && s.you !== was) {
21+ cam.x = me.x;
22+ cam.y = me.y;
2123 }
2224 });
2325
@@ -28,7 +30,6 @@ const loose = new Map<number, Loose>();
2830 const ore = new Map<number, Ore>();
2931 const shots = new Map<number, Shot>();
3032
31-const cam: Camera & { follow: boolean } = { x: 0, y: 0, zoom: 0.5, follow: true };
3233 const keys = new Set<string>();
3334 let pan0: { x: number; y: number } | null = null; // right/middle drag moves the camera
3435 let pulling = false; // left button down: keep reporting where the piece is
@@ -70,26 +71,36 @@ type Target = {
7071 d: number; // world distance from your hull, nearest first
7172 };
7273
73-const canvasBox = () => {
74- const r = canvas.getBoundingClientRect();
75- return { left: r.left, top: r.top, width: r.width, height: r.height, cell: CELL, cols: r.width / CELL, rows: r.height / CELL };
76-};
77-const cellOf = (px: number, py: number) => {
78- const r = canvas.getBoundingClientRect();
79- return { cellX: Math.floor((px - r.left) / CELL), cellY: Math.floor((py - r.top) / CELL) };
80-};
81-const screen = (x: number, y: number) => {
82- const r = canvas.getBoundingClientRect();
83- return { px: r.left + r.width / 2 + (x - cam.x) / wpp(), py: r.top + r.height / 2 - (y - cam.y) / wpp() };
84-};
74+// The canvas box and the world<->screen transform, read once per use: a layout read
75+// per target would make the pointer cost a forced reflow for every thing on screen.
76+const boxOf = (r: DOMRect) => ({
77+ left: r.left,
78+ top: r.top,
79+ width: r.width,
80+ height: r.height,
81+ cell: CELL,
82+ cols: r.width / CELL,
83+ rows: r.height / CELL,
84+});
85+const canvasBox = () => boxOf(canvas.getBoundingClientRect());
86+const cellOf = (px: number, py: number, r = canvas.getBoundingClientRect()) => ({
87+ cellX: Math.floor((px - r.left) / CELL),
88+ cellY: Math.floor((py - r.top) / CELL),
89+});
90+const screenOf = (r: DOMRect, x: number, y: number) => ({
91+ px: r.left + r.width / 2 + (x - cam.x) / wpp(),
92+ py: r.top + r.height / 2 - (y - cam.y) / wpp(),
93+});
94+const screen = (x: number, y: number) => screenOf(canvas.getBoundingClientRect(), x, y);
8595
8696 const targets = (): Target[] => {
8797 const me = ships.get(state?.you ?? 0);
88- const box = canvasBox();
98+ const r = canvas.getBoundingClientRect();
99+ const box = boxOf(r);
89100 const out: Target[] = [];
90101 const add = (id: number, kind: string, owner: number, x: number, y: number) => {
91- const s = screen(x, y);
92- const c = cellOf(s.px, s.py);
102+ const s = screenOf(r, x, y);
103+ const c = cellOf(s.px, s.py, r);
93104 out.push({
94105 id,
95106 kind,
client/src/view.ts+33-6
@@ -6,7 +6,7 @@ import { toWorld, type Body, type Kind, type Loose, type Ore, type Ship, type Sh
66
77 export const CELL = 12; // css px per character cell
88 export const CELL_WORLD = 2; // world units per character cell at zoom 1
9-export const ZOOM_MIN = 0.6; // furthest out: far enough to frame a whole system
9+export const ZOOM_MIN = 0.5; // furthest out: far enough to frame a whole system
1010 export const ZOOM_MAX = 1.8; // closest in
1111 const SHIP_R = 11; // world radius of a hull
1212 const ORE_R = 6.5; // world radius of an ore piece
@@ -108,17 +108,38 @@ function poly(n: number, r: number, inner = 0, tilt = 0): THREE.Shape {
108108 return s;
109109 }
110110
111-// The four piece kinds are named after their shape.
111+// The four piece kinds are named after their shape. Geometry is identical for every
112+// piece of a kind at a scale, so it is built once and shared; only the materials are
113+// per piece, because a piece's colours are its state.
114+const shapes = new Map<string, THREE.BufferGeometry>();
112115 function pieceGeometry(kind: Kind, scale: number): THREE.BufferGeometry {
113- const r = SHIP_R * scale;
116+ const key = `${kind}:${scale}`;
117+ let g = shapes.get(key);
118+ if (!g) shapes.set(key, (g = buildPiece(kind, SHIP_R * scale)));
119+ return g;
120+}
121+
122+function buildPiece(kind: Kind, r: number): THREE.BufferGeometry {
114123 if (kind === 'square') return new THREE.ShapeGeometry(poly(4, r * 0.8, 0, Math.PI / 4));
115124 if (kind === 'circle') return new THREE.CircleGeometry(r * 0.75, 18);
116125 if (kind === 'star') return new THREE.ShapeGeometry(poly(10, r, r * 0.45, -Math.PI / 2));
117126 return new THREE.ShapeGeometry(poly(3, r));
118127 }
119128
129+// A removed mesh hands its materials back: the geometry is shared and cached, but
130+// every piece owns the colours that say what it is.
131+function free(o: THREE.Object3D): void {
132+ o.traverse((m) => ((m as THREE.Mesh).material as THREE.Material | undefined)?.dispose());
133+}
134+
135+// A plate of a given size is always the same quad, so it is shared: a piece, a shot
136+// or a ray only ever needs its own colour, and a removed mesh only gives that back.
137+const quads = new Map<string, THREE.PlaneGeometry>();
120138 function plate(w: number, h: number, v: number): THREE.Mesh<THREE.PlaneGeometry, THREE.MeshBasicMaterial> {
121- return new THREE.Mesh(new THREE.PlaneGeometry(w, h), new THREE.MeshBasicMaterial({ color: lum(v) }));
139+ const key = `${w}:${h}`;
140+ let g = quads.get(key);
141+ if (!g) quads.set(key, (g = new THREE.PlaneGeometry(w, h)));
142+ return new THREE.Mesh(g, new THREE.MeshBasicMaterial({ color: lum(v) }));
122143 }
123144
124145 // A piece is a bright silhouette with a dimmer inset plate, so the cel shading
@@ -178,10 +199,12 @@ function piece(kind: Kind): THREE.Group {
178199
179200 // Ore is a small faceted chunk: a hexagon with a darker core, the one thing in
180201 // space that is neither a ship piece nor a body.
202+const ORE_SHELL = new THREE.ShapeGeometry(poly(6, ORE_R));
203+const ORE_CORE = new THREE.ShapeGeometry(poly(6, ORE_R * 0.55, 0, 0.4));
181204 function oreChunk(): THREE.Group {
182205 const g = new THREE.Group();
183- g.add(new THREE.Mesh(new THREE.ShapeGeometry(poly(6, ORE_R)), new THREE.MeshBasicMaterial({ color: SHADE.ore[0] })));
184- g.add(new THREE.Mesh(new THREE.ShapeGeometry(poly(6, ORE_R * 0.55, 0, 0.4)), new THREE.MeshBasicMaterial({ color: SHADE.ore[1] })));
206+ g.add(new THREE.Mesh(ORE_SHELL, new THREE.MeshBasicMaterial({ color: SHADE.ore[0] })));
207+ g.add(new THREE.Mesh(ORE_CORE, new THREE.MeshBasicMaterial({ color: SHADE.ore[1] })));
185208 shade(g, 'ore');
186209 return g;
187210 }
@@ -373,6 +396,7 @@ export class View {
373396 }
374397 for (const [id, m] of this.bodyMeshes) {
375398 if (seen.has(id)) continue;
399+ free(m);
376400 this.scene.remove(m);
377401 this.bodyMeshes.delete(id);
378402 }
@@ -500,6 +524,7 @@ export class View {
500524 for (const l of loose) this.place(l.id, l.kind, 'loose', l.x, l.y, l.h, l, false, seen);
501525 for (const [id, g] of this.pieceMeshes) {
502526 if (seen.has(id)) continue;
527+ free(g);
503528 this.scene.remove(g);
504529 this.pieceMeshes.delete(id);
505530 }
@@ -520,6 +545,7 @@ export class View {
520545 }
521546 for (const [id, g] of this.oreMeshes) {
522547 if (oreSeen.has(id)) continue;
548+ free(g);
523549 this.scene.remove(g);
524550 this.oreMeshes.delete(id);
525551 }
@@ -541,6 +567,7 @@ export class View {
541567 }
542568 for (const [id, m] of this.shotMeshes) {
543569 if (shotSeen.has(id)) continue;
570+ free(m);
544571 this.scene.remove(m);
545572 this.shotMeshes.delete(id);
546573 }
server/main.go+30-24
@@ -301,14 +301,7 @@ func (h *hub) handle(w http.ResponseWriter, r *http.Request) {
301301 h.mu.Lock()
302302 delete(h.clients, c)
303303 h.forgive(c.p.ID)
304- if l := h.looseByID(c.p.held); l != nil {
305- l.held = 0
306- }
307- for _, id := range c.p.tow {
308- if o := h.oreByID(id); o != nil {
309- o.Towed = 0 // the line goes slack where the ship died
310- }
311- }
304+ h.abandon(c.p)
312305 h.mu.Unlock()
313306 ws.Close()
314307 }
@@ -474,10 +467,26 @@ func (h *hub) service(p *player) bool {
474467 // Ore handed over leaves the world with the line: the yard keeps it.
475468 func (h *hub) spend(p *player) {
476469 for i := 0; i < yardOre && len(p.tow) > 0; i++ {
477- if o := h.oreByID(p.tow[0]); o != nil {
478- p.release(o)
479- h.removeOre(o.ID)
470+ o := h.oreByID(p.tow[0])
471+ if o == nil {
472+ p.tow = p.tow[1:] // an ore that is already gone can never stall the yard
473+ continue
480474 }
475+ p.release(o)
476+ h.removeOre(o.ID)
477+ }
478+}
479+
480+// Whatever the ship was dragging stays where it was: a dead or departed hull takes
481+// nothing with it.
482+func (h *hub) abandon(p *player) {
483+ for _, id := range p.tow {
484+ if o := h.oreByID(id); o != nil {
485+ o.Towed = 0 // the line goes slack where the ship stopped
486+ }
487+ }
488+ if l := h.looseByID(p.held); l != nil {
489+ l.held = 0
481490 }
482491 }
483492
@@ -535,13 +544,19 @@ func (h *hub) sever(p *player, gone map[int]bool) []*loose {
535544 keep = append(keep, q)
536545 continue
537546 }
538- x, y := hullToWorld(p, q.LX, q.LY)
539- off = append(off, &loose{ID: q.ID, Kind: q.Kind, X: x, Y: y, H: p.H, HP: q.HP, MaxHP: q.MaxHP, Tier: q.Tier})
547+ off = append(off, h.wreck(p, q))
540548 }
541549 p.Pieces = keep
542550 return off
543551 }
544552
553+// A piece torn off a hull keeps its identity, its damage and its tier, and lands in
554+// space where the hull was holding it.
555+func (h *hub) wreck(p *player, q piece) *loose {
556+ x, y := hullToWorld(p, q.LX, q.LY)
557+ return &loose{ID: q.ID, Kind: q.Kind, X: x, Y: y, H: p.H, HP: q.HP, MaxHP: q.MaxHP, Tier: q.Tier}
558+}
559+
545560 // A star is not instant death: its heat eats whatever of the hull actually enters
546561 // it, so brushing the limb costs armour and lingering costs the run. A piece is hit
547562 // by its own outline, not the ship's centre, so a big rig can lose an outer piece
@@ -595,18 +610,9 @@ func (h *hub) destroy(p *player, id int) {
595610 func (h *hub) respawn(p *player) *player {
596611 h.forgive(p.ID) // the run that just ended is nobody's enemy any more
597612 for _, q := range p.Pieces[1:] { // the dead root piece is gone, not salvage
598- x, y := hullToWorld(p, q.LX, q.LY)
599- h.loose = append(h.loose, &loose{ID: q.ID, Kind: q.Kind, X: x, Y: y, H: p.H,
600- HP: q.HP, MaxHP: q.MaxHP, Tier: q.Tier})
601- }
602- for _, id := range p.tow {
603- if o := h.oreByID(id); o != nil {
604- o.Towed = 0 // dropped where the line was
605- }
606- }
607- if l := h.looseByID(p.held); l != nil {
608- l.held = 0
613+ h.loose = append(h.loose, h.wreck(p, q))
609614 }
615+ h.abandon(p)
610616 h.nextID++
611617 q := &player{ID: h.nextID, Name: p.Name, // the callsign outlives the ship
612618 X: spawnX, Y: spawnY, TX: spawnX, TY: spawnY}
server/server+0-0

Binary file, not shown.

tools/agent.sh+10-4
@@ -152,8 +152,12 @@ case "$cmd" in
152152 ;;
153153 shot)
154154 ensure
155- ab screenshot "${1:-shot.png}" >/dev/null
156- echo "${1:-shot.png}"
155+ # agent-browser resolves a relative path against its own cwd, so a shot lands
156+ # where the caller asked only when it is absolute.
157+ f=${1:-shot.png}
158+ case "$f" in /*) ;; *) f=$PWD/$f ;; esac
159+ ab screenshot "$f" >/dev/null
160+ echo "$f"
157161 js "JSON.stringify({ cam: __game.cam, canvas: __game.canvas() })"
158162 ;;
159163 peer)
@@ -163,9 +167,11 @@ case "$cmd" in
163167 ab tab list
164168 ;;
165169 tab)
166- ab tab "$1" >/dev/null
170+ id=$1
171+ case "$id" in ''|*[!0-9]*) ;; *) id=t$id ;; esac # 2 and t2 are the same tab
172+ ab tab "$id" >/dev/null
167173 ensure
168- echo "tab $1"
174+ echo "tab $id"
169175 ;;
170176 *)
171177 sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'