001package net.minecraft.pathfinding;
002
003public class Path
004{
005    /** Contains the points in this path */
006    private PathPoint[] pathPoints = new PathPoint[1024];
007
008    /** The number of points in this path */
009    private int count = 0;
010
011    /**
012     * Adds a point to the path
013     */
014    public PathPoint addPoint(PathPoint par1PathPoint)
015    {
016        if (par1PathPoint.index >= 0)
017        {
018            throw new IllegalStateException("OW KNOWS!");
019        }
020        else
021        {
022            if (this.count == this.pathPoints.length)
023            {
024                PathPoint[] apathpoint = new PathPoint[this.count << 1];
025                System.arraycopy(this.pathPoints, 0, apathpoint, 0, this.count);
026                this.pathPoints = apathpoint;
027            }
028
029            this.pathPoints[this.count] = par1PathPoint;
030            par1PathPoint.index = this.count;
031            this.sortBack(this.count++);
032            return par1PathPoint;
033        }
034    }
035
036    /**
037     * Clears the path
038     */
039    public void clearPath()
040    {
041        this.count = 0;
042    }
043
044    /**
045     * Returns and removes the first point in the path
046     */
047    public PathPoint dequeue()
048    {
049        PathPoint pathpoint = this.pathPoints[0];
050        this.pathPoints[0] = this.pathPoints[--this.count];
051        this.pathPoints[this.count] = null;
052
053        if (this.count > 0)
054        {
055            this.sortForward(0);
056        }
057
058        pathpoint.index = -1;
059        return pathpoint;
060    }
061
062    /**
063     * Changes the provided point's distance to target
064     */
065    public void changeDistance(PathPoint par1PathPoint, float par2)
066    {
067        float f1 = par1PathPoint.distanceToTarget;
068        par1PathPoint.distanceToTarget = par2;
069
070        if (par2 < f1)
071        {
072            this.sortBack(par1PathPoint.index);
073        }
074        else
075        {
076            this.sortForward(par1PathPoint.index);
077        }
078    }
079
080    /**
081     * Sorts a point to the left
082     */
083    private void sortBack(int par1)
084    {
085        PathPoint pathpoint = this.pathPoints[par1];
086        int j;
087
088        for (float f = pathpoint.distanceToTarget; par1 > 0; par1 = j)
089        {
090            j = par1 - 1 >> 1;
091            PathPoint pathpoint1 = this.pathPoints[j];
092
093            if (f >= pathpoint1.distanceToTarget)
094            {
095                break;
096            }
097
098            this.pathPoints[par1] = pathpoint1;
099            pathpoint1.index = par1;
100        }
101
102        this.pathPoints[par1] = pathpoint;
103        pathpoint.index = par1;
104    }
105
106    /**
107     * Sorts a point to the right
108     */
109    private void sortForward(int par1)
110    {
111        PathPoint pathpoint = this.pathPoints[par1];
112        float f = pathpoint.distanceToTarget;
113
114        while (true)
115        {
116            int j = 1 + (par1 << 1);
117            int k = j + 1;
118
119            if (j >= this.count)
120            {
121                break;
122            }
123
124            PathPoint pathpoint1 = this.pathPoints[j];
125            float f1 = pathpoint1.distanceToTarget;
126            PathPoint pathpoint2;
127            float f2;
128
129            if (k >= this.count)
130            {
131                pathpoint2 = null;
132                f2 = Float.POSITIVE_INFINITY;
133            }
134            else
135            {
136                pathpoint2 = this.pathPoints[k];
137                f2 = pathpoint2.distanceToTarget;
138            }
139
140            if (f1 < f2)
141            {
142                if (f1 >= f)
143                {
144                    break;
145                }
146
147                this.pathPoints[par1] = pathpoint1;
148                pathpoint1.index = par1;
149                par1 = j;
150            }
151            else
152            {
153                if (f2 >= f)
154                {
155                    break;
156                }
157
158                this.pathPoints[par1] = pathpoint2;
159                pathpoint2.index = par1;
160                par1 = k;
161            }
162        }
163
164        this.pathPoints[par1] = pathpoint;
165        pathpoint.index = par1;
166    }
167
168    /**
169     * Returns true if this path contains no points
170     */
171    public boolean isPathEmpty()
172    {
173        return this.count == 0;
174    }
175}