Files
OpenFrontIO/src/core/execution/SpawnExecution.ts
T
FloPinguin f8156c550b Fix random spawn (#2958)
## Description:

"You can pick your spawn in random spawn games in v29. You need to open
the menu and click on the attack button. That's it."

Thats the fix for this problem.
Radial menu no longer allows to attack (pick a spawn) while random spawn
is enabled.
And SpawnExecution got a check so you cannot send malicious intents.

## 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:

FloPinguin
2026-01-19 23:49:10 +00:00

129 lines
3.0 KiB
TypeScript

import { Execution, Game, Player, PlayerInfo, PlayerType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { GameID } from "../Schemas";
import { simpleHash } from "../Util";
import { BotExecution } from "./BotExecution";
import { PlayerExecution } from "./PlayerExecution";
import { getSpawnTiles } from "./Util";
export class SpawnExecution implements Execution {
private random: PseudoRandom;
active: boolean = true;
private mg: Game;
private static readonly MAX_SPAWN_TRIES = 1_000;
constructor(
gameID: GameID,
private playerInfo: PlayerInfo,
public tile?: TileRef,
) {
this.random = new PseudoRandom(
simpleHash(playerInfo.id) + simpleHash(gameID),
);
}
init(mg: Game, ticks: number) {
this.mg = mg;
}
tick(ticks: number) {
this.active = false;
if (!this.mg.inSpawnPhase()) {
this.active = false;
return;
}
let player: Player | null = null;
if (this.mg.hasPlayer(this.playerInfo.id)) {
player = this.mg.player(this.playerInfo.id);
} else {
player = this.mg.addPlayer(this.playerInfo);
}
// Security: If random spawn is enabled, prevent players from re-rolling their spawn location
if (this.mg.config().isRandomSpawn() && player.hasSpawned()) {
return;
}
this.tile ??= this.randomSpawnLand();
if (this.tile === undefined) {
console.warn(`SpawnExecution: cannot spawn ${this.playerInfo.name}`);
return;
}
player.tiles().forEach((t) => player.relinquish(t));
getSpawnTiles(this.mg, this.tile).forEach((t) => {
player.conquer(t);
});
if (!player.hasSpawned()) {
this.mg.addExecution(new PlayerExecution(player));
if (player.type() === PlayerType.Bot) {
this.mg.addExecution(new BotExecution(player));
}
}
player.setSpawnTile(this.tile);
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return true;
}
private randomSpawnLand(): TileRef | undefined {
let tries = 0;
while (tries < SpawnExecution.MAX_SPAWN_TRIES) {
tries++;
const tile = this.randTile();
if (
!this.mg.isLand(tile) ||
this.mg.hasOwner(tile) ||
this.mg.isBorder(tile)
) {
continue;
}
const isOtherPlayerSpawnedNearby = this.mg
.allPlayers()
.filter((player) => player.id() !== this.playerInfo.id)
.some((player) => {
const spawnTile = player.spawnTile();
if (spawnTile === undefined) {
return false;
}
return (
this.mg.manhattanDist(spawnTile, tile) <
this.mg.config().minDistanceBetweenPlayers()
);
});
if (isOtherPlayerSpawnedNearby) {
continue;
}
return tile;
}
return;
}
private randTile(): TileRef {
const x = this.random.nextInt(0, this.mg.width());
const y = this.random.nextInt(0, this.mg.height());
return this.mg.ref(x, y);
}
}