use of am2.api.math.AMVector3 in project ArsMagica2 by Mithion.
the class ItemLightningCharm method attractItems.
private void attractItems(World world, Entity ent) {
double distance = 16;
int hDist = 5;
List<Entity> entities = world.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(ent.posX - distance, ent.posY - hDist, ent.posZ - distance, ent.posX + distance, ent.posY + hDist, ent.posZ + distance));
for (Entity e : entities) {
EntityItem item = (EntityItem) e;
if (item.age < 10) {
continue;
}
AMVector3 movement = MathUtilities.GetMovementVectorBetweenPoints(new AMVector3(e), new AMVector3(ent.posX, ent.posY, ent.posZ));
if (!world.isRemote) {
float factor = 0.35f;
if (movement.y > 0)
movement.y = 0;
double x = -(movement.x * factor);
double y = -(movement.y * factor);
double z = -(movement.z * factor);
e.addVelocity(x, y, z);
item.delayBeforeCanPickup = 0;
if (Math.abs(e.motionX) > Math.abs(x * 2)) {
e.motionX = x * (e.motionX / e.motionX);
}
if (Math.abs(e.motionY) > Math.abs(y * 2)) {
e.motionY = y * (e.motionY / e.motionY);
}
if (Math.abs(e.motionZ) > Math.abs(z * 2)) {
e.motionZ = z * (e.motionZ / e.motionZ);
}
}
}
}
use of am2.api.math.AMVector3 in project ArsMagica2 by Mithion.
the class ItemCrystalWrench method handleCMPair.
private void handleCMPair(ItemStack stack, World world, EntityPlayer player, TileEntity te, double hitX, double hitY, double hitZ) {
AMVector3 habLocation = AMVector3.readFromNBT(stack.stackTagCompound.getCompoundTag(HAB_PAIRLOC));
if (world.isRemote) {
spawnLinkParticles(world, hitX, hitY, hitZ);
} else {
TileEntityCrystalMarker tecm = (TileEntityCrystalMarker) te;
tecm.linkToHabitat(habLocation, player);
if (!stack.stackTagCompound.hasKey(KEEP_BINDING))
stack.stackTagCompound.removeTag(HAB_PAIRLOC);
}
}
use of am2.api.math.AMVector3 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.api.math.AMVector3 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.api.math.AMVector3 in project ArsMagica2 by Mithion.
the class PowerNodeRegistry method tryPairNodes.
/**
* Attempts to pair the source and destination nodes by either a direct link or by going through conduits.
*
* @param powerSource The power source
* @param destination The destination point
* @return A localized message to return to the entity attempting to pair the nodes, either of success or why it failed.
*/
public String tryPairNodes(IPowerNode powerSource, IPowerNode destination) {
if (powerSource == destination) {
return StatCollector.translateToLocal("am2.tooltip.nodePairToSelf");
}
//Can the power source provide any of the valid power types for the destination?
ArrayList<PowerTypes> typesProvided = new ArrayList<PowerTypes>();
for (PowerTypes type : destination.getValidPowerTypes()) {
if (powerSource.canProvidePower(type)) {
typesProvided.add(type);
}
}
if (typesProvided.size() == 0) {
//no valid power types can be provided
return StatCollector.translateToLocal("am2.tooltip.noSupportedPowertypes");
}
//set up vectors and calculate distance for pathing purposes
AMVector3 sourceLocation = new AMVector3((TileEntity) powerSource);
AMVector3 destLocation = new AMVector3((TileEntity) destination);
double rawDist = sourceLocation.distanceSqTo(destLocation);
if (rawDist > MAX_POWER_SEARCH_RADIUS) {
return StatCollector.translateToLocal("am2.tooltip.nodesTooFar");
}
//construct a list of all valid power types common between the source and destination
int successes = 0;
for (PowerTypes type : typesProvided) {
LinkedList<AMVector3> powerPath = new LinkedList<AMVector3>();
PowerNodePathfinder pathfinder = new PowerNodePathfinder(((TileEntity) powerSource).getWorldObj(), sourceLocation, destLocation, type);
List<AMVector3> path = pathfinder.compute(sourceLocation);
if (path == null)
continue;
for (AMVector3 vec : path) {
powerPath.addFirst(vec);
}
successes++;
getPowerNodeData(destination).registerNodePath(type, powerPath);
}
//are the nodes too far apart?
if (successes == 0) {
return StatCollector.translateToLocal("am2.tooltip.noPathFound");
}
if (successes == typesProvided.size())
return StatCollector.translateToLocal("am2.tooltip.success");
return StatCollector.translateToLocal("am2.tooltip.partialSuccess");
}
Aggregations