--[[
	Hack vs Hack: Redux
--]]

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

require( "datastream" )

include( 'shared.lua' )

-- Dem tables --
SpecPlayers = {
	["STEAM_0:1:1917468"] = true,
	["STEAM_0:1:24332877"] = true,
	["STEAM_0:1:20327511"] = true,
	["STEAM_0:1:19732775"] = true, -- ph0ne #1
	["STEAM_0:1:22241274"] = true -- ph0ne #2
}
-- CLASS -> PRINT NAME --
WeaponNames = {
	["weapon_hvh_m4a1"] = "M4A1",
	["weapon_smg1"] = "HL2 SMG",
	["weapon_deagle"] = "Desert Eagle",
	["weapon_ak47"] = "AK47",
	["weapon_hvh_awp"] = "AWP",
	["weapon_glock"] = "Glock18",
	["wat"] = "Nonexisting Weapon"
}
-- CLASS -> POINTS --
WeaponPoints = {
	["weapon_hvh_m4a1"] = 1,
	["weapon_smg1"] = 5,
	["weapon_deagle"] = 2,
	["weapon_ak47"] = 1,
	["weapon_hvh_awp"] = 3,
	["weapon_glock"] = 3,
	["wat"] = 0
}
-- KILLS REQUIRED -> { NAME -> PRINT MESSAGE } --
Killstreaks = {
	[6] = { "Manslaughter", "committed MANSLAUGHTER" },
	[10] = { "Killing Spree", "is on a KILLING SPREE" },
	[20] = { "Rampage", "is on a RAMPAGE" },
	[35] = { "GENOCIDE", "committed GENOCIDE" }
}
-- CUSTOM SPAWNPOINTS YAY --
SpawnPoints = {
	["gm_construct"] = { 
		[1] = Vector( 455.184601, -1867.468628, -79.968750 ),
		[2] = Vector( -2949.866699, 242.548538, 208.031250 )
	}
}

function _R.Player:WorkSafeSID()
	return self:SteamID():gsub(":" , "_")
end

-- Here goes --
function GM:PlayerAuthed( ply, sid, uid )	
	-- Killstreaks n shit --
	ply.killstreak = 0
	ply:SetNWInt( "hvh_killstreak", ply.killstreak )
	ply.weploadout = {
	"weapon_ak47",
	"weapon_hvh_m4a1",
	"weapon_smg1",
	"weapon_deagle",
	"weapon_glock",
	"weapon_hvh_awp" 
	}
end

function GM:PlayerInitialSpawn( ply )
	local sid = ply:SteamID()
	
	-- Set player teams --
	if SpecPlayers[sid] then ply:SetTeam( 1 ) return end
	
	ply:SetTeam( 2 )
end

function GM:PlayerLoadout( ply )
	ply:StripWeapons()
	
	ply:GiveAmmo( 9999, "pistol" )
	ply:GiveAmmo( 9999, "smg1" )
	
	-- It appears that on a local server table ain't created on me :( --
	if !ply.weploadout || type( ply.weploadout ) != "table" then
		ply.weploadout = {
		"weapon_ak47",
		"weapon_hvh_m4a1",
		"weapon_smg1",
		"weapon_deagle",
		"weapon_glock",
		"weapon_hvh_awp"
		}
	end
	
	for i = 1, #ply.weploadout do
		ply:Give( ply.weploadout[i] )
	end
end

local headshotsnd = { "player/headshot1.wav", "player/headshot2.wav" }

function GM:ScalePlayerDamage( ply, hitgroup, dmginfo )
	if ( hitgroup == HITGROUP_HEAD ) then
		dmginfo:ScaleDamage( 2 )
		ply:EmitSound( table.Random( headshotsnd ) )
	end
end

function GM:PlayerDeathSound()
	return true
end

function GM:DoPlayerDeath( ply, attacker, dmginfo )
	ply:CreateRagdoll()
	ply:AddDeaths( 1 )
	ply.killstreak = 0
	ply:SetNWInt( "hvh_killstreak", ply.killstreak )
	
	if ( attacker:IsValid() && attacker:IsPlayer() ) then
		if ( attacker != ply ) then
			attacker.killstreak = attacker.killstreak + 1
			attacker:SetNWInt( "hvh_killstreak", attacker.killstreak )
		end
	end
	
	local text
	local weapon
	
	if attacker:Health() <= 0 || attacker:GetActiveWeapon() == nil then
		weapon = "wat"
	else
		weapon = attacker:GetActiveWeapon():GetClass()
	end
	
	if attacker == ply then
		text = "[HvH] You killed yourself and lost 2 points for suicide"
		attacker:AddFrags( -2 )
	else
		text = "[HvH] You earned " .. WeaponPoints[weapon] .. " point(s) for killing " .. ply:Nick() .. " with " .. WeaponNames[weapon]
		attacker:AddFrags( WeaponPoints[weapon] )
	end
	
	attacker:SendLua( "chat.AddText( Color( 255, 135, 30 ), [[" .. text .. "]] )" )
	
	if Killstreaks[attacker.killstreak] then
		attacker:PrintMessage( HUD_PRINTCENTER, string.upper( Killstreaks[attacker.killstreak][1] ) )
		
		text = "[HvH] You earned 5 additional points for " .. Killstreaks[attacker.killstreak][1]
		attacker:SendLua( "chat.AddText( Color( 255, 135, 30 ), [[" .. text .. "]] )" )
		
		text = "[HvH] " .. attacker:Nick() .. " " .. Killstreaks[attacker.killstreak][2]
		BroadcastLua( "chat.AddText( Color( 255, 0, 0 ), [[" .. text .. "]] )" )
		
		attacker:AddFrags( 5 )
	end
end

--[[
	NAC - noPE's AntiCheat
	Version 0.4
	Purpose:	Keep script kiddies away. This
				means BaconBot, Cheat Engine,
				and public LUA hacks won't run.
				
	TODO:	- Add cvar checks
			- Check if certain concommands exist
--]]

if SERVER then
	local RCC = RunConsoleCommand

	-- Initial stuff
	hook.Add("PlayerInitialSpawn", "InitialCheck", function( ply )
		
		-- Check if certain modules anf files exist on player's game
		timer.Simple( 5, function()
			
			-- Let's see what modules our clients have, just for the sake of it
			ply:SendLua( [[
				local RCC = RunConsoleCommand
				local modules = file.Find("../lua/includes/modules/*.dll")
				
				for _, f in pairs( modules ) do
					RCC("_nac_modulelist", f)
				end
			]] )
		end )
	end )
	
	-- AUTOMATIC MODULE SURVEY YEEEEAH
	concommand.Add("_nac_modulelist", function( ply, c, a )
		for k, v in pairs( player.GetAll() ) do
			if v:SteamID() == "STEAM_0:1:1917468" || v:SteamID() == "STEAM_0:1:24332877" then
				local text = "Player " .. ply:Nick() .. " has a module named: " .. a[1] .. ""
				
				v:SendLua( string.format( "MsgN( %q )", text ) )
			end
		end
	end )
end