From 819b8ef23a3163968338faaeb4a876ac357b4904 Mon Sep 17 00:00:00 2001 From: Evan Date: Wed, 20 Nov 2024 20:22:51 -0800 Subject: [PATCH] fix port ally finding bug, fix transport ship crashing game when target tile not found --- src/core/PathFinding.ts | 7 +++++++ src/core/execution/FakeHumanExecution.ts | 2 +- src/core/execution/PortExecution.ts | 11 +++++------ src/core/execution/TransportShipExecution.ts | 10 +++++++--- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/core/PathFinding.ts b/src/core/PathFinding.ts index b7b540e93..758ba5e51 100644 --- a/src/core/PathFinding.ts +++ b/src/core/PathFinding.ts @@ -169,6 +169,13 @@ export class PathFinder { ) { } nextTile(curr: Tile, dst: Tile, dist: number = 1): TileResult { + if (curr == null) { + console.error('curr is null') + } + if (dst == null) { + console.error('dst is null') + } + if (manhattanDist(curr.cell(), dst.cell()) < dist) { return { type: PathFindResultType.Completed, tile: curr } } diff --git a/src/core/execution/FakeHumanExecution.ts b/src/core/execution/FakeHumanExecution.ts index 3cb042824..375616f8c 100644 --- a/src/core/execution/FakeHumanExecution.ts +++ b/src/core/execution/FakeHumanExecution.ts @@ -109,7 +109,7 @@ export class FakeHumanExecution implements Execution { const enemyborder = Array.from(this.player.borderTiles()).flatMap(t => t.neighbors()).filter(t => t.hasOwner() && t.owner() != this.player) - if (enemyborder.length == 0 || this.random.chance(5)) { + if (enemyborder.length == 0 || this.random.chance(1)) { this.sendBoat() return } diff --git a/src/core/execution/PortExecution.ts b/src/core/execution/PortExecution.ts index a4ea5a57c..2915cac30 100644 --- a/src/core/execution/PortExecution.ts +++ b/src/core/execution/PortExecution.ts @@ -54,7 +54,7 @@ export class PortExecution implements Execution { for (const port of allPorts) { if (this.computingPaths.has(port)) { const aStar = this.computingPaths.get(port) - switch(aStar.compute()) { + switch (aStar.compute()) { case PathFindResultType.Completed: this.portPaths.set(port, aStar.reconstructPath()) this.computingPaths.delete(port) @@ -76,11 +76,10 @@ export class PortExecution implements Execution { } } - if (this.portPaths.size > 0 && this.random.chance(50)) { - const port = this.random.randElement( - Array.from(this.portPaths.keys()) - .filter(p => this.port.owner().isAlliedWith(p.owner())) - ) + const allyPorts = Array.from(this.portPaths.keys()) + .filter(p => this.port.owner().isAlliedWith(p.owner())) + if (allPorts.length > 0 && this.random.chance(50)) { + const port = this.random.randElement(allPorts) const path = this.portPaths.get(port) this.mg.addExecution(new TradeShipExecution(this._owner, this.port, port, path)) } diff --git a/src/core/execution/TransportShipExecution.ts b/src/core/execution/TransportShipExecution.ts index a7e23ade3..f8d9a9e89 100644 --- a/src/core/execution/TransportShipExecution.ts +++ b/src/core/execution/TransportShipExecution.ts @@ -63,8 +63,13 @@ export class TransportShipExecution implements Execution { this.troops = Math.min(this.troops, this.attacker.troops()) - const dstTile = targetTransportTile(this.mg, this.mg.tile(this.cell)) - const src = this.attacker.canBuild(UnitType.TransportShip, dstTile) + this.dst = targetTransportTile(this.mg, this.mg.tile(this.cell)) + if (this.dst == null) { + console.warn(`${this.attacker} cannot send ship to ${this.target}, cannot find attack tile`) + this.active = false + return + } + const src = this.attacker.canBuild(UnitType.TransportShip, this.dst) if (src == false) { console.warn(`can't build transport ship`) this.active = false @@ -72,7 +77,6 @@ export class TransportShipExecution implements Execution { } this.src = src - this.dst = dstTile this.boat = this.attacker.buildUnit(UnitType.TransportShip, this.troops, this.src) }