Configurable nation count 🤖 (#3338)

## Description:

I hope we can get this into v30?
The nation count is configurable now, just like the bot count.
Replaced the "Disable Nations" toggle with a nations slider (0–400) in
SinglePlayer and Host Lobby modals.

<img width="710" height="121" alt="Screenshot 2026-03-03 021952"
src="https://github.com/user-attachments/assets/c8d0f0c3-db51-4303-95fa-dbc770460ec2"
/>


Public games are staying exactly the same, this is just for singleplayer
and private lobby fun.
Youtubers could play HvN against 400 nations, for example.
Singleplayer enjoyers no longer have to play against 1 nation in HvN,
they can freely choose.

`GameConfig.disableNations: boolean` got replaced by `nations: number
(0-400, optional)`
`undefined` = map default, 
`0` = disabled, 
number = custom count

Nations slider defaults to the map's nation count, shows "(MAP DEFAULT)"
label when unchanged
Compact map toggle reduces nations to 25% when at default, restores when
toggled off (just like we already do with bots)
The nation count for HvN no longer automatically matches the human count
in singleplayer and private games, only in public games.

**What if there aren't enough nations configured for the map?**
We just use the HvN logic (Generate random nations)

### Warning

**This infra PR also needs to get merged:
https://github.com/openfrontio/infra/pull/263
Otherwise players can set 0 nations and get achievements.**

## 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
This commit is contained in:
FloPinguin
2026-03-03 23:07:06 +01:00
committed by GitHub
parent 28bbd933a4
commit 0b9d43cb46
21 changed files with 309 additions and 168 deletions
+6 -1
View File
@@ -219,7 +219,12 @@ export const GameConfigSchema = z.object({
startingGold: z.number().int().min(0).optional(),
})
.optional(),
disableNations: z.boolean(),
nations: z
.number()
.int()
.min(1)
.max(400)
.or(z.enum(["default", "disabled"])),
bots: z.number().int().min(0).max(400),
infiniteGold: z.boolean(),
infiniteTroops: z.boolean(),
+1 -1
View File
@@ -224,7 +224,7 @@ export class DefaultConfig implements Config {
}
spawnNations(): boolean {
return !this._gameConfig.disableNations;
return this._gameConfig.nations !== "disabled";
}
isUnitDisabled(unitType: UnitType): boolean {
+56 -35
View File
@@ -13,9 +13,14 @@ import {
import { Nation as ManifestNation } from "./TerrainMapLoader";
/**
* Creates the nations array for a game, handling HumansVsNations mode specially.
* In HumansVsNations mode, the number of nations matches the number of human players to ensure fair gameplay.
* For compact maps, only 25% of the nations are used.
* Creates the nations array for a game.
* If config.nations is a number (custom count), uses that exact count,
* generating additional nations with random names if needed.
* If config.nations is "disabled", returns no nations.
* If config.nations is "default":
* - Public HumansVsNations: matches nation count to human player count
* - Public compact maps: uses 25% of manifest nations
* - Otherwise: uses all manifest nations
*/
export function createNationsForGame(
gameStart: GameStartInfo,
@@ -23,10 +28,6 @@ export function createNationsForGame(
numHumans: number,
random: PseudoRandom,
): Nation[] {
if (gameStart.config.disableNations) {
return [];
}
const toNation = (n: ManifestNation): Nation =>
new Nation(
new Cell(n.coordinates[0], n.coordinates[1]),
@@ -39,38 +40,59 @@ export function createNationsForGame(
gameStart.config.gameMode === GameMode.Team &&
gameStart.config.playerTeams === HumansVsNations;
// For compact maps, use only 25% of nations (minimum 1)
let effectiveNations = manifestNations;
if (isCompactMap && !isHumansVsNations) {
const targetCount = getCompactMapNationCount(manifestNations.length, true);
const shuffled = random.shuffleArray(manifestNations);
effectiveNations = shuffled.slice(0, targetCount);
}
// For non-HumansVsNations modes, simply use the effective nations
if (!isHumansVsNations) {
return effectiveNations.map(toNation);
}
// HumansVsNations mode: balance nation count to match human count
const isSingleplayer = gameStart.config.gameType === GameType.Singleplayer;
const targetNationCount = isSingleplayer ? 1 : numHumans;
if (targetNationCount === 0) {
const configNations = gameStart.config.nations;
if (configNations === "disabled") {
return [];
}
// If we have enough manifest nations, use a subset
if (manifestNations.length >= targetNationCount) {
// Shuffle manifest nations to add variety
const shuffled = random.shuffleArray(manifestNations);
return shuffled.slice(0, targetNationCount).map(toNation);
// If nations count is explicitly set, use that exact count
if (typeof configNations === "number") {
return createRandomNations(
configNations,
manifestNations,
toNation,
random,
);
}
// If we need more nations than defined in manifest, create additional ones
const nations: Nation[] = manifestNations.map(toNation);
if (gameStart.config.gameType === GameType.Public) {
// For HvN, balance nation count to match human count
if (isHumansVsNations) {
return createRandomNations(numHumans, manifestNations, toNation, random);
}
// For compact maps, use only 25% of nations (minimum 1)
if (isCompactMap) {
const targetCount = getCompactMapNationCount(
manifestNations.length,
true,
);
const shuffled = random.shuffleArray(manifestNations);
const slicedNations = shuffled.slice(0, targetCount);
return slicedNations.map(toNation);
}
}
return manifestNations.map(toNation);
}
/**
* Creates the requested number of nations from manifest data.
* If more nations are needed than available in the manifest, generates additional ones with random names.
*/
function createRandomNations(
targetCount: number,
manifestNations: ManifestNation[],
toNation: (n: ManifestNation) => Nation,
random: PseudoRandom,
): Nation[] {
const shuffled = random.shuffleArray(manifestNations);
if (targetCount <= manifestNations.length) {
return shuffled.slice(0, targetCount).map(toNation);
}
// Need more nations than defined in manifest, create additional ones
const nations: Nation[] = shuffled.map(toNation);
const usedNames = new Set(nations.map((n) => n.playerInfo.name));
const additionalCount = targetNationCount - manifestNations.length;
const additionalCount = targetCount - manifestNations.length;
for (let i = 0; i < additionalCount; i++) {
const name = generateUniqueNationName(random, usedNames);
usedNames.add(name);
@@ -81,7 +103,6 @@ export function createNationsForGame(
),
);
}
return nations;
}