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 NPC npc;
private final NavigatorParameters params; private final NavigatorParameters params;
private Path plan; private Path plan;
private boolean planned = false;
private Vector vector; private Vector vector;
public AStarNavigationStrategy(NPC npc, Iterable<Vector> path, NavigatorParameters params) { public AStarNavigationStrategy(NPC npc, Iterable<Vector> path, NavigatorParameters params) {
@ -42,11 +43,6 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
this.params = params; this.params = params;
this.destination = dest; this.destination = dest;
this.npc = npc; 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 @Override
@ -61,6 +57,7 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
public void setPlan(Path path) { public void setPlan(Path path) {
this.plan = path; this.plan = path;
this.planned = true;
if (plan == null || plan.isComplete()) { if (plan == null || plan.isComplete()) {
setCancelReason(CancelReason.STUCK); setCancelReason(CancelReason.STUCK);
} else { } else {
@ -81,6 +78,13 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
@Override @Override
public boolean update() { 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()) { if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true; return true;
} }

View File

@ -28,6 +28,7 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
private final NPC npc; private final NPC npc;
private final NavigatorParameters parameters; private final NavigatorParameters parameters;
private Path plan; private Path plan;
private boolean planned;
private final Location target; private final Location target;
private Vector vector; private Vector vector;
@ -45,21 +46,6 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
this.target = dest; this.target = dest;
this.parameters = params; this.parameters = params;
this.npc = npc; 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 @Override
@ -94,6 +80,22 @@ public class FlyingAStarNavigationStrategy extends AbstractPathStrategy {
@Override @Override
public boolean update() { 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()) { if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true; return true;
} }

View File

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