Advertisement
Guest User

AxisAlignedBB Minecraft Server Class

a guest
May 19th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.96 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class AxisAlignedBB
  7. {
  8.     /** List of bounding boxes (not all necessarily being actively used) */
  9.     private static List boundingBoxes = new ArrayList();
  10.  
  11.     /** Tracks how many bounding boxes are being used */
  12.     private static int numBoundingBoxesInUse = 0;
  13.     public double minX;
  14.     public double minY;
  15.     public double minZ;
  16.     public double maxX;
  17.     public double maxY;
  18.     public double maxZ;
  19.  
  20.     /**
  21.      * Returns a bounding box with the specified bounds. Args: minX, minY, minZ, maxX, maxY, maxZ
  22.      */
  23.     public static AxisAlignedBB getBoundingBox(double par0, double par2, double par4, double par6, double par8, double par10)
  24.     {
  25.         return new AxisAlignedBB(par0, par2, par4, par6, par8, par10);
  26.     }
  27.  
  28.     /**
  29.      * Sets the number of bounding boxes in use from the pool to 0 so they will be reused
  30.      */
  31.     public static void clearBoundingBoxPool()
  32.     {
  33.         numBoundingBoxesInUse = 0;
  34.     }
  35.  
  36.     /**
  37.      * Returns a bounding box with the specified bounds from the pool.  Args: minX, minY, minZ, maxX, maxY, maxZ
  38.      */
  39.     public static AxisAlignedBB getBoundingBoxFromPool(double par0, double par2, double par4, double par6, double par8, double par10)
  40.     {
  41.         if (numBoundingBoxesInUse >= boundingBoxes.size())
  42.         {
  43.             boundingBoxes.add(getBoundingBox(0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D));
  44.         }
  45.  
  46.         return ((AxisAlignedBB)boundingBoxes.get(numBoundingBoxesInUse++)).setBounds(par0, par2, par4, par6, par8, par10);
  47.     }
  48.  
  49.     private AxisAlignedBB(double par1, double par3, double par5, double par7, double par9, double par11)
  50.     {
  51.         minX = par1;
  52.         minY = par3;
  53.         minZ = par5;
  54.         maxX = par7;
  55.         maxY = par9;
  56.         maxZ = par11;
  57.     }
  58.  
  59.     /**
  60.      * Sets the bounds of the bounding box. Args: minX, minY, minZ, maxX, maxY, maxZ
  61.      */
  62.     public AxisAlignedBB setBounds(double par1, double par3, double par5, double par7, double par9, double par11)
  63.     {
  64.         minX = par1;
  65.         minY = par3;
  66.         minZ = par5;
  67.         maxX = par7;
  68.         maxY = par9;
  69.         maxZ = par11;
  70.         return this;
  71.     }
  72.  
  73.     /**
  74.      * Adds the coordinates to the bounding box extending it if the point lies outside the current ranges. Args: x, y, z
  75.      */
  76.     public AxisAlignedBB addCoord(double par1, double par3, double par5)
  77.     {
  78.         double d = minX;
  79.         double d1 = minY;
  80.         double d2 = minZ;
  81.         double d3 = maxX;
  82.         double d4 = maxY;
  83.         double d5 = maxZ;
  84.  
  85.         if (par1 < 0.0D)
  86.         {
  87.             d += par1;
  88.         }
  89.  
  90.         if (par1 > 0.0D)
  91.         {
  92.             d3 += par1;
  93.         }
  94.  
  95.         if (par3 < 0.0D)
  96.         {
  97.             d1 += par3;
  98.         }
  99.  
  100.         if (par3 > 0.0D)
  101.         {
  102.             d4 += par3;
  103.         }
  104.  
  105.         if (par5 < 0.0D)
  106.         {
  107.             d2 += par5;
  108.         }
  109.  
  110.         if (par5 > 0.0D)
  111.         {
  112.             d5 += par5;
  113.         }
  114.  
  115.         return getBoundingBoxFromPool(d, d1, d2, d3, d4, d5);
  116.     }
  117.  
  118.     /**
  119.      * Returns a bounding box expanded by the specified vector (if negative numbers are given it will shrink). Args: x,
  120.      * y, z
  121.      */
  122.     public AxisAlignedBB expand(double par1, double par3, double par5)
  123.     {
  124.         double d = minX - par1;
  125.         double d1 = minY - par3;
  126.         double d2 = minZ - par5;
  127.         double d3 = maxX + par1;
  128.         double d4 = maxY + par3;
  129.         double d5 = maxZ + par5;
  130.         return getBoundingBoxFromPool(d, d1, d2, d3, d4, d5);
  131.     }
  132.  
  133.     /**
  134.      * Returns a bounding box offseted by the specified vector (if negative numbers are given it will shrink). Args: x,
  135.      * y, z
  136.      */
  137.     public AxisAlignedBB getOffsetBoundingBox(double par1, double par3, double par5)
  138.     {
  139.         return getBoundingBoxFromPool(minX + par1, minY + par3, minZ + par5, maxX + par1, maxY + par3, maxZ + par5);
  140.     }
  141.  
  142.     /**
  143.      * if instance and the argument bounding boxes overlap in the Y and Z dimensions, calculate the offset between them
  144.      * in the X dimension.  return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
  145.      * calculated offset.  Otherwise return the calculated offset.
  146.      */
  147.     public double calculateXOffset(AxisAlignedBB par1AxisAlignedBB, double par2)
  148.     {
  149.         if (par1AxisAlignedBB.maxY <= minY || par1AxisAlignedBB.minY >= maxY)
  150.         {
  151.             return par2;
  152.         }
  153.  
  154.         if (par1AxisAlignedBB.maxZ <= minZ || par1AxisAlignedBB.minZ >= maxZ)
  155.         {
  156.             return par2;
  157.         }
  158.  
  159.         if (par2 > 0.0D && par1AxisAlignedBB.maxX <= minX)
  160.         {
  161.             double d = minX - par1AxisAlignedBB.maxX;
  162.  
  163.             if (d < par2)
  164.             {
  165.                 par2 = d;
  166.             }
  167.         }
  168.  
  169.         if (par2 < 0.0D && par1AxisAlignedBB.minX >= maxX)
  170.         {
  171.             double d1 = maxX - par1AxisAlignedBB.minX;
  172.  
  173.             if (d1 > par2)
  174.             {
  175.                 par2 = d1;
  176.             }
  177.         }
  178.  
  179.         return par2;
  180.     }
  181.  
  182.     /**
  183.      * if instance and the argument bounding boxes overlap in the X and Z dimensions, calculate the offset between them
  184.      * in the Y dimension.  return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
  185.      * calculated offset.  Otherwise return the calculated offset.
  186.      */
  187.     public double calculateYOffset(AxisAlignedBB par1AxisAlignedBB, double par2)
  188.     {
  189.         if (par1AxisAlignedBB.maxX <= minX || par1AxisAlignedBB.minX >= maxX)
  190.         {
  191.             return par2;
  192.         }
  193.  
  194.         if (par1AxisAlignedBB.maxZ <= minZ || par1AxisAlignedBB.minZ >= maxZ)
  195.         {
  196.             return par2;
  197.         }
  198.  
  199.         if (par2 > 0.0D && par1AxisAlignedBB.maxY <= minY)
  200.         {
  201.             double d = minY - par1AxisAlignedBB.maxY;
  202.  
  203.             if (d < par2)
  204.             {
  205.                 par2 = d;
  206.             }
  207.         }
  208.  
  209.         if (par2 < 0.0D && par1AxisAlignedBB.minY >= maxY)
  210.         {
  211.             double d1 = maxY - par1AxisAlignedBB.minY;
  212.  
  213.             if (d1 > par2)
  214.             {
  215.                 par2 = d1;
  216.             }
  217.         }
  218.  
  219.         return par2;
  220.     }
  221.  
  222.     /**
  223.      * if instance and the argument bounding boxes overlap in the Y and X dimensions, calculate the offset between them
  224.      * in the Z dimension.  return var2 if the bounding boxes do not overlap or if var2 is closer to 0 then the
  225.      * calculated offset.  Otherwise return the calculated offset.
  226.      */
  227.     public double calculateZOffset(AxisAlignedBB par1AxisAlignedBB, double par2)
  228.     {
  229.         if (par1AxisAlignedBB.maxX <= minX || par1AxisAlignedBB.minX >= maxX)
  230.         {
  231.             return par2;
  232.         }
  233.  
  234.         if (par1AxisAlignedBB.maxY <= minY || par1AxisAlignedBB.minY >= maxY)
  235.         {
  236.             return par2;
  237.         }
  238.  
  239.         if (par2 > 0.0D && par1AxisAlignedBB.maxZ <= minZ)
  240.         {
  241.             double d = minZ - par1AxisAlignedBB.maxZ;
  242.  
  243.             if (d < par2)
  244.             {
  245.                 par2 = d;
  246.             }
  247.         }
  248.  
  249.         if (par2 < 0.0D && par1AxisAlignedBB.minZ >= maxZ)
  250.         {
  251.             double d1 = maxZ - par1AxisAlignedBB.minZ;
  252.  
  253.             if (d1 > par2)
  254.             {
  255.                 par2 = d1;
  256.             }
  257.         }
  258.  
  259.         return par2;
  260.     }
  261.  
  262.     /**
  263.      * Returns whether the given bounding box intersects with this one. Args: axisAlignedBB
  264.      */
  265.     public boolean intersectsWith(AxisAlignedBB par1AxisAlignedBB)
  266.     {
  267.         if (par1AxisAlignedBB.maxX <= minX || par1AxisAlignedBB.minX >= maxX)
  268.         {
  269.             return false;
  270.         }
  271.  
  272.         if (par1AxisAlignedBB.maxY <= minY || par1AxisAlignedBB.minY >= maxY)
  273.         {
  274.             return false;
  275.         }
  276.  
  277.         return par1AxisAlignedBB.maxZ > minZ && par1AxisAlignedBB.minZ < maxZ;
  278.     }
  279.  
  280.     /**
  281.      * Offsets the current bounding box by the specified coordinates. Args: x, y, z
  282.      */
  283.     public AxisAlignedBB offset(double par1, double par3, double par5)
  284.     {
  285.         minX += par1;
  286.         minY += par3;
  287.         minZ += par5;
  288.         maxX += par1;
  289.         maxY += par3;
  290.         maxZ += par5;
  291.         return this;
  292.     }
  293.  
  294.     /**
  295.      * Returns if the supplied Vec3D is completely inside the bounding box
  296.      */
  297.     public boolean isVecInside(Vec3D par1Vec3D)
  298.     {
  299.         if (par1Vec3D.xCoord <= minX || par1Vec3D.xCoord >= maxX)
  300.         {
  301.             return false;
  302.         }
  303.  
  304.         if (par1Vec3D.yCoord <= minY || par1Vec3D.yCoord >= maxY)
  305.         {
  306.             return false;
  307.         }
  308.  
  309.         return par1Vec3D.zCoord > minZ && par1Vec3D.zCoord < maxZ;
  310.     }
  311.  
  312.     /**
  313.      * Returns a bounding box that is inset by the specified amounts
  314.      */
  315.     public AxisAlignedBB contract(double par1, double par3, double par5)
  316.     {
  317.         double d = minX + par1;
  318.         double d1 = minY + par3;
  319.         double d2 = minZ + par5;
  320.         double d3 = maxX - par1;
  321.         double d4 = maxY - par3;
  322.         double d5 = maxZ - par5;
  323.         return getBoundingBoxFromPool(d, d1, d2, d3, d4, d5);
  324.     }
  325.  
  326.     /**
  327.      * Returns a copy of the bounding box.
  328.      */
  329.     public AxisAlignedBB copy()
  330.     {
  331.         return getBoundingBoxFromPool(minX, minY, minZ, maxX, maxY, maxZ);
  332.     }
  333.  
  334.     public MovingObjectPosition calculateIntercept(Vec3D par1Vec3D, Vec3D par2Vec3D)
  335.     {
  336.         Vec3D vec3d = par1Vec3D.getIntermediateWithXValue(par2Vec3D, minX);
  337.         Vec3D vec3d1 = par1Vec3D.getIntermediateWithXValue(par2Vec3D, maxX);
  338.         Vec3D vec3d2 = par1Vec3D.getIntermediateWithYValue(par2Vec3D, minY);
  339.         Vec3D vec3d3 = par1Vec3D.getIntermediateWithYValue(par2Vec3D, maxY);
  340.         Vec3D vec3d4 = par1Vec3D.getIntermediateWithZValue(par2Vec3D, minZ);
  341.         Vec3D vec3d5 = par1Vec3D.getIntermediateWithZValue(par2Vec3D, maxZ);
  342.  
  343.         if (!isVecInYZ(vec3d))
  344.         {
  345.             vec3d = null;
  346.         }
  347.  
  348.         if (!isVecInYZ(vec3d1))
  349.         {
  350.             vec3d1 = null;
  351.         }
  352.  
  353.         if (!isVecInXZ(vec3d2))
  354.         {
  355.             vec3d2 = null;
  356.         }
  357.  
  358.         if (!isVecInXZ(vec3d3))
  359.         {
  360.             vec3d3 = null;
  361.         }
  362.  
  363.         if (!isVecInXY(vec3d4))
  364.         {
  365.             vec3d4 = null;
  366.         }
  367.  
  368.         if (!isVecInXY(vec3d5))
  369.         {
  370.             vec3d5 = null;
  371.         }
  372.  
  373.         Vec3D vec3d6 = null;
  374.  
  375.         if (vec3d != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d) < par1Vec3D.squareDistanceTo(vec3d6)))
  376.         {
  377.             vec3d6 = vec3d;
  378.         }
  379.  
  380.         if (vec3d1 != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d1) < par1Vec3D.squareDistanceTo(vec3d6)))
  381.         {
  382.             vec3d6 = vec3d1;
  383.         }
  384.  
  385.         if (vec3d2 != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d2) < par1Vec3D.squareDistanceTo(vec3d6)))
  386.         {
  387.             vec3d6 = vec3d2;
  388.         }
  389.  
  390.         if (vec3d3 != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d3) < par1Vec3D.squareDistanceTo(vec3d6)))
  391.         {
  392.             vec3d6 = vec3d3;
  393.         }
  394.  
  395.         if (vec3d4 != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d4) < par1Vec3D.squareDistanceTo(vec3d6)))
  396.         {
  397.             vec3d6 = vec3d4;
  398.         }
  399.  
  400.         if (vec3d5 != null && (vec3d6 == null || par1Vec3D.squareDistanceTo(vec3d5) < par1Vec3D.squareDistanceTo(vec3d6)))
  401.         {
  402.             vec3d6 = vec3d5;
  403.         }
  404.  
  405.         if (vec3d6 == null)
  406.         {
  407.             return null;
  408.         }
  409.  
  410.         byte byte0 = -1;
  411.  
  412.         if (vec3d6 == vec3d)
  413.         {
  414.             byte0 = 4;
  415.         }
  416.  
  417.         if (vec3d6 == vec3d1)
  418.         {
  419.             byte0 = 5;
  420.         }
  421.  
  422.         if (vec3d6 == vec3d2)
  423.         {
  424.             byte0 = 0;
  425.         }
  426.  
  427.         if (vec3d6 == vec3d3)
  428.         {
  429.             byte0 = 1;
  430.         }
  431.  
  432.         if (vec3d6 == vec3d4)
  433.         {
  434.             byte0 = 2;
  435.         }
  436.  
  437.         if (vec3d6 == vec3d5)
  438.         {
  439.             byte0 = 3;
  440.         }
  441.  
  442.         return new MovingObjectPosition(0, 0, 0, byte0, vec3d6);
  443.     }
  444.  
  445.     /**
  446.      * Checks if the specified vector is within the YZ dimensions of the bounding box. Args: Vec3D
  447.      */
  448.     private boolean isVecInYZ(Vec3D par1Vec3D)
  449.     {
  450.         if (par1Vec3D == null)
  451.         {
  452.             return false;
  453.         }
  454.         else
  455.         {
  456.             return par1Vec3D.yCoord >= minY && par1Vec3D.yCoord <= maxY && par1Vec3D.zCoord >= minZ && par1Vec3D.zCoord <= maxZ;
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * Checks if the specified vector is within the XZ dimensions of the bounding box. Args: Vec3D
  462.      */
  463.     private boolean isVecInXZ(Vec3D par1Vec3D)
  464.     {
  465.         if (par1Vec3D == null)
  466.         {
  467.             return false;
  468.         }
  469.         else
  470.         {
  471.             return par1Vec3D.xCoord >= minX && par1Vec3D.xCoord <= maxX && par1Vec3D.zCoord >= minZ && par1Vec3D.zCoord <= maxZ;
  472.         }
  473.     }
  474.  
  475.     /**
  476.      * Checks if the specified vector is within the XY dimensions of the bounding box. Args: Vec3D
  477.      */
  478.     private boolean isVecInXY(Vec3D par1Vec3D)
  479.     {
  480.         if (par1Vec3D == null)
  481.         {
  482.             return false;
  483.         }
  484.         else
  485.         {
  486.             return par1Vec3D.xCoord >= minX && par1Vec3D.xCoord <= maxX && par1Vec3D.yCoord >= minY && par1Vec3D.yCoord <= maxY;
  487.         }
  488.     }
  489.  
  490.     /**
  491.      * Sets the bounding box to the same bounds as the bounding box passed in. Args: axisAlignedBB
  492.      */
  493.     public void setBB(AxisAlignedBB par1AxisAlignedBB)
  494.     {
  495.         minX = par1AxisAlignedBB.minX;
  496.         minY = par1AxisAlignedBB.minY;
  497.         minZ = par1AxisAlignedBB.minZ;
  498.         maxX = par1AxisAlignedBB.maxX;
  499.         maxY = par1AxisAlignedBB.maxY;
  500.         maxZ = par1AxisAlignedBB.maxZ;
  501.     }
  502.  
  503.     public String toString()
  504.     {
  505.         return (new StringBuilder()).append("box[").append(minX).append(", ").append(minY).append(", ").append(minZ).append(" -> ").append(maxX).append(", ").append(maxY).append(", ").append(maxZ).append("]").toString();
  506.     }
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement