-- Description: 		SomSphere Entity for putting AI within a sphere into permanent sleep state
-- Created by Michael Chimella 6May2008

--Version History:
--v4 added radius control, height limit
--v5 no longer puts dead AI into sleep state, shouting by sleeping AI must less often
--v6 no more sleep response of AI

SomSphere = {

	Properties =
		{
		Radius = 25,
		fHeight = 2,
		--fDepth = 1,
		},
	
	Server = {},
	Client = {},
	Editor={
		Model = "Editor/Objects/Particles.cgf",
	}
	
};

------------------------------------------------------------
function SomSphere:Event_Trigger(sender)

local pos = self:GetWorldPos(); -- gets world position of the SomSphere entity
local ents = System.GetEntitiesInSphereByClass(pos, self.Properties.Radius, "Grunt"); --gets all grunts within radius
	for alpha,e in pairs(ents) do -- sets up a loop to deal with all grunt tables one at a time, e are tables
    --System.Log(e:GetName());
    --System.Log(tostring(e));  		
    GruntId = System.GetEntityIdByName(e:GetName()); --gets grunt ids from names
    --System.Log(tostring(GruntId));
    --System.GetEntity(GruntId).actor:SetPhysicalizationProfile("sleep"); -- puts to sleep, note "ragdoll" kills, "unragdoll"? "alive"?
		local myEnt = System.GetEntity(GruntId);
		
		--myEnt.Properties.SoundPack = ""; 
		--myEnt.Properties.aicharacter_character = "Hostage";	
		--myEnt.Properties.species = 0;
		
		local entpos = myEnt:GetWorldPos(); --gets world position of AI
		
		local AIStatus = 1; --status alive
		if (System.GetEntity(GruntId).lastHealth <= 0) then
		AIStatus = 0; --status dead
		end
		
		if ((entpos.z <= pos.z + self.Properties.fHeight) and (AIStatus == 1)) then
		--myEnt:DropItem();
		myEnt.Properties.attackrange = 0;
		myEnt.Properties.commrange = 0;
		myEnt.Properties.Perception.audioScale = 0;
		myEnt.Properties.Perception.sightrange = 0;
		myEnt:OnReset();
		myEnt:StopSound(myEnt.idleSound);
	  myEnt.idleSound = nil;
	  myEnt:StopSounds(); -- stops current sound I believe
	  myEnt:StopAllSounds();
		myEnt:SelectPipe(0,"sleep");
		myEnt:InsertSubpipe( AIGOALPIPE_HIGHPRIORITY, "cv_tranquilized", nil, -190 );
		myEnt:TriggerEvent(AIEVENT_SLEEP);
		System.GetEntity(GruntId).actor:SetPhysicalizationProfile("sleep");
		end


		GruntName = e:GetName();
		--CryAction.Persistant2DText(string.format(tostring(GruntId)),2,{255,255,255},"Event_Start",10); -- HUD for debug of ids out
		--CryAction.Persistant2DText(string.format(GruntName),2,{255,255,255},"Event_Start",10); -- HUD for debug of names out
		--BroadcastEvent( self,"IdOut");
		--BroadcastEvent( self,"NameOut");
		self:ActivateOutput("IdOut",GruntId); -- pumps out ids of grunts from IdOut port
		self:ActivateOutput("NameOut",GruntName); -- pumps out names of grunts from NameOut port
	end

self:ActivateOutput("Done",true);
end


SomSphere.FlowEvents =
{

	Inputs =
	{
		Trigger = { SomSphere.Event_Trigger, "bool" },
	},

	Outputs =
	{
		Done = "bool",
		IdOut = "entity",
		NameOut = "string",		
	},
	
}