use of net.citizensnpcs.api.astar.pathfinder.BlockExaminer in project Citizens2 by CitizensDev.
the class FlyingAStarNavigationStrategy method update.
@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()), Setting.MAXIMUM_ASTAR_ITERATIONS.asInt()));
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}
Location current = npc.getEntity().getLocation(NPC_LOCATION);
if (current.toVector().distanceSquared(vector) <= parameters.distanceMargin()) {
plan.update(npc);
if (plan.isComplete()) {
return true;
}
vector = plan.getCurrentVector();
}
if (parameters.debug()) {
npc.getEntity().getWorld().playEffect(vector.toLocation(npc.getEntity().getWorld()), Effect.ENDER_SIGNAL, 0);
}
double d0 = vector.getX() + 0.5D - current.getX();
double d1 = vector.getY() + 0.1D - current.getY();
double d2 = vector.getZ() + 0.5D - current.getZ();
Vector velocity = npc.getEntity().getVelocity();
double motX = velocity.getX(), motY = velocity.getY(), motZ = velocity.getZ();
motX += (Math.signum(d0) * 0.5D - motX) * 0.1;
motY += (Math.signum(d1) - motY) * 0.1;
motZ += (Math.signum(d2) * 0.5D - motZ) * 0.1;
float targetYaw = (float) (Math.atan2(motZ, motX) * 180.0D / Math.PI) - 90.0F;
float normalisedTargetYaw = (targetYaw - current.getYaw()) % 360;
if (normalisedTargetYaw >= 180.0F) {
normalisedTargetYaw -= 360.0F;
}
if (normalisedTargetYaw < -180.0F) {
normalisedTargetYaw += 360.0F;
}
velocity.setX(motX).setY(motY).setZ(motZ).multiply(parameters.speed());
npc.getEntity().setVelocity(velocity);
if (npc.getEntity().getType() != EntityType.ENDER_DRAGON) {
NMS.setVerticalMovement(npc.getEntity(), 0.5);
float newYaw = current.getYaw() + normalisedTargetYaw;
current.setYaw(newYaw);
NMS.setHeadYaw(npc.getEntity(), newYaw);
npc.teleport(current, TeleportCause.PLUGIN);
}
parameters.run();
plan.run(npc);
return false;
}
Aggregations