transport ship use pathfinding library

This commit is contained in:
evanpelle
2024-11-12 16:51:10 -08:00
committed by Evan
parent c399cb59fb
commit 0b3a92e498
3 changed files with 16 additions and 29 deletions
+2 -2
View File
@@ -78,7 +78,7 @@ export class PathFinder {
private path: Tile[]
private aStar: AStar
constructor() {
constructor(private iterations: number) {
}
@@ -88,7 +88,7 @@ export class PathFinder {
this.dst = dst
this.path = null
this.aStar = new AStar(curr, dst)
if (this.aStar.compute(5000)) {
if (this.aStar.compute(this.iterations)) {
this.path = this.aStar.reconstructPath()
} else {
return null
+1 -1
View File
@@ -10,7 +10,7 @@ export class DestroyerExecution implements Execution {
private mg: MutableGame = null
private target: MutableUnit = null
private pathfinder = new PathFinder()
private pathfinder = new PathFinder(5000)
private patrolTile: Tile;
+13 -26
View File
@@ -2,7 +2,7 @@ import { Unit, Cell, Execution, MutableUnit, MutableGame, MutablePlayer, Player,
import { and, bfs, manhattanDistWrapped, sourceDstOceanShore } from "../Util";
import { AttackExecution } from "./AttackExecution";
import { DisplayMessageEvent, MessageType } from "../../client/graphics/layers/EventsDisplay";
import { AStar } from "../PathFinding";
import { AStar, PathFinder } from "../PathFinding";
export class TransportShipExecution implements Execution {
@@ -22,14 +22,10 @@ export class TransportShipExecution implements Execution {
private src: Tile | null
private dst: Tile | null
private currTileIndex: number = 0
private boat: MutableUnit
private aStarPre: AStar
private aStarComplete: AStar
private finalPath = false
private pathFinder: PathFinder = new PathFinder(100_000)
constructor(
private attackerID: PlayerID,
@@ -82,16 +78,8 @@ export class TransportShipExecution implements Execution {
return
}
this.aStarPre = new AStar(this.src, this.dst)
this.aStarPre.compute(5)
this.path = this.aStarPre.reconstructPath()
if (this.path != null) {
this.boat = this.attacker.addUnit(UnitType.TransportShip, this.troops, this.src)
} else {
console.log('got null path')
this.active = false
}
this.aStarComplete = new AStar(this.path[this.path.length - 1], this.dst)
this.boat = this.attacker.addUnit(UnitType.TransportShip, this.troops, this.src)
}
tick(ticks: number) {
@@ -107,15 +95,8 @@ export class TransportShipExecution implements Execution {
}
this.lastMove = ticks
if (!this.finalPath && this.aStarComplete.compute(30000)) {
this.path.push(...this.aStarComplete.reconstructPath())
this.finalPath = true
}
if (this.currTileIndex >= this.path.length) {
if (!this.finalPath) {
return
}
if (this.boat.tile() == this.dst) {
if (this.dst.owner() == this.attacker) {
this.attacker.addTroops(this.troops)
this.boat.delete()
@@ -135,9 +116,15 @@ export class TransportShipExecution implements Execution {
return
}
const nextTile = this.path[this.currTileIndex]
const nextTile = this.pathFinder.nextTile(this.boat.tile(), this.dst)
if (nextTile == null) {
console.warn('path not found')
this.attacker.addTroops(this.boat.troops())
this.boat.delete()
this.active = false
return
}
this.boat.move(nextTile)
this.currTileIndex++
}
owner(): MutablePlayer {