Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 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.  
  100. double targetPitch = getPitch(lookVec);
  101. double targetYaw = getYaw(lookVec);
  102. double dPitch = rotationPitch - targetPitch;
  103. double dYaw = rotationYaw - targetYaw;
  104.  
  105. float rotationDragFactor = 0.5F;
  106. rotationPitch = ((float) MathHelper.wrapAngleTo180_double(rotationPitch + dPitch * rotationDragFactor));
  107. rotationYaw = ((float) MathHelper.wrapAngleTo180_double(rotationYaw + dYaw * rotationDragFactor));
  108.  
  109. // double dotProduct = new Vector3d(arrowLoc).dot(targetLoc);
  110. // double theta = Math.acos(dotProduct / (arrowLoc.length() * targetLoc.length()));
  111. //
  112. // Vector3d crossProduct = new Vector3d();
  113. // crossProduct.cross(arrowLoc, targetLoc);
  114. // crossProduct.normalize();
  115. //
  116. // AxisAngle4d rotation = new AxisAngle4d(crossProduct, theta * 0.5);
  117.  
  118.  
  119. // CoolAlias's solution (doesn't work)
  120. // System.out.printf("before %f, %f, %f%n", posX, posY, posZ);
  121. // double arcRate = 0.25D;
  122. // double dx = posX - lookVec.x;
  123. // double dy = posY - lookVec.y;
  124. // double dz = posZ - lookVec.z;
  125. // posX += dx * arcRate;
  126. // posY += dy * arcRate;
  127. // posZ += dz * arcRate;
  128. // setPosition(posX, posY, posZ);
  129. // System.out.printf("after %f, %f, %f%n", posX, posY, posZ);
  130.  
  131. // Shaderkiller's solution
  132. // double angleYaw = Math.atan2(lookVec.z, lookVec.x) - Math.PI/2d;
  133. // double anglePitch = Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));
  134. //
  135. // double dYaw = MathHelper.wrapAngleTo180_double(this.rotationYaw - angleYaw);
  136. // double dPitch = MathHelper.wrapAngleTo180_double(this.rotationPitch - anglePitch);
  137. //
  138. // AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
  139. // AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
  140. // Quat4f rot = new Quat4f(0, 0, 0, 1);
  141. // Quat4f yawQuat = new Quat4f();
  142. // Quat4f pitchQuat = new Quat4f();
  143. // yawQuat.set(yaw);
  144. // rot.mul(yawQuat);
  145. // pitchQuat.set(pitch);
  146. // rot.mul(pitchQuat);
  147. // Matrix4d matrix = new Matrix4d();
  148. // matrix.setIdentity();
  149. // matrix.setTranslation(arrowLoc);
  150. // matrix.setRotation(rot);
  151. //
  152. // Vector3d apply = new Vector3d(arrowLoc);
  153. // matrix.get(apply);
  154. //
  155. // posX = apply.x;
  156. // posY = apply.y;
  157. // posZ = apply.z;
  158. //
  159. // setPosition(posX, posY, posZ);
  160. //
  161. // this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, 0.25F, 1.0F);
  162. // this.moveEntity(motionX, motionY, motionZ);
  163.  
  164. // old homing code (sucks)
  165. // double d5 = target.posX - this.posX;
  166. // double d6 = target.boundingBox.minY + target.height - this.posY;
  167. // double d7 = target.posZ - this.posZ;
  168. //
  169. // this.setThrowableHeading(d5, d6, d7, 0.1F, 0.0F);
  170. // super.onUpdate();
  171. }
  172. }
  173.  
  174. private void findNewTarget()
  175. {
  176. List<EntityLiving> candidates = worldObj.getEntitiesWithinAABB(EntityLiving.class, this.boundingBox.expand(8, 8, 8));
  177. Collections.sort(candidates, new Comparator<EntityLiving>() {
  178. @Override
  179. public int compare(EntityLiving o1, EntityLiving o2) {
  180. double dist = EntityHomingArrow.this.getDistanceSqToEntity(o1) - EntityHomingArrow.this.getDistanceSqToEntity(o2);
  181. if (dist == 0.0)
  182. {
  183. return 0;
  184. } else
  185. {
  186. return dist > 0.0 ? 1 : -1;
  187. }
  188. }
  189. });
  190.  
  191. if (!candidates.isEmpty())
  192. {
  193. dataWatcher.updateObject(DW_TARGET_ID, candidates.get(0).getEntityId());
  194. PELogger.logInfo("Found new target");
  195. }
  196.  
  197. newTargetCooldown = 5;
  198. }
  199.  
  200. private EntityLiving getTarget()
  201. {
  202. return ((EntityLiving) worldObj.getEntityByID(dataWatcher.getWatchableObjectInt(DW_TARGET_ID)));
  203. }
  204.  
  205. private boolean hasTarget()
  206. {
  207. return getTarget() != null;
  208. }
  209.  
  210. private void updateHeading(double velocityX, double velocityY, double velocityZ)
  211. {
  212. float f3 = MathHelper.sqrt_double(velocityX * velocityX + velocityZ * velocityZ);
  213. this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(velocityX, velocityZ) * 180.0D / Math.PI);
  214. this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(velocityY, f3) * 180.0D / Math.PI);
  215. }
  216.  
  217. private double getPitch(Vector3d vec)
  218. {
  219. return Math.atan2(vec.y, Math.sqrt(vec.x * vec.x + vec.z * vec.z));
  220. }
  221.  
  222. private double getYaw(Vector3d vec)
  223. {
  224. return Math.atan2(vec.z, vec.x) - Math.PI / 2.0D;
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement