From 82667b413de500ef7fa54e489582ae8bcf780620 Mon Sep 17 00:00:00 2001 From: Evan Date: Thu, 20 Feb 2025 11:41:25 -0800 Subject: [PATCH] warships prioritize other warships, always recalcs target --- src/core/execution/WarshipExecution.ts | 69 ++++++++++++++------------ 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/core/execution/WarshipExecution.ts b/src/core/execution/WarshipExecution.ts index 09f806d8a..59c29b04f 100644 --- a/src/core/execution/WarshipExecution.ts +++ b/src/core/execution/WarshipExecution.ts @@ -72,38 +72,45 @@ export class WarshipExecution implements Execution { if (this.target != null && !this.target.isActive()) { this.target = null; } - if (this.target == null) { - const ships = this.mg - .units(UnitType.TransportShip, UnitType.Warship, UnitType.TradeShip) - .filter( - (u) => this.mg.manhattanDist(u.tile(), this.warship.tile()) < 130, - ) - .filter((u) => u.owner() != this.warship.owner()) - .filter((u) => u != this.warship) - .filter((u) => !u.owner().isAlliedWith(this.warship.owner())) - .filter((u) => !this.alreadySentShell.has(u)); + const ships = this.mg + .units(UnitType.TransportShip, UnitType.Warship, UnitType.TradeShip) + .filter((u) => this.mg.manhattanDist(u.tile(), this.warship.tile()) < 130) + .filter((u) => u.owner() != this.warship.owner()) + .filter((u) => u != this.warship) + .filter((u) => !u.owner().isAlliedWith(this.warship.owner())) + .filter((u) => !this.alreadySentShell.has(u)); - this.target = ships.sort(distSortUnit(this.mg, this.warship))[0] ?? null; - if (this.target == null || this.target.type() != UnitType.TradeShip) { - // Patrol unless we are hunting down a tradeship - const result = this.pathfinder.nextTile( - this.warship.tile(), - this.patrolTile, - ); - switch (result.type) { - case PathFindResultType.Completed: - this.patrolTile = this.randomTile(); - break; - case PathFindResultType.NextTile: - this.warship.move(result.tile); - break; - case PathFindResultType.Pending: - return; - case PathFindResultType.PathNotFound: - consolex.log(`path not found to patrol tile`); - this.patrolTile = this.randomTile(); - break; - } + this.target = + ships.sort((a, b) => { + // First compare by Warship type + if (a.type() === UnitType.Warship && b.type() !== UnitType.Warship) + return -1; + if (a.type() !== UnitType.Warship && b.type() === UnitType.Warship) + return 1; + + // If both are same type, sort by distance + return distSortUnit(this.mg, this.warship)(a, b); + })[0] ?? null; + + if (this.target == null || this.target.type() != UnitType.TradeShip) { + // Patrol unless we are hunting down a tradeship + const result = this.pathfinder.nextTile( + this.warship.tile(), + this.patrolTile, + ); + switch (result.type) { + case PathFindResultType.Completed: + this.patrolTile = this.randomTile(); + break; + case PathFindResultType.NextTile: + this.warship.move(result.tile); + break; + case PathFindResultType.Pending: + return; + case PathFindResultType.PathNotFound: + consolex.log(`path not found to patrol tile`); + this.patrolTile = this.randomTile(); + break; } } if (