local	SPAWNLIMIT = 20
local   NPCNAMES = {
					"npc_combine_s", "npc_combine_s",
					"npc_combine_c", "npc_combine_e",
					"npc_combine_s", "npc_combine_s"
					}

local   NPCNAMES_2MINS = {
					"monster_human_grunt", "monster_human_grunt",
					"monster_human_grunt", "monster_human_grunt",
					"monster_human_grunt", "monster_human_grunt"
					}
					
function InstantSpawnEntity(class, pos)

	local ent = HL2.CreateEntity(class, pos, VECTORZERO)
	if ent ~= nil then
		HL2.SpawnEntity(ent)
	end

	return ent

end

function GetRandomNodePosition()

	local nodes = HL2.GetNodeCounts()
	local retorg = nil

   	if nodes >= 1 then
		local node  = HL2.GetNodeData(HL2.RandomInt(0,nodes-1))
		if node ~= nil then
			retorg = HL2.Vector(node.x, node.y + 16, node.z)
		end
	end

	return retorg

end

local wep_name = {	"weapon_ar2", 
			"weapon_ar2",
			"weapon_smg1",
			"weapon_grease",
			"weapon_shotgun",
			"weapon_custom14",
			"weapon_mp5",			
			"weapon_mp5"
}

function GetWeaponName()

	local rt = HL2.RandomInt(1, 8)
	
	return wep_name[rt]

end

local wep_name2 = {	"weapon_custom5", 
			"weapon_custom5",
			"weapon_svd",
			"weapon_svd",
			"weapon_lasergun",
			"weapon_custom5",
			"weapon_custom15",			
			"weapon_custom15"
}

function GetWeaponName2()

	local rt = HL2.RandomInt(1, 8)
	
	return wep_name2[rt]

end

function SpawnEnemy()

	local pos = GetRandomNodePosition()
	local ent = nil

	if pos ~= nil then

	    local npcname = NPCNAMES[HL2.RandomInt(1, table.getn(NPCNAMES))]

--	    print("Spawn -> " .. npcname)

	    local ang = HL2.Vector(0, HL2.RandomInt(0, 359), 0)
		
		ent = HL2.CreateEntity(npcname, pos, ang)
		if ent ~= nil then

			local pent = HL2.GetPlayer()
			local visinfo = HL2.CheckVisible(pent, ent)
			if visinfo.IsVisible ~= 1 then
			HL2.KeyValue(ent, "additionalequipment", GetWeaponName())
								
				HL2.KeyValue(ent, "spawnflags", 1280 )
				HL2.KeyValue(ent, "targetname", "enemy")
				HL2.KeyValue(ent, "squadname", HL2.RandomFloat(0, 20)) --IMPORTANT! spread out the squads as much as possible or combine turn into knunkleheads who can't decide who should shoot.
				HL2.KeyValue(ent, "numgrenades", HL2.RandomFloat(1, 3))					

-- tougher enemies after 3 min mark				
			    if ROUNDTIME <= GETTOUGHTIME then
				HL2.KeyValue(ent, "runspeedmod", HL2.RandomFloat(1.35, 1.75))			
				end

	    		HL2.SpawnEntity(ent)
	    		ENEMYCOUNT = ENEMYCOUNT + 1
			else
			    HL2.RemoveEntity(ent)
			end
		end

	end

	return ent

end


--if round timer is at 2 mins or under, spawn these enemies
function SpawnEnemy_2mins()

	local pos = GetRandomNodePosition()
	local ent = nil

	if pos ~= nil then

	    local npcname = NPCNAMES_2MINS[HL2.RandomInt(1, table.getn(NPCNAMES_2MINS))]

