Scriptname _00E_A1_DevourSoulSC extends activemagiceffect  

Import Utility

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

Event OnEffectStart(Actor akTarget, Actor akCaster)

	If akTarget.IsInFaction(NoDevourSoulFaction)
		(PlayerREF as _00E_Game_TalentControlSC).SetTalentRecoveryTime(_00E_A1_DevourSoul, 0)
		Return
	ElseIf !akTarget.IsDead()
		_00E_Levelsystem_sEnemyHasToBeDead.Show()
		(PlayerREF as _00E_Game_TalentControlSC).SetTalentRecoveryTime(_00E_A1_DevourSoul, 0)
		Return
	Else
		Target = akTarget
		Caster = akCaster
		akTarget.ApplyHavokImpulse(0.0, 0.0, 100.0, 10.0)
		_00E_A1_DevourSoulIMOD.Apply()
		_00E_A1_DevourSoulScreamM.Play(akTarget)
		
		if akTarget.HasKeyword(TypeHumanoid)
			_00E_A1_DevourSoulVFX.Cast(akTarget, akTarget)
		EndIf

		Int iTalentLevel
		If Caster.HasPerk(_00E_Class_LifeAndDeath_P05_Talent_DevourSoul3)
			iTalentLevel = 3
		ElseIf Caster.HasPerk(_00E_Class_LifeAndDeath_P05_Talent_DevourSoul2)
			iTalentLevel = 2
		Else
			iTalentLevel = 1
		EndIf
		
		Float fTalentMagnitude = _00E_A1_DevourSoulSP.GetNthEffectMagnitude(iTalentLevel - 1) / 100.0

		HealthToDrain = akTarget.GetBaseActorValue("Health") * fTalentMagnitude

		If iTalentLevel >= 2
			MagickaToDrain = akTarget.GetBaseActorValue("Magicka") * fTalentMagnitude
		Else
			MagickaToDrain = 0
		EndIf

		If iTalentLevel >= 3
			StaminaToDrain = akTarget.GetBaseActorValue("Stamina") * fTalentMagnitude
		Else
			StaminaToDrain = 0
		EndIf
		
		Self.PlayVictimAndPlayerFX()
		Wait(1.5)
		akTarget.SetCriticalStage(akTarget.CritStage_DisintegrateStart)
		_00E_A1_DevourSoul_DisintegrateFXS.Play(akTarget)
		Self.AbsorbEnergy()
		Wait(1.5)
		Self.ExplodeVictim()
		akTarget.SetCriticalStage(akTarget.CritStage_DisintegrateEnd)
	EndIf

EndEvent		

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

Function PlayVictimAndPlayerFX()

	ParalyzeFxShader.Play(Target)
	MAGVampireTransform01.Play(Caster)
	CastLPM = MAGMystisicmSoulTrapActiveLP.Play(Caster)
	AbsorbGreenCastVFX01.Play(Caster, akFacingObject = Target)
	AbsorbGreenTargetVFX01.Play(Target, akFacingObject = Caster)
	ParalyzeFxShader.Play(Caster)	

EndFunction

Function ExplodeVictim()

	Game.ShakeCamera(afStrength = 0.2)
	Sound.StopInstance(CastLPM)
	ObjectReference EXPMarker = Target.PlaceAtMe(XMarker, 1)
	EXPMarker.MoveTo(Target, 0.0, 0.0, 50.0)
	EXPMarker.PlaceAtMe(_00E_MAGEldritchShockExplosion, 1)
	Target.AttachAshPile(_00E_EldritchPile)
	Target.SetAlpha(0.0, true)
	ParalyzeFxShader.Stop(Target)
	ParalyzeFxShader.Stop(Caster)
	AbsorbGreenCastVFX01.Stop(Caster)
	AbsorbGreenTargetVFX01.Stop(Target)
	Target.InterruptCast()
	Target.DispelAllSpells()
	Target.Kill(Caster)


EndFunction

Function AbsorbEnergy()

	Caster.RestoreActorValue("Health", HealthToDrain)
	If MagickaToDrain > 0.0
		Caster.RestoreActorValue("Magicka", MagickaToDrain)
	EndIf
	If StaminaToDrain > 0.0
		Caster.RestoreActorValue("Stamina", StaminaToDrain)
	EndIf

	If PlayerREF.HasSpell(_00E_Affinity_AbDarkKeeper)
		
		WerewolfExtractVFX.Play(PlayerREF)
		_00E_Affinity_AbDarkKeeprSP.Cast(PlayerREF, PlayerREF)
		NPCWerewolfTransformation.Play(PlayerREF)
		WerewolfExtractVFX.Stop(PlayerREF)
		
	EndIf

EndFunction


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

Actor Caster
Actor Target

Message Property _00E_Levelsystem_sEnemyHasToBeDead Auto

Actor Property PlayerREF Auto

float Property __Config_StartDelay = 1.0 Auto
{Time to wait before the healing kicks in. Default: 1.0}

float HealthToDrain
float StaminaToDrain
float MagickaToDrain

int CastLPM

Perk Property _00E_Class_LifeAndDeath_P05_Talent_DevourSoul2 Auto
Perk Property _00E_Class_LifeAndDeath_P05_Talent_DevourSoul3 Auto

Sound Property _00E_A1_DevourSoulScreamM  Auto
Sound Property MAGVampireTransform01 Auto
Sound Property MAGMystisicmSoulTrapActiveLP Auto
Sound Property NPCWerewolfTransformation Auto

Explosion Property _00E_MAGEldritchShockExplosion Auto

Keyword Property TypeHumanoid Auto

EffectShader Property ParalyzeFxShader Auto
EffectShader Property _00E_A1_DevourSoul_DisintegrateFXS Auto

Spell Property _00E_A1_DevourSoulVFX Auto
Spell Property _00E_A1_DevourSoulSP Auto
Spell Property _00E_Affinity_AbDarkKeeper Auto
Spell Property _00E_Affinity_AbDarkKeeprSP Auto

Static Property XMarker Auto

VisualEffect Property AbsorbGreenCastVFX01 Auto
VisualEffect Property AbsorbGreenTargetVFX01 Auto
VisualEffect Property WerewolfExtractVFX Auto

Activator Property _00E_EldritchPile Auto

ImagespaceModifier Property _00E_A1_DevourSoulIMOD Auto

Shout Property _00E_A1_DevourSoul Auto

Faction Property NoDevourSoulFaction Auto