Guest User

Untitled

a guest
Dec 19th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 KB | None | 0 0
  1. package moze_intel.projecte.gameObjs.entity;
  2.  
  3. import moze_intel.projecte.utils.PELogger;
  4. import moze_intel.projecte.utils.WorldHelper;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.enchantment.EnchantmentHelper;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.EntityLiving;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.monster.EntityEnderman;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.entity.player.EntityPlayerMP;
  14. import net.minecraft.entity.projectile.EntityArrow;
  15. import net.minecraft.network.play.server.S2BPacketChangeGameState;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.DamageSource;
  18. import net.minecraft.util.MathHelper;
  19. import net.minecraft.util.MovingObjectPosition;
  20. import net.minecraft.util.Vec3;
  21. import net.minecraft.world.World;
  22.  
  23. import javax.vecmath.AxisAngle4d;
  24. import javax.vecmath.Matrix4d;
  25. import javax.vecmath.Matrix4f;
  26. import javax.vecmath.Quat4f;
  27. import javax.vecmath.Vector3d;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import java.util.List;
  31.  
  32. public class EntityHomingArrow extends EntityArrow
  33. {
  34.     private static final int DW_TARGET_ID = 31;
  35.     private static final int NO_TARGET = -1;
  36.  
  37.     private int newTargetCooldown = 0;
  38.  
  39.     public EntityHomingArrow(World world)
  40.     {
  41.         super(world);
  42.     }
  43.  
  44.     public EntityHomingArrow(World world, EntityLivingBase par2, float par3)
  45.     {
  46.         super(world, par2, par3);
  47.     }
  48.  
  49.     @Override
  50.     public void entityInit()
  51.     {
  52.         super.entityInit();
  53.         setIsCritical(true);
  54.         dataWatcher.addObject(DW_TARGET_ID, NO_TARGET); // Target entity id
  55.     }
  56.  
  57.     @Override
  58.     public void onUpdate()
  59.     {
  60.         onEntityUpdate();
  61.         boolean inGround = WorldHelper.isArrowInGround(this);
  62.         if (!worldObj.isRemote)
  63.         {
  64.             if (hasTarget() && (!getTarget().isEntityAlive() || inGround))
  65.             {
  66.                 dataWatcher.updateObject(DW_TARGET_ID, NO_TARGET);
  67.                 PELogger.logInfo("Removing target");
  68.             }
  69.  
  70.             if (!hasTarget() && !inGround && newTargetCooldown <= 0)
  71.             {
  72.                 PELogger.logInfo("Finding new target");
  73.                 findNewTarget();
  74.             } else
  75.             {
  76.                 newTargetCooldown--;
  77.             }
  78.         }
  79.  
  80.         if (!hasTarget())
  81.         {
  82.             super.onUpdate();
  83.             return;
  84.         }
  85.  
  86.         AxisAlignedBB box = this.boundingBox;
  87.        
  88.         if (!WorldHelper.isArrowInGround(this))
  89.         {
  90.             worldObj.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);
  91.             Entity target = getTarget();
  92.  
  93.  
  94.             Vector3d arrowLoc = new Vector3d(posX, posY, posZ);
  95.             Vector3d targetLoc = new Vector3d(target.posX, target.boundingBox.minY + target.height, target.posZ);
  96.             Vector3d lookVec = new Vector3d(targetLoc);
  97.             lookVec.sub(arrowLoc);
  98.  
  99.             double dotProduct = new Vector3d(arrowLoc).dot(targetLoc);
  100.             double theta = arrowLoc.angle(lookVec); //Math.acos(dotProduct / (arrowLoc.length() * targetLoc.length()));
  101.  
  102.             Vector3d crossProduct = new Vector3d();
  103.             crossProduct.cross(arrowLoc, targetLoc);
  104.             crossProduct.normalize();
  105.  
  106.             Matrix4d transform = new Matrix4d();
  107.             transform.set(new AxisAngle4d(crossProduct, theta * 0.75));
  108.  
  109.             Vector3d adjustedLookVec = new Vector3d(lookVec);
  110.             transform.transform(lookVec, adjustedLookVec);
  111.  
  112.             setThrowableHeading(adjustedLookVec.x, adjustedLookVec.y, adjustedLookVec.z, 1F, 0);
  113.             super.onUpdate();
  114. //          double targetPitch = toDegrees(getPitch(lookVec));
  115. //          double targetYaw = toDegrees(getYaw(lookVec));
  116. //          double dPitch = rotationPitch - targetPitch;
  117. //          double dYaw = rotationYaw - targetYaw;
  118. //
  119. //          float rotationDragFactor = 0.5F;
  120. //          rotationPitch = ((float) MathHelper.wrapAngleTo180_double(rotationPitch + dPitch * rotationDragFactor));
  121. //          rotationYaw = ((float) MathHelper.wrapAngleTo180_double(rotationYaw + dYaw * rotationDragFactor));
  122.  
  123.  
  124.  
  125.  
  126.             // CoolAlias's solution (doesn't work)
  127. //          System.out.printf("before %f, %f, %f%n", posX, posY, posZ);
  128. //          double arcRate = 0.25D;
  129. //          double dx = posX - lookVec.x;
  130. //          double dy = posY - lookVec.y;
  131. //          double dz = posZ - lookVec.z;
  132. //          posX += dx * arcRate;
  133. //          posY += dy * arcRate;
  134. //          posZ += dz * arcRate;
  135. //          setPosition(posX, posY, posZ);
  136. //          System.out.printf("after %f, %f, %f%n", posX, posY, posZ);
  137.  
  138. //          Shaderkiller's solution
  139. //          double angleYaw = Math.atan2(lookVec.z, lookVec.x) - Math.PI/2d;
  140. //          double anglePitch = Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));
  141. //
  142. //          double dYaw = MathHelper.wrapAngleTo180_double(this.rotationYaw - angleYaw);
  143. //          double dPitch = MathHelper.wrapAngleTo180_double(this.rotationPitch - anglePitch);
  144. //
  145. //          AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
  146. //          AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
  147. //          Quat4f rot = new Quat4f(0, 0, 0, 1);
  148. //          Quat4f yawQuat = new Quat4f();
  149. //          Quat4f pitchQuat = new Quat4f();
  150. //          yawQuat.set(yaw);
  151. //          rot.mul(yawQuat);
  152. //          pitchQuat.set(pitch);
  153. //          rot.mul(pitchQuat);
  154. //          Matrix4d matrix = new Matrix4d();
  155. //          matrix.setIdentity();
  156. //          matrix.setTranslation(arrowLoc);
  157. //          matrix.setRotation(rot);
  158. //
  159. //          Vector3d apply = new Vector3d(arrowLoc);
  160. //          matrix.get(apply);
  161. //
  162. //          posX = apply.x;
  163. //          posY = apply.y;
  164. //          posZ = apply.z;
  165. //
  166. //          setPosition(posX, posY, posZ);
  167. //
  168. //          this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, 0.25F, 1.0F);
  169. //          this.moveEntity(motionX, motionY, motionZ);
  170.  
  171. //          old homing code (sucks)
  172. //          double d5 = target.posX - this.posX;
  173. //          double d6 = target.boundingBox.minY + target.height - this.posY;
  174. //          double d7 = target.posZ - this.posZ;
  175. //
  176. //          this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
  177. //          super.onUpdate();
  178.         }
  179.     }
  180.  
  181.     private void findNewTarget()
  182.     {
  183.         List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
  184.         Collections.sort(candidates, new Comparator<EntityLiving>() {
  185.             @Override
  186.             public int compare(EntityLiving o1, EntityLiving o2) {
  187.                 double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
  188.                 if (dist == 0.0)
  189.                 {
  190.                     return 0;
  191.                 } else
  192.                 {
  193.                     return dist > 0.0 ? 1 : -1;
  194.                 }
  195.             }
  196.         });
  197.  
  198.         if (!candidates.isEmpty())
  199.         {
  200.             dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
  201.             PELogger.logInfo("Found new target");
  202.         }
  203.  
  204.         newTargetCooldown = 5;
  205.     }
  206.  
  207.     private EntityLiving getTarget()
  208.     {
  209.         return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
  210.     }
  211.  
  212.     private boolean hasTarget()
  213.     {
  214.         return getTarget() != null;
  215.     }
  216.  
  217.     private void updateHeading(double velocityX, double velocityY, double velocityZ)
  218.     {
  219.         float f3 = MathHelper.sqrt_double(velocityX * velocityX + velocityZ * velocityZ);
  220.         this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(velocityX, velocityZ) * 180.0D / Math.PI);
  221.         this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(velocityY, f3) * 180.0D / Math.PI);
  222.     }
  223.  
  224.     private double getPitch(Vector3d vec)
  225.     {
  226.         return Math.atan2(vec.y, Math.sqrt(vec.x * vec.x + vec.z * vec.z));
  227.     }
  228.  
  229.     private double getYaw(Vector3d vec)
  230.     {
  231.         return Math.atan2(vec.z, vec.x) - Math.PI / 2.0D;
  232.     }
  233.  
  234.     private double toDegrees(double radian)
  235.     {
  236.         return radian * 180.0 / Math.PI;
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment