Search in sources :

Example 1 with Node

use of com.minecolonies.coremod.entity.ai.citizen.miner.Node in project minecolonies by Minecolonies.

the class WalkToProxy method getMinerProxy.

/**
     * Returns a proxy point to the goal for the miner especially.
     *
     * @param target         the target.
     * @param distanceToPath the total distance.
     * @return a proxy or, if not applicable null.
     */
@NotNull
private BlockPos getMinerProxy(final BlockPos target, final double distanceToPath, @NotNull final BuildingMiner building) {
    final Level level = building.getCurrentLevel();
    final BlockPos ladderPos = building.getLadderLocation();
    //If his current working level is null, we have nothing to worry about.
    if (level != null) {
        final int levelDepth = level.getDepth() + 2;
        final int targetY = target.getY();
        final int workerY = worker.getPosition().getY();
        //Check if miner is underground in shaft and his target is overground.
        if (workerY <= levelDepth && targetY > levelDepth) {
            if (level.getRandomNode() != null && level.getRandomNode().getParent() != null) {
                Node currentNode = level.getNode(level.getRandomNode().getParent());
                while (nodeDoesntEqualParent(currentNode)) {
                    proxyList.add(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    currentNode = level.getNode(currentNode.getParent());
                }
            }
            proxyList.add(new BlockPos(ladderPos.getX() + building.getVectorX() * OTHER_SIDE_OF_SHAFT, level.getDepth(), ladderPos.getZ() + building.getVectorZ() * OTHER_SIDE_OF_SHAFT));
            return getProxy(target, worker.getPosition(), distanceToPath);
        //If he already is at ladder location, the closest node automatically will be his hut block.
        } else //Check if target is underground in shaft and miner is over it.
        if (targetY <= levelDepth && workerY > levelDepth) {
            final BlockPos buildingPos = building.getLocation();
            final BlockPos newProxy;
            //First calculate way to miner building.
            newProxy = getProxy(buildingPos, worker.getPosition(), BlockPosUtil.getDistanceSquared(worker.getPosition(), buildingPos));
            //Then add the ladder position as the latest node.
            proxyList.add(new BlockPos(ladderPos.getX() + building.getVectorX() * OTHER_SIDE_OF_SHAFT, level.getDepth(), ladderPos.getZ() + building.getVectorZ() * OTHER_SIDE_OF_SHAFT));
            if (level.getRandomNode() != null && level.getRandomNode().getParent() != null) {
                final List<BlockPos> nodesToTarget = new ArrayList<>();
                Node currentNode = level.getNode(level.getRandomNode().getParent());
                while (nodeDoesntEqualParent(currentNode)) {
                    nodesToTarget.add(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    currentNode = level.getNode(currentNode.getParent());
                }
                for (int i = nodesToTarget.size() - 1; i >= 0; i--) {
                    proxyList.add(nodesToTarget.get(i));
                }
            }
            return newProxy;
        } else //If he is on the same Y level as his target and both underground.
        if (targetY <= levelDepth) {
            long closestNode = Long.MAX_VALUE;
            Node lastNode = null;
            for (final Map.Entry<Vec2i, Node> node : level.getNodes().entrySet()) {
                final long distanceToNode = node.getKey().distanceSq(worker.getPosition().getX(), worker.getPosition().getZ());
                if (distanceToNode < closestNode) {
                    lastNode = node.getValue();
                    closestNode = distanceToNode;
                }
            }
            if (lastNode != null && lastNode.getParent() != null) {
                Node currentNode = level.getNode(lastNode.getParent());
                while (nodeDoesntEqualParent(currentNode)) {
                    proxyList.add(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    currentNode = level.getNode(currentNode.getParent());
                }
            }
            if (level.getRandomNode().getParent() != null) {
                final List<BlockPos> nodesToTarget = new ArrayList<>();
                Node currentNode = level.getNode(level.getRandomNode().getParent());
                while (nodeDoesntEqualParent(currentNode)) {
                    nodesToTarget.add(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    currentNode = level.getNode(currentNode.getParent());
                }
                for (int i = nodesToTarget.size() - 1; i >= 0; i--) {
                    proxyList.add(nodesToTarget.get(i));
                }
            }
            if (!proxyList.isEmpty()) {
                return proxyList.get(0);
            }
            return target;
        }
    }
    return getProxy(target, worker.getPosition(), distanceToPath);
}
Also used : Vec2i(com.minecolonies.api.util.Vec2i) Node(com.minecolonies.coremod.entity.ai.citizen.miner.Node) ArrayList(java.util.ArrayList) Level(com.minecolonies.coremod.entity.ai.citizen.miner.Level) BlockPos(net.minecraft.util.math.BlockPos) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Vec2i (com.minecolonies.api.util.Vec2i)1 Level (com.minecolonies.coremod.entity.ai.citizen.miner.Level)1 Node (com.minecolonies.coremod.entity.ai.citizen.miner.Node)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 BlockPos (net.minecraft.util.math.BlockPos)1 NotNull (org.jetbrains.annotations.NotNull)1