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_CHKcsplayersubmit auto
MagicEffect Property SuH_sexE_CScd auto

int procscript
actor attchar1
actor attchar2

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
		procscript = 1
		procall = 1
	endif
	int x = SuH_rank_masochist2.GetValue() as int ; Masochist Stamina Passive
	float phealth = playerRef.GetBaseActorValue("health")
	float nphealth = phealth - x
	if phealth != nphealth && nphealth > 10
		playerRef.RestoreActorValue("Stamina", x*5)
		x = 1 ; Set Maso2 proc as true
	endIf
	; Process ALL cutoff
	if procall == 1
		if x == 1
			playerRef.SetActorValue("health", nphealth) ; Apply Maso2 cost
		endIf
		Actor attChar = akAggressor as Actor ; Set Attacker as usable actor reference
		float charHealthPercent = (playerRef.GetActorValue("health") / playerRef.GetBaseActorValue("health"))
			; Masochist Desire Passive
		int y = Math.Floor(5 * SuH_rank_masochist.GetValue()) ; desire gain
		x = Math.Floor(y + SuH_PCSdesire.GetValue()) ; new desire value
		SuH_PCSdesire.SetValue(x)
			;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
			y = SuH_PCSstacksperm.GetValue() as int ; sperm stacks
			z = SuH_PCSstackmilk.GetValue() as int ; milk stacks
			if k > 0 || y > 0 || z > 0
				int tstack = k + y + z
				if tstack > 0
					x = (rank*2)*tstack
					playerRef.RestoreActorValue("health", x)
					SuH_rank_savingessenceCD.SetValue(1)
					debug.Notification("Saving Essence now on cooldown!")
				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()
					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()
					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()
					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