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

space-browser / commits / 31741eb

fix: enemy and friendly fleet issues.

xonecas5 weeks ago31741eb2ee87b5c83748b6bdf68eeea629d9d3d1parent ce46c226 files changed

client/src/config.ts+1-0
@@ -83,6 +83,7 @@ export interface GameConfig {
8383 npc: {
8484 engageRange: number;
8585 hailRange: number;
86+ spotRange: number;
8687 safeRadius: number;
8788 minFleetSize: number;
8889 maxFleetSize: number;
client/src/engine.ts+20-2
@@ -6,6 +6,17 @@ import type { ItemView, NPCView, PlayerView, POI, ProjView, Shape, Vec2 } from "
66 const PIECE_R = 20;
77 const SHAPES: Shape[] = ["triangle", "square", "circle", "pentagon", "star"];
88
9+// Fleet contacts share the gold accent; the mark shape tells them apart:
10+// a solid disc is a rogue fleet, a hollow ring a friendly one.
11+const FLEET_COLOR = "#ffcf6b";
12+
13+// hullRadius is how far the outermost piece reaches from the ship centre.
14+function hullRadius(pieces: PlayerView["ship"]["pieces"]): number {
15+ let hull = 0;
16+ for (const key in pieces) hull = Math.max(hull, Math.hypot(pieces[key].x, pieces[key].y) + PIECE_R);
17+ return hull;
18+}
19+
920 const KIND_COLOR: Record<string, string> = {
1021 star: "#ffe0a0",
1122 planet: "#c18f45",
@@ -579,8 +590,7 @@ export class Engine {
579590 const add = (id: string, pieces: PlayerView["ship"]["pieces"], x: number, y: number, a: number) => {
580591 seen.add(id);
581592 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);
593+ const hull = hullRadius(pieces);
584594 // zoomed out the hull would vanish between two glyph cells: grow it into
585595 // an icon of itself instead, so the player never loses their ship.
586596 const minHull = (ICON_CELLS * this.viewHeight) / this.cells.y;
@@ -613,6 +623,7 @@ export class Engine {
613623 updateDynamic(
614624 projs: ProjView[],
615625 items: ItemView[],
626+ fleets: NPCView[],
616627 markers: { x: number; y: number }[],
617628 hover: Vec2 | null,
618629 ) {
@@ -633,6 +644,13 @@ export class Engine {
633644 i++;
634645 };
635646 for (const p of projs) put(p.x, p.y, 8, 0, "#ffcf6b");
647+ for (const n of fleets) {
648+ // fleets read as a glyph block at any zoom: a hot filled disc for rogue
649+ // AI, a hollow ring for a friendly fleet.
650+ const floor = (2 * ICON_CELLS * this.viewHeight) / this.cells.y;
651+ const size = Math.max(1.6 * hullRadius(n.ship.pieces), floor);
652+ put(n.x, n.y, size, n.hostile ? 0 : 1, FLEET_COLOR);
653+ }
636654 for (const it of items)
637655 put(it.x, it.y, 22, it.item.kind === "ore" ? 1 : 2, it.item.kind === "ore" ? "#8a5f2c" : "#ffcf6b");
638656 for (const m of markers ?? []) put(m.x, m.y, 7, 2, "#ffcf6b");
client/src/main.ts+1-6
@@ -192,11 +192,6 @@ class Game {
192192 e.tx = n.x;
193193 e.ty = n.y;
194194 e.ta = n.a;
195- const poi = this.pois.get(n.id);
196- if (poi) {
197- poi.x = n.x;
198- poi.y = n.y;
199- }
200195 } else {
201196 this.npcs.set(n.id, { ...n, x: n.x, y: n.y, a: n.a, tx: n.x, ty: n.y, ta: n.a });
202197 }
@@ -279,7 +274,7 @@ class Game {
279274 this.camera.update(dt);
280275 this.engine.setView(this.camera.cx, this.camera.cy, this.camera.viewHeight);
281276 this.engine.updateShips([...this.players.values()], [...this.npcs.values()], this.selfId);
282- this.engine.updateDynamic(this.projs, this.items, this.self?.queue ?? [], this.hoverMark);
277+ this.engine.updateDynamic(this.projs, this.items, [...this.npcs.values()], this.self?.queue ?? [], this.hoverMark);
283278 this.engine.render(dt);
284279 requestAnimationFrame(() => this.frame());
285280 }
server/config.go+1-0
@@ -111,6 +111,7 @@ type Config struct {
111111 NPC struct {
112112 EngageRange float64 `json:"engageRange"`
113113 HailRange float64 `json:"hailRange"`
114+ SpotRange float64 `json:"spotRange"`
114115 SafeRadius float64 `json:"safeRadius"`
115116 MinFleetSize int `json:"minFleetSize"`
116117 MaxFleetSize int `json:"maxFleetSize"`
server/config/game.json+1-0
@@ -101,6 +101,7 @@
101101 "npc": {
102102 "engageRange": 1500,
103103 "hailRange": 5000,
104+ "spotRange": 3500,
104105 "safeRadius": 9000,
105106 "minFleetSize": 1,
106107 "maxFleetSize": 6
server/world.go+11-3
@@ -180,14 +180,22 @@ func (w *World) step(dt float64) {
180180 }
181181
182182 // discovery lifts the shared fog of war: any POI a player gets near becomes
183-// explored for everyone.
183+// explored for everyone. Fleets are spotted from further out than the bodies
184+// they patrol, so a rogue fleet shows up before it starts shooting.
184185 func (w *World) discovery() {
185- r := config.Galaxy.DiscoverRange
186+ reach := math.Max(config.Galaxy.DiscoverRange, config.NPC.SpotRange)
186187 for _, p := range w.players {
187- for _, poi := range w.galaxy.nearby(p.Pos, r) {
188+ for _, poi := range w.galaxy.nearby(p.Pos, reach) {
188189 if poi.Explored {
189190 continue
190191 }
192+ r := config.Galaxy.DiscoverRange
193+ if poi.Kind == kindFleet {
194+ r = config.NPC.SpotRange
195+ }
196+ if poi.Pos().Dist(p.Pos) > r {
197+ continue
198+ }
191199 poi.Explored = true
192200 w.newPOIs = append(w.newPOIs, poi)
193201 if w.store != nil {