--[[
	autorun/client/oubhack.lua
	(that crazy lumbee) Skezixx | (STEAM_0:0:35313849)
	===DStream===
]]

/******************************************************************************
 .d88888b.  888     888 888888b.   888    888        d8888  .d8888b.  888    d8P  
d88P" "Y88b 888     888 888  "88b  888    888       d88888 d88P  Y88b 888   d8P   
888     888 888     888 888  .88P  888    888      d88P888 888    888 888  d8P    
888     888 888     888 8888888K.  8888888888     d88P 888 888        888d88K     
888     888 888     888 888  "Y88b 888    888    d88P  888 888        8888888b    
888     888 888     888 888    888 888    888   d88P   888 888    888 888  Y88b   
Y88b. .d88P Y88b. .d88P 888   d88P 888    888  d8888888888 Y88b  d88P 888   Y88b  
 "Y88888P"   "Y88888P"  8888888P"  888    888 d88P     888  "Y8888P"  888    Y88b 
 By Oubliete and UnknownAlias
*******************************************************************************/
-- Main

local concommand = concommand
local draw = draw
local render = render
local concommand = concommand
local cvars = cvars
local debug = debug
local ents = ents
local file = file
local hook = hook
local math = math
local spawnmenu = spawnmenu
local string = string
local surface = surface
local table = table
local timer = timer
local util = util
local vgui = vgui

local Angle = Angle
local CreateClientConVar = CreateClientConVar
local CurTime = CurTime
local ErrorNoHalt = ErrorNoHalt
local FrameTime = FrameTime
local GetConVarString = GetConVarString
local GetViewEntity = GetViewEntity
local include = include
local ipairs = ipairs
local LocalPlayer = LocalPlayer
local pairs = pairs
local pcall = pcall
local print = print
local RunConsoleCommand = RunConsoleCommand
local ScrH = ScrH
local ScrW = ScrW
local tonumber = tonumber
local type = type
local unpack = unpack
local ValidEntity = ValidEntity
local Vector = Vector
local _G = _G

oubhack = {}

include("oubhack_lib.lua") -- Include relevante libraries.
include("oubhack_derma.lua")
include("oubhack_skin.lua")
include("vgui/oh_settings_tab_button.lua")
include("vgui/oh_settings_tab.lua")
include("vgui/oh_DToggle.lua")

oubhack.aimbot_on = true;
oubhack.xray_on = true;
oubhack.triggerbot_on = false
oubhack.wallhack_on = true;

oubhack.friends = {}; -- player objests
oubhack.friend_ids = {} -- friends steam ID's

oubhack.target_npcs = true;
oubhack.target_bots = true;
oubhack.ingore_admins = false;
oubhack.ingore_super_admins = true;
oubhack.spam_on = false;

oubhack.attacking = false;
oubhack.speedhacking = false;
oubhack.maxtracedist = 500000;
oubhack.notraceweapons = {"weapon_phygsun","gmod_tool","weapon_physcannon"}

oubhack.weapon_cone = 0;
oubhack.weapon_recoil = 0;
oubhack.ang = Angle(0,0,0)
concommand.concommands = {}



-- Turn things on and off

