--Highly cancerous code ahead
--Proceed with caution those who dare edit this file

game.AddParticles( "particles/devtest.pcf" )
game.AddParticles( "particles/hunter_projectile.pcf" )
game.AddParticles( "particles/skybox_smoke.pcf" )
game.AddParticles( "particles/striderbuster.pcf" )
PrecacheParticleSystem( "weapon_muzzle_flash_assaultrifle" )
PrecacheParticleSystem( "portal_lightning_03" )
PrecacheParticleSystem( "striderbuster_shotdown_core_flash" )
PrecacheParticleSystem( "hunter_projectile_explosion_2g" )
PrecacheParticleSystem( "hunter_projectile_explosion_2i" )

sound.Add(
{
    name = "Lonely.AK47_FireNPC",
    channel = CHAN_WEAPON,
    volume = 0.4,
    level = 140,
	pitch = { 95, 110 },
    sound = "weapons/ak47_new/ak47_fire1_new.wav"
})

sound.Add(
{
    name = "Lonely.MP5_FireNPC",
    channel = CHAN_WEAPON,
    volume = 0.3,
    level = 140,
	pitch = { 95, 110 },
    sound = "weapons/pistol/pistol_fire1_new.wav"
})

sound.Add(
{
    name = "Lonely.Scout_FireNPC",
    channel = CHAN_WEAPON,
    volume = 0.5,
    level = 140,
	pitch = { 95, 110 },
    sound = "weapons/scout_new/scout_new_fire2.wav"
})

sound.Add(
{
    name = "Lonely.BigBen_Fire",
    channel = CHAN_WEAPON,
    volume = 1,
    level = 140,
	pitch = { 95, 110 },
    sound = "weapons/exc_bigben/deagle_fire01_new.wav"
})

if (game.GetMap() == "ttt_exclusion_v2") and (engine.ActiveGamemode() == "terrortown") then

if SERVER then
	util.AddNetworkString("exclusionChatPrint")
	util.AddNetworkString("exclusionChatPrint_friendlyfire")
	util.AddNetworkString("exclusionChatPrint_raid")
	util.AddNetworkString("exclusionChatPrint_monolithAnnounce")
end

if CLIENT then
	net.Receive("exclusionChatPrint", function()
		surface.PlaySound( "exclusion/pda.wav" )
		if LocalPlayer():IsTraitor() then
			chat.AddText( Color( 255, 100, 100 ), "Major Kuznetsov:", Color( 255, 255, 255 ), " Soldiers, as members of the State Security Service, it is your duty to disband Stalker groups operating illegally within the zone. You are now disguised as a loner, covertly eliminate them or call in reinforcements.")
		else if LocalPlayer():IsDetective() then
			chat.AddText( Color( 100, 200, 255 ), "N. A. Lebedev:", Color( 255, 255, 255 ), " Detectives, scientists, use your technology and deduction skills to protect the loners from military infiltrators.")
		else
			chat.AddText( Color( 100, 255, 100 ), "Father Valerian:", Color( 255, 255, 255 ), " Stalkers, Spetsnaz forces have infiltrated our group and will stop at nothing to eliminate us from the inside. Get rid of them, or survive long enough for the rest of the boys to get here.")
		end
		end
	end)

	net.Receive("exclusionChatPrint_raid", function()
		local msgType = net.ReadBit()
		surface.PlaySound( "exclusion/pda.wav" )
		if (msgType == 1) then
			if (LocalPlayer():IsTraitor()) then
				chat.AddText( Color( 255, 100, 100 ), "Major Kuznetsov:", Color( 255, 255, 255 ), " Sending troops to sectors C3, C4, C5. Mind the friendly fire.")
			else
				chat.AddText( Color( 100, 255, 100 ), "Father Valerian:", Color( 255, 255, 255 ), " I'm hearing reports of soldiers moving into position. Get ready for a fight.")		
			end
		else
			chat.AddText( Color( 200, 200, 200 ), "Anonymous:", Color( 255, 255, 255 ), " DEATH TO INFIDELS!")
		end
	end)

	net.Receive("exclusionChatPrint_friendlyfire", function()
		local ply = net.ReadString()
		chat.AddText( Color( 255, 100, 100 ), "Major Kuznetsov:", Color( 255, 255, 255 ), " "..ply..", friendly fire will not be tolerated.")
		surface.PlaySound( "exclusion/pda.wav" )
	end)
	
	net.Receive("exclusionChatPrint_monolithAnnounce", function()
		local rng = math.random(1,3)
		surface.PlaySound( "exclusion/pda.wav" )		
		if (rng == 1) then
			chat.AddText( Color( 200, 200, 200 ), "Anonymous:", Color( 255, 255, 255 ), " My squad and I are getting a clear view of some guys in urban camo making their way to the old trainyard. Fanatics? What are they up to now?")
		else if (rng == 2) then
			chat.AddText( Color( 200, 200, 200 ), "Anonymous:", Color( 255, 255, 255 ), " More of them fanatics moving in. Reactivating the brain scorcher wasn't enough. I'd get out of there if I were you.")
		else
			chat.AddText( Color( 200, 200, 200 ), "Anonymous:", Color( 255, 255, 255 ), " Rock worshippers patrolling the perimeter around that ol' trainyard looking place. Tough luck to whoever gets in their way.")
		end
		end
	end)
end

hook.Add("TTTBeginRound", "SetUpNPCRelations", function()
	for i, v in ipairs(player.GetAll()) do 
		if v:IsTraitor() then
			for i, army in ipairs(ents.FindByName("military1")) do
				army:AddEntityRelationship(v, D_LI, 99)
				--print(army:GetName().." loves the fiddlesticks out of ".. v:GetName())
			end
		else
			for i, army in ipairs(ents.FindByName("military1")) do
				army:AddEntityRelationship(v, D_HT, 99)
				--print(army:GetName().." hates the fiddlesticks out of ".. v:GetName())
			end
		end
	end
end)

hook.Add("EntityTakeDamage", "ArmyFriendlyFire", function(target, dmg)
	local attacker = dmg:GetAttacker()
	if (IsValid(attacker)) and (IsValid(target)) and (attacker:IsPlayer()) then
		if (target:GetName() == "military1") and (target:Disposition(attacker) == D_LI) and (attacker:IsTraitor()) then
			net.Start('exclusionChatPrint_friendlyfire') 
			net.WriteString(attacker:GetName())
			net.Send(attacker)
			for i, army in ipairs(ents.FindByName("military1")) do
				army:AddEntityRelationship(attacker, D_HT, 99)
				army:UpdateEnemyMemory(attacker, attacker:GetPos())
			end
		end
	end
end)


function UpdateEnemyMemory(enemyType)
	if (enemyType == "army") then 
		for i, v in ipairs(player.GetAll()) do 
			if (!v:IsTraitor()) then
				for i, army in ipairs(ents.FindByName("military1")) do
					army:AddEntityRelationship(v, D_HT, 99)
					army:UpdateEnemyMemory(v, v:GetPos())
					--print("army memory updated")
				end
			end
		end
	elseif (enemyType == "monolith") then
		for i, v in ipairs(player.GetAll()) do 
			for i, mono in ipairs(ents.FindByName("monolith")) do
				mono:UpdateEnemyMemory(v, v:GetPos())
				--print("monolith memory updated")
			end		
		end		
	end
end

end