From 21752aa214b5ca51f354ee81ddd7c9c5aeb4b41d Mon Sep 17 00:00:00 2001 From: Evan Date: Fri, 4 Apr 2025 17:02:19 -0700 Subject: [PATCH] add perf improvements: don't draw full boat tail, make nukes calculations slightly more efficient --- src/client/graphics/layers/UnitLayer.ts | 8 ++++---- src/core/execution/NukeExecution.ts | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/client/graphics/layers/UnitLayer.ts b/src/client/graphics/layers/UnitLayer.ts index 2858708e2..c903badb5 100644 --- a/src/client/graphics/layers/UnitLayer.ts +++ b/src/client/graphics/layers/UnitLayer.ts @@ -29,7 +29,7 @@ export class UnitLayer implements Layer { private canvas: HTMLCanvasElement; private context: CanvasRenderingContext2D; - private boatToTrail = new Map>(); + private boatToTrail = new Map(); private theme: Theme = null; @@ -478,10 +478,10 @@ export class UnitLayer implements Layer { const rel = this.relationship(unit); if (!this.boatToTrail.has(unit)) { - this.boatToTrail.set(unit, new Set()); + this.boatToTrail.set(unit, []); } const trail = this.boatToTrail.get(unit); - trail.add(unit.lastTile()); + trail.push(unit.lastTile()); // Clear previous area for (const t of this.game.bfs( @@ -493,7 +493,7 @@ export class UnitLayer implements Layer { if (unit.isActive()) { // Paint trail - for (const t of trail) { + for (const t of trail.slice(-4)) { this.paintCell( this.game.x(t), this.game.y(t), diff --git a/src/core/execution/NukeExecution.ts b/src/core/execution/NukeExecution.ts index b1829713d..1ef305d22 100644 --- a/src/core/execution/NukeExecution.ts +++ b/src/core/execution/NukeExecution.ts @@ -54,24 +54,29 @@ export class NukeExecution implements Execution { const rand = new PseudoRandom(this.mg.ticks()); return this.mg.bfs(this.dst, (_, n: TileRef) => { const d = this.mg.euclideanDist(this.dst, n); - return (d <= magnitude.inner || rand.chance(2)) && d <= magnitude.outer; + if (d > magnitude.outer) { + return false; + } + return d <= magnitude.inner || rand.chance(2); }); } - private getAttackedTiles() { + private getAttackedTiles(): Map { const toDestroy = this.tilesToDestroy(); - const attacked = new Map(); + const attacked = new Map(); for (const tile of toDestroy) { - const owner = this.mg.owner(tile); - if (owner.isPlayer()) { - const mp = this.mg.player(owner.id()); - const prev = attacked.get(mp) ?? 0; - attacked.set(mp, prev + 1); + const ownerID = this.mg.ownerID(tile); + if (ownerID != 0) { + const prev = attacked.get(ownerID) ?? 0; + attacked.set(ownerID, prev + 1); } } - - return attacked; + return new Map( + Array.from(attacked.entries()).map(([smallID, value]) => { + return [this.mg.playerBySmallID(smallID), value] as [Player, number]; + }), + ); } private breakAlliances() {