Search in sources :

Example 1 with BlockPosition

use of thpmc.engine.api.entity.ai.pathfinding.BlockPosition in project THP-Engine by TheHollowPlanetMC.

the class Navigator method tick.

/**
 * Run navigation.
 * @param locX entity locX
 * @param locY entity locY
 * @param locZ entity locZ
 */
public void tick(double locX, double locY, double locZ) {
    tick++;
    if (tickSyncTask != null) {
        tickSyncTask.run();
        tickSyncTask = null;
    }
    // Update pathfinding
    if (!entity.isOnGround()) {
        return;
    }
    if (tick % pathfindingInterval == 0) {
        if (pathfindingTask == null) {
            updatePathfinding(NumberConversions.floor(locX), NumberConversions.floor(locY), NumberConversions.floor(locZ));
        } else {
            if (pathfindingTask.isDone()) {
                updatePathfinding(NumberConversions.floor(locX), NumberConversions.floor(locY), NumberConversions.floor(locZ));
            }
        }
    }
    // Make the entity walk along the path.
    if (currentPaths == null) {
        return;
    }
    // Get next path point
    if (currentPaths.size() <= currentPathIndex) {
        currentPaths = null;
        currentPathIndex = 0;
        return;
    }
    BlockPosition next = currentPaths.get(currentPathIndex);
    Vector nextPosition = new Vector(next.x + 0.5, 0, next.z + 0.5);
    // if(nextPosition.lengthSquared() > Math.max(jumpHeight, 4.0F)) return;
    Vector velocity = nextPosition.clone().add(new Vector(-locX, 0, -locZ));
    double velocityLengthSquared = velocity.lengthSquared();
    if (highAccuracy) {
        if (velocityLengthSquared == 0.0) {
            currentPathIndex++;
            return;
        }
    } else {
        int blockX = NumberConversions.floor(locX);
        int blockZ = NumberConversions.floor(locZ);
        if (blockX == next.x && blockZ == next.z) {
            currentPathIndex++;
            if (currentPaths.size() <= currentPathIndex) {
                currentPaths = null;
                currentPathIndex = 0;
                return;
            }
            next = currentPaths.get(currentPathIndex);
            nextPosition = new Vector(next.x + 0.5, 0, next.z + 0.5);
            velocity = nextPosition.clone().add(new Vector(-locX, 0, -locZ));
            velocityLengthSquared = velocity.lengthSquared();
        }
    }
    if (velocityLengthSquared > speed * speed) {
        velocity.normalize().multiply(speed);
        this.move(velocity);
    } else {
        this.move(velocity);
        Vector position = entity.getLocation().toVector();
        double velocityLength = Math.sqrt(velocityLengthSquared);
        double nextVelocityLength = speed - velocityLength;
        currentPathIndex++;
        if (currentPaths.size() <= currentPathIndex) {
            currentPaths = null;
            currentPathIndex = 0;
            return;
        }
        next = currentPaths.get(currentPathIndex);
        nextPosition = new Vector(next.x + 0.5, 0, next.z + 0.5);
        // if(nextPosition.lengthSquared() > Math.max(jumpHeight, 4.0F)) return;
        velocity = nextPosition.clone().add(new Vector(-position.getX(), 0, -position.getZ()));
        velocity.normalize().multiply(nextVelocityLength);
        entity.move(velocity);
    }
}
Also used : BlockPosition(thpmc.engine.api.entity.ai.pathfinding.BlockPosition) Vector(org.bukkit.util.Vector)

Example 2 with BlockPosition

use of thpmc.engine.api.entity.ai.pathfinding.BlockPosition in project THP-Engine by TheHollowPlanetMC.

the class Navigator method updatePathfinding.

/**
 * Update pathfinding.
 * @param locX entity locX
 * @param locY entity locY
 * @param locZ entity locZ
 */
