local Time 	= Game.Time;
local Delay 	= 1; -- game seconds
local Ready	= Time();

function Create()
	Ready = Time()
	Delay = 1
end

function tableSize(T)
    if T == nil then return 0; end
    local count = 0;
    for _ in pairs(T) do count = count + 1; end
    return count;
end


function SpawnObject(ObjectType, x, y)
    return Object.Spawn(tostring(ObjectType), x, y);
end

function FindGuard()
	local guard = this.GetNearbyObjects( "Prisoner", 10)
			for Entity, _ in next, guard do
				Object.ClearRouting()
				Object.NavigateTo( Entity.Pos.x, Entity.Pos.y )
	end
end

function GetDestroyedFool()
	local destroyer = this.GetNearbyObjects( "EliteOps", 4 )
		for Entity, _ in next, destroyer do
		if this.Damage == 0 then
			this.Damage = 1
			this.Sound("_Tools", "Attack_AssaultRifle")
			this.ApplyVelocity( math.random(-1,1),math.random(-1,1),math.random(-1,1))
		 elseif this.Damage == 1 then

		end
	 end
 end

function DoTheThing()
	local classD = this.GetNearbyObjects( "Prisoner", 1.4 )
		for Entity, _ in next, classD do
		if this.Damage == 0 then
          			Entity.Delete()
				this.Sound( "Guard", "Damage" )
				local zom = SpawnObject("SCP_008_1", Entity.Pos.x, Entity.Pos.y);
		elseif this.Damage == 1 then

		end
	end
end

function DoTheGuardThing()
	local guard = this.GetNearbyObjects( "Guard", 1.4 )
		for Entity, _ in next, guard do
			if this.Damage == 0 then
          			Entity.Delete()
				local zomGuard = SpawnObject("SCP_008_1_guard", Entity.Pos.x, Entity.Pos.y);
			elseif this.Damage == 1 then

		end
	end
end

function DoTheStaffThing()
	local staff = this.GetNearbyObjects( "Scientist", 1.4 )
		for Entity, _ in next, staff do
			if this.Damage == 0 then
          			Entity.Delete()
				local zomstaff = SpawnObject("SCP_008_1_staff", Entity.Pos.x, Entity.Pos.y);
			elseif this.Damage == 1 then

		end
	end
end

function IdleMovement() --the idle movement or just not when doing anything
	if roll(30) == true then
            this.NavigateTo(this.Pos.x + math.random(-1,1),this.Pos.y + math.random(-1,1))
		end
	end

function Update()
	if Time() > Ready then
		PerformUpdate( Delay )
		Ready = Ready + Delay
	end
end

function PerformUpdate( elapsedTime )
	IdleMovement()
	DoTheThing()
	GetDestroyedFool()
	DoTheGuardThing()
	DoTheStaffThing()
	DoTheThing()
	end

function roll(chance)
    local randomPercent = math.random(0, 50);
    if randomPercent <= chance then return true; end
    return false;
end
