mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 10:21:27 +00:00
Random spawn (#2375)
## Description: https://github.com/openfrontio/OpenFrontIO/issues/2352 This is my first PR in this project, and I’ll continue refining it based on feedback. <img width="1088" height="859" alt="image" src="https://github.com/user-attachments/assets/07f4f8b1-52fa-4136-add4-19b00aefd963" /> <img width="1157" height="783" alt="image" src="https://github.com/user-attachments/assets/1c5be80d-72f8-4ead-8d4b-706a3a04fd73" /> <img width="1488" height="777" alt="image" src="https://github.com/user-attachments/assets/4d743548-f0c3-4579-963b-43676f68fab1" /> <img width="1499" height="778" alt="image" src="https://github.com/user-attachments/assets/f808e44f-ef97-467f-9e41-812e2857c36e" /> ## 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: nikolaj_mykola --------- Co-authored-by: Evan <evanpelle@gmail.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { PlayerSpawner } from "../../../../src/core/execution/utils/PlayerSpawner";
|
||||
import { PlayerInfo, PlayerType } from "../../../../src/core/game/Game";
|
||||
import { setup } from "../../../util/Setup";
|
||||
|
||||
describe("PlayerSpawner", () => {
|
||||
// Manually calculated based on number of tiles in manifest of each map
|
||||
// and minimum distance between players in PlayerSpawner
|
||||
test.each([
|
||||
["big_plains", 49],
|
||||
["half_land_half_ocean", 1],
|
||||
["ocean_and_land", 1],
|
||||
["plains", 9],
|
||||
])(
|
||||
"Spawn location is found for all players in %s map with %i players",
|
||||
async (mapName, maxPlayers) => {
|
||||
const players: PlayerInfo[] = [];
|
||||
|
||||
for (let i = 0; i < maxPlayers; i++) {
|
||||
players.push(
|
||||
new PlayerInfo(
|
||||
`player${i}`,
|
||||
PlayerType.Human,
|
||||
`client_id${i}`,
|
||||
`player_id${i}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const game = await setup(mapName, undefined, players);
|
||||
|
||||
const executors = new PlayerSpawner(game, "game_id").spawnPlayers();
|
||||
expect(executors.length).toBe(maxPlayers);
|
||||
|
||||
for (const executor of executors) {
|
||||
expect(game.isLand(executor.tile)).toBe(true);
|
||||
expect(game.isBorder(executor.tile)).toBe(false);
|
||||
}
|
||||
|
||||
for (let i = 0; i < executors.length; i++) {
|
||||
for (let j = i + 1; j < executors.length; j++) {
|
||||
const distance = game.manhattanDist(
|
||||
executors[i].tile,
|
||||
executors[j].tile,
|
||||
);
|
||||
expect(distance).toBeGreaterThanOrEqual(30);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test("Handles spawn failure when map is too crowded", async () => {
|
||||
const players: PlayerInfo[] = [];
|
||||
|
||||
// Try to spawn more players than possible on a small map
|
||||
for (let i = 0; i < 5; i++) {
|
||||
players.push(
|
||||
new PlayerInfo(
|
||||
`player${i}`,
|
||||
PlayerType.Human,
|
||||
`client_id${i}`,
|
||||
`player_id${i}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const game = await setup("half_land_half_ocean", undefined, players);
|
||||
const executors = new PlayerSpawner(game, "game_id").spawnPlayers();
|
||||
|
||||
// Should spawn fewer than requested when map is too small
|
||||
expect(executors.length).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user