Advertisement
Guest User

Untitled

a guest
Dec 18th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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.entity.Entity;
  6. import net.minecraft.entity.EntityLiving;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.projectile.EntityArrow;
  9. import net.minecraft.util.AxisAlignedBB;
  10. import net.minecraft.util.MathHelper;
  11. import net.minecraft.world.World;
  12.  
  13. import javax.vecmath.AxisAngle4d;
  14. import javax.vecmath.Matrix4d;
  15. import javax.vecmath.Matrix4f;
  16. import javax.vecmath.Quat4f;
  17. import javax.vecmath.Vector3d;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. import java.util.List;
  21.  
  22. public class EntityHomingArrow extends EntityArrow
  23. {
  24. private static final int DW_TARGET_ID = 31;
  25. private static final int NO_TARGET = -1;
  26.  
  27. private int newTargetCooldown = 0;
  28. private Vector3d homingVec = null;
  29. // The position this arrow should point in for a direct path to the target
  30.  
  31. public EntityHomingArrow(World world)
  32. {
  33. super(world);
  34. }
  35.  
  36. public EntityHomingArrow(World world, EntityLivingBase par2, float par3)
  37. {
  38. super(world, par2, par3);
  39. }
  40.  
  41. @Override
  42. public void entityInit()
  43. {
  44. super.entityInit();
  45. setIsCritical(true);
  46. dataWatcher.addObject(DW_TARGET_ID, NO_TARGET); // Target entity id
  47. }
  48.  
  49. @Override
  50. public void onUpdate()
  51. {
  52. boolean inGround = WorldHelper.isArrowInGround(this);
  53. if (!worldObj.isRemote)
  54. {
  55. if (hasTarget() && (!getTarget().isEntityAlive() || inGround))
  56. {
  57. dataWatcher.updateObject(DW_TARGET_ID, NO_TARGET);
  58. PELogger.logInfo("Removing target");
  59. }
  60.  
  61. if (!hasTarget() && !inGround && newTargetCooldown <= 0)
  62. {
  63. PELogger.logInfo("Finding new target");
  64. findNewTarget();
  65. } else
  66. {
  67. newTargetCooldown--;
  68. }
  69. }
  70.  
  71. if (!hasTarget())
  72. {
  73. super.onUpdate();
  74. return;
  75. }
  76.  
  77. AxisAlignedBB box = this.boundingBox;
  78.  
  79. if (!WorldHelper.isArrowInGround(this))
  80. {
  81. worldObj.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);
  82. Entity target = getTarget();
  83.  
  84.  
  85. Vector3d arrowLoc = new Vector3d(posX, posY, posZ);
  86. Vector3d targetLoc = new Vector3d(target.posX, target.posY, target.posZ);
  87. Vector3d lookVec = new Vector3d(arrowLoc);
  88. lookVec.sub(targetLoc);
  89.  
  90. // CoolAlias's solution (doesn't work)
  91. // System.out.printf("before %f, %f, %f%n", posX, posY, posZ);
  92. // double arcRate = 0.25D;
  93. // double dx = posX - lookVec.x;
  94. // double dy = posY - lookVec.y;
  95. // double dz = posZ - lookVec.z;
  96. // posX += dx * arcRate;
  97. // posY += dy * arcRate;
  98. // posZ += dz * arcRate;
  99. // setPosition(posX, posY, posZ);
  100. // System.out.printf("after %f, %f, %f%n", posX, posY, posZ);
  101.  
  102.  
  103. double angleYaw = Math.atan2(lookVec.z, lookVec.x) - Math.PI/2d;
  104. double anglePitch = Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));
  105.  
  106. double dYaw = MathHelper.wrapAngleTo180_double(this.rotationYaw - angleYaw);
  107. double dPitch = MathHelper.wrapAngleTo180_double(this.rotationPitch - anglePitch);
  108.  
  109. AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
  110. AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
  111. Quat4f rot = new Quat4f(0, 0, 0, 1);
  112. Quat4f yawQuat = new Quat4f();
  113. Quat4f pitchQuat = new Quat4f();
  114. yawQuat.set(yaw);
  115. rot.mul(yawQuat);
  116. pitchQuat.set(pitch);
  117. rot.mul(pitchQuat);
  118. Matrix4d matrix = new Matrix4d();
  119. matrix.setIdentity();
  120. matrix.setRotation(rot);
  121.  
  122. Vector3d apply = new Vector3d(arrowLoc);
  123. matrix.get(apply);
  124.  
  125. posX = apply.x;
  126. posY = apply.y;
  127. posZ = apply.z;
  128.  
  129. setPosition(posX, posY, posZ);
  130.  
  131. this.rotationPitch += dPitch;
  132. this.rotationYaw += dYaw;
  133.  
  134. // old homing code (sucks)
  135. // double d5 = target.posX - this.posX;
  136. // double d6 = target.boundingBox.minY + target.height - this.posY;
  137. // double d7 = target.posZ - this.posZ;
  138. //
  139. // this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
  140. // super.onUpdate();
  141. }
  142. }
  143.  
  144. private void findNewTarget()
  145. {
  146. List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
  147. Collections.sort(candidates, new Comparator<EntityLiving>() {
  148. @Override
  149. public int compare(EntityLiving o1, EntityLiving o2) {
  150. double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
  151. if (dist == 0.0)
  152. {
  153. return 0;
  154. } else
  155. {
  156. return dist > 0.0 ? 1 : -1;
  157. }
  158. }
  159. });
  160.  
  161. if (!candidates.isEmpty())
  162. {
  163. dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
  164. PELogger.logInfo("Found new target");
  165. }
  166.  
  167. newTargetCooldown = 5;
  168. }
  169.  
  170. private EntityLiving getTarget()
  171. {
  172. return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
  173. }
  174.  
  175. private boolean hasTarget()
  176. {
  177. return getTarget() != null;
  178. }
  179.  
  180. private void updateHeading(double x, double y, double z)
  181. {
  182. float f3 = MathHelper.sqrt_double(x * x + z * z);
  183. this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(x, z) * 180.0D / Math.PI);
  184. this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(y, (double)f3) * 180.0D / Math.PI);
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement