Create stations regardless of factory ownership (#1904)

## Description:

Bug fix: cities and ports would only connect to factories owned by the
current player, ignoring those belonging to other players.

This update makes the player ID optional when searching for nearby
units: if no player ID is provided, unit ownership is disregarded,
allowing connections to all factories regardless of ownership.
## 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
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

IngloriousTom
This commit is contained in:
DevelopingTom
2025-08-24 19:41:51 +02:00
committed by Scott Anderson
parent eda49d1ad8
commit fb038f2419
5 changed files with 21 additions and 10 deletions
-1
View File
@@ -52,7 +52,6 @@ export class CityExecution implements Execution {
this.city.tile(),
this.mg.config().trainStationMaxRange(),
UnitType.Factory,
this.player.id(),
);
if (nearbyFactory) {
this.mg.addExecution(new TrainStationExecution(this.city));
-1
View File
@@ -97,7 +97,6 @@ export class PortExecution implements Execution {
this.port.tile(),
this.mg.config().trainStationMaxRange(),
UnitType.Factory,
this.player.id(),
);
if (nearbyFactory) {
this.mg.addExecution(new TrainStationExecution(this.port));
+1 -1
View File
@@ -673,7 +673,7 @@ export type Game = {
tile: TileRef,
searchRange: number,
type: UnitType,
playerId: PlayerID,
playerId?: PlayerID,
): boolean;
nearbyUnits(
tile: TileRef,
+1 -1
View File
@@ -752,7 +752,7 @@ export class GameImpl implements Game {
tile: TileRef,
searchRange: number,
type: UnitType,
playerId: PlayerID,
playerId?: PlayerID,
) {
return this.unitGrid.hasUnitNearby(tile, searchRange, type, playerId);
}
+19 -6
View File
@@ -170,12 +170,28 @@ export class UnitGrid {
return nearby;
}
private unitIsInRange(
unit: Unit | UnitView,
tile: TileRef,
rangeSquared: number,
playerId?: PlayerID,
): boolean {
if (!unit.isActive()) {
return false;
}
if (playerId !== undefined && unit.owner().id() !== playerId) {
return false;
}
const distSquared = this.squaredDistanceFromTile(unit, tile);
return distSquared <= rangeSquared;
}
// Return true if it finds an owned specific unit in range
hasUnitNearby(
tile: TileRef,
searchRange: number,
type: UnitType,
playerId: PlayerID,
playerId?: PlayerID,
): boolean {
const { startGridX, endGridX, startGridY, endGridY } = this.getCellsInRange(
tile,
@@ -187,11 +203,8 @@ export class UnitGrid {
const unitSet = this.grid[cy][cx].get(type);
if (unitSet === undefined) continue;
for (const unit of unitSet) {
if (unit.owner().id() === playerId && unit.isActive()) {
const distSquared = this.squaredDistanceFromTile(unit, tile);
if (distSquared <= rangeSquared) {
return true;
}
if (this.unitIsInRange(unit, tile, rangeSquared, playerId)) {
return true;
}
}
}