Scriptname _00E_NQ_G_07_PetrifiedCombatSC extends ReferenceAlias  

Import Math

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

Event OnInit()

	akSelf = Self.GetActorReference()
	
	fOriginalStrength = __Config_fShieldStrength
	
EndEvent

Event OnDying(Actor akKiller)
	
	If __Config_fShieldStrength > 0
		StopProtectiveBarrier()
	EndIf

EndEvent

Event OnEnterBleedout()

	If (bIsDevra && !bDevraProtected) || (!bIsDevra && !bAlyxProtected)
		SpawnShield()
	EndIf
	
EndEvent

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	
	If akAggressor == PlayerREF && !akSelf.IsDead() && akSelf.IsBleedingOut()
		If _00E_NQ_G_07_PetrifiedProtected.GetValueInt() == 1
			_00E_NQG07_s_PetrifiedCombatAttackHasNoEffect.Show()		
			MAGFail.Play(akSelf)
		Else
			DamageShield(akSource, akSource.GetType())
			MAGShockImpact.Play(akSelf)
			If !bIsFlickering
				Flicker()
			EndIf
		Endif
	EndIf
	
EndEvent

Event OnUpdate()
	
	If __Config_fShieldStrength > 0
	
		akSelf.RestoreAV("Health", akSelf.GetBaseAV("Health"))
		StopProtectiveBarrier()

	EndIf

EndEvent

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

Function SpawnShield()

	_00E_A2_GhostwalkTeleportVFX.Play(akSelf)
	AzuraEvil.Play(akSelf)
	akSelf.SetAlpha(0.75)

	If bIsDevra 
		If !(_00E_NQ_G_07_AlyxREF.IsBleedingOut()) && !(_00E_NQ_G_07_AlyxREF.IsDead())
			NightingaleStrifeAbsorbTargetVFX.Play(_00E_NQ_G_07_AlyxREF, akFacingObject = _00E_NQ_G_07_DevraREF)
			bDevraProtected = True
			_00E_NQ_G_07_PetrifiedProtected.SetValueInt(1)
		EndIf
	Else
		If !(_00E_NQ_G_07_DevraREF.IsBleedingOut()) && !(_00E_NQ_G_07_DevraREF.IsDead())
			NightingaleStrifeAbsorbTargetVFX.Play(_00E_NQ_G_07_DevraREF, akFacingObject = _00E_NQ_G_07_AlyxREF)
			bAlyxProtected = True
			_00E_NQ_G_07_PetrifiedProtected.SetValueInt(1)
		EndIf
	EndIf
	
	If (_00E_NQ_G_07_DevraREF.IsBleedingOut() && _00E_NQ_G_07_AlyxREF.IsBleedingOut()) || (_00E_NQ_G_07_DevraREF.IsDead() || _00E_NQ_G_07_AlyxREF.IsDead())
		_00E_NQ_G_07_PetrifiedProtected.SetValueInt(0)
	EndIf
	
	If !akSelf.IsDead()
		RegisterForSingleUpdate(7)
	EndIf
	_00E_A2_GhostwalkTeleportVFX.Stop(akSelf)

EndFunction 

Function StopProtectiveBarrier()
	
	if bIsDevra
		bDevraProtected = False
	else
		bAlyxProtected = False
	endif
	_00E_NQ_G_07_PetrifiedProtected.SetValueInt(1)
	NQ_G_07_BarrierREF.PlayAnimation("PlayAnim01")
	NQ_G_07_BarrierREF.Disable()
	AzuraEvil.Stop(akSelf)
	NightingaleStrifeAbsorbTargetVFX.Stop(akSelf)
	akSelf.SetAlpha(1)
	MAGRestorationFFFire.Play(akSelf)
	
	If !akSelf.IsDead()
		akSelf.StartCombat(PlayerREF)
	EndIf
		
EndFunction

Function Flicker()
	
	bIsFlickering = true
	MAGShockImpact.Play(akSelf)
	akSelf.SetAlpha(0.3)
	Utility.Wait(0.5)
	akSelf.SetAlpha(0.7)
	Utility.Wait(0.4)
	akSelf.SetAlpha(0.5)
	Utility.Wait(0.6)
	akSelf.SetAlpha(0.75)
	bIsFlickering = false
	
EndFunction

Function DamageShield(Form akDamageSource, int iDamageSourceType)

	__Config_fShieldStrength = __Config_fShieldStrength - GetDealtDamage(akDamageSource, iDamageSourceType)

	If (__Config_fShieldStrength > 0) && ((fOriginalStrength - fCurrentSubtrahend) >= __Config_fShieldStrength)
		float fCurrentPercentage = ((__Config_fShieldStrength)/(fOriginalStrength/100))
		_00E_NQG07_s_PetrifiedCombatPsionicShieldReduced.Show(fCurrentPercentage)
		fCurrentSubtrahend += 40
	ElseIf __Config_fShieldStrength <= 0
		NQ_G_07.RemoveEssentialFlag(akSelf)
		akSelf.Kill(PlayerREF)
		UnregisterForUpdate()
	EndIf
	
EndFunction

float Function GetDealtDamage(Form akDamageSource, int iDamageSourceType)

	if iDamageSourceType == 41
		Weapon wpSourceWeapon = akDamageSource as Weapon
		Return wpSourceWeapon.GetBaseDamage() as Float
	Elseif iDamageSourceType == 22 || iDamageSourceType == 21
		Return 25.0
	EndIf

EndFunction
;=====================================================================================
;              							PROPERTIES                  					 
;=====================================================================================

int iMessageCounter 

bool bDevraProtected
bool bAlyxProtected

bool Property bIsDevra Auto
bool bIsFlickering = false

float fOriginalStrength
float Property fCurrentSubtrahend = 0.0 Auto

float Property __Config_fShieldStrength = 200.0 Auto
float Property __Config_fHealthPercentage = 0.3 Auto

Message Property _00E_NQG07_s_PetrifiedCombatAttackHasNoEffect Auto
Message Property _00E_NQG07_s_PetrifiedCombatPsionicShieldReduced Auto

; ADDED IN FORGOTTEN STORIES
ObjectReference Property NQ_G_07_BarrierREF Auto

Actor akSelf

Sound Property MAGFail Auto
Sound Property MAGShockImpact Auto
Sound Property MAGRestorationFFFire Auto

Actor Property _00E_NQ_G_07_AlyxREF Auto
Actor Property _00E_NQ_G_07_DevraREF Auto
Actor Property PlayerREF Auto

EffectShader Property AzuraEvil Auto

VisualEffect Property NightingaleStrifeAbsorbTargetVFX Auto
VisualEffect Property _00E_A2_GhostwalkTeleportVFX Auto

GlobalVariable Property _00E_NQ_G_07_PetrifiedProtected Auto

_00E_NQ_G_07_Functions Property NQ_G_07 Auto