diff --git a/src/core/execution/TradeShipExecution.ts b/src/core/execution/TradeShipExecution.ts index 1709d4967..600250c88 100644 --- a/src/core/execution/TradeShipExecution.ts +++ b/src/core/execution/TradeShipExecution.ts @@ -98,6 +98,19 @@ export class TradeShipExecution implements Execution { } } + const cachedNextTile = this._dstPort.cacheGet(this.tradeShip.tile()); + if (cachedNextTile !== undefined) { + if ( + this.mg.isWater(cachedNextTile) && + this.mg.isShoreline(cachedNextTile) + ) { + this.tradeShip.setSafeFromPirates(); + } + this.tradeShip.move(cachedNextTile); + this.tilesTraveled++; + return; + } + const result = this.pathFinder.nextTile( this.tradeShip.tile(), this._dstPort.tile(), @@ -112,6 +125,7 @@ export class TradeShipExecution implements Execution { this.tradeShip.move(this.tradeShip.tile()); break; case PathFindResultType.NextTile: + this._dstPort.cachePut(this.tradeShip.tile(), result.tile); // Update safeFromPirates status if (this.mg.isWater(result.tile) && this.mg.isShoreline(result.tile)) { this.tradeShip.setSafeFromPirates(); diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index 0b4bd4e17..99bbc690a 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -370,6 +370,9 @@ export interface Unit { // Updates toUpdate(): UnitUpdate; + + cachePut(from: TileRef, to: TileRef): void; // ports only + cacheGet(from: TileRef): TileRef | undefined; // ports only } export interface TerraNullius { diff --git a/src/core/game/UnitImpl.ts b/src/core/game/UnitImpl.ts index dd803006a..ddb85c167 100644 --- a/src/core/game/UnitImpl.ts +++ b/src/core/game/UnitImpl.ts @@ -28,6 +28,7 @@ export class UnitImpl implements Unit { private _detonationDst: TileRef | undefined = undefined; // Only for nukes private _warshipTarget: Unit | undefined = undefined; private _cooldownDuration: number | undefined = undefined; + private _pathCache: Map = new Map(); constructor( private _type: UnitType, @@ -53,6 +54,13 @@ export class UnitImpl implements Unit { : 0; } + cachePut(from: TileRef, to: TileRef): void { + this._pathCache.set(from, to); + } + cacheGet(from: TileRef): TileRef | undefined { + return this._pathCache.get(from); + } + id() { return this._id; }