made attack execution more efficient

This commit is contained in:
evanpelle
2024-08-11 20:32:19 -07:00
parent 47fa978c35
commit fe52f6035d
4 changed files with 78 additions and 87 deletions
+15 -67
View File
@@ -14,6 +14,7 @@ export class AttackExecution implements Execution {
private mg: MutableGame
private numTilesWithEnemy = 0
private borderTiles: Set<Tile> = new Set()
constructor(
private troops: number,
@@ -35,31 +36,22 @@ export class AttackExecution implements Execution {
if (!this.active) {
return
}
// const t = this.mg.tile(new Cell(0, 0))
// this.toConquer.add(new TileContainer(t, 4))
// this.toConquer.add(new TileContainer(t, 1))
// this.toConquer.add(new TileContainer(t, 2))
// this.toConquer.add(new TileContainer(t, 3))
// while (this.toConquer.size() > 0) {
// console.log(`!!! got ${this.toConquer.poll().priority}`)
// }
let numTilesPerTick = this.numTilesWithEnemy / 2
let numTilesPerTick = this.numTilesWithEnemy / 4
if (this.targetCell != null) {
numTilesPerTick /= 2
}
let badTiles = 0
while (numTilesPerTick > 0) {
if (this.troops < 1) {
this.active = false
return
}
if (this.toConquer.size() < this.numTilesWithEnemy / 2) {
if (this.toConquer.size() < this.numTilesWithEnemy / 1.5) {
this.calculateToConquer()
}
if (this.toConquer.size() == 0) {
if (this.toConquer.size() == 0 || badTiles > 100) {
this.active = false
this._owner.addTroops(this.troops)
return
@@ -69,6 +61,7 @@ export class AttackExecution implements Execution {
const tileToConquer: Tile = toConquerContainer.tile
const onBorder = tileToConquer.neighbors().filter(t => t.owner() == this._owner).length > 0
if (tileToConquer.owner() != this.target || !onBorder) {
badTiles++
continue
}
this._owner.conquer(tileToConquer)
@@ -79,62 +72,21 @@ export class AttackExecution implements Execution {
private calculateToConquer() {
this.numTilesWithEnemy = 0
// console.profile('calc_to_conquer')
// let closestTile: Tile;
// let closestDist: number = Number.POSITIVE_INFINITY;
// for (const enemyTile of enemyBorder) {
// const dist = manhattanDist(enemyTile.cell(), this.targetCell)
// if (dist < closestDist) {
// closestTile = enemyTile
// }
// }
// tileByDist.forEach(t => console.log(`tile dist: ${manhattanDist(t.cell(), closestTile.cell())}`))
// let tileByDist = []
// if (this.targetCell == null) {
// tileByDist = Array.from(enemyBorder).slice().sort((a, b) => this.random.next() - .5)
// } else {
// }
// for (let i = 0; i < Math.min(enemyBorder.size / 2, tileByDist.length); i++) {
// const enemyTile = tileByDist[i]
// const numOwnedByMe = enemyTile.neighbors()
// .filter(t => t.terrain() == TerrainTypes.Land)
// .filter(t => t.owner() == this._owner)
// .length
// // this.toConquer.add(new TileContainer(enemyTile, numOwnedByMe + (this.random.next() % 5) + (-5 * i / tileByDist.length)))
// const r = this.random.next() % 4
// this.toConquer.add(new TileContainer(enemyTile, r + numOwnedByMe * 1000))
// }
this.toConquer.clear()
// if (this.targetCell != null) {
// let tiles = Array.from(enemyBorder)
// tiles = tiles.slice().sort((a, b) => manhattanDist(a.cell(), this.targetCell) - manhattanDist(b.cell(), this.targetCell))
// for (let i = 0; i < tiles.length; i++) {
// const numOwnedByMe = tiles[i].neighbors()
// .filter(t => t.terrain() == TerrainTypes.Land)
// .filter(t => t.owner() == this._owner)
// .length
// let distModifer = 0
// if (this.targetCell != null) {
// distModifer = i / tiles.length * 2
// }
// this.toConquer.add(new TileContainer(tiles[i], distModifer - numOwnedByMe + this.random.nextInt(0, 2)))
// // this.toConquer.add(new TileContainer(tiles[i], i))
// }
// } else {
for (const tile of this._owner.borderTiles()) {
const newBorder: Set<Tile> = new Set()
let existingBorder: ReadonlySet<Tile> = this.borderTiles
if (existingBorder.size == 0) {
existingBorder = this._owner.borderTiles()
}
for (const tile of existingBorder) {
for (const neighbor of tile.neighbors()) {
if (neighbor.terrain() == TerrainTypes.Water || neighbor.owner() != this.target) {
continue
}
newBorder.add(neighbor)
this.numTilesWithEnemy += 1
const numOwnedByMe = tile.neighbors()
let numOwnedByMe = tile.neighbors()
.filter(t => t.terrain() == TerrainTypes.Land)
.filter(t => t.owner() == this._owner)
.length
@@ -145,11 +97,7 @@ export class AttackExecution implements Execution {
this.toConquer.add(new TileContainer(neighbor, dist + -numOwnedByMe + (tile.cell().x * tile.cell().y) % 2))
}
}
// }
// console.profileEnd('calc_to_conquer')
this.borderTiles = newBorder
}
owner(): MutablePlayer {