diff --git a/src/core/execution/MoveWarshipExecution.ts b/src/core/execution/MoveWarshipExecution.ts index fb21d8858b..f8bbc0560a 100644 --- a/src/core/execution/MoveWarshipExecution.ts +++ b/src/core/execution/MoveWarshipExecution.ts @@ -13,6 +13,8 @@ export class MoveWarshipExecution implements Execution { console.warn(`MoveWarshipExecution: position ${this.position} not valid`); return; } + // Get water component of new TargetTile for connectivity check + const newPatrolTileWaterComponent = mg.getWaterComponent(this.position); // Cache warship list and build a lookup map — avoids repeated iteration const warshipMap = new Map( this.owner.units(UnitType.Warship).map((u) => [u.id(), u]), @@ -28,6 +30,10 @@ export class MoveWarshipExecution implements Execution { console.warn(`MoveWarshipExecution: warship ${unitId} is not active`); continue; } + // Do not update the warship's patrolTile if it is in a different Water Component + if (!mg.hasWaterComponent(warship.tile(), newPatrolTileWaterComponent!)) { + continue; + } warship.updateWarshipState({ patrolTile: this.position, }); diff --git a/tests/Warship.test.ts b/tests/Warship.test.ts index 8820842815..ea2dcd0511 100644 --- a/tests/Warship.test.ts +++ b/tests/Warship.test.ts @@ -915,4 +915,36 @@ describe("Warship", () => { } expect(tradeShip.owner()).toBe(player1); }); + + test("Warship doesn't accept a new patrol tile if in a different water component", async () => { + const newPatrolTile = game.ref(coastX + 5, 15); + + const warship = player1.buildUnit( + UnitType.Warship, + game.ref(coastX + 1, 10), + { + patrolTile: game.ref(coastX + 1, 10), + }, + ); + + game.addExecution(new WarshipExecution(warship)); + + // Mock different water components + game.getWaterComponent = (tile: TileRef) => { + if (tile === newPatrolTile) return 1; + return 2; + }; + + game.hasWaterComponent = (tile: TileRef, component: number) => { + return game.getWaterComponent(tile) === component; + }; + + game.addExecution( + new MoveWarshipExecution(player1, [warship.id()], newPatrolTile), + ); + + executeTicks(game, 10); + + expect(warship.warshipState().patrolTile).toBe(game.ref(coastX + 1, 10)); + }); });