diff --git a/src/core/PathFinding.ts b/src/core/PathFinding.ts index 2b31c53db..e3a871ea0 100644 --- a/src/core/PathFinding.ts +++ b/src/core/PathFinding.ts @@ -78,7 +78,7 @@ export class PathFinder { private path: Tile[] private aStar: AStar - constructor() { + constructor(private iterations: number) { } @@ -88,7 +88,7 @@ export class PathFinder { this.dst = dst this.path = null this.aStar = new AStar(curr, dst) - if (this.aStar.compute(5000)) { + if (this.aStar.compute(this.iterations)) { this.path = this.aStar.reconstructPath() } else { return null diff --git a/src/core/execution/DestroyerExecution.ts b/src/core/execution/DestroyerExecution.ts index d1732bd97..2a318d61c 100644 --- a/src/core/execution/DestroyerExecution.ts +++ b/src/core/execution/DestroyerExecution.ts @@ -10,7 +10,7 @@ export class DestroyerExecution implements Execution { private mg: MutableGame = null private target: MutableUnit = null - private pathfinder = new PathFinder() + private pathfinder = new PathFinder(5000) private patrolTile: Tile; diff --git a/src/core/execution/TransportShipExecution.ts b/src/core/execution/TransportShipExecution.ts index 94dc04a5b..64c3e18d0 100644 --- a/src/core/execution/TransportShipExecution.ts +++ b/src/core/execution/TransportShipExecution.ts @@ -2,7 +2,7 @@ import { Unit, Cell, Execution, MutableUnit, MutableGame, MutablePlayer, Player, import { and, bfs, manhattanDistWrapped, sourceDstOceanShore } from "../Util"; import { AttackExecution } from "./AttackExecution"; import { DisplayMessageEvent, MessageType } from "../../client/graphics/layers/EventsDisplay"; -import { AStar } from "../PathFinding"; +import { AStar, PathFinder } from "../PathFinding"; export class TransportShipExecution implements Execution { @@ -22,14 +22,10 @@ export class TransportShipExecution implements Execution { private src: Tile | null private dst: Tile | null - private currTileIndex: number = 0 private boat: MutableUnit - private aStarPre: AStar - private aStarComplete: AStar - - private finalPath = false + private pathFinder: PathFinder = new PathFinder(100_000) constructor( private attackerID: PlayerID, @@ -82,16 +78,8 @@ export class TransportShipExecution implements Execution { return } - this.aStarPre = new AStar(this.src, this.dst) - this.aStarPre.compute(5) - this.path = this.aStarPre.reconstructPath() - if (this.path != null) { - this.boat = this.attacker.addUnit(UnitType.TransportShip, this.troops, this.src) - } else { - console.log('got null path') - this.active = false - } - this.aStarComplete = new AStar(this.path[this.path.length - 1], this.dst) + + this.boat = this.attacker.addUnit(UnitType.TransportShip, this.troops, this.src) } tick(ticks: number) { @@ -107,15 +95,8 @@ export class TransportShipExecution implements Execution { } this.lastMove = ticks - if (!this.finalPath && this.aStarComplete.compute(30000)) { - this.path.push(...this.aStarComplete.reconstructPath()) - this.finalPath = true - } - if (this.currTileIndex >= this.path.length) { - if (!this.finalPath) { - return - } + if (this.boat.tile() == this.dst) { if (this.dst.owner() == this.attacker) { this.attacker.addTroops(this.troops) this.boat.delete() @@ -135,9 +116,15 @@ export class TransportShipExecution implements Execution { return } - const nextTile = this.path[this.currTileIndex] + const nextTile = this.pathFinder.nextTile(this.boat.tile(), this.dst) + if (nextTile == null) { + console.warn('path not found') + this.attacker.addTroops(this.boat.troops()) + this.boat.delete() + this.active = false + return + } this.boat.move(nextTile) - this.currTileIndex++ } owner(): MutablePlayer {