mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 07:50:45 +00:00
implement destroyers
This commit is contained in:
+30
-5
@@ -1,5 +1,6 @@
|
||||
import { PriorityQueue } from "@datastructures-js/priority-queue";
|
||||
import { Tile } from "./game/Game";
|
||||
import { manhattanDist } from "./Util";
|
||||
|
||||
|
||||
export class AStar {
|
||||
@@ -82,20 +83,44 @@ export class PathFinder {
|
||||
}
|
||||
|
||||
nextTile(curr: Tile, dst: Tile): Tile {
|
||||
if (curr != this.curr || dst != this.dst || this.path == null) {
|
||||
if (this.shouldRecompute(curr, dst)) {
|
||||
this.curr = curr
|
||||
this.dst = dst
|
||||
this.path = null
|
||||
this.aStar = new AStar(curr, dst)
|
||||
if (this.aStar.compute(1000)) {
|
||||
if (this.aStar.compute(50000)) {
|
||||
this.path = this.aStar.reconstructPath()
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (this.path.length == 0) {
|
||||
return null
|
||||
if (this.path.length > 0) {
|
||||
this.path.shift()
|
||||
}
|
||||
}
|
||||
return this.path.shift()
|
||||
}
|
||||
|
||||
private shouldRecompute(curr: Tile, dst: Tile) {
|
||||
if (this.path == null || this.curr == null || this.dst == null) {
|
||||
return true
|
||||
}
|
||||
const dist = manhattanDist(curr.cell(), dst.cell())
|
||||
let tolerance = 10
|
||||
if (dist > 50) {
|
||||
tolerance = 10
|
||||
} else if (dist > 25) {
|
||||
tolerance = 5
|
||||
} else if (dist > 10) {
|
||||
tolerance = 3
|
||||
} else {
|
||||
tolerance = 0
|
||||
}
|
||||
if (manhattanDist(this.curr.cell(), curr.cell()) > tolerance) {
|
||||
return true
|
||||
}
|
||||
if (manhattanDist(this.dst.cell(), dst.cell()) > tolerance) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ export class DestroyerExecution implements Execution {
|
||||
private target: MutableUnit = null
|
||||
private pathfinder = new PathFinder()
|
||||
|
||||
private patrolTile: Tile;
|
||||
|
||||
// TODO: put in config
|
||||
private searchRange = 100
|
||||
|
||||
@@ -24,9 +26,11 @@ export class DestroyerExecution implements Execution {
|
||||
init(mg: MutableGame, ticks: number): void {
|
||||
this._owner = mg.player(this.playerID)
|
||||
this.mg = mg
|
||||
this.patrolTile = mg.tile(this.cell)
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
// TODO: remove gold from player
|
||||
if (this.destroyer == null) {
|
||||
this.destroyer = this._owner.addUnit(UnitType.Destroyer, 0, this.mg.tile(this.cell))
|
||||
return
|
||||
@@ -35,24 +39,46 @@ export class DestroyerExecution implements Execution {
|
||||
this.active = false
|
||||
return
|
||||
}
|
||||
if (this.target != null && !this.target.isActive()) {
|
||||
this.target = null
|
||||
}
|
||||
if (this.target == null) {
|
||||
const ships = this.mg.units(UnitType.TransportShip)
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()))
|
||||
.filter(u => u.owner() != this.destroyer.owner())
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()) < 100)
|
||||
// .filter(u => u.owner() != this.destroyer.owner())
|
||||
.filter(u => u != this.destroyer)
|
||||
.filter(u => !u.owner().isAlliedWith(this.destroyer.owner()))
|
||||
if (ships.length == 0) {
|
||||
if (manhattanDist(this.destroyer.tile().cell(), this.cell) > 5) {
|
||||
for (let i = 0; i < 1 + this.mg.ticks() % 2; i++) {
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.patrolTile)
|
||||
if (next == null) {
|
||||
this.target = null
|
||||
return
|
||||
}
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// TODO: sort by distance
|
||||
this.target = ships[0]
|
||||
}
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.target.tile())
|
||||
if (next == null) {
|
||||
if (manhattanDist(this.destroyer.tile().cell(), this.target.tile().cell()) < 5) {
|
||||
this.target.delete()
|
||||
this.target = null
|
||||
return
|
||||
}
|
||||
for (let i = 0; i < 1 + this.mg.ticks() % 2; i++) {
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.target.tile())
|
||||
if (next == null) {
|
||||
this.target = null
|
||||
console.warn(`target not found`)
|
||||
return
|
||||
}
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
|
||||
owner(): MutablePlayer {
|
||||
|
||||
Reference in New Issue
Block a user