    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[] var2 = new PathPoint[this.count << 1];
    025                System.arraycopy(this.pathPoints, 0, var2, 0, this.count);
    026                this.pathPoints = var2;
    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 var1 = 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        var1.index = -1;
    059        return var1;
    060    }
    061
    062    /**
    063     * Changes the provided point's distance to target
    064     */
    065    public void changeDistance(PathPoint par1PathPoint, float par2)
    066    {
    067        float var3 = par1PathPoint.distanceToTarget;
    068        par1PathPoint.distanceToTarget = par2;
    069
    070        if (par2 < var3)
    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 var2 = this.pathPoints[par1];
    086        int var4;
    087
    088        for (float var3 = var2.distanceToTarget; par1 > 0; par1 = var4)
    089        {
    090            var4 = par1 - 1 >> 1;
    091            PathPoint var5 = this.pathPoints[var4];
    092
    093            if (var3 >= var5.distanceToTarget)
    094            {
    095                break;
    096            }
    097
    098            this.pathPoints[par1] = var5;
    099            var5.index = par1;
    100        }
    101
    102        this.pathPoints[par1] = var2;
    103        var2.index = par1;
    104    }
    105
    106    /**
    107     * Sorts a point to the right
    108     */
    109    private void sortForward(int par1)
    110    {
    111        PathPoint var2 = this.pathPoints[par1];
    112        float var3 = var2.distanceToTarget;
    113
    114        while (true)
    115        {
    116            int var4 = 1 + (par1 << 1);
    117            int var5 = var4 + 1;
    118
    119            if (var4 >= this.count)
    120            {
    121                break;
    122            }
    123
    124            PathPoint var6 = this.pathPoints[var4];
    125            float var7 = var6.distanceToTarget;
    126            PathPoint var8;
    127            float var9;
    128
    129            if (var5 >= this.count)
    130            {
    131                var8 = null;
    132                var9 = Float.POSITIVE_INFINITY;
    133            }
    134            else
    135            {
    136                var8 = this.pathPoints[var5];
    137                var9 = var8.distanceToTarget;
    138            }
    139
    140            if (var7 < var9)
    141            {
    142                if (var7 >= var3)
    143                {
    144                    break;
    145                }
    146
    147                this.pathPoints[par1] = var6;
    148                var6.index = par1;
    149                par1 = var4;
    150            }
    151            else
    152            {
    153                if (var9 >= var3)
    154                {
    155                    break;
    156                }
    157
    158                this.pathPoints[par1] = var8;
    159                var8.index = par1;
    160                par1 = var5;
    161            }
    162        }
    163
    164        this.pathPoints[par1] = var2;
    165        var2.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}