Scriptname _00E_A2_RiftSC extends activemagiceffect  

Import _00E_TalentLibrary
import Utility
Import Debug

;=====================================================================================
;              							EVENTS                  					 
;=====================================================================================

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

	bRegisteredForSynergy

	If akSource == _00E_A2_SkyfallBoltSP && !bRegisteredForSynergy
		bRegisteredForSynergy = True
		bSynergyImplosionEffect = True
		;We can't use Dispel() here, it will result in the target being stuck in dimensional rift forever, fixing https://sureai.net/tracker/view.php?id=107 partially
		;Self.Dispel()
	EndIf

EndEvent

Event OnDying(Actor akKiller)

	Self.Dispel()

EndEvent

Event OnEffectStart(Actor akTarget, Actor akCaster)

	Target = akTarget

	If !(IsTargetValid(Target))
		(PlayerREF as _00E_Game_TalentControlSC).SetTalentRecoveryTime(_00E_A1_Rift, 0)
		Return
	Elseif IsTargetValid(Target)
		TargetWasValid = True
		Target.SetActorCause(PlayerREF)
		PlayFXAndAnimation()
		DrainLoop()
	EndIf
	
EndEvent	

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	
	If TargetWasValid
		Finished = True
		StopFXAndCastFinalExplosion()
		Wait(3)
		Target.SetActorCause(None)
		CleanUp()
		TargetWasValid = False
	EndIf	
	
EndEvent

Event OnUpdate()

	If (fEnemyPositionAtStartX != Target.GetPositionX()) || (fEnemyPositionAtStartY != Target.GetPositionY())
		_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
		Wait(1)
		Target.EnableAI(False)
	EndIf

EndEvent

;=====================================================================================
;              							FUNCTIONS                  					 
;=====================================================================================

bool Function IsTargetValid(Actor iTarget)
		
		Float iAllowedLevelDifference = _00E_A1_RiftSP.GetNthEffectMagnitude(3)
		
	If  (Target.GetLevel()) >= (PlayerLevel.GetValueInt() + iAllowedLevelDifference as Int)
		_00E_Levelsystem_sAbilityEnemyLevelTooHigh.Show()
		return False
	ElseIf Target.IsDead()
		_00E_Levelsystem_sAbilityEnemyHasToBeAlive.Show()
		return False
	ElseIf Target.HasKeyword(MagicNoRift)
		_00E_Levelsystem_sAbilityTargetImmune.Show()
		return False
	Else
		Return True
	EndIf

EndFunction


Function PlayFXAndAnimation()	

	Target.PlaceAtMe(ThrowVoiceExplosion)
	_00E_A1_RiftIntroSound.Play(PlayerREF)
	GhostEtherealFXShader.Play(PlayerREF)
	PlaceMarker = Target.PlaceAtMe(XMarker)
	Target.SetGhost(True)
	
	ActorBase TargetBase = Target.GetBaseObject() as ActorBase
	
	if TargetBase.HasKeyword(TypeHumanoid) && !(TargetBase.GetRace() == DraugrRace)
		
		if (TargetBase.GetSex() == 0)
			_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
			Wait(1)
			
			If !Target.PlayIdle(IdleT02AscendMale)
				_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
				Wait(1)
				Target.EnableAI(False)
			EndIf
			
		ElseIf (TargetBase.GetSex() == 1)
			_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
			Wait(1)
			
			If !Target.PlayIdle(IdleT02AscendFemale)
				_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
				Wait(1)
				Target.EnableAI(False)
			EndIf
			
		EndIf
		
	Else
		_00E_Ability_StaggerSelfSpell.RemoteCast(Target, PlayerREF, Target) 
		Wait(1)
		Target.EnableAI(False)
	EndIf
		
	GhostEtherealFXShader.Play(Target)	
	Target.SetAlpha (0.6, true)
	
	iRiftDurationSound = _00E_A1_RiftDurSound.Play(Target)
	
	Wait(2)
	RiftLight = Target.PlaceAtMe(_00E_A1_RiftLight)
	
	
EndFunction

Function DrainLoop()

	float iWait = ((_00E_A1_RiftSP.GetNthEffectDuration(0) - 3)/3)
	iIndex = _00E_TalentLibrary.GetPlayerTalentLevel(_00E_Class_Thaumaturge_P04_Talent_DimensionalRift_01, _00E_Class_Thaumaturge_P04_Talent_DimensionalRift_02, _00E_Class_Thaumaturge_P04_Talent_DimensionalRift_03) - 1
	float fDrainAmountMagicka = ((Target.GetActorValue("Magicka"))*((_00E_A1_RiftSP.GetNthEffectMagnitude(iIndex)/100)/(_00E_A1_RiftSP.GetNthEffectDuration(0)/3)))
	float fDrainAmountStamina = ((Target.GetActorValue("Stamina"))*((_00E_A1_RiftSP.GetNthEffectMagnitude(iIndex)/100)/(_00E_A1_RiftSP.GetNthEffectDuration(0)/3)))
	
	While !Finished
		DrainManaAndStamina(fDrainAmountMagicka, fDrainAmountStamina)
		Wait(iWait)	
	EndWhile

EndFunction
	
Function DrainManaAndStamina(float fDrainAmountMagicka, float fDrainAmountStamina)
	
	GhostEtherealFXShaderIntro.Play(Target)
	_00E_A1_RiftDrainSound.Play(PlayerREF)
	Target.DamageActorValue("Magicka", fDrainAmountMagicka)
	PlayerREF.RestoreActorValue("Magicka", fDrainAmountMagicka)
	Target.DamageActorValue("Stamina", fDrainAmountStamina)
	PlayerREF.RestoreActorValue("Stamina", fDrainAmountStamina)
	iFinalImpact = iFinalImpact + (fDrainAmountMagicka + fDrainAmountStamina)
	_00E_A2_RiftDrainImod.Apply()

