2017-12-30 08:36:36 +01:00
|
|
|
package net.citizensnpcs.npc.ai;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
2018-03-15 09:46:57 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.ai.AbstractPathStrategy;
|
|
|
|
import net.citizensnpcs.api.ai.NavigatorParameters;
|
|
|
|
import net.citizensnpcs.api.ai.TargetType;
|
|
|
|
import net.citizensnpcs.api.ai.event.CancelReason;
|
|
|
|
import net.citizensnpcs.api.astar.AStarMachine;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.Path;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.VectorGoal;
|
|
|
|
import net.citizensnpcs.api.astar.pathfinder.VectorNode;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.util.NMS;
|
|
|
|
|
|
|
|
public class AStarNavigationStrategy extends AbstractPathStrategy {
|
|
|
|
private final Location destination;
|
|
|
|
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) {
|
|
|
|
super(TargetType.LOCATION);
|
|
|
|
List<Vector> list = Lists.newArrayList(path);
|
|
|
|
this.params = params;
|
|
|
|
this.destination = list.get(list.size() - 1).toLocation(npc.getStoredLocation().getWorld());
|
|
|
|
this.npc = npc;
|
|
|
|
setPlan(new Path(list));
|
|
|
|
}
|
|
|
|
|
|
|
|
public AStarNavigationStrategy(NPC npc, Location dest, NavigatorParameters params) {
|
|
|
|
super(TargetType.LOCATION);
|
|
|
|
this.params = params;
|
|
|
|
this.destination = dest;
|
|
|
|
this.npc = npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterable<Vector> getPath() {
|
|
|
|
return plan == null ? null : plan.getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Location getTargetAsLocation() {
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPlan(Path path) {
|
|
|
|
this.plan = path;
|
|
|
|
this.planned = true;
|
|
|
|
if (plan == null || plan.isComplete()) {
|
|
|
|
setCancelReason(CancelReason.STUCK);
|
|
|
|
} else {
|
|
|
|
vector = plan.getCurrentVector();
|
|
|
|
if (params.debug()) {
|
|
|
|
plan.debug();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void stop() {
|
|
|
|
if (plan != null && params.debug()) {
|
|
|
|
plan.debugEnd();
|
|
|
|
}
|
|
|
|
plan = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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()),
|
2018-03-15 09:46:57 +01:00
|
|
|
Setting.MAXIMUM_ASTAR_ITERATIONS.asInt()));
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
if (getCancelReason() != null || plan == null || plan.isComplete()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Location currLoc = npc.getEntity().getLocation(NPC_LOCATION);
|
2019-04-25 10:55:48 +02:00
|
|
|
Vector destVector = new Vector(vector.getX() + 0.5, vector.getY(), vector.getZ() + 0.5);
|
2019-05-23 11:32:38 +02:00
|
|
|
/*Block block = currLoc.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
|
|
|
|
if (MinecraftBlockExaminer.isDoor(block.getType())) {
|
2019-04-25 10:57:36 +02:00
|
|
|
Door door = (Door) block.getState().getData();
|
|
|
|
if (door.isOpen()) {
|
|
|
|
BlockFace targetFace = door.getFacing().getOppositeFace();
|
|
|
|
destVector.setX(vector.getX() + targetFace.getModX());
|
|
|
|
destVector.setZ(vector.getZ() + targetFace.getModZ());
|
|
|
|
}
|
2019-05-23 11:32:38 +02:00
|
|
|
}*/
|
2019-10-16 12:04:55 +02:00
|
|
|
double dX = vector.getBlockX() - currLoc.getX();
|
|
|
|
double dZ = vector.getBlockZ() - currLoc.getZ();
|
|
|
|
double dY = vector.getY() - currLoc.getY();
|
|
|
|
double xzDistance = dX * dX + dZ * dZ;
|
|
|
|
if ((dY * dY) < 1 && xzDistance <= params.distanceMargin()) {
|
2017-12-30 08:36:36 +01:00
|
|
|
plan.update(npc);
|
|
|
|
if (plan.isComplete()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
vector = plan.getCurrentVector();
|
|
|
|
}
|
|
|
|
if (params.debug()) {
|
|
|
|
npc.getEntity().getWorld().playEffect(vector.toLocation(npc.getEntity().getWorld()), Effect.ENDER_SIGNAL,
|
|
|
|
0);
|
|
|
|
}
|
2019-10-16 12:04:55 +02:00
|
|
|
double distance = xzDistance + dY * dY;
|
2017-12-30 08:36:36 +01:00
|
|
|
if (distance > 0 && dY > NMS.getStepHeight(npc.getEntity()) && xzDistance <= 2.75) {
|
|
|
|
NMS.setShouldJump(npc.getEntity());
|
|
|
|
}
|
2019-04-25 10:55:48 +02:00
|
|
|
NMS.setDestination(npc.getEntity(), destVector.getX(), destVector.getY(), destVector.getZ(), params.speed());
|
2017-12-30 08:36:36 +01:00
|
|
|
params.run();
|
|
|
|
plan.run(npc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final AStarMachine<VectorNode, Path> ASTAR = AStarMachine.createWithDefaultStorage();
|
|
|
|
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
|
|
|
|
}
|