Scriptname RenLightCleanerQuest extends Quest

RenLightQuestScript Property renlightquest Auto

RefCollectionAlias property celllightsforms Auto

bool Property enableme = false Auto

int disabledforms = 0

int itr = 0

Event OnInit()
	Self.StartTimer(1.0, 10)
endEvent

Event OnTimer(int timerid)
	if (renlightquest == none)
		renlightquest = Game.GetFormFromFile(0x01000801, "RenLightMod.esp") As RenLightQuestScript
	endif
	
	enableme = false ; needs more tweaking...
	
	if (enableme == false && celllightsforms.GetCount() <= 0)
		Self.StartTimer(1.0, 10)	
		return
	endif
	
	if (enableme == true && Game.GetPlayer().IsInInterior() == true)
		if (celllightsforms.GetCount() <= 0)
			celllightsforms.AddArray(Game.GetPlayer().FindAllReferencesOfType(renlightquest.RenLightFormlist, 20000))
			while (itr < celllightsforms.GetCount())
				Objectreference[] nearbylightobjs = celllightsforms.GetAt(itr).FindAllReferencesOfType(renlightquest.RenLightObjFormlist, renlightquest.flightdistance)
				if (nearbylightobjs.length <= 0)
					; disable this lightform
					celllightsforms.GetAt(itr).Disable(false)
					disabledforms += 1
				endif
				itr += 1
			endWhile
		endif
	else
		itr = 0
		if (celllightsforms.GetCount() > 0)
			while (itr < celllightsforms.GetCount())
				celllightsforms.GetAt(itr).Enable(false)
				itr += 1			
			endWhile
			itr = 0
			celllightsforms.RemoveAll()
		endif
	endif
	Self.StartTimer(1.0, 10)
endEvent