| 1 | package net.minecraft.pathfinding;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.util.BlockPos;
|
| 5 | import net.minecraft.world.IBlockAccess;
|
| 6 | import net.minecraft.world.pathfinder.NodeProcessor;
|
| 7 |
|
| 8 | public class PathFinder
|
| 9 | {
|
| 10 | /** The path being generated */
|
| 11 | private Path path = new Path();
|
| 12 |
|
| 13 | /** Selection of path points to add to the path */
|
| 14 | private PathPoint[] pathOptions = new PathPoint[32];
|
| 15 | private NodeProcessor nodeProcessor;
|
| 16 |
|
| 17 | public PathFinder(NodeProcessor nodeProcessorIn)
|
| 18 | {
|
| 19 | this.nodeProcessor = nodeProcessorIn;
|
| 20 | }
|
| 21 |
|
| 22 | /**
|
| 23 | * Creates a path from one entity to another within a minimum distance
|
| 24 | */
|
| 25 | public PathEntity createEntityPathTo(IBlockAccess blockaccess, Entity entityFrom, Entity entityTo, float dist)
|
| 26 | {
|
| 27 | return this.createEntityPathTo(blockaccess, entityFrom, entityTo.posX, entityTo.getEntityBoundingBox().minY, entityTo.posZ, dist);
|
| 28 | }
|
| 29 |
|
| 30 | /**
|
| 31 | * Creates a path from an entity to a specified location within a minimum distance
|
| 32 | */
|
| 33 | public PathEntity createEntityPathTo(IBlockAccess blockaccess, Entity entityIn, BlockPos targetPos, float dist)
|
| 34 | {
|
| 35 | return this.createEntityPathTo(blockaccess, entityIn, (double)((float)targetPos.getX() + 0.5F), (double)((float)targetPos.getY() + 0.5F), (double)((float)targetPos.getZ() + 0.5F), dist);
|
| 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Internal implementation of creating a path from an entity to a point
|
| 40 | */
|
| 41 | private PathEntity createEntityPathTo(IBlockAccess blockaccess, Entity entityIn, double x, double y, double z, float distance)
|
| 42 | {
|
| 43 | this.path.clearPath();
|
| 44 | this.nodeProcessor.initProcessor(blockaccess, entityIn);
|
| 45 | PathPoint pathpoint = this.nodeProcessor.getPathPointTo(entityIn);
|
| 46 | PathPoint pathpoint1 = this.nodeProcessor.getPathPointToCoords(entityIn, x, y, z);
|
| 47 | PathEntity pathentity = this.addToPath(entityIn, pathpoint, pathpoint1, distance);
|
| 48 | this.nodeProcessor.postProcess();
|
| 49 | return pathentity;
|
| 50 | }
|
| 51 |
|
| 52 | /**
|
| 53 | * Adds a path from start to end and returns the whole path
|
| 54 | */
|
| 55 | private PathEntity addToPath(Entity entityIn, PathPoint pathpointStart, PathPoint pathpointEnd, float maxDistance)
|
| 56 | {
|
| 57 | pathpointStart.totalPathDistance = 0.0F;
|
| 58 | pathpointStart.distanceToNext = pathpointStart.distanceToSquared(pathpointEnd);
|
| 59 | pathpointStart.distanceToTarget = pathpointStart.distanceToNext;
|
| 60 | this.path.clearPath();
|
| 61 | this.path.addPoint(pathpointStart);
|
| 62 | PathPoint pathpoint = pathpointStart;
|
| 63 |
|
| 64 | while (!this.path.isPathEmpty())
|
| 65 | {
|
| 66 | PathPoint pathpoint1 = this.path.dequeue();
|
| 67 |
|
| 68 | if (pathpoint1.equals(pathpointEnd))
|
| 69 | {
|
| 70 | return this.createEntityPath(pathpointStart, pathpointEnd);
|
| 71 | }
|
| 72 |
|
| 73 | if (pathpoint1.distanceToSquared(pathpointEnd) < pathpoint.distanceToSquared(pathpointEnd))
|
| 74 | {
|
| 75 | pathpoint = pathpoint1;
|
| 76 | }
|
| 77 |
|
| 78 | pathpoint1.visited = true;
|
| 79 | int i = this.nodeProcessor.findPathOptions(this.pathOptions, entityIn, pathpoint1, pathpointEnd, maxDistance);
|
| 80 |
|
| 81 | for (int j = 0; j < i; ++j)
|
| 82 | {
|
| 83 | PathPoint pathpoint2 = this.pathOptions[j];
|
| 84 | float f = pathpoint1.totalPathDistance + pathpoint1.distanceToSquared(pathpoint2);
|
| 85 |
|
| 86 | if (f < maxDistance * 2.0F && (!pathpoint2.isAssigned() || f < pathpoint2.totalPathDistance))
|
| 87 | {
|
| 88 | pathpoint2.previous = pathpoint1;
|
| 89 | pathpoint2.totalPathDistance = f;
|
| 90 | pathpoint2.distanceToNext = pathpoint2.distanceToSquared(pathpointEnd);
|
| 91 |
|
| 92 | if (pathpoint2.isAssigned())
|
| 93 | {
|
| 94 | this.path.changeDistance(pathpoint2, pathpoint2.totalPathDistance + pathpoint2.distanceToNext);
|
| 95 | }
|
| 96 | else
|
| 97 | {
|
| 98 | pathpoint2.distanceToTarget = pathpoint2.totalPathDistance + pathpoint2.distanceToNext;
|
| 99 | this.path.addPoint(pathpoint2);
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | if (pathpoint == pathpointStart)
|
| 106 | {
|
| 107 | return null;
|
| 108 | }
|
| 109 | else
|
| 110 | {
|
| 111 | return this.createEntityPath(pathpointStart, pathpoint);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Returns a new PathEntity for a given start and end point
|
| 117 | */
|
| 118 | private PathEntity createEntityPath(PathPoint start, PathPoint end)
|
| 119 | {
|
| 120 | int i = 1;
|
| 121 |
|
| 122 | for (PathPoint pathpoint = end; pathpoint.previous != null; pathpoint = pathpoint.previous)
|
| 123 | {
|
| 124 | ++i;
|
| 125 | }
|
| 126 |
|
| 127 | PathPoint[] apathpoint = new PathPoint[i];
|
| 128 | PathPoint pathpoint1 = end;
|
| 129 | --i;
|
| 130 |
|
| 131 | for (apathpoint[i] = end; pathpoint1.previous != null; apathpoint[i] = pathpoint1)
|
| 132 | {
|
| 133 | pathpoint1 = pathpoint1.previous;
|
| 134 | --i;
|
| 135 | }
|
| 136 |
|
| 137 | return new PathEntity(apathpoint);
|
| 138 | }
|
| 139 | }
|
| 140 |
|