mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-27 00:04:36 +00:00
+ add random bot names
This commit is contained in:
+6
-2
@@ -8,6 +8,10 @@ import { number } from 'zod';
|
||||
import { GameConfig, GameID, GameRecord, PlayerRecord, Turn } from './Schemas';
|
||||
import { customAlphabet, nanoid } from 'nanoid';
|
||||
|
||||
|
||||
export const MIN_USERNAME_LENGTH = 3;
|
||||
export const MAX_USERNAME_LENGTH = 12;
|
||||
|
||||
export function manhattanDist(c1: Cell, c2: Cell): number {
|
||||
return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
|
||||
}
|
||||
@@ -185,7 +189,7 @@ export function getMode(list: string[]): string {
|
||||
}
|
||||
|
||||
export function sanitize(name: string): string {
|
||||
return Array.from(name).slice(0, 10).join('').replace(/[^\p{L}\p{N}\s\p{Emoji}\p{Emoji_Component}]/gu, '');
|
||||
return Array.from(name).join('').replace(/[^\p{L}\p{N}\s\p{Emoji}\p{Emoji_Component}]/gu, '');
|
||||
}
|
||||
|
||||
export function processName(name: string): string {
|
||||
@@ -277,4 +281,4 @@ export function assertNever(x: never): never {
|
||||
export function generateID(): GameID {
|
||||
const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 8)
|
||||
return nanoid()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,45 +3,53 @@ import {Cell, Game, PlayerType, Tile, TileEvent} from "../game/Game";
|
||||
import {PseudoRandom} from "../PseudoRandom";
|
||||
import {GameID, SpawnIntent} from "../Schemas";
|
||||
import {bfs, dist as dist, manhattanDist, simpleHash} from "../Util";
|
||||
import {BOT_NAME_PREFIXES, BOT_NAME_SUFFIXES} from "./utils/BotNames";
|
||||
|
||||
|
||||
export class BotSpawner {
|
||||
private random: PseudoRandom
|
||||
private random: PseudoRandom;
|
||||
private bots: SpawnIntent[] = [];
|
||||
|
||||
constructor(private gs: Game, gameID: GameID) {
|
||||
this.random = new PseudoRandom(simpleHash(gameID))
|
||||
this.random = new PseudoRandom(simpleHash(gameID));
|
||||
}
|
||||
|
||||
spawnBots(numBots: number): SpawnIntent[] {
|
||||
let tries = 0
|
||||
let tries = 0;
|
||||
while (this.bots.length < numBots) {
|
||||
if (tries > 10000) {
|
||||
consolex.log('too many retries while spawning bots, giving up')
|
||||
return this.bots
|
||||
consolex.log("too many retries while spawning bots, giving up");
|
||||
return this.bots;
|
||||
}
|
||||
const spawn = this.spawnBot("Bot" + this.bots.length)
|
||||
const botName = this.randomBotName();
|
||||
const spawn = this.spawnBot(botName);
|
||||
if (spawn != null) {
|
||||
this.bots.push(spawn);
|
||||
} else {
|
||||
tries++
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
return this.bots;
|
||||
}
|
||||
|
||||
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]}`;
|
||||
}
|
||||
|
||||
spawnBot(botName: string): SpawnIntent | null {
|
||||
const tile = this.randTile()
|
||||
const tile = this.randTile();
|
||||
if (!tile.isLand()) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
for (const spawn of this.bots) {
|
||||
if (manhattanDist(new Cell(spawn.x, spawn.y), tile.cell()) < 30) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return {
|
||||
type: 'spawn',
|
||||
type: "spawn",
|
||||
playerID: this.random.nextID(),
|
||||
name: botName,
|
||||
playerType: PlayerType.Bot,
|
||||
@@ -51,10 +59,11 @@ export class BotSpawner {
|
||||
}
|
||||
|
||||
private randTile(): Tile {
|
||||
return this.gs.tile(new Cell(
|
||||
this.random.nextInt(0, this.gs.width()),
|
||||
this.random.nextInt(0, this.gs.height())
|
||||
))
|
||||
return this.gs.tile(
|
||||
new Cell(
|
||||
this.random.nextInt(0, this.gs.width()),
|
||||
this.random.nextInt(0, this.gs.height())
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user