package com.teamderpy.shouldersurfing.math;

import java.util.List;

import com.teamderpy.shouldersurfing.ShoulderSettings;
import com.teamderpy.shouldersurfing.renderer.ShoulderRenderBin;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

/**
 * @author Joshua Powers <jsh.powers@yahoo.com>
 * @version 1.1
 * @since 2013-01-12
 */
@SideOnly(Side.CLIENT)
public final class RayTracer
{
	public static void traceFromEyes(final float tick)
	{
		ShoulderRenderBin.projectedVector = null;
		
		if(Minecraft.getMinecraft().getRenderViewEntity() != null)
		{
			if(Minecraft.getMinecraft().theWorld != null)
			{
				if(Minecraft.getMinecraft().gameSettings.thirdPersonView == 1)
				{
					double playerReach = 1D;
					
					if(ShoulderSettings.USE_CUSTOM_RAYTRACE_DISTANCE)
					{
						playerReach = ShoulderSettings.RAYTRACE_DISTANCE;
					}
					else
					{
						playerReach = (double) Minecraft.getMinecraft().playerController.getBlockReachDistance();
					}
					
					// block collision
					RayTraceResult omo = Minecraft.getMinecraft().getRenderViewEntity().rayTrace(playerReach, tick);
					double blockDist = 0;
					
					if(omo != null)
					{
						ShoulderRenderBin.rayTraceHit = omo.hitVec;
						blockDist = omo.hitVec.distanceTo(new Vec3d(
								Minecraft.getMinecraft().getRenderViewEntity().posX,
								Minecraft.getMinecraft().getRenderViewEntity().posY,
								Minecraft.getMinecraft().getRenderViewEntity().posZ));

						
//						System.out.println("block dist: " + blockDist);
						if(blockDist <= (double) Minecraft.getMinecraft().playerController.getBlockReachDistance())
						{
							ShoulderRenderBin.rayTraceInReach = true;
						}
						else
						{
							ShoulderRenderBin.rayTraceInReach = false;
						}
					}
					else
					{
						ShoulderRenderBin.rayTraceHit = null;
					}
					
					// entity collision
					Vec3d renderViewPos = Minecraft.getMinecraft().getRenderViewEntity().getPositionEyes(tick);
					
					Vec3d sightVector = Minecraft.getMinecraft().getRenderViewEntity().getLook(tick);
					Vec3d sightRay = renderViewPos.addVector(sightVector.xCoord * playerReach - 5, sightVector.yCoord * playerReach, sightVector.zCoord * playerReach);
					
//					System.out.println(sightVector);
					
//					System.out.println(renderViewPos + " " + sightVector + " " + sightRay);
					
					List entityList = Minecraft.getMinecraft().theWorld.getEntitiesWithinAABBExcludingEntity(Minecraft.getMinecraft().getRenderViewEntity(), Minecraft.getMinecraft().getRenderViewEntity().getEntityBoundingBox().addCoord(sightVector.xCoord * playerReach, sightVector.yCoord * playerReach, sightVector.zCoord * playerReach).expand(1.0D, 1.0D, 1.0D));
					
					for(int i = 0; i < entityList.size(); ++i)
					{
						Entity ent = (Entity) entityList.get(i);
						
						if(ent.canBeCollidedWith())
						{
							float collisionSize = ent.getCollisionBorderSize();
							
							AxisAlignedBB aabb = ent.getEntityBoundingBox().expand((double) collisionSize, (double) collisionSize, (double) collisionSize);
							RayTraceResult potentialIntercept = aabb.calculateIntercept(renderViewPos, sightRay);
							
							if(potentialIntercept != null)
							{
								double entityDist = potentialIntercept.hitVec.distanceTo(new Vec3d(Minecraft.getMinecraft().getRenderViewEntity().posX, Minecraft.getMinecraft().getRenderViewEntity().posY, Minecraft.getMinecraft().getRenderViewEntity().posZ));
								
								if(entityDist < blockDist)
								{
									ShoulderRenderBin.rayTraceHit = potentialIntercept.hitVec;
									
									// System.out.println("entity dist: " +
									// entityDist);
									if(entityDist <= (double) Minecraft.getMinecraft().playerController.getBlockReachDistance())
									{
										ShoulderRenderBin.rayTraceInReach = true;
									}
									else
									{
										ShoulderRenderBin.rayTraceInReach = false;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

@@ -829,28 +829,25 @@
 Minecraft().
-theW
+w
 orld != null
@@ -1500,25 +1500,16 @@
 w Vec3d(
-%0A%09%09%09%09%09%09%09%09
 Minecraf
@@ -1552,25 +1552,17 @@
 ().posX,
-%0A%09%09%09%09%09%09%09%09
+ 
 Minecraf
@@ -1605,25 +1605,17 @@
 ().posY,
-%0A%09%09%09%09%09%09%09%09
+ 
 Minecraf
@@ -1662,23 +1662,16 @@
 osZ));%0A%0A
-%09%09%09%09%09%09%0A
 //%09%09%09%09%09%09
@@ -2596,12 +2596,9 @@
 t().
-theW
+w
 orld

Update RayTracer.java