function oubhack:spam_toggle_on(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.spam_on = true;
end
concommand.Add("+oh_spam",function(ply,cmd,args) oubhack:spam_toggle_on(ply,cmd,args) end)

function oubhack:spam_toggle_off(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.spam_on = false;
end
concommand.Add("-oh_spam",function(ply,cmd,args) oubhack:spam_toggle_off(ply,cmd,args) end)


----------------

function oubhack:aimbot_toggle_on(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.aimbot_on = true;
end
concommand.Add("+oh_aimbot",function(ply,cmd,args) oubhack:aimbot_toggle_on(ply,cmd,args) end)

function oubhack:aimbot_toggle_off(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.aimbot_on = false;
end
concommand.Add("-oh_aimbot",function(ply,cmd,args) oubhack:aimbot_toggle_off(ply,cmd,args) end)


----------------

function oubhack:trigger_bot_toggle_on(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.triggerbot_on = true;
end
concommand.Add("+oh_triggerbot",function(ply,cmd,args) oubhack:trigger_bot_toggle_on(ply,cmd,args) end)

function oubhack:trigger_bot_toggle_off(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.triggerbot_on = false;
end
concommand.Add("-oh_triggerbot",function(ply,cmd,args) oubhack:trigger_bot_toggle_off(ply,cmd,args) end)

----------------

function oubhack:xray_toggle_on(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.xray_on = true;
end
concommand.Add("+oh_xray",function(ply,cmd,args) oubhack:xray_toggle_on(ply,cmd,args) end)

function oubhack:xray_toggle_off(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.xray_on = false;
end
concommand.Add("-oh_xray",function(ply,cmd,args) oubhack:xray_toggle_off(ply,cmd,args) end)

----------------

function oubhack:wallhack_toggle_on(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.wallhack_on = true;
end
concommand.Add("+oh_wallhack",function(ply,cmd,args) oubhack:wallhack_toggle_on(ply,cmd,args) end)

function oubhack:wallhack_toggle_off(ply,cmd,args)
	if not ply == LocalPlayer() then return false end
	self.wallhack_on = false;
end
concommand.Add("-oh_wallhack",function(ply,cmd,args) oubhack:wallhack_toggle_off(ply,cmd,args) end)

----------------------------------------------------------------------------------------------------------------------------------------------------------------|


function oubhack:CanTrace()
	if !LocalPlayer():Alive() or !LocalPlayer():GetActiveWeapon():IsValid() then return false end;
	return !table.HasValue(self.notraceweapons,LocalPlayer():GetActiveWeapon():GetClass()) and !(LocalPlayer():GetActiveWeapon():Clip1()<=0) 
end

function oubhack:Aimbot() -- Main aimbot function
	
	local ply = LocalPlayer()
	local targ = self:GetBestTarget()
	if targ == ply then  
		--RunConsoleCommand("-attack")
		self.ang = Angle(0,0,0)
	elseif self:CanTrace() then
		if not ply:GetActiveWeapon() then return false end
		if not targ then return false end
		
		local id = targ:LookupBone("ValveBiped.Bip01_Head1")
		local pos = targ:GetBonePosition(id)
		local wep = ply:GetActiveWeapon():GetClass()
		local comp = 45
		
		self.targ = targ
		if not targ.lastpos then targ.lastpos = pos end
		if not self.lastpos then self.lastpos = LocalPlayer():GetPos() end
		local true_pos = pos - (targ.lastpos-pos) + (self.lastpos-LocalPlayer():GetPos())
		local targ_ang = (true_pos-ply:GetShootPos()):Angle()
		
		targ_ang.r = 0

		
		local ang = LocalPlayer():EyeAngles()
		ang.r = 0;
		ang.p = math.ApproachAngle(ang.p,targ_ang.p,(10.5/GetConVarNumber("aimbot_speed")*(ang.p-targ_ang.p)))
		ang.y = math.ApproachAngle(ang.y,targ_ang.y,(10.5/GetConVarNumber("aimbot_speed")*(ang.y-targ_ang.y)))
		self.ang = ang
		targ.lastpos = pos;
		self.lastpos = LocalPlayer():GetPos();
	else
		self.ang = Angle(0,0,0)
	end
	
end
hook.Add("Think","AimbotThink",function() if oubhack.aimbot_on then oubhack:Aimbot() end end,-10) -- Runs if oubhack.aimbot_on is true.

function oubhack:WallHack() -- Tick
	local targ = oubhack:GetBestTarget()
	local tbl = self:GetTargets()
	local friends = self:GetFriends()
	table.Add(tbl,friends)
	for k,v in pairs(tbl) do
			if ValidEntity(v) then
				local colour = Color(255,255,255,255)
				
				if self:IsFriend(v) then -- He's a friend, he's green.
					colour = Color(0,255,0,255)
				elseif !table.HasValue(self.vis_targs,v) then -- He's a target, but he's not visible, make it yellow.
					colour = Color(255,255,0,255)
				else -- OMG KILL IT!!
					colour = Color(255,0,0,255) 
				end
					
				
				local centre = v:LocalToWorld(v:OBBCenter())
				local min,max = v:WorldSpaceAABB()
				local avg = max-min
				
				local front = v:GetForward()*(avg.y/2)
				local right = v:GetRight()*(avg.x/2)
				local top = v:GetUp()*(avg.z/2)
				local back = (v:GetForward()*-1)*(avg.y/2)
				local left = (v:GetRight()*-1)*(avg.x/2)
				local bottom = (v:GetUp()*-1)*(avg.z/2)
				
				local FRT = (centre + front + right + top):ToScreen()
				local BLB = (centre + back + left + bottom):ToScreen()
				local FLT = (centre + front + left + top):ToScreen()
				local BRT = (centre + back + right + top):ToScreen()
				local BLT = (centre + back + left + top):ToScreen()
				local FRB = (centre + front + right + bottom):ToScreen()
				local FLB = (centre + front + left + bottom):ToScreen()
				local BRB = (centre + back + right + bottom):ToScreen()
				
				local x_max = math.max(FRT.x,BLB.x,FLT.x,BRT.x,BLT.x,FRB.x,FLB.x,BRB.x)
				local x_min = math.min(FRT.x,BLB.x,FLT.x,BRT.x,BLT.x,FRB.x,FLB.x,BRB.x)
				local y_max = math.max(FRT.y,BLB.y,FLT.y,BRT.y,BLT.y,FRB.y,FLB.y,BRB.y)
				local y_min = math.min(FRT.y,BLB.y,FLT.y,BRT.y,BLT.y,FRB.y,FLB.y,BRB.y)
				
			
				surface.SetDrawColor(colour)
				surface.DrawLine(x_max,y_max,x_max,y_min)
				surface.DrawLine(x_max,y_min,x_min,y_min)
				surface.DrawLine(x_min,y_min,x_min,y_max)
				surface.DrawLine(x_min,y_max,x_max,y_max)
				
				draw.SimpleText(v:Nick().." -  HP: "..v:Health(),"coolvetica",(x_max-50),y_min,colour,TEXT_ALIGN_CENTER)
			end
	end

end
hook.Add("HUDPaint","AimbotWallHackPaint",function() if oubhack.wallhack_on then oubhack:WallHack() end end)

function oubhack:XRay()
	for k,v in pairs(ents.GetAll()) do
		cam.Start3D( EyePos(), EyeAngles()); 
			if v:IsValid() and (v!=LocalPlayer()) then
				if ((v:IsPlayer() and v:Alive()) or v:IsWeapon() or v:IsNPC() or v:IsVehicle() or v:GetClass() == "prop_physics") then
					cam.IgnoreZ( true );

						render.ResetModelLighting( 1, 1, 1 );
						render.SetColorModulation( 1, 1, 1 );
						
						v:DrawModel();
						
						render.SuppressEngineLighting( false );
					cam.IgnoreZ( false );
				end
			end
		cam.End3D(); 
	end

end
hook.Add("HUDPaint","AimbotX-RayPaint",function() if oubhack.xray_on then oubhack:XRay() end end)

function oubhack:GetBestTarget() -- EDIT: Now constantly checks for the best target.
	local targ = LocalPlayer();
	local bone = "ValveBiped.Bip01_Head1"
	self.vis_targs = {};
	for k,v in pairs(self:GetTargets()) do
		local trace = {};
		local boneid = v:LookupBone(bone);
		local vehicle = LocalPlayer():GetVehicle() or LocalPlayer();
		trace.start = EyePos();
		trace.endpos = v:GetBonePosition(boneid);
		trace.filter = {LocalPlayer(),vehicle};
		trace.mask = MASK_SHOT;
		
		local traceline = util.TraceLine(trace);
		local ent = traceline.Entity
		
		if table.HasValue(self:GetTargets(),ent) then
			self.vis_targs[#self.vis_targs+1] = ent;
		end
	end
	
	for k,v in pairs(self.vis_targs) do
		local pos = v:GetPos();
		local closest = {nil, nil}
			if !closest[1] or (pos:Distance(v:GetPos()) < closest[1]) then
				closest = {pos:Distance(v:GetPos()), v};
			end
		targ = closest[2] or nil;
	end
	
	return targ;
end

function oubhack:NoRecoil()
	local ply = LocalPlayer();
	local wep = ply:GetActiveWeapon();
	if !wep then return false end;
	if !wep.Primary then return false end;
	ply:GetActiveWeapon().OldRecoil = ply:GetActiveWeapon().Primary.Recoil;
	ply:GetActiveWeapon().Primary.Recoil = self.weapon_recoil;
end

function oubhack:Fire()
	local ply = LocalPlayer()
	if ply:GetActiveWeapon() then
		ply:GetActiveWeapon():PrimaryAttack()
	end
end

function oubhack:Spam()
	RunConsoleCommand("say","Oubhack is great, go buy it!")
end
hook.Add("HUDPaint","AimbotX-RayPaint",function() if oubhack.spam_on then oubhack:Spam() end end)
	
function oubhack:CreateMove(cmd,x,y,angles) -- Handles snapping of aim-angles, and triggerbot	
	local ply = LocalPlayer()
	if not self.lastang then self.lastang = self.ang or angles end
	if self.ang != Angle(0,0,0) and self.aimbot_on then
		local ang = self.ang
		cmd:SetViewAngles((ang or angles))
	end
	-- trigger bot stuff from here on in
	-- if ply:GetActiveWeapon() then return true end
	-- if table.HasValue(self.notraceweapons,ply:GetActiveWeapon():GetClass()) then return true end
	if (self.triggerbot_on and self.vis_targs and table.HasValue(self.vis_targs,LocalPlayer():GetEyeTrace().Entity) ) then
		-- Activate the triggerbot, how we do this is decided in here
		if !ply:GetActiveWeapon().Primary then -- We can't tell if it's automatic or not, so were just gonna speed tap it
			if self.attacking then 
				RunConsoleCommand("-attack")
				self.attacking = false;
			else
				RunConsoleCommand("+attack")
				self.attacking = true;
			end
		else -- We can tell if it's automatic, so we will hold it down, if it is, and tap it, if it's not.
			-- For future. I'm leaving this here
			if self.attacking then 
				RunConsoleCommand("-attack")
				self.attacking = false;
			else
				RunConsoleCommand("+attack")
				self.attacking = true;
			end
		end
		
	else
		if self.attacking then
			RunConsoleCommand("-attack")
			self.attacking = false;
		end
	end
end
hook.Add("CreateMove","Test",function(cmd,x,y,angles) oubhack:CreateMove(cmd,x,y,angles) end)
