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

space-browser / commits / 8fcb510

fix: balance, fog of war, zooming issues and others

xonecas5 weeks ago8fcb5100580188a46d09051ed2faa0724eeb1f56parent c8251306 files changed

client/src/config.ts+2-0
@@ -56,6 +56,8 @@ export interface GameConfig {
5656 fuelPerPoint: number;
5757 minSeparation: number;
5858 attachMax: number;
59+ cargoBaseSlots: number;
60+ cargoPointsPerSlot: number;
5961 maxPieces: number;
6062 };
6163 combat: {
client/src/engine.ts+165-82
@@ -31,11 +31,16 @@ attribute float aShape;
3131 attribute float aPhase;
3232 uniform float uZoom;
3333 uniform float uTime;
34+uniform sampler2D tFog;
35+uniform float uFogRadius;
36+uniform float uFogDim;
37+uniform float uFogged;
3438 varying vec3 vColor;
3539 varying float vShape;
3640 varying float vTw;
3741 void main() {
38- vColor = aColor;
42+ float known = 1.0 - texture2D(tFog, position.xy / (2.0 * uFogRadius) + 0.5).r;
43+ vColor = aColor * mix(1.0, uFogDim + (1.0 - uFogDim) * known, uFogged);
3944 vShape = aShape;
4045 vTw = 0.7 + 0.3 * sin(uTime * 1.3 + aPhase);
4146 vec4 mv = modelViewMatrix * vec4(position, 1.0);
@@ -64,6 +69,50 @@ void main() {
6469 }
6570 `;
6671
72+// The fog is a world-plane: the mask holds 1 where space is still unknown and
73+// 0 where someone has been, the noise turns that into slow drifting cloud.
74+const FOG_VERT = /* glsl */ `
75+varying vec2 vWorld;
76+void main() {
77+ vWorld = position.xy;
78+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
79+}
80+`;
81+
82+const FOG_FRAG = /* glsl */ `
83+uniform sampler2D tFog;
84+uniform float uRadius;
85+uniform float uTime;
86+uniform float uGrain;
87+uniform vec3 uMist;
88+varying vec2 vWorld;
89+float hash(vec2 p) {
90+ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
91+}
92+float noise(vec2 p) {
93+ vec2 i = floor(p);
94+ vec2 f = fract(p);
95+ f = f * f * (3.0 - 2.0 * f);
96+ return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x),
97+ mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
98+}
99+void main() {
100+ float unknown = texture2D(tFog, vWorld / (2.0 * uRadius) + 0.5).r;
101+ if (unknown < 0.01) discard;
102+ // Three glyph-locked octaves: lobes of cloud, puffs inside them and grain,
103+ // so the fog reads as drifting vapour at any zoom.
104+ vec2 fine = vec2(uTime * 0.10, uTime * -0.07);
105+ float lobe = noise(vWorld * uGrain * 0.28 + fine * 0.2);
106+ float puff = noise(vWorld * uGrain + fine);
107+ float grain = noise(vWorld * uGrain * 3.2 - fine * 2.0);
108+ float n = lobe * 0.30 + puff * 0.50 + grain * 0.20;
109+ float a = unknown * smoothstep(0.34, 0.60, lobe) * smoothstep(0.40, 0.68, n) * 0.85;
110+ a *= 0.85 + 0.15 * sin(uTime * 0.3 + n * 8.0);
111+ if (a < 0.01) discard;
112+ gl_FragColor = vec4(uMist, a);
113+}
114+`;
115+
67116 const POST_FRAG = /* glsl */ `
68117 uniform sampler2D tScene;
69118 uniform sampler2D tGlyph;
@@ -94,9 +143,14 @@ void main() {
94143
95144 type GalaxyShape = GameConfig["galaxy"]["shape"];
96145
97-// Radius that lights the dust up around explored POIs, and how dim the fog is.
98-const FOG_REVEAL = 20000;
99-const FOG_DIM = 0.55;
146+// Fog of war mask: texels per side, and how much of the dust survives under
147+// the fog, plus the mist tone.
148+const FOG_TEXELS = 1024;
149+const FOG_DIM = 0.28;
150+const FOG_MIST = "#8a5f2c";
151+
152+// The player's hull is never drawn smaller than this many glyph cells across.
153+const ICON_CELLS = 1.5;
100154
101155 // Pieces trail the direction of travel by a few units, subtle jiggle.
102156 const JIGGLE = 5;
@@ -210,12 +264,13 @@ export class Engine {
210264 private postUniforms: Record<string, THREE.IUniform>;
211265 private cells = new THREE.Vector2(1, 1);
212266
213- private poiPoints: THREE.Points;
214267 private poiGeom = new THREE.BufferGeometry();
215268 private dust: THREE.Points;
216- private dustShape = new Float32Array(0);
217- private lastPOIs: POI[] = [];
218- private projPoints: THREE.Points;
269+ private fogTex: THREE.DataTexture;
270+ private fogData: Uint8Array<ArrayBuffer>;
271+ private fogMat: THREE.ShaderMaterial;
272+ private pointMats: THREE.ShaderMaterial[] = [];
273+ private lit = new Set<string>();
219274 private projGeom = new THREE.BufferGeometry();
220275 private projPos: Float32Array;
221276 private projCol: Float32Array;
@@ -275,26 +330,46 @@ export class Engine {
275330 );
276331 this.postScene.add(post);
277332
278- const poiMat = new THREE.ShaderMaterial({
279- uniforms: { uZoom: { value: 1 }, uTime: { value: 0 } },
280- vertexShader: POINT_VERT,
281- fragmentShader: POINT_FRAG,
333+ // fog: one mask texture holding what has been seen, one world plane that
334+ // turns the unseen into cloud.
335+ this.fogData = new Uint8Array(FOG_TEXELS * FOG_TEXELS * 4).fill(255);
336+ this.fogTex = new THREE.DataTexture(this.fogData, FOG_TEXELS, FOG_TEXELS, THREE.RGBAFormat);
337+ this.fogTex.minFilter = THREE.LinearFilter;
338+ this.fogTex.magFilter = THREE.LinearFilter;
339+ this.fogTex.wrapS = THREE.ClampToEdgeWrapping;
340+ this.fogTex.wrapT = THREE.ClampToEdgeWrapping;
341+ this.fogTex.needsUpdate = true;
342+ this.fogMat = new THREE.ShaderMaterial({
343+ uniforms: {
344+ tFog: { value: this.fogTex },
345+ uRadius: { value: cfg.galaxy.radius },
346+ uTime: { value: 0 },
347+ uGrain: { value: 1 / 8 },
348+ uMist: { value: new THREE.Color(FOG_MIST) },
349+ },
350+ vertexShader: FOG_VERT,
351+ fragmentShader: FOG_FRAG,
282352 transparent: true,
283- depthTest: true,
353+ depthTest: false,
284354 depthWrite: false,
285355 });
286- this.poiPoints = new THREE.Points(this.poiGeom, poiMat);
287- this.poiPoints.frustumCulled = false;
288- this.scene.add(this.poiPoints);
356+ const fog = new THREE.Mesh(
357+ new THREE.PlaneGeometry(cfg.galaxy.radius * 2, cfg.galaxy.radius * 2),
358+ this.fogMat,
359+ );
360+ fog.position.z = -4;
361+ fog.renderOrder = -1;
362+ fog.frustumCulled = false;
363+ this.scene.add(fog);
364+
365+ const poiPoints = new THREE.Points(this.poiGeom, this.pointMat(false, true));
366+ poiPoints.frustumCulled = false;
367+ this.scene.add(poiPoints);
289368
290- const dustMat = poiMat.clone();
291- this.dust = new THREE.Points(this.buildDust(cfg.galaxy.seedFallback.toString()), dustMat);
369+ this.dust = new THREE.Points(this.buildDust(cfg.galaxy.seedFallback.toString()), this.pointMat(true, true));
292370 this.dust.frustumCulled = false;
293371 this.scene.add(this.dust);
294372
295- const projMat = poiMat.clone();
296- projMat.depthTest = false;
297- projMat.depthWrite = false;
298373 this.projPos = new Float32Array(4096 * 3);
299374 this.projCol = new Float32Array(4096 * 3);
300375 const projSize = new Float32Array(4096);
@@ -305,9 +380,9 @@ export class Engine {
305380 this.projGeom.setAttribute("aSize", new THREE.BufferAttribute(projSize, 1));
306381 this.projGeom.setAttribute("aShape", new THREE.BufferAttribute(projShape, 1));
307382 this.projGeom.setAttribute("aPhase", new THREE.BufferAttribute(projPhase, 1));
308- this.projPoints = new THREE.Points(this.projGeom, projMat);
309- this.projPoints.frustumCulled = false;
310- this.scene.add(this.projPoints);
383+ const projPoints = new THREE.Points(this.projGeom, this.pointMat(false, false));
384+ projPoints.frustumCulled = false;
385+ this.scene.add(projPoints);
311386
312387 this.shipMeshes = {} as Record<Shape, THREE.InstancedMesh>;
313388 this.pieceGeoms = {} as Record<Shape, THREE.BufferGeometry>;
@@ -357,6 +432,28 @@ export class Engine {
357432 this.resize();
358433 }
359434
435+ // pointMat builds one glyph point material; the dust samples the fog mask so
436+ // seen space is bright and the rest sits dim under the cloud.
437+ private pointMat(fogged: boolean, depthTest: boolean): THREE.ShaderMaterial {
438+ const mat = new THREE.ShaderMaterial({
439+ uniforms: {
440+ uZoom: { value: 1 },
441+ uTime: { value: 0 },
442+ tFog: { value: this.fogTex },
443+ uFogRadius: { value: this.cfg.galaxy.radius },
444+ uFogDim: { value: FOG_DIM },
445+ uFogged: { value: fogged ? 1 : 0 },
446+ },
447+ vertexShader: POINT_VERT,
448+ fragmentShader: POINT_FRAG,
449+ transparent: true,
450+ depthTest,
451+ depthWrite: false,
452+ });
453+ this.pointMats.push(mat);
454+ return mat;
455+ }
456+
360457 // dust carries the galaxy shape but stays dim under the fog; explored
361458 // areas light up as POIs are revealed.
362459 private buildDust(seed: string): THREE.BufferGeometry {
@@ -365,7 +462,6 @@ export class Engine {
365462 const rnd = seeded(seed);
366463 const n = 14000;
367464 const peak = 1 + sh.armStrength + sh.bulgeWeight;
368- this.dustShape = new Float32Array(n);
369465 const pos = new Float32Array(n * 3);
370466 const size = new Float32Array(n);
371467 const col = new Float32Array(n * 3);
@@ -381,10 +477,10 @@ export class Engine {
381477 pos[k * 3 + 1] = y;
382478 pos[k * 3 + 2] = -2;
383479 size[k] = 3 + rnd() * 6;
384- this.dustShape[k] = Math.pow(galaxyShape(x, y, sh, radius) / peak, 0.35);
385- col[k * 3] = c.r;
386- col[k * 3 + 1] = c.g;
387- col[k * 3 + 2] = c.b;
480+ const b = 1.3 + 3.4 * Math.pow(galaxyShape(x, y, sh, radius) / peak, 0.35);
481+ col[k * 3] = c.r * b;
482+ col[k * 3 + 1] = c.g * b;
483+ col[k * 3 + 2] = c.b * b;
388484 shape[k] = 0;
389485 phase[k] = rnd() * 6.28;
390486 }
@@ -400,7 +496,6 @@ export class Engine {
400496 setDustSeed(seed: string) {
401497 this.dust.geometry.dispose();
402498 this.dust.geometry = this.buildDust(seed);
403- this.applyFog();
404499 }
405500
406501 setPOIs(pois: Iterable<POI>) {
@@ -423,55 +518,40 @@ export class Engine {
423518 col[i * 3 + 2] = c.b;
424519 shape[i] = KIND_SHAPE[p.kind] ?? 0;
425520 phase[i] = (i * 2.399) % 6.28;
521+ if (!this.lit.has(p.id)) {
522+ this.lit.add(p.id);
523+ this.revealFog(p);
524+ }
426525 });
427526 this.poiGeom.setAttribute("position", new THREE.BufferAttribute(pos, 3));
428527 this.poiGeom.setAttribute("aSize", new THREE.BufferAttribute(size, 1));
429528 this.poiGeom.setAttribute("aColor", new THREE.BufferAttribute(col, 3));
430529 this.poiGeom.setAttribute("aShape", new THREE.BufferAttribute(shape, 1));
431530 this.poiGeom.setAttribute("aPhase", new THREE.BufferAttribute(phase, 1));
432- this.lastPOIs = list;
433- this.applyFog();
531+ this.fogTex.needsUpdate = true;
434532 }
435533
436- private applyFog() {
437- const g = this.dust.geometry;
438- const posA = g.getAttribute("position") as THREE.BufferAttribute;
439- const colA = g.getAttribute("aColor") as THREE.BufferAttribute;
440- if (!posA || !colA || this.dustShape.length === 0) return;
441- const cell = FOG_REVEAL;
442- const grid = new Map<number, POI[]>();
443- for (const p of this.lastPOIs) {
444- const k = (Math.floor(p.x / cell) + 100) * 1000 + (Math.floor(p.y / cell) + 100);
445- let a = grid.get(k);
446- if (!a) grid.set(k, (a = []));
447- a.push(p);
448- }
449- const r2 = FOG_REVEAL * FOG_REVEAL;
450- const c = new THREE.Color("#2a1d10");
451- for (let i = 0; i < this.dustShape.length; i++) {
452- const x = posA.getX(i);
453- const y = posA.getY(i);
454- const cx = Math.floor(x / cell);
455- const cy = Math.floor(y / cell);
456- let seen = false;
457- for (let dx = -1; dx <= 1 && !seen; dx++) {
458- for (let dy = -1; dy <= 1 && !seen; dy++) {
459- const a = grid.get((cx + dx + 100) * 1000 + (cy + dy + 100));
460- if (!a) continue;
461- for (const p of a) {
462- const ddx = p.x - x;
463- const ddy = p.y - y;
464- if (ddx * ddx + ddy * ddy <= r2) {
465- seen = true;
466- break;
467- }
468- }
469- }
534+ // revealFog clears a soft disc of fog around a discovered POI: the fog is
535+ // shared, so every player sees the space anyone has already been near. The
536+ // disc is twice the discovery range, so a visited system reads as a clear
537+ // island in the cloud.
538+ private revealFog(p: POI) {
539+ const span = this.cfg.galaxy.radius * 2;
540+ const cx = ((p.x + this.cfg.galaxy.radius) / span) * FOG_TEXELS;
541+ const cy = ((p.y + this.cfg.galaxy.radius) / span) * FOG_TEXELS;
542+ const r = ((this.cfg.galaxy.discoverRange * 2) / span) * FOG_TEXELS;
543+ const x0 = Math.max(0, Math.floor(cx - r));
544+ const x1 = Math.min(FOG_TEXELS - 1, Math.ceil(cx + r));
545+ const y0 = Math.max(0, Math.floor(cy - r));
546+ const y1 = Math.min(FOG_TEXELS - 1, Math.ceil(cy + r));
547+ for (let y = y0; y <= y1; y++) {
548+ for (let x = x0; x <= x1; x++) {
549+ const d = Math.hypot(x + 0.5 - cx, y + 0.5 - cy) / r;
550+ const v = 255 * Math.min(1, Math.max(0, (d - 0.55) / 0.45));
551+ const k = (y * FOG_TEXELS + x) * 4;
552+ if (v < this.fogData[k]) this.fogData[k] = v;
470553 }
471- const b = seen ? 1.3 + 3.4 * this.dustShape[i] : FOG_DIM * (1 + 1.6 * this.dustShape[i]);
472- colA.setXYZ(i, c.r * b, c.g * b, c.b * b);
473554 }
474- colA.needsUpdate = true;
475555 }
476556
477557 // jiggle returns how far a hull trails its heading, so a moving ship
@@ -493,22 +573,28 @@ export class Engine {
493573 return { x: (-j.vx / sp) * amp, y: (-j.vy / sp) * amp };
494574 }
495575
496- updateShips(players: PlayerView[], npcs: NPCView[]) {
576+ updateShips(players: PlayerView[], npcs: NPCView[], selfId: string) {
497577 const counts: Record<Shape, number> = { triangle: 0, square: 0, circle: 0, pentagon: 0, star: 0 };
498578 const seen = new Set<string>();
499579 const add = (id: string, pieces: PlayerView["ship"]["pieces"], x: number, y: number, a: number) => {
500580 seen.add(id);
501581 const jig = this.jiggle(id, x, y);
582+ let hull = 0;
583+ for (const key in pieces) hull = Math.max(hull, Math.hypot(pieces[key].x, pieces[key].y) + PIECE_R);
584+ // zoomed out the hull would vanish between two glyph cells: grow it into
585+ // an icon of itself instead, so the player never loses their ship.
586+ const minHull = (ICON_CELLS * this.viewHeight) / this.cells.y;
587+ const k = id === selfId ? Math.max(1, minHull / Math.max(hull, 1)) : 1;
502588 for (const key in pieces) {
503589 const p = pieces[key];
504590 const mesh = this.shipMeshes[p.shape];
505591 const n = counts[p.shape];
506592 if (n >= 4096) continue;
507- const wx = x + jig.x + Math.cos(a) * p.x - Math.sin(a) * p.y;
508- const wy = y + jig.y + Math.sin(a) * p.x + Math.cos(a) * p.y;
593+ const wx = x + jig.x + k * (Math.cos(a) * p.x - Math.sin(a) * p.y);
594+ const wy = y + jig.y + k * (Math.sin(a) * p.x + Math.cos(a) * p.y);
509595 this.pos.set(wx, wy, 0);
510596 this.quat.setFromAxisAngle({ x: 0, y: 0, z: 1 }, a);
511- this.scale.setScalar(1);
597+ this.scale.setScalar(k);
512598 this.matrix.compose(this.pos, this.quat, this.scale);
513599 mesh.setMatrixAt(n, this.matrix);
514600 counts[p.shape] = n + 1;
@@ -614,15 +700,12 @@ export class Engine {
614700 c.updateProjectionMatrix();
615701
616702 const zoom = this.cells.y / this.viewHeight;
617- const poiMat = this.poiPoints.material as THREE.ShaderMaterial;
618- poiMat.uniforms.uZoom.value = zoom;
619- poiMat.uniforms.uTime.value = this.time;
620- const dustMat = this.dust.material as THREE.ShaderMaterial;
621- dustMat.uniforms.uZoom.value = zoom;
622- dustMat.uniforms.uTime.value = this.time;
623- const projMat = this.projPoints.material as THREE.ShaderMaterial;
624- projMat.uniforms.uZoom.value = zoom;
625- projMat.uniforms.uTime.value = this.time;
703+ for (const m of this.pointMats) {
704+ m.uniforms.uZoom.value = zoom;
705+ m.uniforms.uTime.value = this.time;
706+ }
707+ this.fogMat.uniforms.uTime.value = this.time;
708+ this.fogMat.uniforms.uGrain.value = zoom / 8; // cloud grain stays ~8 cells wide
626709
627710 this.renderer.setRenderTarget(this.rt);
628711 this.renderer.clear();
client/src/main.ts+1-1
@@ -278,7 +278,7 @@ class Game {
278278 if (me) this.camera.setTarget(me.x, me.y);
279279 this.camera.update(dt);
280280 this.engine.setView(this.camera.cx, this.camera.cy, this.camera.viewHeight);
281- this.engine.updateShips([...this.players.values()], [...this.npcs.values()]);
281+ this.engine.updateShips([...this.players.values()], [...this.npcs.values()], this.selfId);
282282 this.engine.updateDynamic(this.projs, this.items, this.self?.queue ?? [], this.hoverMark);
283283 this.engine.render(dt);
284284 requestAnimationFrame(() => this.frame());
server/config.go+10-8
@@ -71,14 +71,16 @@ type Config struct {
7171 } `json:"poi"`
7272 } `json:"galaxy"`
7373 Ship struct {
74- PieceBaseHP float64 `json:"pieceBaseHP"`
75- PieceBaseStat float64 `json:"pieceBaseStat"`
76- ShapeBonus float64 `json:"shapeBonus"`
77- QualityMult []float64 `json:"qualityMult"`
78- SpeedPerPoint float64 `json:"speedPerPoint"`
79- FuelPerPoint float64 `json:"fuelPerPoint"`
80- MinSeparation float64 `json:"minSeparation"`
81- AttachMax float64 `json:"attachMax"`
74+ PieceBaseHP float64 `json:"pieceBaseHP"`
75+ PieceBaseStat float64 `json:"pieceBaseStat"`
76+ ShapeBonus float64 `json:"shapeBonus"`
77+ QualityMult []float64 `json:"qualityMult"`
78+ SpeedPerPoint float64 `json:"speedPerPoint"`
79+ FuelPerPoint float64 `json:"fuelPerPoint"`
80+ MinSeparation float64 `json:"minSeparation"`
81+ AttachMax float64 `json:"attachMax"`
82+ CargoBaseSlots float64 `json:"cargoBaseSlots"`
83+ CargoPointsPerSlot float64 `json:"cargoPointsPerSlot"`
8284 } `json:"ship"`
8385 Combat struct {
8486 Range float64 `json:"range"`
server/config/game.json+7-5
@@ -51,7 +51,7 @@
5151 "asteroidChance": 0.6,
5252 "commsChance": 0.35,
5353 "shipyardChance": 0.3,
54- "wreckChance": 0.45,
54+ "wreckChance": 0.28,
5555 "fleetChance": 0.4,
5656 "edgeFade": 0.15
5757 }
@@ -68,7 +68,9 @@
6868 "speedPerPoint": 10,
6969 "fuelPerPoint": 10,
7070 "minSeparation": 45,
71- "attachMax": 140
71+ "attachMax": 140,
72+ "cargoBaseSlots": 4,
73+ "cargoPointsPerSlot": 2.5
7274 },
7375 "combat": {
7476 "range": 320,
@@ -93,14 +95,14 @@
9395 "craftCost": 5,
9496 "upgradeCostPerLevel": 3,
9597 "repairCost": 1,
96- "fieldOre": 24,
97- "wreckPieces": 4
98+ "fieldOre": 10,
99+ "wreckPieces": 2
98100 },
99101 "npc": {
100102 "engageRange": 1500,
101103 "hailRange": 5000,
102104 "safeRadius": 9000,
103105 "minFleetSize": 1,
104- "maxFleetSize": 12
106+ "maxFleetSize": 6
105107 }
106108 }
server/world.go+3-1
@@ -48,7 +48,9 @@ type Player struct {
4848 }
4949
5050 func (p *Player) fuelCap() float64 { return p.Ship.Stats().Fuel * config.Ship.FuelPerPoint }
51-func (p *Player) cargoCap() int { return int(math.Round(p.Ship.Stats().Cargo)) }
51+func (p *Player) cargoCap() int {
52+ return int(config.Ship.CargoBaseSlots + math.Round(p.Ship.Stats().Cargo/config.Ship.CargoPointsPerSlot))
53+}
5254 func (p *Player) maxSpeed() float64 {
5355 if p.Fuel <= 0 {
5456 return 0 // stranded