mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-30 01:22:12 +00:00
Special bot names (#2552)
## Description: Special bot names. If the solution seems convoluted for such an easy thing, that is because: not all bots find a spawn position, so only assign a candidate name after finding a spawn. And the first few are almost always overwritten by Nation spawns so the first 20 just get a random name. Only then do we assign from the provided lists. For the random names, some might get the same name but that's not an issue as no-one will notice and they're off the map quite fast anyway. ## 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: tryout33
This commit is contained in:
@@ -4,11 +4,16 @@ import { PseudoRandom } from "../PseudoRandom";
|
||||
import { GameID } from "../Schemas";
|
||||
import { simpleHash } from "../Util";
|
||||
import { SpawnExecution } from "./SpawnExecution";
|
||||
import { BOT_NAME_PREFIXES, BOT_NAME_SUFFIXES } from "./utils/BotNames";
|
||||
import {
|
||||
COMMUNITY_FULL_ELF_NAMES,
|
||||
COMMUNITY_PREFIXES,
|
||||
SPECIAL_FULL_ELF_NAMES,
|
||||
} from "./utils/BotNames";
|
||||
|
||||
export class BotSpawner {
|
||||
private random: PseudoRandom;
|
||||
private bots: SpawnExecution[] = [];
|
||||
private nameIndex = 0;
|
||||
|
||||
constructor(
|
||||
private gs: Game,
|
||||
@@ -24,9 +29,13 @@ export class BotSpawner {
|
||||
console.log("too many retries while spawning bots, giving up");
|
||||
return this.bots;
|
||||
}
|
||||
const botName = this.randomBotName();
|
||||
const spawn = this.spawnBot(botName);
|
||||
const candidate = this.nextCandidateName();
|
||||
const spawn = this.spawnBot(candidate.name);
|
||||
if (spawn !== null) {
|
||||
// Only use candidate name once bot successfully spawned
|
||||
if (candidate.source === "list") {
|
||||
this.nameIndex++;
|
||||
}
|
||||
this.bots.push(spawn);
|
||||
} else {
|
||||
tries++;
|
||||
@@ -51,10 +60,42 @@ export class BotSpawner {
|
||||
);
|
||||
}
|
||||
|
||||
private randomBotName(): string {
|
||||
const prefixIndex = this.random.nextInt(0, BOT_NAME_PREFIXES.length);
|
||||
const suffixIndex = this.random.nextInt(0, BOT_NAME_SUFFIXES.length);
|
||||
return `${BOT_NAME_PREFIXES[prefixIndex]} ${BOT_NAME_SUFFIXES[suffixIndex]}`;
|
||||
private nextCandidateName(): {
|
||||
name: string;
|
||||
source: "list" | "random";
|
||||
} {
|
||||
if (this.bots.length < 20) {
|
||||
//first few usually overwritten by Nation spawn
|
||||
return { name: this.getRandomElf(), source: "random" };
|
||||
}
|
||||
|
||||
if (this.nameIndex < COMMUNITY_FULL_ELF_NAMES.length) {
|
||||
return {
|
||||
name: COMMUNITY_FULL_ELF_NAMES[this.nameIndex],
|
||||
source: "list",
|
||||
};
|
||||
}
|
||||
const specialOffset = COMMUNITY_FULL_ELF_NAMES.length;
|
||||
if (this.nameIndex < specialOffset + SPECIAL_FULL_ELF_NAMES.length) {
|
||||
return {
|
||||
name: SPECIAL_FULL_ELF_NAMES[this.nameIndex - specialOffset],
|
||||
source: "list",
|
||||
};
|
||||
}
|
||||
const prefixOffset = specialOffset + SPECIAL_FULL_ELF_NAMES.length;
|
||||
if (this.nameIndex < prefixOffset + COMMUNITY_PREFIXES.length) {
|
||||
return {
|
||||
name: `${COMMUNITY_PREFIXES[this.nameIndex - prefixOffset]} the Elf`,
|
||||
source: "list",
|
||||
};
|
||||
}
|
||||
|
||||
return { name: this.getRandomElf(), source: "random" };
|
||||
}
|
||||
|
||||
private getRandomElf(): string {
|
||||
const suffixNumber = this.random.nextInt(1, 10001);
|
||||
return `Elf ${suffixNumber}`;
|
||||
}
|
||||
|
||||
private randTile(): TileRef {
|
||||
|
||||
Reference in New Issue
Block a user