spread out boat pathing over multiple ticks, fix neighborsWrapped bug

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