| 1 | package net.minecraft.world.pathfinder;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.block.BlockDoor;
|
| 5 | import net.minecraft.block.BlockFence;
|
| 6 | import net.minecraft.block.BlockFenceGate;
|
| 7 | import net.minecraft.block.BlockRailBase;
|
| 8 | import net.minecraft.block.BlockWall;
|
| 9 | import net.minecraft.block.material.Material;
|
| 10 | import net.minecraft.entity.Entity;
|
| 11 | import net.minecraft.init.Blocks;
|
| 12 | import net.minecraft.pathfinding.PathPoint;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.MathHelper;
|
| 15 | import net.minecraft.world.IBlockAccess;
|
| 16 |
|
| 17 | public class WalkNodeProcessor extends NodeProcessor
|
| 18 | {
|
| 19 | private boolean canEnterDoors;
|
| 20 | private boolean canBreakDoors;
|
| 21 | private boolean avoidsWater;
|
| 22 | private boolean canSwim;
|
| 23 | private boolean shouldAvoidWater;
|
| 24 |
|
| 25 | public void initProcessor(IBlockAccess iblockaccessIn, Entity entityIn)
|
| 26 | {
|
| 27 | super.initProcessor(iblockaccessIn, entityIn);
|
| 28 | this.shouldAvoidWater = this.avoidsWater;
|
| 29 | }
|
| 30 |
|
| 31 | /**
|
| 32 | * This method is called when all nodes have been processed and PathEntity is created.
|
| 33 | * {@link net.minecraft.world.pathfinder.WalkNodeProcessor WalkNodeProcessor} uses this to change its field {@link
|
| 34 | * net.minecraft.world.pathfinder.WalkNodeProcessor#avoidsWater avoidsWater}
|
| 35 | */
|
| 36 | public void postProcess()
|
| 37 | {
|
| 38 | super.postProcess();
|
| 39 | this.avoidsWater = this.shouldAvoidWater;
|
| 40 | }
|
| 41 |
|
| 42 | /**
|
| 43 | * Returns given entity's position as PathPoint
|
| 44 | */
|
| 45 | public PathPoint getPathPointTo(Entity entityIn)
|
| 46 | {
|
| 47 | int i;
|
| 48 |
|
| 49 | if (this.canSwim && entityIn.isInWater())
|
| 50 | {
|
| 51 | i = (int)entityIn.getEntityBoundingBox().minY;
|
| 52 | BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos(MathHelper.floor_double(entityIn.posX), i, MathHelper.floor_double(entityIn.posZ));
|
| 53 |
|
| 54 | for (Block block = this.blockaccess.getBlockState(blockpos$mutableblockpos).getBlock(); block == Blocks.flowing_water || block == Blocks.water; block = this.blockaccess.getBlockState(blockpos$mutableblockpos).getBlock())
|
| 55 | {
|
| 56 | ++i;
|
| 57 | blockpos$mutableblockpos.set(MathHelper.floor_double(entityIn.posX), i, MathHelper.floor_double(entityIn.posZ));
|
| 58 | }
|
| 59 |
|
| 60 | this.avoidsWater = false;
|
| 61 | }
|
| 62 | else
|
| 63 | {
|
| 64 | i = MathHelper.floor_double(entityIn.getEntityBoundingBox().minY + 0.5D);
|
| 65 | }
|
| 66 |
|
| 67 | return this.openPoint(MathHelper.floor_double(entityIn.getEntityBoundingBox().minX), i, MathHelper.floor_double(entityIn.getEntityBoundingBox().minZ));
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Returns PathPoint for given coordinates
|
| 72 | */
|
| 73 | public PathPoint getPathPointToCoords(Entity entityIn, double x, double y, double target)
|
| 74 | {
|
| 75 | return this.openPoint(MathHelper.floor_double(x - (double)(entityIn.width / 2.0F)), MathHelper.floor_double(y), MathHelper.floor_double(target - (double)(entityIn.width / 2.0F)));
|
| 76 | }
|
| 77 |
|
| 78 | public int findPathOptions(PathPoint[] pathOptions, Entity entityIn, PathPoint currentPoint, PathPoint targetPoint, float maxDistance)
|
| 79 | {
|
| 80 | int i = 0;
|
| 81 | int j = 0;
|
| 82 |
|
| 83 | if (this.getVerticalOffset(entityIn, currentPoint.xCoord, currentPoint.yCoord + 1, currentPoint.zCoord) == 1)
|
| 84 | {
|
| 85 | j = 1;
|
| 86 | }
|
| 87 |
|
| 88 | PathPoint pathpoint = this.getSafePoint(entityIn, currentPoint.xCoord, currentPoint.yCoord, currentPoint.zCoord + 1, j);
|
| 89 | PathPoint pathpoint1 = this.getSafePoint(entityIn, currentPoint.xCoord - 1, currentPoint.yCoord, currentPoint.zCoord, j);
|
| 90 | PathPoint pathpoint2 = this.getSafePoint(entityIn, currentPoint.xCoord + 1, currentPoint.yCoord, currentPoint.zCoord, j);
|
| 91 | PathPoint pathpoint3 = this.getSafePoint(entityIn, currentPoint.xCoord, currentPoint.yCoord, currentPoint.zCoord - 1, j);
|
| 92 |
|
| 93 | if (pathpoint != null && !pathpoint.visited && pathpoint.distanceTo(targetPoint) < maxDistance)
|
| 94 | {
|
| 95 | pathOptions[i++] = pathpoint;
|
| 96 | }
|
| 97 |
|
| 98 | if (pathpoint1 != null && !pathpoint1.visited && pathpoint1.distanceTo(targetPoint) < maxDistance)
|
| 99 | {
|
| 100 | pathOptions[i++] = pathpoint1;
|
| 101 | }
|
| 102 |
|
| 103 | if (pathpoint2 != null && !pathpoint2.visited && pathpoint2.distanceTo(targetPoint) < maxDistance)
|
| 104 | {
|
| 105 | pathOptions[i++] = pathpoint2;
|
| 106 | }
|
| 107 |
|
| 108 | if (pathpoint3 != null && !pathpoint3.visited && pathpoint3.distanceTo(targetPoint) < maxDistance)
|
| 109 | {
|
| 110 | pathOptions[i++] = pathpoint3;
|
| 111 | }
|
| 112 |
|
| 113 | return i;
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Returns a point that the entity can safely move to
|
| 118 | */
|
| 119 | private PathPoint getSafePoint(Entity entityIn, int x, int y, int z, int p_176171_5_)
|
| 120 | {
|
| 121 | PathPoint pathpoint = null;
|
| 122 | int i = this.getVerticalOffset(entityIn, x, y, z);
|
| 123 |
|
| 124 | if (i == 2)
|
| 125 | {
|
| 126 | return this.openPoint(x, y, z);
|
| 127 | }
|
| 128 | else
|
| 129 | {
|
| 130 | if (i == 1)
|
| 131 | {
|
| 132 | pathpoint = this.openPoint(x, y, z);
|
| 133 | }
|
| 134 |
|
| 135 | if (pathpoint == null && p_176171_5_ > 0 && i != -3 && i != -4 && this.getVerticalOffset(entityIn, x, y + p_176171_5_, z) == 1)
|
| 136 | {
|
| 137 | pathpoint = this.openPoint(x, y + p_176171_5_, z);
|
| 138 | y += p_176171_5_;
|
| 139 | }
|
| 140 |
|
| 141 | if (pathpoint != null)
|
| 142 | {
|
| 143 | int j = 0;
|
| 144 | int k;
|
| 145 |
|
| 146 | for (k = 0; y > 0; pathpoint = this.openPoint(x, y, z))
|
| 147 | {
|
| 148 | k = this.getVerticalOffset(entityIn, x, y - 1, z);
|
| 149 |
|
| 150 | if (this.avoidsWater && k == -1)
|
| 151 | {
|
| 152 | return null;
|
| 153 | }
|
| 154 |
|
| 155 | if (k != 1)
|
| 156 | {
|
| 157 | break;
|
| 158 | }
|
| 159 |
|
| 160 | if (j++ >= entityIn.getMaxFallHeight())
|
| 161 | {
|
| 162 | return null;
|
| 163 | }
|
| 164 |
|
| 165 | --y;
|
| 166 |
|
| 167 | if (y <= 0)
|
| 168 | {
|
| 169 | return null;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | if (k == -2)
|
| 174 | {
|
| 175 | return null;
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | return pathpoint;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * Checks if an entity collides with blocks at a position.
|
| 185 | * Returns 1 if clear, 0 for colliding with any solid block, -1 for water(if avoids water),
|
| 186 | * -2 for lava, -3 for fence and wall, -4 for closed trapdoor, 2 if otherwise clear except for open trapdoor or
|
| 187 | * water(if not avoiding)
|
| 188 | */
|
| 189 | private int getVerticalOffset(Entity entityIn, int x, int y, int z)
|
| 190 | {
|
| 191 | return func_176170_a(this.blockaccess, entityIn, x, y, z, this.entitySizeX, this.entitySizeY, this.entitySizeZ, this.avoidsWater, this.canBreakDoors, this.canEnterDoors);
|
| 192 | }
|
| 193 |
|
| 194 | public static int func_176170_a(IBlockAccess blockaccessIn, Entity entityIn, int x, int y, int z, int sizeX, int sizeY, int sizeZ, boolean avoidWater, boolean breakDoors, boolean enterDoors)
|
| 195 | {
|
| 196 | boolean flag = false;
|
| 197 | BlockPos blockpos = new BlockPos(entityIn);
|
| 198 | BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
|
| 199 |
|
| 200 | for (int i = x; i < x + sizeX; ++i)
|
| 201 | {
|
| 202 | for (int j = y; j < y + sizeY; ++j)
|
| 203 | {
|
| 204 | for (int k = z; k < z + sizeZ; ++k)
|
| 205 | {
|
| 206 | blockpos$mutableblockpos.set(i, j, k);
|
| 207 | Block block = blockaccessIn.getBlockState(blockpos$mutableblockpos).getBlock();
|
| 208 |
|
| 209 | if (block.getMaterial() != Material.air)
|
| 210 | {
|
| 211 | if (block != Blocks.trapdoor && block != Blocks.iron_trapdoor)
|
| 212 | {
|
| 213 | if (block != Blocks.flowing_water && block != Blocks.water)
|
| 214 | {
|
| 215 | if (!enterDoors && block instanceof BlockDoor && block.getMaterial() == Material.wood)
|
| 216 | {
|
| 217 | return 0;
|
| 218 | }
|
| 219 | }
|
| 220 | else
|
| 221 | {
|
| 222 | if (avoidWater)
|
| 223 | {
|
| 224 | return -1;
|
| 225 | }
|
| 226 |
|
| 227 | flag = true;
|
| 228 | }
|
| 229 | }
|
| 230 | else
|
| 231 | {
|
| 232 | flag = true;
|
| 233 | }
|
| 234 |
|
| 235 | if (entityIn.worldObj.getBlockState(blockpos$mutableblockpos).getBlock() instanceof BlockRailBase)
|
| 236 | {
|
| 237 | if (!(entityIn.worldObj.getBlockState(blockpos).getBlock() instanceof BlockRailBase) && !(entityIn.worldObj.getBlockState(blockpos.down()).getBlock() instanceof BlockRailBase))
|
| 238 | {
|
| 239 | return -3;
|
| 240 | }
|
| 241 | }
|
| 242 | else if (!block.isPassable(blockaccessIn, blockpos$mutableblockpos) && (!breakDoors || !(block instanceof BlockDoor) || block.getMaterial() != Material.wood))
|
| 243 | {
|
| 244 | if (block instanceof BlockFence || block instanceof BlockFenceGate || block instanceof BlockWall)
|
| 245 | {
|
| 246 | return -3;
|
| 247 | }
|
| 248 |
|
| 249 | if (block == Blocks.trapdoor || block == Blocks.iron_trapdoor)
|
| 250 | {
|
| 251 | return -4;
|
| 252 | }
|
| 253 |
|
| 254 | Material material = block.getMaterial();
|
| 255 |
|
| 256 | if (material != Material.lava)
|
| 257 | {
|
| 258 | return 0;
|
| 259 | }
|
| 260 |
|
| 261 | if (!entityIn.isInLava())
|
| 262 | {
|
| 263 | return -2;
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | return flag ? 2 : 1;
|
| 272 | }
|
| 273 |
|
| 274 | public void setEnterDoors(boolean canEnterDoorsIn)
|
| 275 | {
|
| 276 | this.canEnterDoors = canEnterDoorsIn;
|
| 277 | }
|
| 278 |
|
| 279 | public void setBreakDoors(boolean canBreakDoorsIn)
|
| 280 | {
|
| 281 | this.canBreakDoors = canBreakDoorsIn;
|
| 282 | }
|
| 283 |
|
| 284 | public void setAvoidsWater(boolean avoidsWaterIn)
|
| 285 | {
|
| 286 | this.avoidsWater = avoidsWaterIn;
|
| 287 | }
|
| 288 |
|
| 289 | public void setCanSwim(boolean canSwimIn)
|
| 290 | {
|
| 291 | this.canSwim = canSwimIn;
|
| 292 | }
|
| 293 |
|
| 294 | public boolean getEnterDoors()
|
| 295 | {
|
| 296 | return this.canEnterDoors;
|
| 297 | }
|
| 298 |
|
| 299 | public boolean getCanSwim()
|
| 300 | {
|
| 301 | return this.canSwim;
|
| 302 | }
|
| 303 |
|
| 304 | public boolean getAvoidsWater()
|
| 305 | {
|
| 306 | return this.avoidsWater;
|
| 307 | }
|
| 308 | }
|
| 309 |
|