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);
}
Aggregations