Search in sources :

Example 11 with MinerLevelManagementModule

use of com.minecolonies.coremod.colony.buildings.modules.MinerLevelManagementModule in project minecolonies by ldtteam.

the class EntityAIStructureMiner method advanceLadder.

private IAIState advanceLadder(final IAIState state) {
    if (!checkIfRequestForItemExistOrCreate(new ItemStack(getLadderBackFillBlock()), COBBLE_REQUEST_BATCHES, 1) || !checkIfRequestForItemExistOrCreate(new ItemStack(Blocks.LADDER), LADDER_REQUEST_BATCHES, 1)) {
        return state;
    }
    if (ladderDamaged()) {
        return MINER_REPAIRING_LADDER;
    }
    final BlockPos vector = getOwnBuilding().getLadderLocation().subtract(getOwnBuilding().getCobbleLocation());
    final int xOffset = SHAFT_RADIUS * vector.getX();
    final int zOffset = SHAFT_RADIUS * vector.getZ();
    @NotNull final BlockPos nextLadder = new BlockPos(getOwnBuilding().getLadderLocation().getX(), getLastLadder(getOwnBuilding().getLadderLocation(), world) - 1, getOwnBuilding().getLadderLocation().getZ());
    @NotNull final BlockPos safeCobble = new BlockPos(getOwnBuilding().getLadderLocation().getX(), getLastLadder(getOwnBuilding().getLadderLocation(), world) - 2, getOwnBuilding().getLadderLocation().getZ());
    // Check for safe floor
    for (int x = -SAFE_CHECK_RANGE; x <= SAFE_CHECK_RANGE; x++) {
        for (int z = -SAFE_CHECK_RANGE; z <= SAFE_CHECK_RANGE; z++) {
            @NotNull final BlockPos curBlock = new BlockPos(safeCobble.getX() + x + xOffset, safeCobble.getY(), safeCobble.getZ() + z + zOffset);
            if (!secureBlock(curBlock, currentStandingPosition)) {
                return state;
            }
        }
    }
    @NotNull final BlockPos safeStand = new BlockPos(getOwnBuilding().getLadderLocation().getX(), getLastLadder(getOwnBuilding().getLadderLocation(), world), getOwnBuilding().getLadderLocation().getZ());
    @NotNull final BlockPos nextCobble = new BlockPos(getOwnBuilding().getCobbleLocation().getX(), getLastLadder(getOwnBuilding().getLadderLocation(), world) - 1, getOwnBuilding().getCobbleLocation().getZ());
    final MinerLevelManagementModule module = getOwnBuilding().getFirstModuleOccurance(MinerLevelManagementModule.class);
    if (module.getStartingLevelShaft() == 0) {
        module.setStartingLevelShaft(nextCobble.getY() - 4);
    }
    if (nextCobble.getY() < module.getStartingLevelShaft()) {
        return MINER_BUILDING_SHAFT;
    }
    if ((!mineBlock(nextCobble, safeStand) && !world.getBlockState(nextCobble).getMaterial().isReplaceable()) || (!mineBlock(nextLadder, safeStand) && !world.getBlockState(nextLadder).getMaterial().isReplaceable())) {
        // waiting until blocks are mined
        return state;
    }
    // Get ladder orientation
    final BlockState metadata = getBlockState(safeStand);
    // set solid block
    setBlockFromInventory(nextCobble, getLadderBackFillBlock());
    // set ladder
    setBlockFromInventory(nextLadder, Blocks.LADDER, metadata);
    this.incrementActionsDoneAndDecSaturation();
    return MINER_CHECK_MINESHAFT;
}
Also used : MinerLevelManagementModule(com.minecolonies.coremod.colony.buildings.modules.MinerLevelManagementModule) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with MinerLevelManagementModule

use of com.minecolonies.coremod.colony.buildings.modules.MinerLevelManagementModule in project minecolonies by ldtteam.

the class EntityCitizenWalkToProxy method getMinerProxy.

/**
 * Returns a proxy point to the goal for the miner especially.
 *
 * @param target         the target.
 * @param distanceToPath the total distance.
 * @param building       the building to walk to.
 * @return a proxy or, if not applicable null.
 */
