use of am2.power.PowerNodeEntry in project ArsMagica2 by Mithion.
the class PowerNodeRegistry method getAllNearbyNodes.
/**
* Returns all power nodes within POWER_SEARCH_RADIUS
*
* @param location The center point to search from
* @param power The power type that the provider needs to supply or accept to be considered valid
* @return An array of IPowerNodes
*/
public IPowerNode[] getAllNearbyNodes(World world, AMVector3 location, PowerTypes power) {
//get the list of chunks we'll need to search in
ChunkCoordIntPair[] search = getSearchChunks(location);
//build the list of nodes from that chunk
HashMap<AMVector3, PowerNodeEntry> nodesToSearch = new HashMap<AMVector3, PowerNodeEntry>();
for (ChunkCoordIntPair pair : search) {
HashMap<AMVector3, PowerNodeEntry> nodesInChunk = powerNodes.get(pair);
if (nodesInChunk != null) {
//Add only vectors that are less than or equal to POWER_SEARCH_RADIUS_SQ away
for (AMVector3 vector : nodesInChunk.keySet()) {
if (location.distanceSqTo(vector) <= POWER_SEARCH_RADIUS_SQ && !vector.equals(location)) {
nodesToSearch.put(vector, nodesInChunk.get(vector));
}
}
}
}
//spin through and create our list of providers
ArrayList<IPowerNode> nodes = new ArrayList<IPowerNode>();
int deadNodesRemoved = 0;
for (AMVector3 vector : nodesToSearch.keySet()) {
if (!world.checkChunksExist((int) vector.x, (int) vector.y, (int) vector.z, (int) vector.x, (int) vector.y, (int) vector.z)) {
continue;
}
Chunk chunk = world.getChunkFromBlockCoords((int) vector.x, (int) vector.z);
if (!chunk.isChunkLoaded)
continue;
TileEntity te = world.getTileEntity((int) vector.x, (int) vector.y, (int) vector.z);
if (te == null || !(te instanceof IPowerNode)) {
//opportune time to remove dead power nodes
removePowerNode(chunk.getChunkCoordIntPair(), vector);
deadNodesRemoved++;
continue;
}
IPowerNode node = (IPowerNode) te;
nodes.add(node);
}
if (deadNodesRemoved > 0)
LogHelper.trace("Removed %d dead power nodes", deadNodesRemoved);
IPowerNode[] nodeArray = nodes.toArray(new IPowerNode[nodes.size()]);
LogHelper.trace("Located %d nearby power providers", nodeArray.length);
return nodeArray;
}
use of am2.power.PowerNodeEntry in project ArsMagica2 by Mithion.
the class PowerNodeRegistry method SaveChunkToNBT.
public void SaveChunkToNBT(ChunkCoordIntPair chunk, NBTTagCompound compound) {
HashMap<AMVector3, PowerNodeEntry> nodeData = powerNodes.get(chunk);
if (nodeData == null) {
//we're not tracking anything in this chunk
return;
}
NBTTagList powerNodeTagList = new NBTTagList();
for (AMVector3 location : nodeData.keySet()) {
NBTTagCompound nodeCompound = new NBTTagCompound();
nodeCompound.setInteger("xCoord", (int) location.x);
nodeCompound.setInteger("yCoord", (int) location.y);
nodeCompound.setInteger("zCoord", (int) location.z);
PowerNodeEntry pnd = nodeData.get(location);
nodeCompound.setTag("nodeData", pnd.saveToNBT());
powerNodeTagList.appendTag(nodeCompound);
}
LogHelper.trace("Saved %d power node entries", powerNodeTagList.tagCount());
compound.setTag("AM2PowerData", powerNodeTagList);
}
use of am2.power.PowerNodeEntry in project ArsMagica2 by Mithion.
the class PowerNodeRegistry method registerPowerNodeInternal.
PowerNodeEntry registerPowerNodeInternal(IPowerNode node) {
ChunkCoordIntPair chunk = getChunkFromNode(node);
HashMap<AMVector3, PowerNodeEntry> nodeList;
TileEntity te = ((TileEntity) node);
World world = te.getWorldObj();
if (powerNodes.containsKey(chunk)) {
nodeList = powerNodes.get(chunk);
LogHelper.trace("Located Power Node list for chunk %d, %d", chunk.chunkXPos, chunk.chunkZPos);
} else {
LogHelper.trace("Node list not found. Checking cache/files for prior data");
NBTTagCompound compound = PowerNodeCache.instance.getNBTForChunk(world, chunk);
nodeList = new HashMap<AMVector3, PowerNodeEntry>();
if (compound == null || !compound.hasKey("AM2PowerData")) {
powerNodes.put(chunk, nodeList);
LogHelper.trace("Prior node list not found. Created Power Node list for chunk %d, %d", chunk.chunkXPos, chunk.chunkZPos);
} else {
LoadChunkFromNBT(chunk, compound);
nodeList = powerNodes.get(chunk);
//sanity check
if (nodeList == null)
nodeList = new HashMap<AMVector3, PowerNodeEntry>();
LogHelper.trace("Loaded power data for chunk %d, %d", chunk.chunkXPos, chunk.chunkZPos);
}
}
AMVector3 nodeLoc = new AMVector3((TileEntity) node);
//prevent duplicate registrations
if (nodeList.containsKey(nodeLoc))
return nodeList.get(nodeLoc);
PowerNodeEntry pnd = new PowerNodeEntry();
nodeList.put(nodeLoc, pnd);
LogHelper.trace("Successfully registered power node at {%d, %d, %d}", ((TileEntity) node).xCoord, ((TileEntity) node).yCoord, ((TileEntity) node).zCoord);
return pnd;
}
Aggregations