create FakeHumanExecution

This commit is contained in:
evanpelle
2024-09-07 21:11:34 -07:00
parent 5931d15caf
commit 24ad59c75d
4 changed files with 64 additions and 44 deletions
+1
View File
@@ -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);
}
+1
View File
@@ -34,6 +34,7 @@ export enum PlayerType {
export interface ExecutionView {
isActive(): boolean
// TODO: remove owner
owner(): Player
activeDuringSpawnPhase(): boolean
}
+13 -2
View File
@@ -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
}
}
+49 -42
View File
@@ -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
}
}