From ddc7ddb8b8b71d4ba45438a1ccee3f2b93b1b5eb Mon Sep 17 00:00:00 2001 From: Evan Date: Fri, 15 Nov 2024 19:49:18 -0800 Subject: [PATCH] only trade with allies --- TODO.txt | 6 +++--- src/core/Util.ts | 7 ++++++- src/core/execution/DestroyerExecution.ts | 7 +++---- src/core/execution/PortExecution.ts | 17 +++++++++++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/TODO.txt b/TODO.txt index ba92f527f..d41f42b41 100644 --- a/TODO.txt +++ b/TODO.txt @@ -179,11 +179,11 @@ * add destroyer DONE 11/12/2024 * add ports DONE 11/14/2024 * destroyer spawn from port DONE 11/14/2024 +* create trade routes DONE 11/15/2024 +* add trade ship DONE 11/15/2024 +* trade ship gives gold when completes route DONE 11/15/2024 * add missile silo * nuke spawns from missile silo -* create trade routes -* add trade ship -* trade ship gives gold when completes route * destroyer can capture trade ships * add battleship * NPC has relations diff --git a/src/core/Util.ts b/src/core/Util.ts index 86205edd7..1b8c4947d 100644 --- a/src/core/Util.ts +++ b/src/core/Util.ts @@ -3,7 +3,7 @@ import twemoji from 'twemoji'; import DOMPurify from 'dompurify'; -import { Cell, Game, Player, TerraNullius, Tile } from "./game/Game"; +import { Cell, Game, Player, TerraNullius, Tile, Unit } from "./game/Game"; import { number } from 'zod'; export function manhattanDist(c1: Cell, c2: Cell): number { @@ -45,6 +45,11 @@ export function distSort(target: Tile): (a: Tile, b: Tile) => number { } } +export function distSortUnit(target: Unit): (a: Unit, b: Unit) => number { + return (a: Unit, b: Unit) => { + return manhattanDist(a.tile().cell(), target.tile().cell()) - manhattanDist(b.tile().cell(), target.tile().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 7a0d0f674..99e508201 100644 --- a/src/core/execution/DestroyerExecution.ts +++ b/src/core/execution/DestroyerExecution.ts @@ -1,7 +1,7 @@ import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game"; import { AStar, PathFinder } from "../PathFinding"; import { PseudoRandom } from "../PseudoRandom"; -import { distSort, manhattanDist } from "../Util"; +import { distSort, distSortUnit, manhattanDist } from "../Util"; export class DestroyerExecution implements Execution { private random: PseudoRandom @@ -56,7 +56,7 @@ export class DestroyerExecution implements Execution { if (this.target == null) { const ships = this.mg.units(UnitType.TransportShip) .filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()) < 100) - // .filter(u => u.owner() != this.destroyer.owner()) + .filter(u => u.owner() != this.destroyer.owner()) .filter(u => u != this.destroyer) .filter(u => !u.owner().isAlliedWith(this.destroyer.owner())) if (ships.length == 0) { @@ -72,8 +72,7 @@ export class DestroyerExecution implements Execution { } return } - // TODO: sort by distance - this.target = ships[0] + this.target = ships.sort(distSortUnit(this.destroyer))[0] } if (manhattanDist(this.destroyer.tile().cell(), this.target.tile().cell()) < 5) { this.target.delete() diff --git a/src/core/execution/PortExecution.ts b/src/core/execution/PortExecution.ts index 107e80715..8ca92aa31 100644 --- a/src/core/execution/PortExecution.ts +++ b/src/core/execution/PortExecution.ts @@ -57,12 +57,12 @@ export class PortExecution implements Execution { this.port.setOwner(this.port.tile().owner() as Player) } - const ports = this.mg.units(UnitType.Port) + const allPorts = this.mg.units(UnitType.Port) .filter(u => u.owner() != this.player) - if (ports.length == 0) { + if (allPorts.length == 0) { return } - for (const port of ports) { + for (const port of allPorts) { if (this.computingPaths.has(port)) { const aStar = this.computingPaths.get(port) if (aStar.compute(10_000)) { @@ -76,8 +76,17 @@ export class PortExecution implements Execution { continue } } + for (const port of this.portPaths.keys()) { + if (!port.isActive()) { + this.portPaths.delete(port) + } + } + if (this.random.chance(50)) { - const port = this.random.randElement(Array.from(this.portPaths.keys())) + const port = this.random.randElement( + Array.from(this.portPaths.keys()) + .filter(p => this.port.owner().isAlliedWith(p.owner())) + ) const path = this.portPaths.get(port) this.mg.addExecution(new TradeShipExecution(this._owner, this.port, port, path)) }