bot attacks terranullius first

This commit is contained in:
evanpelle
2024-08-11 17:57:24 -07:00
parent 14e7cb2ac9
commit fab17bd48a
3 changed files with 38 additions and 12 deletions
+1
View File
@@ -4,6 +4,7 @@
* render player info efficiently DONE 8/11/2024
* better troop addition logic DONE 8/11/2024
* better expansion, add back directed expansion
* harder to expand on other players (defense)
* use pastel theme for territories
* improve front page
* add username in front page
+11 -4
View File
@@ -10,6 +10,7 @@ type CellString = string
class TileImpl implements Tile {
public _isBorder = false
private _neighbors: Tile[] = null
constructor(
private readonly gs: GameImpl,
@@ -32,7 +33,10 @@ class TileImpl implements Tile {
terrain(): TerrainType {return this._terrain}
neighbors(): Tile[] {
return this.gs.neighbors(this)
if (this._neighbors == null) {
this._neighbors = this.gs.neighbors(this)
}
return this._neighbors
}
game(): Game {return this.gs}
@@ -285,7 +289,6 @@ export class GameImpl implements MutableGame {
}
neighbors(tile: Tile): Tile[] {
this.assertIsOnMap(tile.cell())
const x = tile.cell().x
const y = tile.cell().y
const ns: TileImpl[] = []
@@ -338,7 +341,11 @@ export class GameImpl implements MutableGame {
const tiles: Tile[] = []
tiles.push(tile)
tile.neighbors().forEach(t => tiles.push(t))
tiles.filter(t => t.hasOwner()).forEach(t => {
for (const t of tiles) {
if (!t.hasOwner()) {
continue
}
if (this.isBorder(t)) {
(t.owner() as PlayerImpl)._borderTiles.set(t.cell().toString(), t);
(t.owner() as PlayerImpl)._borderTileSet.add(t);
@@ -348,7 +355,7 @@ export class GameImpl implements MutableGame {
(t.owner() as PlayerImpl)._borderTileSet.delete(t);
(t as TileImpl)._isBorder = false
}
})
}
}
isBorder(tile: Tile): boolean {
+26 -8
View File
@@ -1,4 +1,4 @@
import {Cell, Execution, MutableGame, MutablePlayer, PlayerID, PlayerInfo} from "../Game"
import {Cell, Execution, MutableGame, MutablePlayer, Player, PlayerID, PlayerInfo, TerraNullius} from "../Game"
import {PseudoRandom} from "../PseudoRandom"
import {AttackExecution} from "./AttackExecution";
@@ -8,6 +8,7 @@ export class BotExecution implements Execution {
private random: PseudoRandom;
private attackRate: number
private gs: MutableGame
private neighborsTerra = true
constructor(private bot: MutablePlayer) {
@@ -17,6 +18,7 @@ export class BotExecution implements Execution {
init(gs: MutableGame, ticks: number) {
this.gs = gs
// this.neighborsTerra = this.bot.neighbors().filter(n => n == this.gs.terraNullius()).length > 0
}
tick(ticks: number) {
@@ -27,22 +29,38 @@ export class BotExecution implements Execution {
if (ticks % this.attackRate == 0) {
if (this.neighborsTerra) {
for (const b of this.bot.borderTiles()) {
for (const n of b.neighbors()) {
if (n.owner() == this.gs.terraNullius()) {
this.sendAttack(this.gs.terraNullius())
return
}
}
}
this.neighborsTerra = false
}
const ns = this.bot.neighbors()
if (ns.length == 0) {
return
}
const toAttack = ns[this.random.nextInt(0, ns.length)]
this.sendAttack(toAttack)
this.gs.addExecution(new AttackExecution(
this.bot.troops() / 100,
this.bot.id(),
toAttack.isPlayer() ? toAttack.id() : null,
null
))
}
}
sendAttack(toAttack: Player | TerraNullius) {
this.gs.addExecution(new AttackExecution(
this.bot.troops() / 20,
this.bot.id(),
toAttack.isPlayer() ? toAttack.id() : null,
null
))
}
owner(): MutablePlayer {
return this.bot
}