EndFunction
	
Function StopFXAndCastFinalExplosion()

	Target.SetGhost(False)
	

	if bSynergyImplosionEffect 
		;shifted the code around and introduced a Kill() to properly get rid of the target after the explosion
		;together with the removed Dispel() from line 20 this will fix problems with the synergy https://sureai.net/tracker/view.php?id=107
		;but changing the synergy in a way that the enemy won't explode right away
		bSynergyImplosionEffect = False
		bRegisteredForSynergy = False

		Game.ShakeCamera(afStrength = 0.25)
		Target.RemovePerk(_00E_A2_RiftEtheralPerk)
		GhostEtherealFXShader.Stop(PlayerREF)
		GhostEtherealFXShader.Stop(Target)
		;Target.SetAlpha(1, true)
		;Target.EnableAI(true)
		_00E_A2_SynergyRiftImplosionDMGEnch.SetNthEffectMagnitude(0, iFinalImpact)
		Target.KnockAreaEffect(1, 512)
		Target.PlaceAtMe(_00E_A2_SynergyRiftImplosion, 1)
		MAGShockExpMassImod.Apply()
		
		If ((Target.GetActorValue("Health") - iFinalImpact*2) <= 0)

			AtronachUnsummonDeathFXS.Play(Target)
			Target.Kill(PlayerREF)
			Target.SetAlpha (0.0,True)
			Target.SetCriticalStage(Target.CritStage_DisintegrateStart)
			Target.AttachAshPile(_00E_NPCOorbayaAshPile)
			Wait(0.5)
			Target.SetCriticalStage(Target.CritStage_DisintegrateEnd)
		EndIf
		
		If _00E_Synergy_RiftSkyfall.GetValueInt() == 0
			_00E_Synergy_RiftSkyfall.SetValueInt(1)
			_00E_PlayerFunctions.GetSkillControl().ShowSynergyMessage()
		EndIf
		
		Self.Dispel()
		
	Else

		MAGMysticismSoulTrapCapture.Play(Target)
		_00E_A1_RiftExplosionSound.Play(Target)
		Target.PlaceAtMe(ThrowVoiceExplosion)
		_00E_A2_RiftExplosionImod.Apply()
		Target.DamageActorValue("Health", iFinalImpact/2)	
		PlayerREF.PushActorAway(Target, 1)
		Target.RemovePerk(_00E_A2_RiftEtheralPerk)
		Target.EnableAI(true)
		Game.ShakeCamera(afStrength = 0.5)
		GhostEtherealFXShader.Stop(PlayerREF)
		GhostEtherealFXShader.Stop(Target)
		Target.SetAlpha(1, true)
	
	EndIf
	
	Sound.StopInstance(iRiftDurationSound)
	
EndFunction

Function CleanUp()

	PlaceMarker.Delete()
	RiftLight.Delete()

EndFunction

;=====================================================================================
;              							PROPERTIES                  					 
;=====================================================================================

float fEnemyPositionAtStartX
float fEnemyPositionAtStartY

bool TargetWasValid
bool Finished
bool bSynergyImplosionEffect
bool bRegisteredForSynergy
float iFinalImpact
int iindex
int iRiftDurationSound

Message Property _00E_Levelsystem_sAbilityEnemyLevelTooHigh Auto
Message Property _00E_Levelsystem_sAbilityEnemyHasToBeAlive Auto
Message Property _00E_Levelsystem_sAbilityTargetImmune Auto

Actor Target
ObjectReference PlaceMarker
ObjectReference RiftLight

Activator Property _00E_NPCOorbayaAshPile Auto

Static Property XMarker Auto

Enchantment Property _00E_A2_SynergyRiftImplosionDMGEnch Auto

ImageSpaceModifier Property MAGShockExpMassImod Auto

EffectShader Property GhostEtherealFXShaderIntro Auto
EffectShader Property GhostEtherealFXShader Auto
EffectShader Property AtronachUnsummonDeathFXS Auto

Explosion Property _00E_A2_SynergyRiftImplosion Auto
Explosion Property ThrowVoiceExplosion Auto

Perk Property _00E_Class_Thaumaturge_P04_Talent_DimensionalRift_01 Auto
Perk Property _00E_Class_Thaumaturge_P04_Talent_DimensionalRift_02 Auto
Perk Property _00E_Class_Thaumaturge_P04_Talent_DimensionalRift_03 Auto

Idle Property IdleT02AscendMale Auto
Idle Property IdleT02AscendFemale Auto

Light Property _00E_A1_RiftLight Auto

Race Property DraugrRace Auto

Perk Property _00E_A2_RiftEtheralPerk Auto

Sound Property _00E_A1_RiftIntroSound Auto
Sound Property _00E_A1_RiftDurSound Auto
Sound Property MAGMysticismSoulTrapCapture Auto
Sound Property _00E_A1_RiftExplosionSound Auto
Sound Property _00E_A1_RiftDrainSound Auto

Spell Property _00E_Ability_StaggerSelfSpell Auto
Spell Property _00E_A1_RiftSP Auto
Spell Property _00E_A2_SkyfallBoltSP Auto

Actor Property PlayerREF Auto

GlobalVariable Property PlayerLevel Auto
GlobalVariable Property _00E_Synergy_RiftSkyfall Auto

ImagespaceModifier Property _00E_A2_RiftDrainImod Auto
ImagespaceModifier Property _00E_A2_RiftExplosionImod Auto

KeyWord Property TypeHumanoid Auto
KeyWord Property MagicNoRift Auto

Shout Property _00E_A1_Rift Auto