Fixed pirating not being disabled when a warship's owner has no port in that body of water (#4458)

> **Before opening a PR:** discuss new features on
[Discord](https://discord.gg/K9zernJB5z) first, and file bugs or small
improvements as
[issues](https://github.com/openfrontio/OpenFrontIO/issues/new/choose).
You must be assigned to an `approved` issue — unsolicited PRs will be
auto-closed.

**Add approved & assigned issue number here:**

Resolves #4291 

## Description:

Fixes pirating not being disabled when a warship's owner has no port in
that body of water

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory

## Please put your Discord username so you can be contacted if a bug or
regression is found:

tktk1234567
This commit is contained in:
TKTK123456
2026-07-10 16:31:21 +00:00
committed by GitHub
parent 8e5f27949f
commit 6a6c142388
2 changed files with 53 additions and 14 deletions
+15 -13
View File
@@ -248,7 +248,7 @@ export class WarshipExecution implements Execution {
);
// Trade-ship-specific state, lazily computed.
let hasPort: boolean | undefined;
let hasReachablePort: boolean | undefined;
let patrolTile: number | undefined;
let patrolRangeSquared: number | undefined;
let warshipComponent: number | null | undefined = undefined;
@@ -272,13 +272,24 @@ export class WarshipExecution implements Execution {
const type = unit.type();
if (includeTradeShips && type === UnitType.TradeShip) {
if (hasPort === undefined) {
hasPort = owner.unitCount(UnitType.Port) > 0;
if (warshipComponent === undefined) {
warshipComponent = mg.getWaterComponent(this.warship.tile());
hasReachablePort =
warshipComponent !== null &&
owner
.units(UnitType.Port)
.some(
(port) =>
port.isActive() &&
!port.isMarkedForDeletion() &&
!port.isUnderConstruction() &&
mg.hasWaterComponent(port.tile(), warshipComponent!),
);
patrolTile = this.warship.warshipState().patrolTile;
patrolRangeSquared = config.warshipPatrolRange() ** 2;
}
if (
!hasPort ||
!hasReachablePort ||
patrolTile === undefined ||
unit.isSafeFromPirates() ||
unit.targetUnit()?.owner() === owner ||
@@ -286,15 +297,6 @@ export class WarshipExecution implements Execution {
) {
continue;
}
if (warshipComponent === undefined) {
warshipComponent = mg.getWaterComponent(this.warship.tile());
}
if (
warshipComponent !== null &&
!mg.hasWaterComponent(unit.tile(), warshipComponent)
) {
continue;
}
if (
mg.euclideanDistSquared(patrolTile, unit.tile()) > patrolRangeSquared!
) {