Search in sources :

Example 1 with BlockExaminer

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;
}
Also used : VectorGoal(net.citizensnpcs.api.astar.pathfinder.VectorGoal) FlyingBlockExaminer(net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer) BlockExaminer(net.citizensnpcs.api.astar.pathfinder.BlockExaminer) FlyingBlockExaminer(net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer) ChunkBlockSource(net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource) VectorNode(net.citizensnpcs.api.astar.pathfinder.VectorNode) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

BlockExaminer (net.citizensnpcs.api.astar.pathfinder.BlockExaminer)1 ChunkBlockSource (net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource)1 FlyingBlockExaminer (net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer)1 VectorGoal (net.citizensnpcs.api.astar.pathfinder.VectorGoal)1 VectorNode (net.citizensnpcs.api.astar.pathfinder.VectorNode)1 Location (org.bukkit.Location)1 Vector (org.bukkit.util.Vector)1