--	    print("Spawn -> " .. npcname)

	    local ang = HL2.Vector(0, HL2.RandomInt(0, 359), 0)
		
		ent = HL2.CreateEntity(npcname, pos, ang)
		if ent ~= nil then

			local pent = HL2.GetPlayer()
			local visinfo = HL2.CheckVisible(pent, ent)
			if visinfo.IsVisible ~= 1 then

			if npcname == "monster_human_grunt" then			
				HL2.KeyValue(ent, "rendercolor", "0 0 250")					
				HL2.KeyValue(ent, "runspeedmod", HL2.RandomFloat(3, 5))		
				HL2.KeyValue(ent, "numgrenades", HL2.RandomFloat(3, 6))					
				end	
	
				HL2.KeyValue(ent, "additionalequipment", GetWeaponName2())	
				HL2.KeyValue(ent, "spawnflags", 1024 )
				HL2.KeyValue(ent, "targetname", "enemy")
				HL2.KeyValue(ent, "squadname", HL2.RandomFloat(21, 40)) --IMPORTANT! spread out the squads as much as possible or combine turn into knunkleheads who can't decide who should shoot.				
				HL2.KeyValue(ent, "runspeedmod", HL2.RandomFloat(1.5, 2))
				HL2.KeyValue(ent, "numgrenades", HL2.RandomFloat(1, 3))		
				
	    		HL2.SpawnEntity(ent)
	    		ENEMYCOUNT = ENEMYCOUNT + 1
			else
			    HL2.RemoveEntity(ent)
			end
		end

	end

	return ent

end

function StatusTimer()

	if GAMEEND == true then
		return 0
	end

-- refresh enemy search every run	
	HL2.CallMapaddLabel("search")		
	
--	HL2.ShowInfoMessage(0, 2, ENEMYCOUNT .. " ENEMIES.")
	return 2
	
end


function SpawnTimer()

	local timestr = HL2.TimeStr(ROUNDTIME)

	if GAMEEND == true then
		return 0
	end

	if ENEMYCOUNT < SPAWNLIMIT then
		SpawnEnemy()
	end

	if ROUNDTIME <= SPAWNHARDENEMY and ENEMYCOUNT < SPAWNLIMIT and HARDENEMIES == true then
		SpawnEnemy_2mins()
	end	
	
	return SPAWNTIME	
	
end

function Initialize()

 	HL2.CreateTimer("SpawnTimer", 3)
 	HL2.CreateTimer("StatusTimer", 2)
 	HL2.CreateTimer("TimeCount", 1)			
	HL2.HookKilledEvent("Killed")	

	local i;

	for i=1, 15, 1 do
		SpawnEnemy()
	end

end

function Killed(info)

	local entinfo = HL2.GetEntInfo(info.KilledEnt)

	if entinfo.Name == "enemy" then
		ENEMYCOUNT = ENEMYCOUNT - 1
		KILLCOUNT = KILLCOUNT + 1
		if entinfo.Classname == "npc_antlionguard" then
			--  done this way due to how rewards are given
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)	
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)			
		end			
		if entinfo.Classname == "npc_combine_e" or entinfo.Classname == "npc_combine_ace" or entinfo.Classname == "monster_human_grunt" or entinfo.Classname == "npc_poisonzombie" 
		or entinfo.Classname == "npc_bullsquid" then
			--  done this way due to how rewards are given
			IncreaseMoney(1)
			IncreaseMoney(1)			
			IncreaseMoney(1)				
		end		
		if entinfo.Classname == "npc_combine_s" or entinfo.Classname == "npc_combine_c" or entinfo.Classname == "npc_combine_p" or entinfo.Classname == "npc_zombie2" or entinfo.Classname == "npc_stalker" 
		or entinfo.Classname == "npc_zombine" or entinfo.Classname == "npc_houndeye" then
			--  done this way due to how rewards are given
			IncreaseMoney(1)
			IncreaseMoney(1)				
		else
			IncreaseMoney(1)
		end
		if KILLITEMS == true and info.DamageType == 128 then
			IncreaseMoney(1) IncreaseMoney(1) IncreaseMoney(1)
		end			
	end


	if info.IsPlayer == 1 then
	    GameOver()
	end

end

--bad end
function GameOver()

	HL2.HookKilledEvent()
	GAMEEND = true
	HL2.CallMapaddLabel("GameOver")
	HL2.ShowInfoMessage(1, 15, "YOU KILLED " .. KILLCOUNT .. " ENEMIES.")

end		