From 2045c26c7df29091cd895fb6bd513746b8303527 Mon Sep 17 00:00:00 2001 From: Ben Spiers Date: Fri, 23 Aug 2013 04:19:44 +0100 Subject: [PATCH] UE update --- .../core/item/ItemElectric.java | 4 +- .../core/vector/Vector3.java | 592 +++++++++++++++--- 2 files changed, 497 insertions(+), 99 deletions(-) diff --git a/common/universalelectricity/core/item/ItemElectric.java b/common/universalelectricity/core/item/ItemElectric.java index 449214945..2d4d2b1ab 100644 --- a/common/universalelectricity/core/item/ItemElectric.java +++ b/common/universalelectricity/core/item/ItemElectric.java @@ -47,7 +47,7 @@ public abstract class ItemElectric extends Item implements IItemElectric color = "\u00a76"; } - list.add(color + ElectricityDisplay.getDisplay(joules, ElectricUnit.JOULES) + "/" + ElectricityDisplay.getDisplay(this.getMaxElectricityStored(itemStack), ElectricUnit.JOULES)); + list.add(color + ElectricityDisplay.getDisplayShort(joules, ElectricUnit.JOULES) + "/" + ElectricityDisplay.getDisplayShort(this.getMaxElectricityStored(itemStack), ElectricUnit.JOULES)); } /** @@ -127,7 +127,7 @@ public abstract class ItemElectric extends Item implements IItemElectric { if (itemStack.getTagCompound() == null) { - return 0; + itemStack.setTagCompound(new NBTTagCompound()); } float electricityStored = itemStack.getTagCompound().getFloat("electricity"); diff --git a/common/universalelectricity/core/vector/Vector3.java b/common/universalelectricity/core/vector/Vector3.java index ff0d7f421..6e63c7311 100644 --- a/common/universalelectricity/core/vector/Vector3.java +++ b/common/universalelectricity/core/vector/Vector3.java @@ -26,11 +26,6 @@ public class Vector3 implements Cloneable public double y; public double z; - public Vector3() - { - this(0, 0, 0); - } - public Vector3(double x, double y, double z) { this.x = x; @@ -38,46 +33,69 @@ public class Vector3 implements Cloneable this.z = z; } + public Vector3() + { + this(0, 0, 0); + } + + public Vector3(Vector3 vector) + { + this(vector.x, vector.y, vector.z); + } + + public Vector3(double amount) + { + this(amount, amount, amount); + } + public Vector3(Entity par1) { - this.x = par1.posX; - this.y = par1.posY; - this.z = par1.posZ; + this(par1.posX, par1.posY, par1.posZ); } public Vector3(TileEntity par1) { - this.x = par1.xCoord; - this.y = par1.yCoord; - this.z = par1.zCoord; + this(par1.xCoord, par1.yCoord, par1.zCoord); } public Vector3(Vec3 par1) { - this.x = par1.xCoord; - this.y = par1.yCoord; - this.z = par1.zCoord; + this(par1.xCoord, par1.yCoord, par1.zCoord); + } public Vector3(MovingObjectPosition par1) { - this.x = par1.blockX; - this.y = par1.blockY; - this.z = par1.blockZ; + this(par1.blockX, par1.blockY, par1.blockZ); } public Vector3(ChunkCoordinates par1) { - this.x = par1.posX; - this.y = par1.posY; - this.z = par1.posZ; + this(par1.posX, par1.posY, par1.posZ); } public Vector3(ForgeDirection direction) { - this.x = direction.offsetX; - this.y = direction.offsetY; - this.z = direction.offsetZ; + this(direction.offsetX, direction.offsetY, direction.offsetZ); + } + + /** + * Loads a Vector3 from an NBT compound. + */ + public Vector3(NBTTagCompound nbt) + { + this(nbt.getDouble("x"), nbt.getDouble("y"), nbt.getDouble("z")); + } + + /** + * Get a Vector3 based on the rotationYaw and rotationPitch. + * + * @param rotationYaw - Degree + * @param rotationPitch- Degree + */ + public Vector3(float rotationYaw, float rotationPitch) + { + this(Math.cos(Math.toRadians(rotationYaw + 90)), Math.sin(Math.toRadians(-rotationPitch)), Math.sin(Math.toRadians(rotationYaw + 90))); } /** @@ -98,13 +116,28 @@ public class Vector3 implements Cloneable return (int) Math.floor(this.z); } + public float floatX() + { + return (float) this.x; + } + + public float floatY() + { + return (float) this.y; + } + + public float floatZ() + { + return (float) this.z; + } + /** * Makes a new copy of this Vector. Prevents variable referencing problems. */ @Override public Vector3 clone() { - return new Vector3(this.x, this.y, this.z); + return new Vector3(this); } /** @@ -143,6 +176,9 @@ public class Vector3 implements Cloneable return this.setBlock(world, id, 0); } + /** + * ---------------------- CONVERSION FUNCTIONS ---------------------------- + */ /** * Converts this Vector3 into a Vector2 by dropping the Y axis. */ @@ -159,6 +195,22 @@ public class Vector3 implements Cloneable return Vec3.createVectorHelper(this.x, this.y, this.z); } + /** + * Converts Vector3 into a ForgeDirection. + */ + public ForgeDirection toForgeDirection() + { + for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) + { + if (this.x == direction.offsetX && this.y == direction.offsetY && this.z == direction.offsetZ) + { + return direction; + } + } + + return ForgeDirection.UNKNOWN; + } + public double getMagnitude() { return Math.sqrt(this.getMagnitudeSquared()); @@ -166,17 +218,18 @@ public class Vector3 implements Cloneable public double getMagnitudeSquared() { - return x * x + y * y + z * z; + return this.x * this.x + this.y * this.y + this.z * this.z; } public Vector3 normalize() { - double d = getMagnitude(); + double d = this.getMagnitude(); if (d != 0) { - multiply(1 / d); + this.scale(1 / d); } + return this; } @@ -185,44 +238,21 @@ public class Vector3 implements Cloneable * * @return The distance */ - public static double distance(Vector3 par1, Vector3 par2) + public static double distance(Vector3 vec1, Vector3 vec2) { - double var2 = par1.x - par2.x; - double var4 = par1.y - par2.y; - double var6 = par1.z - par2.z; - return Math.sqrt(var2 * var2 + var4 * var4 + var6 * var6); + return vec1.distance(vec2); } + @Deprecated public double distanceTo(Vector3 vector3) { - double var2 = vector3.x - this.x; - double var4 = vector3.y - this.y; - double var6 = vector3.z - this.z; - return Math.sqrt(var2 * var2 + var4 * var4 + var6 * var6); + return this.distance(vector3); } - public Vector3 add(Vector3 par1) + public double distance(Vector3 compare) { - this.x += par1.x; - this.y += par1.y; - this.z += par1.z; - return this; - } - - public Vector3 add(double par1) - { - this.x += par1; - this.y += par1; - this.z += par1; - return this; - } - - public Vector3 subtract(Vector3 amount) - { - this.x -= amount.x; - this.y -= amount.y; - this.z -= amount.z; - return this; + Vector3 difference = this.clone().difference(compare); + return difference.getMagnitude(); } /** @@ -230,11 +260,77 @@ public class Vector3 implements Cloneable */ public Vector3 invert() { - this.multiply(-1); + this.scale(-1); return this; } - public Vector3 multiply(double amount) + public Vector3 translate(Vector3 par1) + { + this.x += par1.x; + this.y += par1.y; + this.z += par1.z; + return this; + } + + public Vector3 translate(double par1) + { + this.x += par1; + this.y += par1; + this.z += par1; + return this; + } + + public static Vector3 translate(Vector3 translate, Vector3 par1) + { + translate.x += par1.x; + translate.y += par1.y; + translate.z += par1.z; + return translate; + } + + public static Vector3 translate(Vector3 translate, double par1) + { + translate.x += par1; + translate.y += par1; + translate.z += par1; + return translate; + } + + @Deprecated + public Vector3 add(Vector3 amount) + { + return translate(amount); + } + + @Deprecated + public Vector3 add(double amount) + { + return translate(amount); + } + + @Deprecated + public Vector3 subtract(Vector3 amount) + { + return this.translate(amount.clone().invert()); + } + + @Deprecated + public Vector3 subtract(double amount) + { + return this.translate(-amount); + } + + public Vector3 difference(Vector3 amount) + { + return this.translate(amount.clone().invert()); + } + + public Vector3 difference(double amount) + { + return this.translate(-amount); + } + + public Vector3 scale(double amount) { this.x *= amount; this.y *= amount; @@ -242,34 +338,64 @@ public class Vector3 implements Cloneable return this; } - public Vector3 multiply(Vector3 vec) + public Vector3 scale(Vector3 amount) { - this.x *= vec.x; - this.y *= vec.y; - this.z *= vec.z; + this.x *= amount.x; + this.y *= amount.y; + this.z *= amount.z; return this; } + public static Vector3 scale(Vector3 vec, double amount) + { + return vec.scale(amount); + } + + public static Vector3 scale(Vector3 vec, Vector3 amount) + { + return vec.scale(amount); + } + + @Deprecated + public Vector3 multiply(double amount) + { + return this.scale(amount); + } + + @Deprecated + public Vector3 multiply(Vector3 amount) + { + return this.scale(amount); + } + + /** + * Static versions of a lot of functions + */ + @Deprecated public static Vector3 subtract(Vector3 par1, Vector3 par2) { return new Vector3(par1.x - par2.x, par1.y - par2.y, par1.z - par2.z); } + @Deprecated public static Vector3 add(Vector3 par1, Vector3 par2) { return new Vector3(par1.x + par2.x, par1.y + par2.y, par1.z + par2.z); } + @Deprecated public static Vector3 add(Vector3 par1, double par2) { return new Vector3(par1.x + par2, par1.y + par2, par1.z + par2); } + @Deprecated public static Vector3 multiply(Vector3 vec1, Vector3 vec2) { return new Vector3(vec1.x * vec2.x, vec1.y * vec2.y, vec1.z * vec2.z); } + @Deprecated public static Vector3 multiply(Vector3 vec1, double vec2) { return new Vector3(vec1.x * vec2, vec1.y * vec2, vec1.z * vec2); @@ -331,28 +457,7 @@ public class Vector3 implements Cloneable */ public Vector3 modifyPositionFromSide(ForgeDirection side, double amount) { - switch (side.ordinal()) - { - case 0: - this.y -= amount; - break; - case 1: - this.y += amount; - break; - case 2: - this.z -= amount; - break; - case 3: - this.z += amount; - break; - case 4: - this.x -= amount; - break; - case 5: - this.x += amount; - break; - } - return this; + return this.translate(new Vector3(side).scale(amount)); } public Vector3 modifyPositionFromSide(ForgeDirection side) @@ -361,30 +466,211 @@ public class Vector3 implements Cloneable return this; } + /** + * Cross product functions + * + * @return The cross product between this vector and another. + */ + public Vector3 toCrossProduct(Vector3 compare) + { + double newX = this.y * compare.z - this.z * compare.y; + double newY = this.z * compare.x - this.x * compare.z; + double newZ = this.x * compare.y - this.y * compare.x; + this.x = newX; + this.y = newY; + this.z = newZ; + return this; + } + + public Vector3 crossProduct(Vector3 compare) + { + return this.clone().toCrossProduct(compare); + } + + public Vector3 xCrossProduct() + { + return new Vector3(0.0D, this.z, -this.y); + } + + public Vector3 zCrossProduct() + { + return new Vector3(-this.y, this.x, 0.0D); + } + + public double dotProduct(Vector3 vec2) + { + return this.x * vec2.x + this.y * vec2.y + this.z * vec2.z; + } + + /** + * @return The perpendicular vector. + */ + public Vector3 getPerpendicular() + { + if (this.z == 0.0F) + { + return this.zCrossProduct(); + } + + return this.xCrossProduct(); + } + + /** + * @return True if this Vector3 is zero. + */ + public boolean isZero() + { + return (this.x == 0) && (this.y == 0) && (this.z == 0); + } + + /** + * Rotate by a this vector around an axis. + * + * @return The new Vector3 rotation. + */ + public Vector3 rotate(float angle, Vector3 axis) + { + return translateMatrix(getRotationMatrix(angle, axis), this.clone()); + } + + public double[] getRotationMatrix(float angle) + { + double[] matrix = new double[16]; + Vector3 axis = this.clone().normalize(); + double x = axis.x; + double y = axis.y; + double z = axis.z; + angle *= 0.0174532925D; + float cos = (float) Math.cos(angle); + float ocos = 1.0F - cos; + float sin = (float) Math.sin(angle); + matrix[0] = (x * x * ocos + cos); + matrix[1] = (y * x * ocos + z * sin); + matrix[2] = (x * z * ocos - y * sin); + matrix[4] = (x * y * ocos - z * sin); + matrix[5] = (y * y * ocos + cos); + matrix[6] = (y * z * ocos + x * sin); + matrix[8] = (x * z * ocos + y * sin); + matrix[9] = (y * z * ocos - x * sin); + matrix[10] = (z * z * ocos + cos); + matrix[15] = 1.0F; + return matrix; + } + + public static Vector3 translateMatrix(double[] matrix, Vector3 translation) + { + double x = translation.x * matrix[0] + translation.y * matrix[1] + translation.z * matrix[2] + matrix[3]; + double y = translation.x * matrix[4] + translation.y * matrix[5] + translation.z * matrix[6] + matrix[7]; + double z = translation.x * matrix[8] + translation.y * matrix[9] + translation.z * matrix[10] + matrix[11]; + translation.x = x; + translation.y = y; + translation.z = z; + return translation; + } + + public static double[] getRotationMatrix(float angle, Vector3 axis) + { + return axis.getRotationMatrix(angle); + } + + /** + * Rotates this Vector by a yaw, pitch and roll value. + */ + public void rotate(double yaw, double pitch, double roll) + { + double yawRadians = Math.toRadians(yaw); + double pitchRadians = Math.toRadians(pitch); + double rollRadians = Math.toRadians(roll); + + double x = this.x; + double y = this.y; + double z = this.z; + + this.x = x * Math.cos(yawRadians) * Math.cos(pitchRadians) + z * (Math.cos(yawRadians) * Math.sin(pitchRadians) * Math.sin(rollRadians) - Math.sin(yawRadians) * Math.cos(rollRadians)) + y * (Math.cos(yawRadians) * Math.sin(pitchRadians) * Math.cos(rollRadians) + Math.sin(yawRadians) * Math.sin(rollRadians)); + this.z = x * Math.sin(yawRadians) * Math.cos(pitchRadians) + z * (Math.sin(yawRadians) * Math.sin(pitchRadians) * Math.sin(rollRadians) + Math.cos(yawRadians) * Math.cos(rollRadians)) + y * (Math.sin(yawRadians) * Math.sin(pitchRadians) * Math.cos(rollRadians) - Math.cos(yawRadians) * Math.sin(rollRadians)); + this.y = -x * Math.sin(pitchRadians) + z * Math.cos(pitchRadians) * Math.sin(rollRadians) + y * Math.cos(pitchRadians) * Math.cos(rollRadians); + } + + /** + * Rotates a point by a yaw and pitch around the anchor 0,0 by a specific angle. + */ + public void rotate(double yaw, double pitch) + { + this.rotate(yaw, pitch, 0); + } + + public void rotate(double yaw) + { + double yawRadians = Math.toRadians(yaw); + + double x = this.x; + double z = this.z; + + if (yaw != 0) + { + this.x = x * Math.cos(yawRadians) - z * Math.sin(yawRadians); + this.z = x * Math.sin(yawRadians) + z * Math.cos(yawRadians); + } + } + + /** + * Gets the delta look position based on the rotation yaw and pitch. Minecraft coordinates are + * messed up. Y and Z are flipped. Yaw is displaced by 90 degrees. Pitch is inversed. + * + * @param rotationYaw + * @param rotationPitch + */ + public static Vector3 getDeltaPositionFromRotation(float rotationYaw, float rotationPitch) + { + return new Vector3(rotationYaw, rotationPitch); + } + + /** + * Gets the angle between this vector and another vector. + * + * @return Angle in degrees + */ + public double getAngle(Vector3 vec2) + { + return anglePreNorm(this.clone().normalize(), vec2.clone().normalize()); + } + + public static double getAngle(Vector3 vec1, Vector3 vec2) + { + return vec1.getAngle(vec2); + } + + public double anglePreNorm(Vector3 vec2) + { + return Math.acos(this.dotProduct(vec2)); + } + + public static double anglePreNorm(Vector3 vec1, Vector3 vec2) + { + return Math.acos(vec1.clone().dotProduct(vec2)); + } + /** * Loads a Vector3 from an NBT compound. */ - public static Vector3 readFromNBT(NBTTagCompound nbtCompound) + @Deprecated + public static Vector3 readFromNBT(NBTTagCompound nbt) { - Vector3 tempVector = new Vector3(); - tempVector.x = nbtCompound.getDouble("x"); - tempVector.y = nbtCompound.getDouble("y"); - tempVector.z = nbtCompound.getDouble("z"); - return tempVector; + return new Vector3(nbt); } /** * Saves this Vector3 to disk * * @param prefix - The prefix of this save. Use some unique string. - * @param par1NBTTagCompound - The NBT compound object to save the data in + * @param nbt - The NBT compound object to save the data in */ - public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound) + public NBTTagCompound writeToNBT(NBTTagCompound nbt) { - par1NBTTagCompound.setDouble("x", this.x); - par1NBTTagCompound.setDouble("y", this.y); - par1NBTTagCompound.setDouble("z", this.z); - return par1NBTTagCompound; + nbt.setDouble("x", this.x); + nbt.setDouble("y", this.y); + nbt.setDouble("z", this.z); + return nbt; } public static Vector3 UP() @@ -417,6 +703,117 @@ public class Vector3 implements Cloneable return new Vector3(1, 0, 0); } + /** + * RayTrace Code, retrieved from MachineMuse. + * + * @author MachineMuse + */ + public MovingObjectPosition rayTrace(World world, float rotationYaw, float rotationPitch, boolean collisionFlag, double reachDistance) + { + // Somehow this destroys the playerPosition vector -.- + MovingObjectPosition pickedBlock = this.rayTraceBlocks(world, rotationYaw, rotationPitch, collisionFlag, reachDistance); + MovingObjectPosition pickedEntity = this.rayTraceEntities(world, rotationYaw, rotationPitch, collisionFlag, reachDistance); + + if (pickedBlock == null) + { + return pickedEntity; + } + else if (pickedEntity == null) + { + return pickedBlock; + } + else + { + double dBlock = this.distance(new Vector3(pickedBlock.hitVec)); + double dEntity = this.distance(new Vector3(pickedEntity.hitVec)); + + if (dEntity < dBlock) + { + return pickedEntity; + } + else + { + return pickedBlock; + } + } + } + + public MovingObjectPosition rayTraceBlocks(World world, float rotationYaw, float rotationPitch, boolean collisionFlag, double reachDistance) + { + Vector3 lookVector = getDeltaPositionFromRotation(rotationYaw, rotationPitch); + Vector3 reachPoint = translate(this, Vector3.scale(lookVector, reachDistance)); + return world.rayTraceBlocks_do_do(this.toVec3(), reachPoint.toVec3(), collisionFlag, !collisionFlag); + } + + @Deprecated + public MovingObjectPosition rayTraceEntities(World world, float rotationYaw, float rotationPitch, boolean collisionFlag, double reachDistance) + { + return this.rayTraceEntities(world, rotationYaw, rotationPitch, reachDistance); + } + + public MovingObjectPosition rayTraceEntities(World world, float rotationYaw, float rotationPitch, double reachDistance) + { + return this.rayTraceEntities(world, getDeltaPositionFromRotation(rotationYaw, rotationPitch).scale(reachDistance)); + } + + public MovingObjectPosition rayTraceEntities(World world, Vector3 target) + { + MovingObjectPosition pickedEntity = null; + Vec3 startingPosition = this.toVec3(); + Vec3 look = target.clone().difference(this).normalize().toVec3(); + double reachDistance = this.distance(target); + Vec3 reachPoint = Vec3.createVectorHelper(startingPosition.xCoord + look.xCoord * reachDistance, startingPosition.yCoord + look.yCoord * reachDistance, startingPosition.zCoord + look.zCoord * reachDistance); + + double checkBorder = 1.1 * reachDistance; + AxisAlignedBB boxToScan = AxisAlignedBB.getAABBPool().getAABB(-checkBorder, -checkBorder, -checkBorder, checkBorder, checkBorder, checkBorder).offset(this.x, this.y, this.z); + + @SuppressWarnings("unchecked") + List entitiesHit = world.getEntitiesWithinAABBExcludingEntity(null, boxToScan); + double closestEntity = reachDistance; + + if (entitiesHit == null || entitiesHit.isEmpty()) + { + return null; + } + for (Entity entityHit : entitiesHit) + { + if (entityHit != null && entityHit.canBeCollidedWith() && entityHit.boundingBox != null) + { + float border = entityHit.getCollisionBorderSize(); + AxisAlignedBB aabb = entityHit.boundingBox.expand(border, border, border); + MovingObjectPosition hitMOP = aabb.calculateIntercept(startingPosition, reachPoint); + + if (hitMOP != null) + { + if (aabb.isVecInside(startingPosition)) + { + if (0.0D < closestEntity || closestEntity == 0.0D) + { + pickedEntity = new MovingObjectPosition(entityHit); + if (pickedEntity != null) + { + pickedEntity.hitVec = hitMOP.hitVec; + closestEntity = 0.0D; + } + } + } + else + { + double distance = startingPosition.distanceTo(hitMOP.hitVec); + + if (distance < closestEntity || closestEntity == 0.0D) + { + pickedEntity = new MovingObjectPosition(entityHit); + pickedEntity.hitVec = hitMOP.hitVec; + closestEntity = distance; + } + } + } + } + } + return pickedEntity; + } + @Override public int hashCode() { @@ -440,4 +837,5 @@ public class Vector3 implements Cloneable { return "Vector3 [" + this.x + "," + this.y + "," + this.z + "]"; } + } \ No newline at end of file