port spawns on coast

This commit is contained in:
evanpelle
2024-11-13 16:32:40 -08:00
committed by Evan
parent febabf00db
commit 1c8489f099
6 changed files with 73 additions and 24 deletions
+20 -2
View File
@@ -1,4 +1,6 @@
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, PlayerID, UnitType } from "../game/Game";
import { BuildValidator } from "../game/BuildValidator";
import { AllPlayers, BuildItem, BuildItems, Cell, Execution, MutableGame, MutablePlayer, PlayerID, UnitType } from "../game/Game";
import { bfs, dist, manhattanDist } from "../Util";
export class PortExecution implements Execution {
@@ -18,7 +20,23 @@ export class PortExecution implements Execution {
}
tick(ticks: number): void {
this.player.addUnit(UnitType.Port, 0, this.mg.tile(this.cell))
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.player.addUnit(UnitType.Port, 0, spawns[0])
this.active = false
}