Search in sources :

Example 1 with VectorNode

use of net.citizensnpcs.api.astar.pathfinder.VectorNode in project Citizens2 by CitizensDev.

the class AStarNavigationStrategy method update.

@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()), Setting.MAXIMUM_ASTAR_ITERATIONS.asInt()));
    }
    if (getCancelReason() != null || plan == null || plan.isComplete()) {
        return true;
    }
    Location currLoc = npc.getEntity().getLocation(NPC_LOCATION);
    if (currLoc.toVector().distanceSquared(vector) <= params.distanceMargin()) {
        plan.update(npc);
        if (plan.isComplete()) {
            return true;
        }
        vector = plan.getCurrentVector();
    }
    double dX = vector.getBlockX() - currLoc.getX();
    double dZ = vector.getBlockZ() - currLoc.getZ();
    double dY = vector.getY() - currLoc.getY();
    double xzDistance = dX * dX + dZ * dZ;
    double distance = xzDistance + dY * dY;
    if (params.debug()) {
        npc.getEntity().getWorld().playEffect(vector.toLocation(npc.getEntity().getWorld()), Effect.ENDER_SIGNAL, 0);
    }
    if (distance > 0 && dY > NMS.getStepHeight(npc.getEntity()) && xzDistance <= 2.75) {
        NMS.setShouldJump(npc.getEntity());
    }
    double destX = vector.getX() + 0.5, destZ = vector.getZ() + 0.5;
    Block block = currLoc.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
    if (MinecraftBlockExaminer.isDoor(block.getType())) {
        Door door = (Door) block.getState().getData();
        if (door.isOpen()) {
            BlockFace targetFace = door.getFacing().getOppositeFace();
            destX = vector.getX() + targetFace.getModX();
            destZ = vector.getZ() + targetFace.getModZ();
        }
    }
    NMS.setDestination(npc.getEntity(), destX, vector.getY(), destZ, params.speed());
    params.run();
    plan.run(npc);
    return false;
}
Also used : VectorGoal(net.citizensnpcs.api.astar.pathfinder.VectorGoal) BlockFace(org.bukkit.block.BlockFace) ChunkBlockSource(net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource) Block(org.bukkit.block.Block) VectorNode(net.citizensnpcs.api.astar.pathfinder.VectorNode) Door(org.bukkit.material.Door) Location(org.bukkit.Location)

Example 2 with VectorNode

use of net.citizensnpcs.api.astar.pathfinder.VectorNode 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

ChunkBlockSource (net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource)2 VectorGoal (net.citizensnpcs.api.astar.pathfinder.VectorGoal)2 VectorNode (net.citizensnpcs.api.astar.pathfinder.VectorNode)2 Location (org.bukkit.Location)2 BlockExaminer (net.citizensnpcs.api.astar.pathfinder.BlockExaminer)1 FlyingBlockExaminer (net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer)1 Block (org.bukkit.block.Block)1 BlockFace (org.bukkit.block.BlockFace)1 Door (org.bukkit.material.Door)1 Vector (org.bukkit.util.Vector)1