From 24ad59c75dcc44e900b372de5c9f1d61e18f685f Mon Sep 17 00:00:00 2001 From: evanpelle Date: Sat, 7 Sep 2024 21:11:34 -0700 Subject: [PATCH] create FakeHumanExecution --- src/client/ClientGame.ts | 1 + src/core/Game.ts | 1 + src/core/execution/ExecutionManager.ts | 15 +++- src/core/execution/FakeHumanExecution.ts | 91 +++++++++++++----------- 4 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/client/ClientGame.ts b/src/client/ClientGame.ts index 0855b50bd..9e983b2c9 100644 --- a/src/client/ClientGame.ts +++ b/src/client/ClientGame.ts @@ -130,6 +130,7 @@ export class ClientGame { this.renderer.initialize() this.input.initialize() this.gs.addExecution(...this.executor.spawnBots(this.gs.config().numBots())) + this.gs.addExecution(...this.executor.fakeHumanExecutions(1)) this.intervalID = setInterval(() => this.tick(), 10); } diff --git a/src/core/Game.ts b/src/core/Game.ts index 500893859..3aabb32e1 100644 --- a/src/core/Game.ts +++ b/src/core/Game.ts @@ -34,6 +34,7 @@ export enum PlayerType { export interface ExecutionView { isActive(): boolean + // TODO: remove owner owner(): Player activeDuringSpawnPhase(): boolean } diff --git a/src/core/execution/ExecutionManager.ts b/src/core/execution/ExecutionManager.ts index 793323e8a..3891adcec 100644 --- a/src/core/execution/ExecutionManager.ts +++ b/src/core/execution/ExecutionManager.ts @@ -1,4 +1,4 @@ -import {Cell, Execution, MutableGame, Game, MutablePlayer, PlayerInfo, TerraNullius, Tile} from "../Game"; +import {Cell, Execution, MutableGame, Game, MutablePlayer, PlayerInfo, TerraNullius, Tile, PlayerType} from "../Game"; import {AttackIntent, BoatAttackIntentSchema, Intent, Turn} from "../Schemas"; import {AttackExecution} from "./AttackExecution"; import {SpawnExecution} from "./SpawnExecution"; @@ -60,7 +60,18 @@ export class Executor { } fakeHumanExecutions(numFakes: number): Execution[] { - return [new FakeHumanExecution(null)] + const execs = [] + for (let i = 0; i < numFakes; i++) { + execs.push( + new FakeHumanExecution(new PlayerInfo( + "fake_human" + i, + PlayerType.FakeHuman, + this.random.nextID(), + this.random.nextID() + )) + ) + } + return execs } } \ No newline at end of file diff --git a/src/core/execution/FakeHumanExecution.ts b/src/core/execution/FakeHumanExecution.ts index 8f5fe4409..e87ed8d93 100644 --- a/src/core/execution/FakeHumanExecution.ts +++ b/src/core/execution/FakeHumanExecution.ts @@ -1,4 +1,4 @@ -import {Cell, Execution, MutableGame, MutablePlayer, Player, PlayerID, PlayerInfo, TerraNullius} from "../Game" +import {Cell, Execution, MutableGame, MutablePlayer, Player, PlayerID, PlayerInfo, TerraNullius, Tile} from "../Game" import {PseudoRandom} from "../PseudoRandom" import {simpleHash} from "../Util"; import {AttackExecution} from "./AttackExecution"; @@ -13,13 +13,10 @@ export class FakeHumanExecution implements Execution { private neighborsTerraNullius = true - constructor(private bot: MutablePlayer) { - this.random = new PseudoRandom(simpleHash(bot.id())) + constructor(private playerInfo: PlayerInfo) { + this.random = new PseudoRandom(simpleHash(playerInfo.id)) this.attackRate = this.random.nextInt(10, 50) } - activeDuringSpawnPhase(): boolean { - return true - } init(mg: MutableGame, ticks: number) { this.mg = mg @@ -27,60 +24,70 @@ export class FakeHumanExecution implements Execution { tick(ticks: number) { - if (ticks < this.mg.config().numSpawnPhaseTurns()) { - if(ticks % 10 == 0) { + if (this.mg.inSpawnPhase()) { + if (ticks % 10 == 0) { this.mg.addExecution(new SpawnExecution( - null, null + this.playerInfo, + this.randomLand().cell() )) } } - if (!this.bot.isAlive()) { - this.active = false - return - } - if (ticks % this.attackRate != 0) { return } - if (this.neighborsTerraNullius) { - for (const b of this.bot.borderTiles()) { - for (const n of b.neighbors()) { - if (n.owner() == this.mg.terraNullius() && n.isLand()) { - this.sendAttack(this.mg.terraNullius()) - return - } - } + // if (this.neighborsTerraNullius) { + // for (const b of this.bot.borderTiles()) { + // for (const n of b.neighbors()) { + // if (n.owner() == this.mg.terraNullius() && n.isLand()) { + // this.sendAttack(this.mg.terraNullius()) + // return + // } + // } + // } + // this.neighborsTerraNullius = false + // } + + // const border = Array.from(this.bot.borderTiles()).flatMap(t => t.neighbors()).filter(t => t.hasOwner() && t.owner() != this.bot) + + // if (border.length == 0) { + // return + // } + + // const toAttack = border[this.random.nextInt(0, border.length)] + // this.sendAttack(toAttack.owner()) + } + + randomLand(): Tile { + while (true) { + const cell = new Cell(this.random.nextInt(0, this.mg.width()), this.random.nextInt(0, this.mg.height())) + const tile = this.mg.tile(cell) + if (tile.isLand()) { + return tile } - this.neighborsTerraNullius = false } - - const border = Array.from(this.bot.borderTiles()).flatMap(t => t.neighbors()).filter(t => t.hasOwner() && t.owner() != this.bot) - - if (border.length == 0) { - return - } - - const toAttack = border[this.random.nextInt(0, border.length)] - this.sendAttack(toAttack.owner()) } - sendAttack(toAttack: Player | TerraNullius) { - this.mg.addExecution(new AttackExecution( - this.bot.troops() / 20, - this.bot.id(), - toAttack.isPlayer() ? toAttack.id() : null, - null, - null - )) - } + // sendAttack(toAttack: Player | TerraNullius) { + // this.mg.addExecution(new AttackExecution( + // this.bot.troops() / 20, + // this.bot.id(), + // toAttack.isPlayer() ? toAttack.id() : null, + // null, + // null + // )) + // } owner(): MutablePlayer { - return this.bot + return null } isActive(): boolean { return this.active } + + activeDuringSpawnPhase(): boolean { + return true + } } \ No newline at end of file