
CreateClientConVar( "rp_headbob", "1", true, false );

HeadBobX = 0;
HeadBobY = 0;
HeadBobAng = 0;

local MapViewData = {
	
	{ "gm_galactic_rc1", Vector( 12178.543945, 4013.306885, -196.641907 ), Angle( -39.160004, -123.439949, 0 ) },
	{ "rp_sst_bahamut_v1", Vector( 3587.781250, -697.750000, 1255.906250 ), Angle( 6.379993, 155.540070, 0.000000 ) },
	{ "rp_sst_bahamut_v2", Vector( 3587.781250, -697.750000, 1255.906250 ), Angle( 6.379993, 155.540070, 0.000000 ) },
	{ "gm_megadesertv3", Vector( -1570.626587, -2194.280518, 567.312195 ), Angle( -6.306620, 64.015396, 0 ) },
	{ "gm_forest", Vector( 1038.357788, 506.454132, -65.254105 ), Angle( -7.985990, 119.444801, 0 ) },
	{ "gm_forest2008", Vector( 47.541473, 1969.254883, 514.662109 ), Angle( 17.629330, -42.657684, 0 ) },
	{ "gm_winterforest", Vector( 2088.4289550781, -258.41708374023, 56.658195495605 ), Angle( 5.734677, -159.068100, 0 ) },
	{ "as_hot_cn", Vector( 1930.8430175781, -985.67858886719, -832.27905273438 ), Angle( -53.638660, -59.406261, 0 ) },
	{ "dm_cn_complex_beta", Vector( 1543.3482666016, 289.1155090332, -1864.1473388672 ), Angle( -89, -179.476059, 0 ) }
	
};
 
function GM:CalcView( ply, origin, angles, fov )

	if( ValidEntity( LocalPlayer() ) ) then
		
		if( CharacterCreatePanel ) then
			
			for k, v in pairs( MapViewData ) do
			
				if( string.lower( game.GetMap() ) == v[1] ) then
					
					local view = { }
					
					view.origin = v[2];
					view.angles = v[3];
					
					return view;
					
				end
				
			end
		
		end
		
		local view 	= { }
		view.origin = origin;
		view.angles = angles;
		view.fov 	= fov;
		
		if( HeadBobAng > 360 ) then HeadBobAng = 0; end
		
		if( ply:GetNWInt( "lyingdown" ) == 1 ) then
			
			local rag = ply:GetRagdoll();
			
			if( rag and rag:IsValid() ) then
			
				eyes = rag:GetAttachment( rag:LookupAttachment( "eyes" ) );
				
				view.origin = eyes.Pos;
				view.angles = eyes.Ang;
					
				return view;
				
			end
			
		end
		
		if( ply:GetNWInt( "viewTargEnt" ) > 0 ) then
			
			local ent = ents.GetByIndex( ply:GetNWInt( "viewTargEnt" ) );
			view.origin = ent:GetPos() + ent:GetForward() * 250;
			view.angles = ent:GetAngles() + Angle( 180, 0, 0 );
			
			return view;
			
		end
		--[[
		if( ply:GetNWInt( "targViewEntity" ) > 0 ) then
			
			local ent = ents.GetByIndex( ply:GetNWInt( "targViewEntity" ) );
			
			if( ent and ent:IsValid() ) then
				
				view.origin = ent:EyePos();
				view.angles = ent:EyeAngles();
				
				return view;
				
			else
				
				ply:SetNWInt( "targViewEntity", 0 );
				
			end
			
		end
--]]
		if( CrackDrug and CurTime() - DrugTime > 4 ) then
		
			CrackDrugAng = CrackDrugAng or Angle( 0, 0, 0 );		
			CrackDrugAng.y = CrackDrugAng.y + 180 * FrameTime();
			
			view.angles = ply:GetAimVector():Angle() + CrackDrugAng;
			
		end
		
		if( LocalPlayer():GetInfo( "rp_headbob" ) == "1" ) then
			
			if( ( ply:KeyDown( IN_FORWARD ) or
				ply:KeyDown( IN_BACK ) or
				ply:KeyDown( IN_MOVERIGHT ) or
				ply:KeyDown( IN_MOVELEFT ) ) and ply:IsOnGround() ) then
				
				if( ply:GetVelocity():Length() > 150 ) then
					
					HeadBobAng = HeadBobAng + 10 * FrameTime();
					
					view.angles.pitch = view.angles.pitch + math.sin( HeadBobAng ) * .5;
					view.angles.yaw = view.angles.yaw + math.cos( HeadBobAng ) * .2;
							
				else
				
					HeadBobAng = HeadBobAng + 6 * FrameTime();
					
					view.angles.pitch = view.angles.pitch + math.sin( HeadBobAng ) * .5;
					view.angles.yaw = view.angles.yaw + math.cos( HeadBobAng ) * .3;
					
				end
				
			end
			
			view.angles.pitch = view.angles.pitch + math.sin( HeadBobAng );
			view.angles.yaw = view.angles.yaw + math.cos( HeadBobAng );

		end
		
		local wep = ply:GetActiveWeapon();
		
		if( ValidEntity( wep ) ) then 
			
			local func = wep.GetViewModelPosition;
			if( func ) then
				view.vm_origin, view.vm_angles = func( wep, origin * 1, angles * 1 );
			end
		 
		end
		
		return view;
		
  	end
	
end
