Only pathfind on update

This commit is contained in:
fullwall 2016-09-05 19:22:23 +08:00
parent b5c433c594
commit 97feb5390e
3 changed files with 28 additions and 26 deletions

View File

@ -26,6 +26,7 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
private final NPC npc;
private final NavigatorParameters params;
private Path plan;
private boolean planned = false;
private Vector vector;
public AStarNavigationStrategy(NPC npc, Iterable<Vector> path, NavigatorParameters params) {
@ -42,11 +43,6 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
this.params = params;
this.destination = dest;
this.npc = npc;
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(dest, (float) params.pathDistanceMargin());
setPlan(ASTAR.runFully(goal,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()),
50000));
}
@Override
@ -61,6 +57,7 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
public void setPlan(Path path) {
this.plan = path;
this.planned = true;
if (plan == null || plan.isComplete()) {
setCancelReason(CancelReason.STUCK);
} else {
@ -81,6 +78,13 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
@Override
public boolean update() {
if (!planned) {
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(destination, (float) params.pathDistanceMargin());
setPlan(ASTAR.runFully(goal,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()),
50000));
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}

View File

@ -28,6 +28,7 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
private final NPC npc;
private final NavigatorParameters parameters;
private Path plan;
private boolean planned;
private final Location target;
private Vector vector;
@ -45,21 +46,6 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
this.target = dest;
this.parameters = params;
this.npc = npc;
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(dest, (float) params.pathDistanceMargin());
boolean found = false;
for (BlockExaminer examiner : params.examiners()) {
if (examiner instanceof FlyingBlockExaminer) {
found = true;
break;
}
}
if (!found) {
params.examiner(new FlyingBlockExaminer());
}
setPlan(ASTAR.runFully(goal,
new VectorNode(goal, location, new ChunkBlockSource(location, params.range()), params.examiners()),
50000));
}
@Override
@ -94,6 +80,22 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
@Override
public boolean update() {
if (!planned) {
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(target, (float) parameters.pathDistanceMargin());
boolean found = false;
for (BlockExaminer examiner : parameters.examiners()) {
if (examiner instanceof FlyingBlockExaminer) {
found = true;
break;
}
}
if (!found) {
parameters.examiner(new FlyingBlockExaminer());
}
setPlan(ASTAR.runFully(goal, new VectorNode(goal, location,
new ChunkBlockSource(location, parameters.range()), parameters.examiners()), 50000));
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}

View File

@ -24,7 +24,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private final NavigatorParameters parameters;
private final Entity target;
private final TargetNavigator targetNavigator;
private int updateCounter;
private int updateCounter = -1;
public MCTargetStrategy(NPC npc, org.bukkit.entity.Entity target, boolean aggro, NavigatorParameters params) {
this.npc = npc;
@ -115,7 +115,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
if (!aggro && distanceSquared() <= parameters.distanceMargin()) {
stop();
return false;
} else if (updateCounter++ > parameters.updatePathRate()) {
} else if (updateCounter == -1 || updateCounter++ > parameters.updatePathRate()) {
targetNavigator.setPath();
updateCounter = 0;
}
@ -141,10 +141,6 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private int failureTimes = 0;
private PathStrategy strategy;
public AStarTargeter() {
setStrategy();
}
@Override
public Iterable<Vector> getPath() {
return strategy.getPath();