spread out boat pathing over multiple ticks, fix neighborsWrapped bug

This commit is contained in:
Evan
2024-11-15 14:24:49 -08:00
parent 24121ad41b
commit b559204076
4 changed files with 39 additions and 30 deletions
+24 -11
View File
@@ -126,6 +126,7 @@ export class PathFinder {
private dst: Tile = null
private path: Tile[]
private aStar: AStar
private inProgress = false
constructor(private iterations: number) {
@@ -133,20 +134,32 @@ export class PathFinder {
nextTile(curr: Tile, dst: Tile): Tile {
if (this.shouldRecompute(curr, dst)) {
this.curr = curr
this.dst = dst
this.path = null
this.aStar = new AStar(curr, dst)
if (this.aStar.compute(this.iterations)) {
this.path = this.aStar.reconstructPath()
if (this.inProgress) {
if (this.aStar.compute(this.iterations)) {
this.path = this.aStar.reconstructPath()
this.inProgress = false
} else {
return null
}
} else {
return null
}
if (this.path.length > 0) {
this.path.shift()
this.curr = curr
this.dst = dst
this.path = null
this.aStar = new AStar(curr, dst)
if (this.aStar.compute(this.iterations)) {
this.inProgress = false
this.path = this.aStar.reconstructPath()
} else {
this.inProgress = true
return null
}
if (this.path.length > 0) {
this.path.shift()
}
}
} else {
return this.path.shift()
}
return this.path.shift()
}
private shouldRecompute(curr: Tile, dst: Tile) {
+3
View File
@@ -22,4 +22,7 @@ export const devConfig = new class extends DefaultConfig {
return 400
}
boatMaxDistance(): number {
return 2000
}
}
+2 -5
View File
@@ -25,7 +25,7 @@ export class TransportShipExecution implements Execution {
private boat: MutableUnit
private pathFinder: PathFinder = new PathFinder(100_000)
private pathFinder: PathFinder = new PathFinder(10_000)
constructor(
private attackerID: PlayerID,
@@ -118,10 +118,7 @@ export class TransportShipExecution implements Execution {
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
console.warn('boat computing')
return
}
this.boat.move(nextTile)
+10 -14
View File
@@ -1,8 +1,8 @@
import {Tile, Cell, TerrainType, Player, TerraNullius, MutablePlayer} from "./Game";
import {Terrain} from "./TerrainMapLoader";
import {GameImpl} from "./GameImpl";
import {PlayerImpl} from "./PlayerImpl";
import {TerraNulliusImpl} from "./TerraNulliusImpl";
import { Tile, Cell, TerrainType, Player, TerraNullius, MutablePlayer } from "./Game";
import { Terrain } from "./TerrainMapLoader";
import { GameImpl } from "./GameImpl";
import { PlayerImpl } from "./PlayerImpl";
import { TerraNulliusImpl } from "./TerraNulliusImpl";
export class TileImpl implements Tile {
@@ -25,15 +25,11 @@ export class TileImpl implements Tile {
// Check top neighbor
if (y > 0) {
ns.push(this.gs.map[x][y - 1]);
} else {
ns.push(this.gs.map[x][this.gs.height() - 1]);
}
// Check bottom neighbor
if (y < this.gs.height() - 1) {
ns.push(this.gs.map[x][y + 1]);
} else {
ns.push(this.gs.map[x][0]);
}
// Check left neighbor (wrap around)
@@ -94,11 +90,11 @@ export class TileImpl implements Tile {
.length > 0;
}
hasOwner(): boolean {return this._owner != this.gs._terraNullius;}
owner(): MutablePlayer | TerraNullius {return this._owner;}
isBorder(): boolean {return this._isBorder;}
isInterior(): boolean {return this.hasOwner() && !this.isBorder();}
cell(): Cell {return this._cell;}
hasOwner(): boolean { return this._owner != this.gs._terraNullius; }
owner(): MutablePlayer | TerraNullius { return this._owner; }
isBorder(): boolean { return this._isBorder; }
isInterior(): boolean { return this.hasOwner() && !this.isBorder(); }
cell(): Cell { return this._cell; }
neighbors(): Tile[] {
if (this._neighbors == null) {