local delay = {}
local init_delay = {}
local on = {}
local minRange = {}
local maxRange = {}
local rangeChangeSpeed = {}
local currentRange
local lightE
local direction = {}
local flash = {}
local randomiseRange = {}
local newChangeSpeed

function lightflash_init(e) -- name
	lightE = GetEntityLightNumber( e )
	--change time here to adjust flicker timing
	flash[e] = false 
	randomiseRange[e] = true 
	delay[e] = 250
	minRange[e] = 50
	maxRange[e] = 200
	rangeChangeSpeed[e] = 900
	
	if GetEntityName(e) == "fire_arrow_light" then 
		minRange[e] = 1
		maxRange[e] = 20
	end
	
	newChangeSpeed = rangeChangeSpeed[e]
	direction[e] = math.random(0,1)
	if direction[e] == 0 then direction[e] = -1 end 
	currentRange = GetLightRange(lightE)
	if currentRange < minRange[e] then 
		currentRange = minRange[e]
		direction[e] = 1
	elseif currentRange > maxRange[e] then 
		currentRange = maxRange[e]
		direction[e] = -1
	end 
	init_delay[e] = delay[e]
	on[e] = 0
	SetActivated(e,0)
end
function lightflash_main(e) -- name	
	if g_Entity[e]['activated'] == 0 then 
		ShowLight(e)
		SetActivated(e,1)
		
	elseif g_Entity[e]['activated'] == 1 then 
		if flash[e] == true then 
			if GetTimer(e) > delay[e] then
				if on[e] == 0 then
					on[e] = 1
					ShowLight(e)
				else
					on[e] = 0
					HideLight(e)
				end
				delay[e] = GetTimer(e) + init_delay[e]
				--to make more random use this line
				delay[e] = GetTimer(e) + math.random(init_delay[e] * 0.8,init_delay[e] * 1.2)
			end
		end
		lightE = GetEntityLightNumber( e )
		currentRange = GetLightRange(lightE)					
		if direction[e] == -1 then 
			if currentRange < minRange[e] then 
				direction[e] = 1
				if rangeChangeSpeed[e] == true then 
					newChangeSpeed = rangeChangeSpeed[e] * math.random(0.7,1.3)
				end 
			end 
		elseif direction[e] == 1 then 
			if currentRange > maxRange[e] then 
				direction[e] = -1
				if rangeChangeSpeed[e] == true then 
					newChangeSpeed = rangeChangeSpeed[e] * math.random(0.7,1.3)
				end 
			end 
		end 
		local newRange = currentRange + newChangeSpeed * direction[e] * GetElapsedTime()
		if newRange < 1 then 
			newRange = 1 
		elseif newRange > 1000 then 
			newRange = 1000
		end
		SetLightRange(lightE, newRange)
				
	elseif g_Entity[e]['activated'] == 2 then 
		HideLight(e)
		SetActivated(e,3)
		
	elseif g_Entity[e]['activated'] == 3 then 
	
	end
end