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

function Create()
	this.HeliSpawn = false
    	this.Heli = false
	this.AliveHeli = false
	this.Deadheli = false
	Ready = Time()
	Delay = 1
        Interface.AddComponent(this,"HeliStatus", "Caption", "Helicopter Present: No")
        Interface.AddComponent(this,"BeginCopter", "Button", "Call Helicopter")
	Interface.AddComponent(this,"CancelHeli", "Button", "Return Helicopter")
end

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

function BeginCopterClicked()
	local Spawn = this.GetNearbyObjects("HelicopterSpawn", 9999)
		for obj, dist in pairs(Spawn) do
			this.HeliSpawn = true
			local heli = SpawnObject("SCPAttackHeliCopter", obj.Pos.x, obj.Pos.y);
			this.SetInterfaceCaption("HeliStatus", "Helicopter Present: Yes")
		if this.HeliSpawn == false then
		
		end
	end
end

function CancelHeliClicked()
	local Copter = this.GetNearbyObjects("SCPAttackHeliCopter", 9999)
		for copters, dist in pairs(Copter) do
			copters.ClearRouting()
			copters.LeaveMap()
			this.SetInterfaceCaption("HeliStatus", "Helicopter Present: No")
	end
end

function CheckForDedHeli()
	local DedCopter = this.GetNearbyObjects("SCPAttackHeliCopterCrashed", 9999)
		for Dedcopters, dist in pairs(DedCopter) do
			dedcop = Dedcopters;
			this.RemoveInterfaceComponent("CancelHeli", "Return Helicopter")
			Interface.AddComponent(this,"CopterStatusDead", "Caption", "Helicopter Destroyed")
		if dedcop == nil then
			this.RemoveInterfaceComponent("CopterStatusDead", "Helicopter Destroyed")
			Interface.AddComponent(this,"BeginCopter", "Caption", "Call Helicopter")	
		end
	end
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 Update()
	if Time() > Ready then
		PerformUpdate( Delay )
		Ready = Ready + Delay
	end
end

function PerformUpdate( elapsedTime )	

end

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