local	SPAWNLIMIT = 40
local   NPCNAMES = {
					"npc_bullsquid", "npc_houndeye",
					"npc_zombie", "npc_fastzombie",
					"npc_fastzombie", "npc_zombie"
					}

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

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

	return ent

end

function KillItems()
	    HL2.CallMapaddLabel("killitems")
end


function Relations()
	    HL2.CallMapaddLabel("relations")
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

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
				
			    if npcname == "npc_antlion" then
					HL2.KeyValue(ent, "startburrowed", HL2.RandomFloat(0, 1))
				end	
				
				HL2.KeyValue(ent, "spawnflags", 1024 )
				HL2.KeyValue(ent, "targetname", "enemy")
				HL2.KeyValue(ent, "squadname", "x") --IMPORTANT! spread out the squads as much as possible or combine turn into knunkleheads who can't decide who should shoot.

-- tougher enemies after 3 min mark				
			    if ROUNDTIME <= GETTOUGHTIME then
				HL2.KeyValue(ent, "runspeedmod", HL2.RandomFloat(1.2, 1.25))		
				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
					
				HL2.KeyValue(ent, "spawnflags", 1024 )
				HL2.KeyValue(ent, "targetname", "enemy")
				HL2.KeyValue(ent, "squadname", "x") --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.3, 1.6))
								
	    		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.CallMapaddLabel("relations")	
	
--	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")

--    SpawnItems()
--    PlayerRandomSpawn()
--    LoadedWeapons()
	
	local i;

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

	HL2.CallMapaddLabel("relations")	
	
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		