fixed bug where each intent was duplicated

destroyers find and capture trade ships
ports generate more trade ships
This commit is contained in:
Evan
2024-11-22 20:28:58 -08:00
parent cf39fe38cc
commit a2a6654bf5
9 changed files with 104 additions and 26 deletions
+14 -3
View File
@@ -52,7 +52,7 @@ export class DestroyerExecution implements Execution {
this.target = null
}
if (this.target == null) {
const ships = this.mg.units(UnitType.TransportShip)
const ships = this.mg.units(UnitType.TransportShip, UnitType.Destroyer, UnitType.TradeShip)
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()) < 100)
.filter(u => u.owner() != this.destroyer.owner())
.filter(u => u != this.destroyer)
@@ -82,7 +82,18 @@ export class DestroyerExecution implements Execution {
const result = this.pathfinder.nextTile(this.destroyer.tile(), this.target.tile(), 5)
switch (result.type) {
case PathFindResultType.Completed:
this.target.delete()
switch (this.target.type()) {
case UnitType.TransportShip:
this.target.delete()
break
case UnitType.TradeShip:
this.owner().captureUnit(this.target)
break
case UnitType.Destroyer:
this.target.delete()
this.destroyer.delete()
break
}
this.target = null
return
case PathFindResultType.NextTile:
@@ -98,7 +109,7 @@ export class DestroyerExecution implements Execution {
}
owner(): MutablePlayer {
return null
return this._owner
}
isActive(): boolean {
+1 -1
View File
@@ -86,7 +86,7 @@ export class PortExecution implements Execution {
const portConnections = Array.from(this.portPaths.keys())
if (portConnections.length > 0 && this.random.chance(500 * this.player().units(UnitType.Port).length)) {
if (portConnections.length > 0 && this.random.chance(250 * this.player().units(UnitType.Port).length)) {
const port = this.random.randElement(portConnections)
const path = this.portPaths.get(port)
if (path != null) {
+54 -7
View File
@@ -1,17 +1,19 @@
import { MessageType } from "../../client/graphics/layers/EventsDisplay";
import { renderNumber } from "../../client/graphics/Utils";
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, Tile, Unit, UnitType } from "../game/Game";
import { AStar, PathFinder } from "../PathFinding";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PseudoRandom } from "../PseudoRandom";
import { bfs, dist, manhattanDist } from "../Util";
import { bfs, dist, distSortUnit, manhattanDist } from "../Util";
export class TradeShipExecution implements Execution {
private active = true
private mg: MutableGame
private player: MutablePlayer
private origOwner: MutablePlayer
private tradeShip: MutableUnit
private index = 0
private pathFinder: PathFinder = new PathFinder(5_000, t => t.isOcean(), 10)
private wasCaptured = false
constructor(
private _owner: PlayerID,
@@ -24,23 +26,68 @@ export class TradeShipExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.mg = mg
this.player = mg.player(this._owner)
this.origOwner = mg.player(this._owner)
}
tick(ticks: number): void {
if (this.tradeShip == null) {
const spawn = this.player.canBuild(UnitType.TradeShip, this.srcPort.tile())
const spawn = this.origOwner.canBuild(UnitType.TradeShip, this.srcPort.tile())
if (spawn == false) {
console.warn(`cannot build trade ship`)
this.active = false
return
}
this.tradeShip = this.player.buildUnit(UnitType.TradeShip, 0, spawn)
this.tradeShip = this.origOwner.buildUnit(UnitType.TradeShip, 0, spawn)
}
if (!this.tradeShip.isActive()) {
this.active = false
return
}
if (this.origOwner != this.tradeShip.owner()) {
// Store as vairable in case ship is recaptured by previous owner
this.wasCaptured = true
}
if (this.wasCaptured) {
const ports = this.tradeShip.owner().units(UnitType.Port).sort(distSortUnit(this.tradeShip))
if (ports.length == 0) {
this.tradeShip.delete()
this.active = false
return
}
const dstPort = ports[0]
const result = this.pathFinder.nextTile(this.tradeShip.tile(), dstPort.tile())
switch (result.type) {
case PathFindResultType.Completed:
const gold = this.mg.config().tradeShipGold(this.srcPort, dstPort)
this.tradeShip.owner().addGold(gold)
this.mg.displayMessage(
`Your trade ship captured from ${this.origOwner.displayName()}, giving you ${renderNumber(gold)} gold`,
MessageType.SUCCESS,
this.tradeShip.owner().id()
)
this.tradeShip.delete()
break
case PathFindResultType.Pending:
break
case PathFindResultType.NextTile:
this.tradeShip.move(result.tile)
break
case PathFindResultType.PathNotFound:
console.warn('captured trade ship cannot find route')
this.active = false
break
}
return
}
if (this.index >= this.path.length) {
this.active = false
const dist = manhattanDist(this.srcPort.tile().cell(), this.dstPort.tile().cell())
const gold = dist * 100
const gold = dist * dist
this.srcPort.owner().addGold(gold)
this.dstPort.owner().addGold(gold)
this.mg.displayMessage(`Trade ship from ${this.tradeShip.owner().displayName()} has reached your port, giving you ${renderNumber(gold)} gold`, MessageType.SUCCESS, this.dstPort.owner().id())