Advertisement
Admiral_Damage

Untitled

Mar 8th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package eu.tetrabyte.contentmod.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import codechicken.lib.vec.Rotation;
  7. import codechicken.lib.vec.Vector3;
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.util.AxisAlignedBB;
  14. import net.minecraft.util.BlockPos;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraft.util.MathHelper;
  17. import net.minecraft.util.MovingObjectPosition;
  18. import net.minecraft.world.World;
  19.  
  20. public class HookUtil
  21. {  
  22.     private static List<AxisAlignedBB> collidingBoundingBoxes = new ArrayList<AxisAlignedBB>();
  23.    
  24.     public static float MARGIN_OF_ERROR = 1/16f;
  25.    
  26.     private static void addCollidingBoundingBoxes(Entity e, World w, AxisAlignedBB aabb) {
  27.         int i = MathHelper.floor_double(aabb.minX);
  28.         int j = MathHelper.floor_double(aabb.maxX + 1.0D);
  29.         int k = MathHelper.floor_double(aabb.minY);
  30.         int l = MathHelper.floor_double(aabb.maxY + 1.0D);
  31.         int i1 = MathHelper.floor_double(aabb.minZ);
  32.         int j1 = MathHelper.floor_double(aabb.maxZ + 1.0D);
  33.  
  34.         for (int k1 = i; k1 < j; ++k1)
  35.         {
  36.             for (int l1 = i1; l1 < j1; ++l1)
  37.             {
  38.                 if (w.isBlockLoaded(new BlockPos(k1, 64, l1)))
  39.                 {
  40.                     for (int i2 = k - 1; i2 < l; ++i2)
  41.                     {
  42.                         Block block;
  43.  
  44.                         if (k1 >= -30000000 && k1 < 30000000 && l1 >= -30000000 && l1 < 30000000)
  45.                         {
  46.                             block = (Block) w.getBlockState(new BlockPos(k1, i2, l1));
  47.                         }
  48.                         else
  49.                         {
  50.                             block = Blocks.stone;
  51.                         }
  52.  
  53.                         block.addCollisionBoxesToList(w, new BlockPos(k1, i2, l1), block.getDefaultState(), aabb, collidingBoundingBoxes, e);
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.    
  60.     private static ExtendedVector3<MovingObjectPosition> traceAABB(Vector3 start, Vector3 end, AxisAlignedBB aabb) {
  61.         if(aabb == null || start == null || end == null)
  62.             return null;
  63.         MovingObjectPosition mop = aabb.expand(MARGIN_OF_ERROR, MARGIN_OF_ERROR, MARGIN_OF_ERROR).calculateIntercept(start.vec3(), end.vec3());
  64.         if(mop == null)
  65.             return null;
  66.         else
  67.             return ( new ExtendedVector3(mop.hitVec) ).setData(mop);
  68.     }
  69.    
  70.     public static ExtendedVector3<EnumFacing> collisionRayCast(World world, Vector3 start, Vector3 offset, Entity e)
  71.     {
  72.         Vector3 end   = start.copy().add(offset);
  73.  
  74.         double minX = Math.min(start.x, end.x);
  75.         double minY = Math.min(start.y, end.y);
  76.         double minZ = Math.min(start.z, end.z);
  77.        
  78.         double maxX = Math.max(start.x, end.x);
  79.         double maxY = Math.max(start.y, end.y);
  80.         double maxZ = Math.max(start.z, end.z);
  81.        
  82. //      collidingBoundingBoxes.clear();
  83.         List<AxisAlignedBB> scanBBs = new ArrayList<AxisAlignedBB>();
  84.         double magS = offset.magSquared();
  85.         if(magS > 50) {
  86.             double count = Math.ceil( magS/50.0 ); // this is a double so the division below doesn't round
  87.             for(int i = 0; i <= count; i++) {
  88.                 scanBBs.add(
  89.                         AxisAlignedBB.fromBounds(
  90.                                 minX+( offset.x * i/count), minY+( offset.y * i/count), minZ+( offset.z * i/count),
  91.                                 maxX-( offset.x * (count-i)/count), maxY-( offset.y * (count-i)/count), maxZ-( offset.z * (count-i)/count)
  92.                             ).expand(1, 1, 1)
  93.                         );
  94.             }
  95.         } else {
  96.             scanBBs.add(
  97.                     AxisAlignedBB.fromBounds(
  98.                             minX, minY, minZ,
  99.                             maxX, maxY, maxZ
  100.                         ).expand(0.5, 0.5, 0.5)
  101.                 );
  102.         }
  103.         for (AxisAlignedBB aabb : scanBBs)
  104.         {
  105.             collidingBoundingBoxes = new ArrayList<AxisAlignedBB>();
  106.             aabb.expand(MARGIN_OF_ERROR, MARGIN_OF_ERROR, MARGIN_OF_ERROR);
  107.             addCollidingBoundingBoxes(e, world, aabb);
  108.            
  109.             Vector3 shortestHit = null;
  110.             double shortestMagSquared = Double.MAX_VALUE;
  111.             int shortestSide = 0;
  112.             for (int i = 0; i < collidingBoundingBoxes.size(); i++)
  113.             {
  114.                 AxisAlignedBB currentBB = (AxisAlignedBB)collidingBoundingBoxes.get(i);
  115.                 ExtendedVector3<MovingObjectPosition> currentHit = traceAABB(start, end, currentBB);
  116.                 if(currentHit != null)
  117.                 {
  118.                     double currentMagS = currentHit.copy().sub(start).magSquared();
  119.                     if(currentMagS < shortestMagSquared) {
  120.                         shortestHit = currentHit;
  121.                         shortestMagSquared = currentMagS;
  122.                         shortestSide = currentHit.getData().sideHit.getIndex();
  123.                     }
  124.                 }
  125.             }
  126.             if(shortestHit != null) {
  127.                 return new ExtendedVector3<EnumFacing>(shortestHit.sub(start)).setData(EnumFacing.getFront(shortestSide));
  128.             }
  129.         }
  130.         return new ExtendedVector3<EnumFacing>(offset).setData(null);
  131.     }
  132.    
  133.     public static Vector3 getPlayerHookPoint(EntityPlayer player, double partialTicks) {
  134.         return new Vector3(
  135.                 player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTicks,
  136.                 (player.getEntityBoundingBox().maxY+player.getEntityBoundingBox().minY)/2 + (player.posY - player.lastTickPosY) * partialTicks,
  137.                 player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks
  138.                 );
  139.     }
  140.     public static Vector3 getPlayerHookPoint(EntityPlayer player) {
  141.         return getPlayerHookPoint(player, 0);
  142.     }
  143.    
  144.    
  145.    
  146.     static Vector3 Y = new Vector3(0, 1, 0);
  147.     public static Rotation getAlignYRotation(Vector3 along) {
  148.         double rot_angle = Math.acos( along.dotProduct(Y) );
  149.         if( Math.abs(rot_angle) > 1.0/65536 )
  150.         {
  151.             Vector3 rot_axis = along.copy().crossProduct(Y).normalize();
  152.             rot_axis.x *= -1;
  153.             rot_axis.z *= -1;
  154.            
  155.             return new Rotation(rot_angle, rot_axis);
  156.         }
  157.         return null;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement