Scriptname SuH_dynaES_player extends activemagiceffect  

SuH_Fun property _Fun auto
actor property playerRef auto
GlobalVariable property SuH_SETcombatseduction auto
GlobalVariable property SuH_SETpc2npchealth auto
GlobalVariable property SuH_SETpc2npchealthmin auto
GlobalVariable property SuH_SETpc2npcstamina auto
GlobalVariable property SuH_SETspecialdeath auto
GlobalVariable property SuH_rank_masochist auto
GlobalVariable property SuH_rank_masochist2 auto
GlobalVariable property SuH_rank_savingessence auto
GlobalVariable property SuH_rank_savingessenceCD auto
GlobalVariable property SuH_PCSdesire auto
GlobalVariable property SuH_PCSstacksex auto
GlobalVariable property SuH_PCSstacksperm auto
GlobalVariable property SuH_PCSstackmilk auto
GlobalVariable property SuH_PCSstackmaxsex auto
GlobalVariable property SuH_PCSstackmaxmilk auto
GlobalVariable property SuH_PCSstackmaxsperm auto
GlobalVariable property SuH_CHKcsplayersubmit auto
GlobalVariable property SuH_CHKcsprocessing auto
GlobalVariable Property SuH_PCSbpoint auto
MagicEffect Property SuH_sexE_CScd auto

int procscript
int prochostile
actor attchar1
actor attchar2
int desireup

Event OnEffectStart(Actor akTarget, Actor akCaster)
	
EndEvent

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	int procall = 0
	if procscript == 0 ; Check if script is already calculating, if not then exicute initiate function
		procscript = 1
		procall = 1
		if akSource as Spell
			Spell aggspell = akSource as spell
			if aggspell.IsHostile()
				prochostile = 1
			else
				prochostile = 0
			endIf
		else
			prochostile = 1
		endIf
	endif
	; Process ALL cutoff
	if procall == 1 && prochostile == 1
		Actor attChar = akAggressor as Actor ; Set Attacker as usable actor reference
		float charHealthPercent = (playerRef.GetActorValue("health") / playerRef.GetBaseActorValue("health"))
		;Masochist Proc
		int maso1 = SuH_rank_masochist.GetValue() as int
		int maso2 = SuH_rank_masochist2.GetValue() as int
		int maso = maso1 + maso2
		if maso2 > 0
			playerRef.RestoreActorValue("Stamina", (1 + ((SuH_PCSbpoint.GetValue() * 0.1) * maso2)))
		endIf
		if maso1 > 0
			int y = Math.Floor((1 + ((SuH_PCSbpoint.GetValue() * 0.1) * maso1)) + SuH_PCSdesire.GetValue()) ; desire gain
			SuH_PCSdesire.SetValue(y)
		endIf
			;Saving Essence Passive
		int rank = SuH_rank_savingessence.GetValue() as int ; saving rank
		int z = SuH_rank_savingessenceCD.GetValue() as int ; saving Cooldown
		if charHealthPercent <= 0.3 && rank > 0 && z == 0
			int k = SuH_PCSstacksex.GetValue() as int ; sex stacks
			if k > SuH_PCSstackmaxsex.GetValue()
				k = SuH_PCSstackmaxsex.GetValue() as int
			endIf
			int y = SuH_PCSstacksperm.GetValue() as int ; sperm stacks
			if y > SuH_PCSstackmaxsperm.GetValue()
				y = SuH_PCSstackmaxsperm.GetValue() as int
			endIf
			z = SuH_PCSstackmilk.GetValue() as int ; milk stacks
			if z > SuH_PCSstackmaxmilk.GetValue()
				z = SuH_PCSstackmaxmilk.GetValue() as int
			endIf
			if k > 0 || y > 0 || z > 0
				int tstack = k + y + z
				if tstack > 0
					int x = Math.Floor((1 + (rank * (SuH_PCSbpoint.GetValue() * 0.05)))*tstack)
					playerRef.RestoreActorValue("health", x)
					SuH_rank_savingessenceCD.SetValue(1)
					debug.Notification("Saving Essence now on cooldown!")
					SuH_PCSstacksex.SetValue(SuH_PCSstacksex.GetValue() - k)
					SuH_PCSstacksperm.SetValue(SuH_PCSstacksperm.GetValue() - y)
					SuH_PCSstackmilk.SetValue(SuH_PCSstackmilk.GetValue() - z)
				endIf
			endIf
			;_Fun.perkcalc("savingessence")
		endIf
		float charHealthCurrent = playerRef.GetActorValue("health")
		float charHealthBase = playerRef.GetBaseActorValue("health")
		charHealthPercent = (charHealthCurrent / charHealthBase)
		float charStaminaCurrent = playerRef.GetActorValue("Stamina")
		float charStaminaBase = playerRef.GetBaseActorValue("Stamina")
		float charStaminaPercent = (charStaminaCurrent / charStaminaBase)
		if attChar != playerRef && SuH_SETcombatseduction.GetValue()
			; if attchar != attchar1
				; attchar2 = attchar1
				; attchar1 = attchar
			; endIf
				;Combat Seduction
			if SuH_CHKcsplayersubmit.GetValue() || attChar.HasMagicEffect(SuH_sexE_CScd)
			else
				SuH_CHKcsplayersubmit.SetValue(1)
				if charHealthPercent <= SuH_SETpc2npchealth.GetValue() && SuH_SETpc2npchealth.GetValue() > 0.00
					SuH_CHKcsprocessing.SetValue(1)
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_Fun.ANIMcalc(animActor, playerRef) ; Send aggressive act to SuH_Fun
				elseif charHealthCurrent <= SuH_SETpc2npchealthmin.GetValue() && SuH_SETpc2npchealthmin.GetValue() > 0.00
					SuH_CHKcsprocessing.SetValue(1)
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_Fun.ANIMcalc(animActor, playerRef) ; Send aggressive act to SuH_Fun
				elseif charStaminaPercent <= SuH_SETpc2npcstamina.GetValue() && SuH_SETpc2npcstamina.GetValue() > 0.00
					SuH_CHKcsprocessing.SetValue(1)
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_Fun.ANIMcalc(animActor, playerRef) ; Send aggressive act to SuH_Fun
				endIf
				SuH_CHKcsplayersubmit.SetValue(0)
			endIf
		endIf
	endIf
	if procall == 1
		procscript = 0 ; Mark script as no longer calculating
	endIf
endEvent