Advertisement
Corosus

EntityCreature.java - Pathfinding Activated Super Basic

Jul 16th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. //I've added documentation where changes happen.
  2.  
  3. package net.minecraft.src;
  4.  
  5. import net.minecraft.src.Entity;
  6. import net.minecraft.src.EntityLiving;
  7. import net.minecraft.src.MathHelper;
  8. import net.minecraft.src.PathEntity;
  9. import net.minecraft.src.Vec3D;
  10. import net.minecraft.src.World;
  11.  
  12. public class EntityCreature extends EntityLiving {
  13.  
  14. private PathEntity pathToEntity;
  15. protected Entity playerToAttack;
  16. protected boolean hasAttacked = false;
  17.  
  18.  
  19. public EntityCreature(World var1) {
  20. super(var1);
  21. }
  22.  
  23. protected boolean isMovementCeased() {
  24. return false;
  25. }
  26.  
  27. protected void updatePlayerActionState() {
  28. this.hasAttacked = this.isMovementCeased();
  29. //default max distance allowed to path - change if needed
  30. float var1 = 16.0F;
  31. if(this.playerToAttack == null) {
  32. this.playerToAttack = this.findPlayerToAttack();
  33. if(this.playerToAttack != null) {
  34. this.pathToEntity = this.worldObj.getPathToEntity(this, this.playerToAttack, var1);
  35. }
  36. } else if(!this.playerToAttack.isEntityAlive()) {
  37. this.playerToAttack = null;
  38. } else {
  39. float var2 = this.playerToAttack.getDistanceToEntity(this);
  40. if(this.canEntityBeSeen(this.playerToAttack)) {
  41. this.attackEntity(this.playerToAttack, var2);
  42. } else {
  43. this.attackBlockedEntity(this.playerToAttack, var2);
  44. }
  45. }
  46.  
  47. if(!this.hasAttacked && this.playerToAttack != null && (this.pathToEntity == null || this.rand.nextInt(20) == 0)) {
  48. this.pathToEntity = this.worldObj.getPathToEntity(this, this.playerToAttack, var1);
  49. } else if(!this.hasAttacked && (this.pathToEntity == null && this.rand.nextInt(80) == 0 || this.rand.nextInt(80) == 0)) {
  50. this.func_31026_E();
  51. }
  52.  
  53. int var21 = MathHelper.floor_double(this.boundingBox.minY + 0.5D);
  54. boolean var3 = this.isInWater();
  55. boolean var4 = this.handleLavaMovement();
  56. this.rotationPitch = 0.0F;
  57. if(this.pathToEntity != null && this.rand.nextInt(100) != 0) {
  58. Vec3D var5 = this.pathToEntity.getPosition(this);
  59. //changed 2.0 to 1.2, this is how close an entity has to get to each pathnode before proceeding to next
  60. //making smaller allows for tighter navigation (eg: spiral stairs), too small a number and entities will spin too much
  61. double var6 = (double)(this.width * 1.2F);
  62.  
  63. while(var5 != null && var5.squareDistanceTo(this.posX, var5.yCoord, this.posZ) < var6 * var6) {
  64. this.pathToEntity.incrementPathIndex();
  65. if(this.pathToEntity.isFinished()) {
  66. var5 = null;
  67. this.pathToEntity = null;
  68. } else {
  69. var5 = this.pathToEntity.getPosition(this);
  70. }
  71. }
  72.  
  73. this.isJumping = false;
  74. if(var5 != null) {
  75. double var8 = var5.xCoord - this.posX;
  76. double var10 = var5.zCoord - this.posZ;
  77. double var12 = var5.yCoord - (double)var21;
  78. float var14 = (float)(Math.atan2(var10, var8) * 180.0D / 3.1415927410125732D) - 90.0F;
  79. float var15 = var14 - this.rotationYaw;
  80.  
  81. for(this.moveForward = this.moveSpeed; var15 < -180.0F; var15 += 360.0F) {
  82. ;
  83. }
  84.  
  85. while(var15 >= 180.0F) {
  86. var15 -= 360.0F;
  87. }
  88.  
  89. if(var15 > 30.0F) {
  90. var15 = 30.0F;
  91. }
  92.  
  93. if(var15 < -30.0F) {
  94. var15 = -30.0F;
  95. }
  96.  
  97. this.rotationYaw += var15;
  98. if(this.hasAttacked && this.playerToAttack != null) {
  99. double var16 = this.playerToAttack.posX - this.posX;
  100. double var18 = this.playerToAttack.posZ - this.posZ;
  101. float var20 = this.rotationYaw;
  102. this.rotationYaw = (float)(Math.atan2(var18, var16) * 180.0D / 3.1415927410125732D) - 90.0F;
  103. var15 = (var20 - this.rotationYaw + 90.0F) * 3.1415927F / 180.0F;
  104. this.moveStrafing = -MathHelper.sin(var15) * this.moveForward * 1.0F;
  105. this.moveForward = MathHelper.cos(var15) * this.moveForward * 1.0F;
  106. }
  107.  
  108. if(var12 > 0.0D) {
  109. this.isJumping = true;
  110. }
  111. }
  112.  
  113. //this is the main change, allowing an entity with a target to actually follow its path, instead of always moving strait towards target
  114. //a new function, see below
  115. if(isSolidPath(this.playerToAttack)) {
  116. this.faceEntity(this.playerToAttack, 30.0F, 30.0F);
  117. }
  118.  
  119. if(this.isCollidedHorizontally && !this.hasPath()) {
  120. this.isJumping = true;
  121. }
  122.  
  123. if(this.rand.nextFloat() < 0.8F && (var3 || var4)) {
  124. this.isJumping = true;
  125. }
  126.  
  127. } else {
  128. super.updatePlayerActionState();
  129. this.pathToEntity = null;
  130. }
  131. }
  132.  
  133. //If has target, and can see, and is within 5 blocks(so it wont walk past you, as little as 2 works decent), and if entity is within 1.5 of you on the Y axis
  134. public boolean isSolidPath(Entity var1) {
  135. return var1 != null && this.canEntityBeSeen(var1) && this.getDistanceToEntity(var1) < 5.0F && Math.abs(this.posY - (double)this.yOffset - (var1.posY - (double)var1.yOffset)) <= 1.5D;
  136. }
  137.  
  138. protected void func_31026_E() {
  139. boolean var1 = false;
  140. int var2 = -1;
  141. int var3 = -1;
  142. int var4 = -1;
  143. float var5 = -99999.0F;
  144.  
  145. for(int var6 = 0; var6 < 10; ++var6) {
  146. int var7 = MathHelper.floor_double(this.posX + (double)this.rand.nextInt(13) - 6.0D);
  147. int var8 = MathHelper.floor_double(this.posY + (double)this.rand.nextInt(7) - 3.0D);
  148. int var9 = MathHelper.floor_double(this.posZ + (double)this.rand.nextInt(13) - 6.0D);
  149. float var10 = this.getBlockPathWeight(var7, var8, var9);
  150. if(var10 > var5) {
  151. var5 = var10;
  152. var2 = var7;
  153. var3 = var8;
  154. var4 = var9;
  155. var1 = true;
  156. }
  157. }
  158.  
  159. if(var1) {
  160. this.pathToEntity = this.worldObj.getEntityPathToXYZ(this, var2, var3, var4, 10.0F);
  161. }
  162.  
  163. }
  164.  
  165. protected void attackEntity(Entity var1, float var2) {}
  166.  
  167. protected void attackBlockedEntity(Entity var1, float var2) {}
  168.  
  169. protected float getBlockPathWeight(int var1, int var2, int var3) {
  170. return 0.0F;
  171. }
  172.  
  173. protected Entity findPlayerToAttack() {
  174. return null;
  175. }
  176.  
  177. public boolean getCanSpawnHere() {
  178. int var1 = MathHelper.floor_double(this.posX);
  179. int var2 = MathHelper.floor_double(this.boundingBox.minY);
  180. int var3 = MathHelper.floor_double(this.posZ);
  181. return super.getCanSpawnHere() && this.getBlockPathWeight(var1, var2, var3) >= 0.0F;
  182. }
  183.  
  184. public boolean hasPath() {
  185. return this.pathToEntity != null;
  186. }
  187.  
  188. public void setPathToEntity(PathEntity var1) {
  189. this.pathToEntity = var1;
  190. }
  191.  
  192. public Entity getTarget() {
  193. return this.playerToAttack;
  194. }
  195.  
  196. public void setTarget(Entity var1) {
  197. this.playerToAttack = var1;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement