Scriptname _00E_FrozenMEScript extends activemagiceffect  
{Script that controls the behaviour of frozen enemies!}

;=====================================================================================
;              							STATES                  					 
;=====================================================================================

Auto State Default

Event OnEffectStart(Actor TheTarget, Actor TheCaster)

	FrozenActor = TheCaster

	If FrozenActor.GetAVPercentage("Health") > 0.1

		KillMove.SetValueInt(0)
		_00E_A1_ArcticWindFreezeM.Play(TheTarget)
		FrozenActor.AddPerk(_00E_FrozenPerk)
		FrozenActor.EnableAI(False)
		GoToState("Frozen")

	EndIf

EndEvent

EndState

;======================================FROZEN STATE================================

State Frozen

Event OnUpdate()

	bHitLock = False

EndEvent

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

	If !_00E_NoShatterNPCFormList.HasForm(FrozenActor.GetActorBase())

		If !PlayerREF.HasPerk(_04E_30_UNI_SongOfWinterFrostDamagePerk)
		
			ShatterChance = __Config_BaseShatterChance
			
		Else
		
			ShatterChance = __Config_BaseShatterChance*2
			
		EndIf
		
		If !bHitLock
		
			bHitLock = True
			
			If akSource.HasKeyword(WeaponTypeBluntWeapon)
				ShatterChance = (ShatterChance + __Config_ShatterChanceBluntWeaponBonus)
			EndIf
	
			If abPowerAttack == True
				ShatterChance = (ShatterChance + __Config_ShatterChancePowerAttackBonus)
			EndIf
	
			If ShockSpells.HasForm(akSource)
				ShatterChance = (ShatterChance + __Config_ShatterChanceShockSpellBonus)
			EndIf
		
			If akSource == _00E_A1_OnslaughtCloakDMG
				ShatterChance = (ShatterChance + __Config_ShatterChanceOnslaughtSpellBonus)
			
				If _00E_Synergy_ArcticWindOnslaught.GetValueInt() == 0
					_00E_Synergy_ArcticWindOnslaught.SetValueInt(1)
					Levelsystem.ShowSynergyMessage()
				EndIf
			
			EndIf

			If Utility.RandomFloat() < ShatterChance
				HasBeenShattered = true
				ShatterEnemy()
				GoToState("Finished")
			EndIf
		
			RegisterForSingleUpdate(fHitTimer)
	
		Endif

	EndIf
	
EndEvent

Event OnDying(Actor akKiller)

	FrozenActor.EnableAI(True)
	Self.Dispel()

EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	
	;If FrozenActor.GetAV("Health") < 1 && !_00E_NoShatterNPCFormList.HasForm(FrozenActor.GetActorBase())
	;	ShatterEnemy()	
	;Else
		_00E_A1_ArcticWindFreezeM.Play(akTarget)
		FrozenActor.SetAlpha(1.0, true)
	;EndIf

	FrozenActor.EnableAI(True)
	FrozenActor.RemovePerk(_00E_FrozenPerk)
	If Utility.GetINIBool("bVATSDisable:VATS") == 0
		KillMove.SetValueInt(1)
	EndIf
	GoToState("Finished")

EndEvent
	
EndState

;======================================FINISHED STATE================================

State Finished

	; Do nothing

EndState

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

Function ShatterEnemy()

	FrozenActor.Kill(PlayerREF)
	Utility.Wait(0.1)
	FrozenActor.PlaceAtMe(_00E_FrostShatteringExplosion02)
	
	if FrozenActor.HasKeyword(ActorTypeNPC) || FrozenActor.GetRace() == DraugrRace
		FrozenActor.PlaceAtMe(_00E_FS_MAGIceGoreExplosion)
	Else
		FrozenActor.PlaceAtMe(_00E_FrostShatteringExplosion)
	EndIf		
	
	FrozenActor.SetAlpha (0.0, true)
	FrozenActor.SetCriticalStage(FrozenActor.CritStage_DisintegrateStart)
	FrozenActor.AttachAshPile(_00E_ShatteredEnemyPile)
	WPNImpactBladeVsIce.Play(FrozenActor)
	Game.ShakeCamera(afStrength = 0.3)
	Utility.Wait(0.5)
	FrozenActor.SetCriticalStage(FrozenActor.CritStage_DisintegrateEnd)
	
	If _00E_AchievementsEnabled.GetValueInt() == 1 && _00E_FrozenAchievementUnlocked.GetValueInt() == 0
		Game.UnlockAchievement("END_FROZEN_01")
		_00E_FrozenAchievementUnlocked.SetValueInt(1)
	EndIf
	
EndFunction

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

float ShatterChance
float KillMoveChanceOriginal
bool HasBeenShattered
bool bHitLock

float fHitTimer = 1.5

float __Config_BaseShatterChance = 0.06
float __Config_ShatterChancePowerAttackBonus = 0.035
float __Config_ShatterChanceBluntWeaponBonus = 0.02
float __Config_ShatterChanceShockSpellBonus  = 0.025
float __Config_ShatterChanceOnslaughtSpellBonus  = 0.04

_00E_QuestFunctions Property Levelsystem Auto

Actor FrozenActor

GlobalVariable Property KillMove Auto
GlobalVariable Property _00E_Synergy_ArcticWindOnslaught Auto
GlobalVariable Property _00E_FrozenAchievementUnlocked Auto
GlobalVariable Property _00E_AchievementsEnabled Auto

Spell Property _00E_A1_OnslaughtCloakDMG Auto

Sound Property _00E_A1_ArcticWindFreezeM Auto

Race Property DraugrRace Auto

Actor Property PlayerREF Auto

Activator Property _00E_ShatteredEnemyPile Auto

Keyword Property WeaponTypeBluntWeapon Auto
Keyword Property ActorTypeNPC Auto

Explosion Property _00E_FrostShatteringExplosion Auto
Explosion Property _00E_FrostShatteringExplosion02 Auto
Explosion Property _00E_FS_MAGIceGoreExplosion Auto

FormList Property ShockSpells Auto

Perk Property _00E_FrozenPerk Auto
Perk Property _04E_30_UNI_SongOfWinterFrostDamagePerk Auto

Sound Property WPNImpactBladeVsIce Auto

FormList Property _00E_NoShatterNPCFormList Auto