| 1 | package net.minecraft.pathfinding;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.entity.Entity;
|
| 5 | import net.minecraft.entity.EntityLiving;
|
| 6 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 7 | import net.minecraft.entity.ai.attributes.IAttributeInstance;
|
| 8 | import net.minecraft.util.AxisAlignedBB;
|
| 9 | import net.minecraft.util.BlockPos;
|
| 10 | import net.minecraft.util.MathHelper;
|
| 11 | import net.minecraft.util.Vec3;
|
| 12 | import net.minecraft.world.ChunkCache;
|
| 13 | import net.minecraft.world.World;
|
| 14 |
|
| 15 | public abstract class PathNavigate
|
| 16 | {
|
| 17 | protected EntityLiving theEntity;
|
| 18 | protected World worldObj;
|
| 19 |
|
| 20 | /** The PathEntity being followed. */
|
| 21 | protected PathEntity currentPath;
|
| 22 | protected double speed;
|
| 23 |
|
| 24 | /**
|
| 25 | * The number of blocks (extra) +/- in each axis that get pulled out as cache for the pathfinder's search space
|
| 26 | */
|
| 27 | private final IAttributeInstance pathSearchRange;
|
| 28 |
|
| 29 | /** Time, in number of ticks, following the current path */
|
| 30 | private int totalTicks;
|
| 31 |
|
| 32 | /**
|
| 33 | * The time when the last position check was done (to detect successful movement)
|
| 34 | */
|
| 35 | private int ticksAtLastPos;
|
| 36 |
|
| 37 | /**
|
| 38 | * Coordinates of the entity's position last time a check was done (part of monitoring getting 'stuck')
|
| 39 | */
|
| 40 | private Vec3 lastPosCheck = new Vec3(0.0D, 0.0D, 0.0D);
|
| 41 | private float heightRequirement = 1.0F;
|
| 42 | private final PathFinder pathFinder;
|
| 43 |
|
| 44 | public PathNavigate(EntityLiving entitylivingIn, World worldIn)
|
| 45 | {
|
| 46 | this.theEntity = entitylivingIn;
|
| 47 | this.worldObj = worldIn;
|
| 48 | this.pathSearchRange = entitylivingIn.getEntityAttribute(SharedMonsterAttributes.followRange);
|
| 49 | this.pathFinder = this.getPathFinder();
|
| 50 | }
|
| 51 |
|
| 52 | protected abstract PathFinder getPathFinder();
|
| 53 |
|
| 54 | /**
|
| 55 | * Sets the speed
|
| 56 | */
|
| 57 | public void setSpeed(double speedIn)
|
| 58 | {
|
| 59 | this.speed = speedIn;
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Gets the maximum distance that the path finding will search in.
|
| 64 | */
|
| 65 | public float getPathSearchRange()
|
| 66 | {
|
| 67 | return (float)this.pathSearchRange.getAttributeValue();
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Returns the path to the given coordinates. Args : x, y, z
|
| 72 | */
|
| 73 | public final PathEntity getPathToXYZ(double x, double y, double z)
|
| 74 | {
|
| 75 | return this.getPathToPos(new BlockPos(MathHelper.floor_double(x), (int)y, MathHelper.floor_double(z)));
|
| 76 | }
|
| 77 |
|
| 78 | /**
|
| 79 | * Returns path to given BlockPos
|
| 80 | */
|
| 81 | public PathEntity getPathToPos(BlockPos pos)
|
| 82 | {
|
| 83 | if (!this.canNavigate())
|
| 84 | {
|
| 85 | return null;
|
| 86 | }
|
| 87 | else
|
| 88 | {
|
| 89 | float f = this.getPathSearchRange();
|
| 90 | this.worldObj.theProfiler.startSection("pathfind");
|
| 91 | BlockPos blockpos = new BlockPos(this.theEntity);
|
| 92 | int i = (int)(f + 8.0F);
|
| 93 | ChunkCache chunkcache = new ChunkCache(this.worldObj, blockpos.add(-i, -i, -i), blockpos.add(i, i, i), 0);
|
| 94 | PathEntity pathentity = this.pathFinder.createEntityPathTo(chunkcache, this.theEntity, pos, f);
|
| 95 | this.worldObj.theProfiler.endSection();
|
| 96 | return pathentity;
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | /**
|
| 101 | * Try to find and set a path to XYZ. Returns true if successful. Args : x, y, z, speed
|
| 102 | */
|
| 103 | public boolean tryMoveToXYZ(double x, double y, double z, double speedIn)
|
| 104 | {
|
| 105 | PathEntity pathentity = this.getPathToXYZ((double)MathHelper.floor_double(x), (double)((int)y), (double)MathHelper.floor_double(z));
|
| 106 | return this.setPath(pathentity, speedIn);
|
| 107 | }
|
| 108 |
|
| 109 | /**
|
| 110 | * Sets vertical space requirement for path
|
| 111 | */
|
| 112 | public void setHeightRequirement(float jumpHeight)
|
| 113 | {
|
| 114 | this.heightRequirement = jumpHeight;
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * Returns the path to the given EntityLiving. Args : entity
|
| 119 | */
|
| 120 | public PathEntity getPathToEntityLiving(Entity entityIn)
|
| 121 | {
|
| 122 | if (!this.canNavigate())
|
| 123 | {
|
| 124 | return null;
|
| 125 | }
|
| 126 | else
|
| 127 | {
|
| 128 | float f = this.getPathSearchRange();
|
| 129 | this.worldObj.theProfiler.startSection("pathfind");
|
| 130 | BlockPos blockpos = (new BlockPos(this.theEntity)).up();
|
| 131 | int i = (int)(f + 16.0F);
|
| 132 | ChunkCache chunkcache = new ChunkCache(this.worldObj, blockpos.add(-i, -i, -i), blockpos.add(i, i, i), 0);
|
| 133 | PathEntity pathentity = this.pathFinder.createEntityPathTo(chunkcache, this.theEntity, entityIn, f);
|
| 134 | this.worldObj.theProfiler.endSection();
|
| 135 | return pathentity;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Try to find and set a path to EntityLiving. Returns true if successful. Args : entity, speed
|
| 141 | */
|
| 142 | public boolean tryMoveToEntityLiving(Entity entityIn, double speedIn)
|
| 143 | {
|
| 144 | PathEntity pathentity = this.getPathToEntityLiving(entityIn);
|
| 145 | return pathentity != null ? this.setPath(pathentity, speedIn) : false;
|
| 146 | }
|
| 147 |
|
| 148 | /**
|
| 149 | * Sets a new path. If it's diferent from the old path. Checks to adjust path for sun avoiding, and stores start
|
| 150 | * coords. Args : path, speed
|
| 151 | */
|
| 152 | public boolean setPath(PathEntity pathentityIn, double speedIn)
|
| 153 | {
|
| 154 | if (pathentityIn == null)
|
| 155 | {
|
| 156 | this.currentPath = null;
|
| 157 | return false;
|
| 158 | }
|
| 159 | else
|
| 160 | {
|
| 161 | if (!pathentityIn.isSamePath(this.currentPath))
|
| 162 | {
|
| 163 | this.currentPath = pathentityIn;
|
| 164 | }
|
| 165 |
|
| 166 | this.removeSunnyPath();
|
| 167 |
|
| 168 | if (this.currentPath.getCurrentPathLength() == 0)
|
| 169 | {
|
| 170 | return false;
|
| 171 | }
|
| 172 | else
|
| 173 | {
|
| 174 | this.speed = speedIn;
|
| 175 | Vec3 vec3 = this.getEntityPosition();
|
| 176 | this.ticksAtLastPos = this.totalTicks;
|
| 177 | this.lastPosCheck = vec3;
|
| 178 | return true;
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * gets the actively used PathEntity
|
| 185 | */
|
| 186 | public PathEntity getPath()
|
| 187 | {
|
| 188 | return this.currentPath;
|
| 189 | }
|
| 190 |
|
| 191 | public void onUpdateNavigation()
|
| 192 | {
|
| 193 | ++this.totalTicks;
|
| 194 |
|
| 195 | if (!this.noPath())
|
| 196 | {
|
| 197 | if (this.canNavigate())
|
| 198 | {
|
| 199 | this.pathFollow();
|
| 200 | }
|
| 201 | else if (this.currentPath != null && this.currentPath.getCurrentPathIndex() < this.currentPath.getCurrentPathLength())
|
| 202 | {
|
| 203 | Vec3 vec3 = this.getEntityPosition();
|
| 204 | Vec3 vec31 = this.currentPath.getVectorFromIndex(this.theEntity, this.currentPath.getCurrentPathIndex());
|
| 205 |
|
| 206 | if (vec3.yCoord > vec31.yCoord && !this.theEntity.onGround && MathHelper.floor_double(vec3.xCoord) == MathHelper.floor_double(vec31.xCoord) && MathHelper.floor_double(vec3.zCoord) == MathHelper.floor_double(vec31.zCoord))
|
| 207 | {
|
| 208 | this.currentPath.setCurrentPathIndex(this.currentPath.getCurrentPathIndex() + 1);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | if (!this.noPath())
|
| 213 | {
|
| 214 | Vec3 vec32 = this.currentPath.getPosition(this.theEntity);
|
| 215 |
|
| 216 | if (vec32 != null)
|
| 217 | {
|
| 218 | AxisAlignedBB axisalignedbb1 = (new AxisAlignedBB(vec32.xCoord, vec32.yCoord, vec32.zCoord, vec32.xCoord, vec32.yCoord, vec32.zCoord)).expand(0.5D, 0.5D, 0.5D);
|
| 219 | List<AxisAlignedBB> list = this.worldObj.getCollidingBoundingBoxes(this.theEntity, axisalignedbb1.addCoord(0.0D, -1.0D, 0.0D));
|
| 220 | double d0 = -1.0D;
|
| 221 | axisalignedbb1 = axisalignedbb1.offset(0.0D, 1.0D, 0.0D);
|
| 222 |
|
| 223 | for (AxisAlignedBB axisalignedbb : list)
|
| 224 | {
|
| 225 | d0 = axisalignedbb.calculateYOffset(axisalignedbb1, d0);
|
| 226 | }
|
| 227 |
|
| 228 | this.theEntity.getMoveHelper().setMoveTo(vec32.xCoord, vec32.yCoord + d0, vec32.zCoord, this.speed);
|
| 229 | }
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | protected void pathFollow()
|
| 235 | {
|
| 236 | Vec3 vec3 = this.getEntityPosition();
|
| 237 | int i = this.currentPath.getCurrentPathLength();
|
| 238 |
|
| 239 | for (int j = this.currentPath.getCurrentPathIndex(); j < this.currentPath.getCurrentPathLength(); ++j)
|
| 240 | {
|
| 241 | if (this.currentPath.getPathPointFromIndex(j).yCoord != (int)vec3.yCoord)
|
| 242 | {
|
| 243 | i = j;
|
| 244 | break;
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | float f = this.theEntity.width * this.theEntity.width * this.heightRequirement;
|
| 249 |
|
| 250 | for (int k = this.currentPath.getCurrentPathIndex(); k < i; ++k)
|
| 251 | {
|
| 252 | Vec3 vec31 = this.currentPath.getVectorFromIndex(this.theEntity, k);
|
| 253 |
|
| 254 | if (vec3.squareDistanceTo(vec31) < (double)f)
|
| 255 | {
|
| 256 | this.currentPath.setCurrentPathIndex(k + 1);
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | int j1 = MathHelper.ceiling_float_int(this.theEntity.width);
|
| 261 | int k1 = (int)this.theEntity.height + 1;
|
| 262 | int l = j1;
|
| 263 |
|
| 264 | for (int i1 = i - 1; i1 >= this.currentPath.getCurrentPathIndex(); --i1)
|
| 265 | {
|
| 266 | if (this.isDirectPathBetweenPoints(vec3, this.currentPath.getVectorFromIndex(this.theEntity, i1), j1, k1, l))
|
| 267 | {
|
| 268 | this.currentPath.setCurrentPathIndex(i1);
|
| 269 | break;
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | this.checkForStuck(vec3);
|
| 274 | }
|
| 275 |
|
| 276 | /**
|
| 277 | * Checks if entity haven't been moved when last checked and if so, clears current {@link
|
| 278 | * net.minecraft.pathfinding.PathEntity}
|
| 279 | */
|
| 280 | protected void checkForStuck(Vec3 positionVec3)
|
| 281 | {
|
| 282 | if (this.totalTicks - this.ticksAtLastPos > 100)
|
| 283 | {
|
| 284 | if (positionVec3.squareDistanceTo(this.lastPosCheck) < 2.25D)
|
| 285 | {
|
| 286 | this.clearPathEntity();
|
| 287 | }
|
| 288 |
|
| 289 | this.ticksAtLastPos = this.totalTicks;
|
| 290 | this.lastPosCheck = positionVec3;
|
| 291 | }
|
| 292 | }
|
| 293 |
|
| 294 | /**
|
| 295 | * If null path or reached the end
|
| 296 | */
|
| 297 | public boolean noPath()
|
| 298 | {
|
| 299 | return this.currentPath == null || this.currentPath.isFinished();
|
| 300 | }
|
| 301 |
|
| 302 | /**
|
| 303 | * sets active PathEntity to null
|
| 304 | */
|
| 305 | public void clearPathEntity()
|
| 306 | {
|
| 307 | this.currentPath = null;
|
| 308 | }
|
| 309 |
|
| 310 | protected abstract Vec3 getEntityPosition();
|
| 311 |
|
| 312 | /**
|
| 313 | * If on ground or swimming and can swim
|
| 314 | */
|
| 315 | protected abstract boolean canNavigate();
|
| 316 |
|
| 317 | /**
|
| 318 | * Returns true if the entity is in water or lava, false otherwise
|
| 319 | */
|
| 320 | protected boolean isInLiquid()
|
| 321 | {
|
| 322 | return this.theEntity.isInWater() || this.theEntity.isInLava();
|
| 323 | }
|
| 324 |
|
| 325 | /**
|
| 326 | * Trims path data from the end to the first sun covered block
|
| 327 | */
|
| 328 | protected void removeSunnyPath()
|
| 329 | {
|
| 330 | }
|
| 331 |
|
| 332 | /**
|
| 333 | * Returns true when an entity of specified size could safely walk in a straight line between the two points. Args:
|
| 334 | * pos1, pos2, entityXSize, entityYSize, entityZSize
|
| 335 | */
|
| 336 | protected abstract boolean isDirectPathBetweenPoints(Vec3 posVec31, Vec3 posVec32, int sizeX, int sizeY, int sizeZ);
|
| 337 | }
|
| 338 |
|