Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package moze_intel.projecte.gameObjs.entity;
- import moze_intel.projecte.utils.PELogger;
- import moze_intel.projecte.utils.WorldHelper;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.enchantment.EnchantmentHelper;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.monster.EntityEnderman;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.entity.projectile.EntityArrow;
- import net.minecraft.network.play.server.S2BPacketChangeGameState;
- import net.minecraft.util.AxisAlignedBB;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.MathHelper;
- import net.minecraft.util.MovingObjectPosition;
- import net.minecraft.util.Vec3;
- import net.minecraft.world.World;
- import javax.vecmath.AxisAngle4d;
- import javax.vecmath.Matrix4d;
- import javax.vecmath.Matrix4f;
- import javax.vecmath.Quat4f;
- import javax.vecmath.Vector3d;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class EntityHomingArrow extends EntityArrow
- {
- private static final int DW_TARGET_ID = 31;
- private static final int NO_TARGET = -1;
- private int newTargetCooldown = 0;
- public EntityHomingArrow(World world)
- {
- super(world);
- }
- public EntityHomingArrow(World world, EntityLivingBase par2, float par3)
- {
- super(world, par2, par3);
- }
- @Override
- public void entityInit()
- {
- super.entityInit();
- setIsCritical(true);
- dataWatcher.addObject(DW_TARGET_ID, NO_TARGET); // Target entity id
- }
- @Override
- public void onUpdate()
- {
- onEntityUpdate();
- boolean inGround = WorldHelper.isArrowInGround(this);
- if (!worldObj.isRemote)
- {
- if (hasTarget() && (!getTarget().isEntityAlive() || inGround))
- {
- dataWatcher.updateObject(DW_TARGET_ID, NO_TARGET);
- PELogger.logInfo("Removing target");
- }
- if (!hasTarget() && !inGround && newTargetCooldown <= 0)
- {
- PELogger.logInfo("Finding new target");
- findNewTarget();
- } else
- {
- newTargetCooldown--;
- }
- }
- if (!hasTarget())
- {
- super.onUpdate();
- return;
- }
- AxisAlignedBB box = this.boundingBox;
- if (!WorldHelper.isArrowInGround(this))
- {
- worldObj.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);
- Entity target = getTarget();
- Vector3d arrowLoc = new Vector3d(posX, posY, posZ);
- Vector3d targetLoc = new Vector3d(target.posX, target.boundingBox.minY + target.height, target.posZ);
- Vector3d lookVec = new Vector3d(targetLoc);
- lookVec.sub(arrowLoc);
- double targetPitch = getPitch(lookVec);
- double targetYaw = getYaw(lookVec);
- double dPitch = rotationPitch - targetPitch;
- double dYaw = rotationYaw - targetYaw;
- float rotationDragFactor = 0.5F;
- rotationPitch = ((float) MathHelper.wrapAngleTo180_double(rotationPitch + dPitch * rotationDragFactor));
- rotationYaw = ((float) MathHelper.wrapAngleTo180_double(rotationYaw + dYaw * rotationDragFactor));
- // double dotProduct = new Vector3d(arrowLoc).dot(targetLoc);
- // double theta = Math.acos(dotProduct / (arrowLoc.length() * targetLoc.length()));
- //
- // Vector3d crossProduct = new Vector3d();
- // crossProduct.cross(arrowLoc, targetLoc);
- // crossProduct.normalize();
- //
- // AxisAngle4d rotation = new AxisAngle4d(crossProduct, theta * 0.5);
- // CoolAlias's solution (doesn't work)
- // System.out.printf("before %f, %f, %f%n", posX, posY, posZ);
- // double arcRate = 0.25D;
- // double dx = posX - lookVec.x;
- // double dy = posY - lookVec.y;
- // double dz = posZ - lookVec.z;
- // posX += dx * arcRate;
- // posY += dy * arcRate;
- // posZ += dz * arcRate;
- // setPosition(posX, posY, posZ);
- // System.out.printf("after %f, %f, %f%n", posX, posY, posZ);
- // Shaderkiller's solution
- // double angleYaw = Math.atan2(lookVec.z, lookVec.x) - Math.PI/2d;
- // double anglePitch = Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));
- //
- // double dYaw = MathHelper.wrapAngleTo180_double(this.rotationYaw - angleYaw);
- // double dPitch = MathHelper.wrapAngleTo180_double(this.rotationPitch - anglePitch);
- //
- // AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
- // AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
- // Quat4f rot = new Quat4f(0, 0, 0, 1);
- // Quat4f yawQuat = new Quat4f();
- // Quat4f pitchQuat = new Quat4f();
- // yawQuat.set(yaw);
- // rot.mul(yawQuat);
- // pitchQuat.set(pitch);
- // rot.mul(pitchQuat);
- // Matrix4d matrix = new Matrix4d();
- // matrix.setIdentity();
- // matrix.setTranslation(arrowLoc);
- // matrix.setRotation(rot);
- //
- // Vector3d apply = new Vector3d(arrowLoc);
- // matrix.get(apply);
- //
- // posX = apply.x;
- // posY = apply.y;
- // posZ = apply.z;
- //
- // setPosition(posX, posY, posZ);
- //
- // this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, 0.25F, 1.0F);
- // this.moveEntity(motionX, motionY, motionZ);
- // old homing code (sucks)
- // double d5 = target.posX - this.posX;
- // double d6 = target.boundingBox.minY + target.height - this.posY;
- // double d7 = target.posZ - this.posZ;
- //
- // this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
- // super.onUpdate();
- }
- }
- private void findNewTarget()
- {
- List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
- Collections.sort(candidates, new Comparator<EntityLiving>() {
- @Override
- public int compare(EntityLiving o1, EntityLiving o2) {
- double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
- if (dist == 0.0)
- {
- return 0;
- } else
- {
- return dist > 0.0 ? 1 : -1;
- }
- }
- });
- if (!candidates.isEmpty())
- {
- dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
- PELogger.logInfo("Found new target");
- }
- newTargetCooldown = 5;
- }
- private EntityLiving getTarget()
- {
- return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
- }
- private boolean hasTarget()
- {
- return getTarget() != null;
- }
- private void updateHeading(double velocityX, double velocityY, double velocityZ)
- {
- float f3 = MathHelper.sqrt_double(velocityX * velocityX + velocityZ * velocityZ);
- this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(velocityX, velocityZ) * 180.0D / Math.PI);
- this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(velocityY, f3) * 180.0D / Math.PI);
- }
- private double getPitch(Vector3d vec)
- {
- return Math.atan2(vec.y, Math.sqrt(vec.x * vec.x + vec.z * vec.z));
- }
- private double getYaw(Vector3d vec)
- {
- return Math.atan2(vec.z, vec.x) - Math.PI / 2.0D;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement