diff --git a/src/core/PseudoRandom.ts b/src/core/PseudoRandom.ts index 3a88c7a16..be1410870 100644 --- a/src/core/PseudoRandom.ts +++ b/src/core/PseudoRandom.ts @@ -16,7 +16,9 @@ export class PseudoRandom { // Generates a random integer between min (inclusive) and max (exclusive). nextInt(min: number, max: number): number { - return Math.floor(this.rng() * (max - min)) + min; + const lo = Math.floor(min); + const hi = Math.floor(max); + return Math.floor(this.rng() * (hi - lo)) + lo; } // Generates a random float between min (inclusive) and max (exclusive). diff --git a/src/core/execution/WarshipExecution.ts b/src/core/execution/WarshipExecution.ts index 527056df6..121e7f2d7 100644 --- a/src/core/execution/WarshipExecution.ts +++ b/src/core/execution/WarshipExecution.ts @@ -242,12 +242,17 @@ export class WarshipExecution implements Execution { // Get warship's water component for connectivity check const warshipComponent = this.mg.getWaterComponent(this.warship.tile()); + const patrolTile = this.warship.patrolTile(); + if (patrolTile === undefined) { + return undefined; + } + while (expandCount < 3) { const x = - this.mg.x(this.warship.patrolTile()!) + + this.mg.x(patrolTile) + this.random.nextInt(-warshipPatrolRange / 2, warshipPatrolRange / 2); const y = - this.mg.y(this.warship.patrolTile()!) + + this.mg.y(patrolTile) + this.random.nextInt(-warshipPatrolRange / 2, warshipPatrolRange / 2); if (!this.mg.isValidCoord(x, y)) { continue; diff --git a/src/core/game/GameMap.ts b/src/core/game/GameMap.ts index b885a9403..592d02ca4 100644 --- a/src/core/game/GameMap.ts +++ b/src/core/game/GameMap.ts @@ -167,7 +167,14 @@ export class GameMapImpl implements GameMap { } isValidCoord(x: number, y: number): boolean { - return x >= 0 && x < this.width_ && y >= 0 && y < this.height_; + return ( + Number.isInteger(x) && + Number.isInteger(y) && + x >= 0 && + x < this.width_ && + y >= 0 && + y < this.height_ + ); } // Terrain getters (immutable)