Scriptname SuH_dynaES_player extends activemagiceffect  

SuH_Fun property _f auto
SuH_Status property _s auto

int procscript
int prochostile
actor attchar1
actor attchar2
int desireup

Event OnInit()
	_f = Game.GetFormFromFile(0x814, "Succubus Heart.esp") as SuH_Fun
	_s = Game.GetFormFromFile(0x16E496, "Succubus Heart.esp") as SuH_Status
endEvent

;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 = (_s.playerRef.GetActorValue("health") / _s.playerRef.GetBaseActorValue("health"))
		;Masochist Proc
		if _s.SuH_rank_masochistCD == 0
			if _s.SuH_rank_masochist2 > 0
				_s.playerRef.RestoreActorValue("Stamina", (1 + ((_s.SuH_PCSbpoint * 0.1) * _s.SuH_rank_masochist2)))
				_s.SuH_rank_masochistCD = 5
			endIf
			if _s.SuH_rank_masochist > 0
				_s.SuH_PCSdesire += Math.Floor(1 + ((_s.SuH_PCSbpoint * 0.1) * _s.SuH_rank_masochist))
				_s.SuH_rank_masochistCD = 5
			endIf
		endIf
			;Saving Essence Passive
		int rank = _s.SuH_rank_savingessence ; saving rank
		int z = _s.SuH_rank_savingessenceCD ; saving Cooldown
		if charHealthPercent <= 0.3 && rank > 0 && z == 0
			int k = _s.SuH_PCSstacksex ; sex stacks
			if k > _s.SuH_PCSstackmaxsex
				k = _s.SuH_PCSstackmaxsex
			endIf
			int y = _s.SuH_PCSstacksperm ; sperm stacks
			if y > _s.SuH_PCSstackmaxsperm
				y = _s.SuH_PCSstackmaxsperm
			endIf
			z = _s.SuH_PCSstackmilk ; milk stacks
			if z > _s.SuH_PCSstackmaxmilk
				z = _s.SuH_PCSstackmaxmilk
			endIf
			if k > 0 || y > 0 || z > 0
				int tstack = k + y + z
				if tstack > 0
					int x = Math.Floor((1 + (rank * (_s.SuH_PCSbpoint * 0.05)))*tstack)
					_s.playerRef.RestoreActorValue("health", x)
					_s.SuH_rank_savingessenceCD = 1
					debug.Notification("Saving Essence now on cooldown!")
					_s.SuH_PCSstacksex -= k
					_s.SuH_PCSstacksperm -= y
					_s.SuH_PCSstackmilk -= z
				endIf
			endIf
			;_Fun.perkcalc("savingessence")
		endIf
		float charHealthCurrent = _s.playerRef.GetActorValue("health")
		float charHealthBase = _s.playerRef.GetBaseActorValue("health")
		charHealthPercent = (charHealthCurrent / charHealthBase)
		float charStaminaCurrent = _s.playerRef.GetActorValue("Stamina")
		float charStaminaBase = _s.playerRef.GetBaseActorValue("Stamina")
		float charStaminaPercent = (charStaminaCurrent / charStaminaBase)
		if attChar != _s.playerRef && _s.SuH_SETcombatseduction
			; if attchar != attchar1
				; attchar2 = attchar1
				; attchar1 = attchar
			; endIf
				;Combat Seduction
			if _s.SuH_CHKcsplayersubmit || attChar.HasMagicEffect(_s.SuH_sexE_CScd)
			else
				_s.SuH_CHKcsplayersubmit = 1
				if charHealthPercent <= _s.SuH_SETpc2npchealth && _s.SuH_SETpc2npchealth > 0.00
					_s.SuH_CHKcsprocessing = 1
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = _s.playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_f.ANIMcalc(animActor, _s.playerRef) ; Send aggressive act to SuH_Fun
				elseif charHealthCurrent <= _s.SuH_SETpc2npchealthmin && _s.SuH_SETpc2npchealthmin > 0.00
					_s.SuH_CHKcsprocessing = 1
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = _s.playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_f.ANIMcalc(animActor, _s.playerRef) ; Send aggressive act to SuH_Fun
				elseif charStaminaPercent <= _s.SuH_SETpc2npcstamina && _s.SuH_SETpc2npcstamina > 0.00
					_s.SuH_CHKcsprocessing = 1
					actor[] animActor = new actor[2] ; Create Array of actors
					animActor[0] = attChar ; Set Aggressor
					animActor[1] = _s.playerRef ; Set player
					;animActor[2] = attChar2 ; Set Extra Aggressor
					_f.ANIMcalc(animActor, _s.playerRef) ; Send aggressive act to SuH_Fun
				endIf
				_s.SuH_CHKcsplayersubmit = 0
			endIf
		endIf
	endIf
	if procall == 1
		procscript = 0 ; Mark script as no longer calculating
	endIf
endEvent