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

Simplified slope inclusion in costs #41

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
30 changes: 23 additions & 7 deletions src/main/java/org/terasology/pathfinding/model/HAStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.terasology.navgraph.WalkableBlock;

import java.util.BitSet;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -53,22 +52,26 @@ public class HAStar {
private BitSet closedList = new BitSet(16 * 1024);
private boolean useContour;
private LineOfSight lineOfSight;
private boolean useSlope;
private final int beta;

public HAStar(LineOfSight lineOfSight, boolean useContour) {
public HAStar(LineOfSight lineOfSight, boolean useContour, boolean useSlope) {
this.useSlope = useSlope;
this.lineOfSight = lineOfSight;
this.useContour = useContour;
this.beta = 1;
openList = new BinaryHeap((a, b) -> {
float fA = nodes.get(a).f;
float fB = nodes.get(b).f;
return -(fA < fB ? -1 : (fA > fB ? 1 : 0));
}, MAX_NODES, MAX_NODES);
if (useContour) {
localAStar = new HAStar(null, false);
localAStar = new HAStar(null, false, false);
}
}

public HAStar(LineOfSight lineOfSight) {
this(lineOfSight, true);
this(lineOfSight, true, false);
}

public void reset() {
Expand Down Expand Up @@ -195,7 +198,8 @@ private void updateNode(int current, Node currentNode, int successor, Node succe
}

private void computeCosts(int current, Node currentNode, int successor, Node successorNode) {
if (lineOfSight != null && currentNode.p != null && lineOfSight.inSight(currentNode.p.block, successorNode.block)) {
if (lineOfSight != null && currentNode.p != null && lineOfSight.inSight(currentNode.p.block,
successorNode.block)) {
float tentativeG = currentNode.p.g + c(currentNode.p.id, successor, true);
if (tentativeG <= successorNode.g) {
successorNode.path = null;
Expand Down Expand Up @@ -227,11 +231,23 @@ protected float c(int from, int to, boolean inSight) {
Vector3i toPos = toNode.block.getBlockPosition();
int diffX = Math.abs(fromPos.x - toPos.x);
int diffZ = Math.abs(fromPos.z - toPos.z);
int diffY = Math.abs(fromPos.y - toPos.y);
if (toNode.block.hasNeighbor(fromNode.block)) {
if (diffX + diffZ == 1) {
return 1;

if (useSlope) {
return 1 * (1 + diffY*beta);
} else {
return 1;
}

} else {
return BitMap.SQRT_2;
if (useSlope) {
return BitMap.SQRT_2 * (1 + diffY*beta);
} else {
return BitMap.SQRT_2;
}

}
}
if (fromNode.block.floor.navGraphChunk.pathCache.hasPath(fromNode.block, toNode.block)) {
Expand Down