Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include FOW exposure in pathfinding bounds #4407

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
// Note: zoneRenderer will be null if map is not visible to players.
Area newVbl = new Area();
Area newFowExposedArea = new Area();
final var zoneRenderer = MapTool.getFrame().getCurrentZoneRenderer();
final var zoneRenderer = MapTool.getFrame().getZoneRenderer(zone);
if (zoneRenderer != null) {
final var zoneView = zoneRenderer.getZoneView();

Expand Down Expand Up @@ -229,17 +229,16 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
newVbl = mbl;
}

var view = zoneRenderer.getPlayerView();
newFowExposedArea =
zoneRenderer.getZone().hasFog()
? zoneView.getExposedArea(zoneRenderer.getPlayerView())
: null;
zone.hasFog() && !view.isGMView() ? zoneView.getExposedArea(view) : new Area();
}

boolean blockedMovesHasChanged = false;
if (!newVbl.equals(vbl)) {
blockedMovesHasChanged = true;
vbl = newVbl;
// The move cache may no longer accurately reflect the VBL limitations.
this.vblBlockedMovesByGoal.clear();

vbl = newVbl;
// VBL has changed. Let's update the JTS geometry to match.
if (vbl.isEmpty()) {
this.vblGeometry = null;
Expand All @@ -264,11 +263,12 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
// log.info("vblGeometry bounds: " + vblGeometry.toString());
}
if (!Objects.equals(newFowExposedArea, fowExposedArea)) {
blockedMovesHasChanged = true;
fowExposedArea = newFowExposedArea;
// The move cache may no longer accurately reflect the FOW limitations.
this.fowBlockedMovesByGoal.clear();

fowExposedArea = newFowExposedArea;
// FoW has changed. Let's update the JTS geometry to match.
if (fowExposedArea == null || fowExposedArea.isEmpty()) {
if (fowExposedArea.isEmpty()) {
this.fowExposedAreaGeometry = null;
} else {
try {
Expand All @@ -291,10 +291,6 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
}
}
}
if (blockedMovesHasChanged) {
// The move cache may no longer accurately reflect the VBL limitations.
this.vblBlockedMovesByGoal.clear();
}

// Erase previous debug labels.
EventQueue.invokeLater(
Expand All @@ -314,15 +310,21 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {

Rectangle pathfindingBounds = this.getPathfindingBounds(start, goal);

log.debug("Starting pathfinding");
log.debug("Pathfinding bounds are {}", pathfindingBounds);
while (!openList.isEmpty()) {
log.debug("Open list has {} elements", openList.size());

if (System.currentTimeMillis() > timeOut + estimatedTimeoutNeeded) {
log.info("Timing out after " + estimatedTimeoutNeeded);
break;
}

currentNode = openList.remove();
log.debug("Current node is {}", currentNode.position);
openSet.remove(currentNode);
if (currentNode.position.equals(goal)) {
log.debug("Achieved our goal at {}", goal);
break;
}

Expand All @@ -348,6 +350,7 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {

openList.add(currentNeighbor);
openSet.put(currentNeighbor, currentNeighbor);
log.debug("Added neighbor to open set: {}", currentNeighbor.position);
}

closedSet.add(currentNode);
Expand All @@ -359,11 +362,17 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
recent path request. Clearing the list effectively finishes this thread gracefully.
*/
if (Thread.interrupted()) {
// log.info("Thread interrupted!");
log.debug("Pathfinding cancelled");
openList.clear();
}
}

if (currentNode == null) {
log.debug("Failed pathfinding");
} else {
log.debug("Completed pathfinding at {}", goal);
}

List<CellPoint> returnedCellPointList = new LinkedList<>();
while (currentNode != null) {
returnedCellPointList.add(currentNode.position);
Expand Down Expand Up @@ -416,6 +425,7 @@ protected List<CellPoint> calculatePath(CellPoint start, CellPoint goal) {
protected Rectangle getPathfindingBounds(CellPoint start, CellPoint goal) {
// Bounding box must contain all VBL/MBL ...
Rectangle pathfindingBounds = vbl.getBounds();
pathfindingBounds = pathfindingBounds.union(fowExposedArea.getBounds());
// ... and the footprints of all terrain tokens ...
for (var cellPoint : terrainCells.keySet()) {
pathfindingBounds = pathfindingBounds.union(zone.getGrid().getBounds(cellPoint));
Expand Down Expand Up @@ -452,11 +462,14 @@ protected List<AStarCellPoint> getNeighbors(
node.position.x + neighborArray[0],
node.position.y + neighborArray[1],
node.isOddStepOfOneTwoOneMovement ^ invertEvenOddDiagonals);
log.debug("Checking neighbor: {}", neighbor.position);
if (closedSet.contains(neighbor)) {
log.debug("Rejected neighbor for being in the closed set: {}", neighbor.position);
continue;
}

if (!zone.getGrid().getBounds(node.position).intersects(pathfindingBounds)) {
log.debug("Rejected neighbor for being out of bounds: {}", neighbor.position);
// This position is too far out to possibly be part of the optimal path.
closedSet.add(neighbor);
continue;
Expand All @@ -470,7 +483,7 @@ protected List<AStarCellPoint> getNeighbors(
if (tokenFootprintIntersectsVBL(neighbor.position)) {
// The token would overlap VBL if moved to this position, so it is not a valid position.
closedSet.add(neighbor);
blockNode = true;
log.debug("Rejected neighbor for being inside MBL: {}", neighbor.position);
continue;
}

Expand All @@ -481,9 +494,11 @@ protected List<AStarCellPoint> getNeighbors(
new CellPoint(cellPoint.x + neighborArray[0], cellPoint.y + neighborArray[1]);
if (vblBlocksMovement(cellPoint, cellNeighbor)) {
blockNode = true;
log.debug("MBL blocked movement to neighbor: {}", neighbor.position);
break;
}
if (fowBlocksMovement(cellPoint, cellNeighbor)) {
log.debug("FOW blocked movement to neighbor to {}", neighbor.position);
blockNode = true;
break;
}
Expand Down Expand Up @@ -522,6 +537,7 @@ protected List<AStarCellPoint> getNeighbors(
terrainAdder = terrainAdder / cell_cost;

if (blockNode) {
log.debug("Terrain blocked movement to neighbor to {}", neighbor.position);
continue;
}

Expand Down Expand Up @@ -555,6 +571,7 @@ protected List<AStarCellPoint> getNeighbors(
}
}

log.debug("Accepted neighbor: {}", neighbor.position);
neighbors.add(neighbor);
}

Expand Down Expand Up @@ -638,10 +655,6 @@ private boolean vblBlocksMovement(CellPoint start, CellPoint goal) {
}

private boolean fowBlocksMovement(CellPoint start, CellPoint goal) {
if (MapTool.getPlayer().isEffectiveGM()) {
return false;
}

if (fowExposedAreaGeometry == null) {
return false;
}
Expand Down
Loading