use of com.minecolonies.coremod.entity.ai.citizen.miner.Level 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.coremod.entity.ai.citizen.miner.Level in project minecolonies by Minecolonies.
the class BuildingMiner method readFromNBT.
/**
* Reads the information from NBT from permanent storage.
*
* @param compound the compound key.
*/
@Override
public void readFromNBT(@NotNull final NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey(TAG_FENCE_BLOCK)) {
fenceBlock = Block.getBlockFromName(compound.getString(TAG_FENCE_BLOCK));
}
if (compound.hasKey(TAG_SHAFT_BLOCK)) {
shaftBlock = Block.getBlockFromName(compound.getString(TAG_SHAFT_BLOCK));
}
startingLevelShaft = compound.getInteger(TAG_STARTING_LEVEL);
clearedShaft = compound.getBoolean(TAG_CLEARED);
vectorX = compound.getInteger(TAG_VECTORX);
vectorZ = compound.getInteger(TAG_VECTORZ);
active = compound.getInteger(TAG_ACTIVE);
currentLevel = compound.getInteger(TAG_CURRENT_LEVEL);
ladderLocation = BlockPosUtil.readFromNBT(compound, TAG_LLOCATION);
foundLadder = compound.getBoolean(TAG_LADDER);
shaftStart = BlockPosUtil.readFromNBT(compound, TAG_SLOCATION);
cobbleLocation = BlockPosUtil.readFromNBT(compound, TAG_CLOCATION);
startingLevelNode = compound.getInteger(TAG_SN);
final NBTTagList levelTagList = compound.getTagList(TAG_LEVELS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < levelTagList.tagCount(); i++) {
this.levels.add(new Level(levelTagList.getCompoundTagAt(i)));
}
}
use of com.minecolonies.coremod.entity.ai.citizen.miner.Level in project minecolonies by Minecolonies.
the class BuildingMiner method serializeToView.
/**
* Method to serialize data to send it to the view.
*
* @param buf the used ByteBuffer.
*/
@Override
public void serializeToView(@NotNull final ByteBuf buf) {
super.serializeToView(buf);
buf.writeInt(currentLevel);
buf.writeInt(levels.size());
for (@NotNull final Level level : levels) {
buf.writeInt(level.getNumberOfNodes());
}
}
use of com.minecolonies.coremod.entity.ai.citizen.miner.Level in project minecolonies by Minecolonies.
the class BuildingMiner method writeToNBT.
/**
* Writes the information to NBT to store it permanently.
*
* @param compound the compound key.
*/
@Override
public void writeToNBT(@NotNull final NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setString(TAG_FENCE_BLOCK, Block.REGISTRY.getNameForObject(fenceBlock).toString());
compound.setString(TAG_FLOOR_BLOCK, Block.REGISTRY.getNameForObject(shaftBlock).toString());
compound.setInteger(TAG_STARTING_LEVEL, startingLevelShaft);
compound.setBoolean(TAG_CLEARED, clearedShaft);
compound.setInteger(TAG_VECTORX, vectorX);
compound.setInteger(TAG_VECTORZ, vectorZ);
compound.setInteger(TAG_ACTIVE, active);
compound.setInteger(TAG_CURRENT_LEVEL, currentLevel);
compound.setBoolean(TAG_LADDER, foundLadder);
compound.setInteger(TAG_SN, startingLevelNode);
if (shaftStart != null && cobbleLocation != null) {
BlockPosUtil.writeToNBT(compound, TAG_SLOCATION, shaftStart);
BlockPosUtil.writeToNBT(compound, TAG_CLOCATION, cobbleLocation);
}
if (ladderLocation != null) {
BlockPosUtil.writeToNBT(compound, TAG_LLOCATION, ladderLocation);
}
@NotNull final NBTTagList levelTagList = new NBTTagList();
for (@NotNull final Level level : levels) {
@NotNull final NBTTagCompound levelCompound = new NBTTagCompound();
level.writeToNBT(levelCompound);
levelTagList.appendTag(levelCompound);
}
compound.setTag(TAG_LEVELS, levelTagList);
}
Aggregations