From 71be40a2cd6e04ab0d25ed7215857f78295c4ff9 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Wed, 13 Nov 2024 16:44:01 -0800 Subject: [PATCH] destroyer spawns from port --- src/core/Util.ts | 7 +++++++ src/core/execution/DestroyerExecution.ts | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/core/Util.ts b/src/core/Util.ts index f4e500ab7..86205edd7 100644 --- a/src/core/Util.ts +++ b/src/core/Util.ts @@ -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) } diff --git a/src/core/execution/DestroyerExecution.ts b/src/core/execution/DestroyerExecution.ts index 2a318d61c..d19f7b204 100644 --- a/src/core/execution/DestroyerExecution.ts +++ b/src/core/execution/DestroyerExecution.ts @@ -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()) {