can capture and destroy ports

This commit is contained in:
evanpelle
2024-11-14 08:09:24 -08:00
committed by Evan
parent 530f2dc2df
commit d3d0651659
5 changed files with 40 additions and 18 deletions
+24 -15
View File
@@ -1,5 +1,5 @@
import { BuildValidator } from "../game/BuildValidator";
import { AllPlayers, BuildItem, BuildItems, Cell, Execution, MutableGame, MutablePlayer, PlayerID, UnitType } from "../game/Game";
import { AllPlayers, BuildItem, BuildItems, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, UnitType } from "../game/Game";
import { bfs, dist, manhattanDist } from "../Util";
export class PortExecution implements Execution {
@@ -7,6 +7,7 @@ export class PortExecution implements Execution {
private active = true
private mg: MutableGame
private player: MutablePlayer
private port: MutableUnit
constructor(
private _owner: PlayerID,
@@ -20,24 +21,32 @@ export class PortExecution implements Execution {
}
tick(ticks: number): void {
const tile = this.mg.tile(this.cell)
if (!new BuildValidator(this.mg).canBuild(this.player, tile, BuildItems.Port)) {
console.warn(`player ${this.player} cannot build port at ${this.cell}`)
if (this.port == null) {
const tile = this.mg.tile(this.cell)
if (!new BuildValidator(this.mg).canBuild(this.player, tile, BuildItems.Port)) {
console.warn(`player ${this.player} cannot build port at ${this.cell}`)
this.active = false
return
}
const spawns = Array.from(bfs(tile, dist(tile, 20)))
.filter(t => t.isOceanShore() && t.owner() == this.player)
.sort((a, b) => manhattanDist(a.cell(), tile.cell()) - manhattanDist(b.cell(), tile.cell()))
if (spawns.length == 0) {
console.warn(`cannot find spawn for port`)
this.active = false
return
}
this.port = this.player.addUnit(UnitType.Port, 0, spawns[0])
}
if (!this.port.tile().hasOwner()) {
this.port.delete()
this.active = false
return
}
const spawns = Array.from(bfs(tile, dist(tile, 20)))
.filter(t => t.isOceanShore() && t.owner() == this.player)
.sort((a, b) => manhattanDist(a.cell(), tile.cell()) - manhattanDist(b.cell(), tile.cell()))
if (spawns.length == 0) {
console.warn(`cannot find spawn for port`)
this.active = false
return
if (this.port.tile().owner() != this.port.owner()) {
this.port.setOwner(this.port.tile().owner() as Player)
}
this.player.addUnit(UnitType.Port, 0, spawns[0])
this.active = false
}
owner(): MutablePlayer {