use of com.minecolonies.api.util.Vec2i 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);
}
use of com.minecolonies.api.util.Vec2i in project minecolonies by Minecolonies.
the class Level method closeNextNode.
/**
* Closes the first Node in the list (Has been returned previously probably).
* Then creates the new nodes connected to it.
*
* @param rotation the rotation of the node.
*/
public void closeNextNode(final int rotation) {
final Node tempNode = openNodes.poll();
final List<Vec2i> nodeCenterList = new ArrayList<>(3);
switch(tempNode.getStyle()) {
case TUNNEL:
nodeCenterList.add(getNextNodePositionFromNodeWithRotation(tempNode, rotation, 0));
break;
case BEND:
nodeCenterList.add(getNextNodePositionFromNodeWithRotation(tempNode, rotation, ROTATE_THREE_TIMES));
break;
case CROSSROAD:
nodeCenterList.add(getNextNodePositionFromNodeWithRotation(tempNode, rotation, 0));
nodeCenterList.add(getNextNodePositionFromNodeWithRotation(tempNode, rotation, ROTATE_ONCE));
nodeCenterList.add(getNextNodePositionFromNodeWithRotation(tempNode, rotation, ROTATE_THREE_TIMES));
break;
default:
return;
}
for (final Vec2i pos : nodeCenterList) {
if (nodes.containsKey(pos)) {
continue;
}
final Node tempNodeToAdd = new Node(pos.getX(), pos.getZ(), new Vec2i(tempNode.getX(), tempNode.getZ()));
tempNodeToAdd.setStyle(getRandomNodeType());
nodes.put(pos, tempNodeToAdd);
openNodes.add(tempNodeToAdd);
}
nodes.get(new Vec2i(tempNode.getX(), tempNode.getZ())).setStatus(Node.NodeStatus.COMPLETED);
}
use of com.minecolonies.api.util.Vec2i in project minecolonies by Minecolonies.
the class EntityAIStructureMiner method getNodeMiningPosition.
/**
* Create a save mining position for the miner.
*
* @param blockToMine block which should be mined or placed.
* @return the save position.
*/
private BlockPos getNodeMiningPosition(final BlockPos blockToMine) {
final BuildingMiner buildingMiner = getOwnBuilding();
if (buildingMiner.getCurrentLevel() == null || buildingMiner.getCurrentLevel().getRandomNode() == null) {
return blockToMine;
}
final Vec2i parentPos = buildingMiner.getCurrentLevel().getRandomNode().getParent();
if (parentPos != null && buildingMiner.getCurrentLevel().getNode(parentPos) != null && buildingMiner.getCurrentLevel().getNode(parentPos).getStyle() == Node.NodeType.SHAFT) {
final BlockPos ladderPos = buildingMiner.getLadderLocation();
return new BlockPos(ladderPos.getX() + buildingMiner.getVectorX() * OTHER_SIDE_OF_SHAFT, buildingMiner.getCurrentLevel().getDepth(), ladderPos.getZ() + buildingMiner.getVectorZ() * OTHER_SIDE_OF_SHAFT);
}
final Vec2i pos = buildingMiner.getCurrentLevel().getRandomNode().getParent();
return new BlockPos(pos.getX(), buildingMiner.getCurrentLevel().getDepth(), pos.getZ());
}
use of com.minecolonies.api.util.Vec2i in project minecolonies by Minecolonies.
the class Node method createFromNBT.
/**
* Creates a node from the NBT Tag.
* Returns the created node
*
* @param compound Compound to read from
* @return Node created from compound
*/
@NotNull
public static Node createFromNBT(@NotNull final NBTTagCompound compound) {
// for backwards compatibility check if the types are doubles
final boolean hasDoubles = compound.hasKey(TAG_X, Constants.NBT.TAG_DOUBLE);
int x;
int z;
if (hasDoubles) {
x = MathHelper.floor(compound.getDouble(TAG_X));
z = MathHelper.floor(compound.getDouble(TAG_Z));
} else {
x = compound.getInteger(TAG_X);
z = compound.getInteger(TAG_Z);
}
final NodeType style = NodeType.valueOf(compound.getString(TAG_STYLE));
final NodeStatus status = NodeStatus.valueOf(compound.getString(TAG_STATUS));
Vec2i parent = null;
if (compound.hasKey(TAG_PARENTX)) {
if (hasDoubles) {
parent = new Vec2i(MathHelper.floor(compound.getDouble(TAG_PARENTX)), MathHelper.floor(compound.getDouble(TAG_PARENTZ)));
} else {
parent = new Vec2i(compound.getInteger(TAG_PARENTX), compound.getInteger(TAG_PARENTZ));
}
}
//Set the node status in all directions.
@NotNull final Node node = new Node(x, z, parent);
node.setStyle(style);
node.setStatus(status);
return node;
}
Aggregations