--[[
	MOD/lua/bonedebug.lua
	[G-moder]NanoCat :D | STEAM_0:1:42964759 <176.36.12.19:27005> | [04-11-13 07:11:18PM]
	===BadFile===
]]

local bonedebug = CreateConVar( 'bonedebug', 1 )
local mask = bit.band( MASK_SHOT, bit.bnot( CONTENTS_WINDOW ) )

local SetTextPos = surface.SetTextPos
local DrawText = surface.DrawText
local DrawRect = surface.DrawRect

hook.Add( 'HUDPaint', 'bonedebug', function()
	if not bonedebug:GetBool() then return end
	
	local ent = LocalPlayer():GetEyeTraceNoCursor( mask ).Entity
	if not IsValid( ent ) then return end
	if not ent:GetBoneCount() then return end
	
	surface.SetFont( 'DermaDefault' )
	surface.SetTextColor( color_white )
	surface.SetDrawColor( color_white )
	
	for i = 0, ent:GetBoneCount() - 1 do
		local name = ent:GetBoneName( i )
		if name == '__INVALIDBONE__' then continue end
		
		local pos = ent:GetBonePosition( i )
		
		if pos then
			pos = pos:ToScreen()
			
			DrawRect( pos.x - 2, pos.y - 2, 4, 4 )
			
			SetTextPos( pos.x, pos.y )
			DrawText( name )
		end
	end
end )

concommand.Add( 'buildbones', function( ply )
	local ent = LocalPlayer():GetEyeTraceNoCursor( mask ).Entity
	if not IsValid( ent ) then return end
	
	local lines = {}
	for i = 0, ent:GetBoneCount() - 1 do
		local name = ent:GetBoneName( i )
		local bone = ent:GetPhysicsObjectNum( i )
		if not IsValid( bone ) or name == '__INVALIDBONE__' then continue end
		
		local vec, ang = bone:GetPos(), bone:GetAngles()
			vec = ent:WorldToLocal( vec )
			ang = ent:WorldToLocalAngles( ang )
			
			vec = { math.Round( vec.x ), math.Round( vec.y ), math.Round( vec.z ) }
			ang = { math.Round( ang.p ), math.Round( ang.y ), math.Round( ang.r ) }
		
		table.insert( lines, '\t[ \'' .. name .. '\' ] = { Vector( ' .. table.concat( vec, ', ' ) .. ' ), Angle( ' .. table.concat( ang, ', ' ) .. ' ) }' )
	end
	
	lines = '{\n' .. table.concat( lines, ',\n' ) .. '\n}'
	SetClipboardText( lines )
end )