
AnimConvert = { }

AnimConvert["gmod_tool"] = "PISTOL";
AnimConvert["weapon_physgun"] = "RIFLE";
AnimConvert["weapon_physcannon"] = "RIFLE";

function FindCorrectAnimTable( model )

	for k, v in pairs( TS.AnimTables ) do
	
		if( table.HasValue( v.Models, string.lower( model ) ) ) then
			return TS.AnimTables[k];
		end
	
	end
	
	return TS.AnimTables[1];

end

-- Anim could be:
-- PLAYER_RELOAD 
-- PLAYER_JUMP 
-- PLAYER_ATTACK1

function GM:SetPlayerAnimation( ply, anim )

	local act = "";
	
	local skipanimation = false;
	local activeweap = ply:GetActiveWeapon();

	if( not ply:GetTable().ForcedAnimationMode and not skipanimation ) then
	
		if( ply:OnGround() and ply:WaterLevel() < 4 ) then
	
			local cansprint = true;
		
			if( ply:Crouching() ) then
				act = "CROUCH";
				cansprint = false;
			else
				act = "STAND";
			end
			
			
			if( activeweap:IsValid() ) then

				if( activeweap:GetTable().TS2HoldType ) then
				
					act = act .. "_" .. activeweap:GetTable().TS2HoldType;
				
				elseif( AnimConvert[activeweap:GetClass()] ) then
				
					act = act .. "_" .. AnimConvert[activeweap:GetClass()];
				
				end
			
			end
			
			if( not ply:GetPlayerHolstered() ) then
			
				act = act .. "_AIM";
			
			end
			
			local vel = ply:GetVelocity():Length();

			if( vel == 0 ) then
			
				act = act .. "_idle";
			
			elseif( vel > 130 and cansprint ) then
			
				act = act .. "_run";
			
			else
			
				act = act .. "_walk";
			
			end
			
		else
		
			act = "jump";
		
		end
		
		local AnimTable = { }
		
		if( ply:GetTable().AnimLastModel ~= ply:GetModel() ) then
		
			ply:GetTable().AnimTable = FindCorrectAnimTable( ply:GetModel() );
			ply:GetTable().AnimLastModel = ply:GetModel();
			
		end
		
		AnimTable = ply:GetTable().AnimTable;

		act = AnimTable.Anim[act] or 1;

	else
	
		act = ply:GetTable().ForcedAnimation;
	
	end

	local seq;

	if( type( act ) == "string" ) then
		seq = ply:LookupSequence( act );
	else
		seq = ply:SelectWeightedSequence( act );
	end
	
	--If we're already playing this sequence, let's not restart it!
	if( ply:GetSequence() == seq ) then return; end

	ply:SetPlaybackRate( 1 );
	ply:ResetSequence( seq );
	ply:SetCycle( 0 );

end


function CCToggleHolster( ply, cmd, arg )

	local weap = ply:GetActiveWeapon();
	
	if( weap:IsValid() ) then
	
		local class = weap:GetClass();

		if( class == "weapon_physcannon" or
			class == "weapon_physgun" or
			class == "gmod_tool" or
			class == "ts2_keys" or
			class == "ts2_zipties" ) then return; end
	
		
		if( weap:GetTable().HolsterToggle ) then
			weap:GetTable().HolsterToggle( weap );
		end
		
	
	end

	ply:SetPlayerHolstered( !ply:GetPlayerHolstered() );
	
	--ply:DrawViewModel( true );

end
concommand.Add( "eng_toggleholster", CCToggleHolster );