only trade with allies

This commit is contained in:
Evan
2024-11-15 20:43:15 -08:00
parent b1e1d7263c
commit ddc7ddb8b8
4 changed files with 25 additions and 12 deletions
+3 -4
View File
@@ -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()
+13 -4
View File
@@ -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))
}