mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-15 13:33:03 +00:00
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:
@@ -8,7 +8,6 @@ import {
|
||||
GameMapType,
|
||||
GameMode,
|
||||
GameType,
|
||||
HumansVsNations,
|
||||
UnitType,
|
||||
} from "../core/game/Game";
|
||||
import { TeamCountConfig } from "../core/Schemas";
|
||||
@@ -26,18 +25,21 @@ import { JoinLobbyEvent } from "./Main";
|
||||
import { UsernameInput } from "./UsernameInput";
|
||||
import {
|
||||
getBotsForCompactMap,
|
||||
getNationsForCompactMap,
|
||||
getRandomMapType,
|
||||
getUpdatedDisabledUnits,
|
||||
parseBoundedFloatFromInput,
|
||||
parseBoundedIntegerFromInput,
|
||||
preventDisallowedKeys,
|
||||
sliderToNationsConfig,
|
||||
toOptionalNumber,
|
||||
} from "./utilities/GameConfigHelpers";
|
||||
|
||||
import { terrainMapFileLoader } from "./TerrainMapFileLoader";
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
selectedMap: GameMapType.World,
|
||||
selectedDifficulty: Difficulty.Easy,
|
||||
disableNations: false,
|
||||
bots: 400,
|
||||
infiniteGold: false,
|
||||
infiniteTroops: false,
|
||||
@@ -61,7 +63,8 @@ export class SinglePlayerModal extends BaseModal {
|
||||
@state() private selectedMap: GameMapType = DEFAULT_OPTIONS.selectedMap;
|
||||
@state() private selectedDifficulty: Difficulty =
|
||||
DEFAULT_OPTIONS.selectedDifficulty;
|
||||
@state() private disableNations: boolean = DEFAULT_OPTIONS.disableNations;
|
||||
@state() private nations: number = 0;
|
||||
@state() private defaultNationCount: number = 0;
|
||||
@state() private bots: number = DEFAULT_OPTIONS.bots;
|
||||
@state() private infiniteGold: boolean = DEFAULT_OPTIONS.infiniteGold;
|
||||
@state() private infiniteTroops: boolean = DEFAULT_OPTIONS.infiniteTroops;
|
||||
@@ -88,12 +91,15 @@ export class SinglePlayerModal extends BaseModal {
|
||||
...DEFAULT_OPTIONS.disabledUnits,
|
||||
];
|
||||
|
||||
private mapLoader = terrainMapFileLoader;
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
document.addEventListener(
|
||||
"userMeResponse",
|
||||
this.handleUserMeResponse as EventListener,
|
||||
);
|
||||
void this.loadNationCount();
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
@@ -265,7 +271,7 @@ export class SinglePlayerModal extends BaseModal {
|
||||
},
|
||||
difficulty: {
|
||||
selected: this.selectedDifficulty,
|
||||
disabled: this.disableNations,
|
||||
disabled: this.nations === 0,
|
||||
},
|
||||
gameMode: {
|
||||
selected: this.gameMode,
|
||||
@@ -280,14 +286,13 @@ export class SinglePlayerModal extends BaseModal {
|
||||
labelKey: "single_modal.bots",
|
||||
disabledKey: "single_modal.bots_disabled",
|
||||
},
|
||||
nations: {
|
||||
value: this.nations,
|
||||
defaultValue: this.defaultNationCount,
|
||||
labelKey: "single_modal.nations",
|
||||
disabledKey: "single_modal.nations_disabled",
|
||||
},
|
||||
toggles: [
|
||||
{
|
||||
labelKey: "single_modal.disable_nations",
|
||||
checked: this.disableNations,
|
||||
hidden:
|
||||
this.gameMode === GameMode.Team &&
|
||||
this.teamCount === HumansVsNations,
|
||||
},
|
||||
{
|
||||
labelKey: "single_modal.instant_build",
|
||||
checked: this.instantBuild,
|
||||
@@ -322,6 +327,7 @@ export class SinglePlayerModal extends BaseModal {
|
||||
@game-mode-selected=${this.handleConfigGameModeSelected}
|
||||
@team-count-selected=${this.handleConfigTeamCountSelected}
|
||||
@bots-changed=${this.handleBotsChange}
|
||||
@nations-changed=${this.handleNationsChange}
|
||||
@option-toggle-changed=${this.handleConfigOptionToggleChanged}
|
||||
@unit-toggle-changed=${this.handleConfigUnitToggleChanged}
|
||||
></game-config-settings>
|
||||
@@ -366,7 +372,7 @@ export class SinglePlayerModal extends BaseModal {
|
||||
// Check if any options other than map and difficulty have been changed from defaults
|
||||
private hasOptionsChanged(): boolean {
|
||||
return (
|
||||
this.disableNations !== DEFAULT_OPTIONS.disableNations ||
|
||||
this.nations !== this.defaultNationCount ||
|
||||
this.bots !== DEFAULT_OPTIONS.bots ||
|
||||
this.infiniteGold !== DEFAULT_OPTIONS.infiniteGold ||
|
||||
this.infiniteTroops !== DEFAULT_OPTIONS.infiniteTroops ||
|
||||
@@ -387,8 +393,9 @@ export class SinglePlayerModal extends BaseModal {
|
||||
this.selectedDifficulty = DEFAULT_OPTIONS.selectedDifficulty;
|
||||
this.gameMode = DEFAULT_OPTIONS.gameMode;
|
||||
this.useRandomMap = DEFAULT_OPTIONS.useRandomMap;
|
||||
this.disableNations = DEFAULT_OPTIONS.disableNations;
|
||||
this.bots = DEFAULT_OPTIONS.bots;
|
||||
this.nations = 0;
|
||||
this.defaultNationCount = 0;
|
||||
this.infiniteGold = DEFAULT_OPTIONS.infiniteGold;
|
||||
this.infiniteTroops = DEFAULT_OPTIONS.infiniteTroops;
|
||||
this.compactMap = DEFAULT_OPTIONS.compactMap;
|
||||
@@ -404,8 +411,14 @@ export class SinglePlayerModal extends BaseModal {
|
||||
this.startingGoldValue = DEFAULT_OPTIONS.startingGoldValue;
|
||||
}
|
||||
|
||||
protected onOpen(): void {
|
||||
void this.loadNationCount();
|
||||
}
|
||||
|
||||
private handleSelectRandomMap() {
|
||||
this.useRandomMap = true;
|
||||
this.selectedMap = getRandomMapType();
|
||||
void this.loadNationCount();
|
||||
}
|
||||
|
||||
private handleConfigRandomMapSelected = () => {
|
||||
@@ -415,6 +428,7 @@ export class SinglePlayerModal extends BaseModal {
|
||||
private handleMapSelection(value: GameMapType) {
|
||||
this.selectedMap = value;
|
||||
this.useRandomMap = false;
|
||||
void this.loadNationCount();
|
||||
}
|
||||
|
||||
private handleConfigMapSelected = (e: Event) => {
|
||||
@@ -444,6 +458,11 @@ export class SinglePlayerModal extends BaseModal {
|
||||
private handleCompactMapChange(val: boolean) {
|
||||
this.compactMap = val;
|
||||
this.bots = getBotsForCompactMap(this.bots, val);
|
||||
this.nations = getNationsForCompactMap(
|
||||
this.nations,
|
||||
this.defaultNationCount,
|
||||
val,
|
||||
);
|
||||
}
|
||||
|
||||
private handleConfigOptionToggleChanged = (e: Event) => {
|
||||
@@ -454,9 +473,6 @@ export class SinglePlayerModal extends BaseModal {
|
||||
const { labelKey, checked } = customEvent.detail;
|
||||
|
||||
switch (labelKey) {
|
||||
case "single_modal.disable_nations":
|
||||
this.disableNations = checked;
|
||||
break;
|
||||
case "single_modal.instant_build":
|
||||
this.instantBuild = checked;
|
||||
break;
|
||||
@@ -496,6 +512,15 @@ export class SinglePlayerModal extends BaseModal {
|
||||
this.bots = value;
|
||||
};
|
||||
|
||||
private handleNationsChange = (e: Event) => {
|
||||
const customEvent = e as CustomEvent<{ value: number }>;
|
||||
const value = customEvent.detail.value;
|
||||
if (isNaN(value) || value < 0 || value > 400) {
|
||||
return;
|
||||
}
|
||||
this.nations = value;
|
||||
};
|
||||
|
||||
private handleMaxTimerToggle = (
|
||||
checked: boolean,
|
||||
value: number | string | undefined,
|
||||
@@ -604,11 +629,6 @@ export class SinglePlayerModal extends BaseModal {
|
||||
finalMaxTimerValue = Math.max(1, Math.min(120, this.maxTimerValue));
|
||||
}
|
||||
|
||||
// If random map is selected, choose a random map now
|
||||
if (this.useRandomMap) {
|
||||
this.selectedMap = getRandomMapType();
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Starting single player game with map: ${GameMapType[this.selectedMap as keyof typeof GameMapType]}${this.useRandomMap ? " (Randomly selected)" : ""}`,
|
||||
);
|
||||
@@ -657,14 +677,10 @@ export class SinglePlayerModal extends BaseModal {
|
||||
disabledUnits: this.disabledUnits
|
||||
.map((u) => Object.values(UnitType).find((ut) => ut === u))
|
||||
.filter((ut): ut is UnitType => ut !== undefined),
|
||||
...(this.gameMode === GameMode.Team &&
|
||||
this.teamCount === HumansVsNations
|
||||
? {
|
||||
disableNations: false,
|
||||
}
|
||||
: {
|
||||
disableNations: this.disableNations,
|
||||
}),
|
||||
nations: sliderToNationsConfig(
|
||||
this.nations,
|
||||
this.defaultNationCount,
|
||||
),
|
||||
...(this.goldMultiplier && this.goldMultiplierValue
|
||||
? { goldMultiplier: this.goldMultiplierValue }
|
||||
: {}),
|
||||
@@ -682,4 +698,22 @@ export class SinglePlayerModal extends BaseModal {
|
||||
);
|
||||
this.close();
|
||||
}
|
||||
|
||||
private async loadNationCount() {
|
||||
const currentMap = this.selectedMap;
|
||||
try {
|
||||
const mapData = this.mapLoader.getMapData(currentMap);
|
||||
const manifest = await mapData.manifest();
|
||||
// Only update if the map hasn't changed
|
||||
if (this.selectedMap === currentMap) {
|
||||
this.defaultNationCount = manifest.nations.length;
|
||||
this.nations = this.compactMap
|
||||
? Math.max(0, Math.floor(manifest.nations.length * 0.25))
|
||||
: manifest.nations.length;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Failed to load nation count", error);
|
||||
// Leave existing values unchanged so the UI stays consistent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user