Scriptname VampireStableInvisibility extends activemagiceffect

Actor Property Player Auto Hidden

PlayerVampireQuestScript Property PlayerVampireQuest  Auto
GlobalVariable Property RECASTVampireDeflectLight Auto
GlobalVariable Property VampireAbilityTimers Auto
GlobalVariable Property EnableVampireBloodPoints Auto
GlobalVariable Property VampireBloodPoints Auto
GlobalVariable Property VampireDeflectLightToggle Auto

Spell Property VampireInvisibilityRecast Auto
Spell Property VampireInvisibilityPC Auto

MagicEffect Property StableVampireInvisibility Auto
MagicEffect Property crVampireInvisibillity2 Auto

EVENT OnEffectStart(Actor akTarget, Actor akCaster)

	If VampireDeflectLightToggle.GetValue() == 0

		VampireDeflectLightToggle.SetValue(1)
	
		Player = akTarget
		Player.AddSpell(VampireInvisibilityRecast, abVerbose = False)				
		VampireInvisibilityRecast.Cast(Game.GetPlayer(), Game.GetPlayer())

		If EnableVampireBloodPoints.GetValue() == 10000 && RECASTVampireDeflectLight.GetValue() == 0
			If Player == Game.GetPlayer()
				PlayerVampireQuest.VampireBloodPoints(40)
			EndIf	
		EndIf
		
		While (Player.HasMagicEffect(crVampireInvisibillity2)) && VampireBloodPoints.GetValue() > 0
			Player.SetAlpha(0)
			
			If (! Player.HasMagicEffect(StableVampireInvisibility))
				Player.AddSpell(VampireInvisibilityRecast, abVerbose = False)				
				VampireInvisibilityRecast.Cast(Game.GetPlayer(), Game.GetPlayer())
			EndIf
			
			If (Player.HasMagicEffect(StableVampireInvisibility))
			EndIf
		EndWhile	

		utility.wait(1)
		Player.DispelSpell(VampireInvisibilityRecast)
		Player.RemoveSpell(VampireInvisibilityRecast)
		Player.DispelSpell(VampireInvisibilityPC)	
		Player.SetAlpha(1, true)
		
		If Player.IsOnMount()
		Else
			Float Weight = Player.GetWeight()
			Player.SetWeight(50)			
			Player.SetWeight(Weight)
			Player.QueueNiNodeUpdate()
		EndIf
		
	Else

		Game.GetPlayer().DispelSpell(VampireInvisibilityPC)
		Game.GetPlayer().DispelSpell(VampireInvisibilityRecast)
		VampireDeflectLightToggle.SetValue(0)

	EndIf	

EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	
	If VampireDeflectLightToggle.GetValue() == 1
		VampireInvisibilityPC.Cast(Game.GetPlayer(), Game.GetPlayer())
	EndIf
	
	If RECASTVampireDeflectLight.GetValue() <= 1
		RECASTVampireDeflectLight.SetValue(1)
	EndIf	
	
	Player = akTarget
	utility.wait(1)
	Player.DispelSpell(VampireInvisibilityRecast)
	Player.DispelSpell(VampireInvisibilityPC)		
	Player.RemoveSpell(VampireInvisibilityRecast)
	Player.SetAlpha(1, true)
	utility.wait(1)
	Player.DispelSpell(VampireInvisibilityRecast)
	Player.DispelSpell(VampireInvisibilityPC)		
	Player.RemoveSpell(VampireInvisibilityRecast)
	Player.SetAlpha(1, true)
	
	If Player.IsOnMount()
	Else
		Float Weight = Player.GetWeight()
		Player.SetWeight(50)		
		Player.SetWeight(Weight)
		Player.QueueNiNodeUpdate()
	EndIf	
	
	If RECASTVampireDeflectLight.GetValue() == 1
	
		RECASTVampireDeflectLight.SetValue(2)
		If VampireAbilityTimers.GetValue() == 0	
			Debug.Notification("You need to wait before attempting to deflect light again")	
			Utility.WaitGameTime(5)
			Utility.Wait(0.1)
			If Game.GetPlayer().HasSpell(VampireInvisibilityPC)	
				Debug.Notification("Deflect Light and Shadow is available for use")
			EndIf	
		EndIf	
		RECASTVampireDeflectLight.SetValue(0)
		
	EndIf
	
	VampireDeflectLightToggle.SetValue(0)	
	
EndEvent