public void updatePathfinding(int locX, int locY, int locZ) {
    if (navigationGoal == null)
        return;
    currentPaths = null;
    if (!entity.isOnGround())
        return;
    if (!asyncPathfinding) {
        BlockPosition start = new BlockPosition(locX, locY, locZ);
        List<BlockPosition> paths = new AsyncAStarMachine(entity.getWorld(), start, navigationGoal, descendingHeight, (int) jumpHeight, 50, avoidEntityCollision, entity.getMovementCollideOption()).runPathFinding();
        // merge
        currentPaths = new ArrayList<>();
        BlockPosition previousPosition = null;
        int previousDeltaX = 0;
        int previousDeltaZ = 0;
        for (BlockPosition currentPosition : paths) {
            int currentDeltaX;
            int currentDeltaZ;
            if (previousPosition == null) {
                previousPosition = currentPosition;
                currentPaths.add(currentPosition);
                continue;
            } else {
                currentDeltaX = currentPosition.x - previousPosition.x;
                currentDeltaZ = currentPosition.z - previousPosition.z;
            }
            if (previousDeltaX != currentDeltaX || previousDeltaZ != currentDeltaZ) {
                currentPaths.add(currentPosition);
                previousDeltaX = currentDeltaX;
                previousDeltaZ = currentDeltaZ;
            }
        }
        currentPathIndex = 0;
        return;
    }
    // Start AsyncAStarMachine
    BlockPosition start = new BlockPosition(locX, locY, locZ);
    AsyncAStarMachine asyncAStarMachine = new AsyncAStarMachine(entity.getWorld(), start, navigationGoal, descendingHeight, (int) jumpHeight, 500, avoidEntityCollision, entity.getMovementCollideOption());
    this.pathfindingTask = asyncAStarMachine.runPathfindingAsync();
    // Then finish pathfinding
    this.pathfindingTask.thenAccept(paths -> {
        if (paths.size() == 0) {
            // Failure
            tickSyncTask = () -> {
                currentPaths = null;
                currentPathIndex = 0;
            };
            return;
        }
        Collections.reverse(paths);
        tickSyncTask = () -> {
            currentPaths = paths;
            currentPathIndex = 0;
        };
    });
}
Also used : BlockPosition(thpmc.engine.api.entity.ai.pathfinding.BlockPosition) AsyncAStarMachine(thpmc.engine.api.entity.ai.pathfinding.AsyncAStarMachine)

Example 3 with BlockPosition

use of thpmc.engine.api.entity.ai.pathfinding.BlockPosition in project THP-Engine by TheHollowPlanetMC.

the class EntityFollowGoal method run.

@Override
public void run(GoalSelector goalSelector, Navigator navigator) {
    tick++;
    if (tick % TRACK_INTERVAL != 0) {
        goalSelector.setFinished(true);
        return;
    }
    INMSHandler nmsHandler = THPEngineAPI.getInstance().getNMSHandler();
    EngineWorld world = navigator.getEntity().getWorld();
    if (!world.getName().equals(target.getWorld().getName()))
        return;
    Location location = target.getLocation();
    EngineChunk chunk = world.getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4);
    if (chunk == null)
        return;
    for (int dy = 0; dy < 5; dy++) {
        Location l = location.clone().add(new Vector(0, -dy, 0));
        Object nmsBlockData = world.getNMSBlockData(l.getBlockX(), l.getBlockY(), l.getBlockZ());
        if (nmsBlockData == null)
            continue;
        if (nmsHandler.hasCollision(new EngineBlock(world, chunk, l.getBlockX(), l.getBlockY(), l.getBlockZ(), nmsBlockData), navigator.getEntity().getMovementCollideOption())) {
            // Goal set
            navigator.setNavigationGoal(new BlockPosition(l.getBlockX(), l.getBlockY() + 1, l.getBlockZ()));
            break;
        }
    }
    goalSelector.setFinished(true);
}
Also used : EngineChunk(thpmc.engine.api.world.cache.EngineChunk) BlockPosition(thpmc.engine.api.entity.ai.pathfinding.BlockPosition) EngineBlock(thpmc.engine.api.world.block.EngineBlock) EngineWorld(thpmc.engine.api.world.cache.EngineWorld) INMSHandler(thpmc.engine.api.nms.INMSHandler) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

BlockPosition (thpmc.engine.api.entity.ai.pathfinding.BlockPosition)3 Vector (org.bukkit.util.Vector)2 Location (org.bukkit.Location)1 AsyncAStarMachine (thpmc.engine.api.entity.ai.pathfinding.AsyncAStarMachine)1 INMSHandler (thpmc.engine.api.nms.INMSHandler)1 EngineBlock (thpmc.engine.api.world.block.EngineBlock)1 EngineChunk (thpmc.engine.api.world.cache.EngineChunk)1 EngineWorld (thpmc.engine.api.world.cache.EngineWorld)1