@NotNull
private BlockPos getMinerProxy(final BlockPos target, final double distanceToPath, @NotNull final BuildingMiner building) {
    final MinerLevelManagementModule module = building.getFirstModuleOccurance(MinerLevelManagementModule.class);
    final Level level = module.getCurrentLevel();
    final BlockPos ladderPos = building.getLadderLocation();
    // If his current working level is null, we have nothing to worry about.
    if (level != null) {
        final BlockPos vector = building.getLadderLocation().subtract(building.getCobbleLocation());
        final int levelDepth = level.getDepth() + 2;
        final int targetY = target.getY();
        final int workerY = citizen.blockPosition().getY();
        // Check if miner is underground in shaft and his target is overground.
        if (workerY <= levelDepth && targetY > levelDepth) {
            if (module.getActiveNode() != null && module.getActiveNode().getParent() != null) {
                com.minecolonies.coremod.entity.ai.citizen.miner.Node currentNode = level.getNode(module.getActiveNode().getParent());
                if (currentNode == null) {
                    module.setActiveNode(null);
                    module.setOldNode(null);
                    return getProxy(target, citizen.blockPosition(), distanceToPath);
                }
                while (currentNode.getParent() != null) {
                    if (currentNode.getStyle() == Node.NodeType.SHAFT) {
                        final Direction facing = BlockPosUtil.getXZFacing(ladderPos, new BlockPos(currentNode.getX(), 0, currentNode.getZ()));
                        final BlockPos ladderHeight = new BlockPos(ladderPos.getX(), targetY + 1, ladderPos.getZ());
                        return new BlockPos(ladderHeight.relative(facing, 7));
                    } else {
                        addToProxyList(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    }
                    currentNode = level.getNode(currentNode.getParent());
                }
            }
            addToProxyList(new BlockPos(ladderPos.getX() + vector.getX() * OTHER_SIDE_OF_SHAFT, level.getDepth(), ladderPos.getZ() + vector.getZ() * OTHER_SIDE_OF_SHAFT));
            return getProxy(target, citizen.blockPosition(), 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.getPosition();
            final BlockPos newProxy;
            // First calculate way to miner building.
            newProxy = getProxy(buildingPos, citizen.blockPosition(), BlockPosUtil.getDistanceSquared(citizen.blockPosition(), buildingPos));
            if (buildingPos.getY() - level.getDepth() > 25) {
                addToProxyList(new BlockPos(ladderPos.getX() + vector.getX(), level.getDepth() + (buildingPos.getY() - level.getDepth()) / 2, ladderPos.getZ() + vector.getZ()));
            }
            // Then add the ladder position as the latest node.
            addToProxyList(new BlockPos(ladderPos.getX() + vector.getX() * OTHER_SIDE_OF_SHAFT, level.getDepth(), ladderPos.getZ() + vector.getZ() * OTHER_SIDE_OF_SHAFT));
            if (module.getActiveNode() != null && module.getActiveNode().getParent() != null) {
                calculateNodes(level, levelDepth, building);
            }
            return newProxy;
        } else // If he is on the same Y level as his target and both underground.
        if (targetY <= levelDepth) {
            double closestNode = Double.MAX_VALUE;
            com.minecolonies.coremod.entity.ai.citizen.miner.Node lastNode = null;
            for (final Map.Entry<Vec2i, com.minecolonies.coremod.entity.ai.citizen.miner.Node> node : level.getNodes().entrySet()) {
                final double distanceToNode = node.getKey().distanceSq(citizen.blockPosition().getX(), citizen.blockPosition().getZ());
                if (distanceToNode < closestNode) {
                    lastNode = node.getValue();
                    closestNode = distanceToNode;
                }
            }
            if (lastNode != null && lastNode.getStyle() == Node.NodeType.SHAFT) {
                final Direction facing = BlockPosUtil.getXZFacing(ladderPos, new BlockPos(lastNode.getX(), 0, lastNode.getZ()));
                final BlockPos ladderHeight = new BlockPos(ladderPos.getX(), targetY + 1, ladderPos.getZ());
                return new BlockPos(ladderHeight.relative(facing, 7));
            }
            if (lastNode != null && lastNode.getParent() != null) {
                com.minecolonies.coremod.entity.ai.citizen.miner.Node currentNode = level.getNode(lastNode.getParent());
                while (new Vec2i(currentNode.getX(), currentNode.getZ()).equals(currentNode.getParent()) && currentNode.getParent() != null) {
                    addToProxyList(new BlockPos(currentNode.getX(), levelDepth, currentNode.getZ()));
                    currentNode = level.getNode(currentNode.getParent());
                }
            }
            if (module.getActiveNode() != null && module.getActiveNode().getParent() != null) {
                calculateNodes(level, levelDepth, building);
            }
            if (!getProxyList().isEmpty()) {
                return getProxyList().get(0);
            }
            return target;
        }
    }
    return getProxy(target, citizen.blockPosition(), distanceToPath);
}
Also used : Vec2i(com.minecolonies.api.util.Vec2i) Node(com.minecolonies.coremod.entity.ai.citizen.miner.Node) Direction(net.minecraft.util.Direction) MinerLevelManagementModule(com.minecolonies.coremod.colony.buildings.modules.MinerLevelManagementModule) Node(com.minecolonies.coremod.entity.ai.citizen.miner.Node) Level(com.minecolonies.coremod.entity.ai.citizen.miner.Level) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MinerLevelManagementModule (com.minecolonies.coremod.colony.buildings.modules.MinerLevelManagementModule)12 BlockPos (net.minecraft.util.math.BlockPos)10 NotNull (org.jetbrains.annotations.NotNull)10 BuildingMiner (com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingMiner)6 Nullable (org.jetbrains.annotations.Nullable)4 Vec2i (com.minecolonies.api.util.Vec2i)2 Level (com.minecolonies.coremod.entity.ai.citizen.miner.Level)2 Node (com.minecolonies.coremod.entity.ai.citizen.miner.Node)2 ItemStack (net.minecraft.item.ItemStack)2 Direction (net.minecraft.util.Direction)2