destroyer spawns from port

This commit is contained in:
evanpelle
2024-11-13 16:44:01 -08:00
committed by Evan
parent 1c8489f099
commit 71be40a2cd
2 changed files with 15 additions and 2 deletions
+7
View File
@@ -4,6 +4,7 @@ import DOMPurify from 'dompurify';
import { Cell, Game, Player, TerraNullius, Tile } from "./game/Game";
import { number } from 'zod';
export function manhattanDist(c1: Cell, c2: Cell): number {
return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
@@ -38,6 +39,12 @@ export function dist(root: Tile, dist: number): (tile: Tile) => boolean {
return (n: Tile) => manhattanDist(root.cell(), n.cell()) <= dist;
}
export function distSort(target: Tile): (a: Tile, b: Tile) => number {
return (a: Tile, b: Tile) => {
return manhattanDist(a.cell(), target.cell()) - manhattanDist(b.cell(), target.cell());
}
}
export function and(x: (tile: Tile) => boolean, y: (tile: Tile) => boolean): (tile: Tile) => boolean {
return (tile: Tile) => x(tile) && y(tile)
}
+8 -2
View File
@@ -1,6 +1,6 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { AStar, PathFinder } from "../PathFinding";
import { manhattanDist } from "../Util";
import { distSort, manhattanDist } from "../Util";
export class DestroyerExecution implements Execution {
@@ -32,7 +32,13 @@ export class DestroyerExecution implements Execution {
tick(ticks: number): void {
// TODO: remove gold from player
if (this.destroyer == null) {
this.destroyer = this._owner.addUnit(UnitType.Destroyer, 0, this.mg.tile(this.cell))
const spawns = this._owner.units(UnitType.Port).map(u => u.tile()).sort(distSort(this.patrolTile))
if (spawns.length == 0) {
console.warn(`no ports found for destoryer for player ${this._owner}`)
this.active = false
return
}
this.destroyer = this._owner.addUnit(UnitType.Destroyer, 0, spawns[0])
return
}
if (!this.destroyer.isActive()) {