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_rank_masochist auto
GlobalVariable property SuH_rank_savingessence auto
GlobalVariable property SuH_PCSdesire auto
GlobalVariable property SuH_PCSstacksperm auto
GlobalVariable property SuH_PCSstackmilk auto
GlobalVariable property SuH_CHKcsplayersubmit auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
	
EndEvent

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	Actor attChar = akAggressor as Actor ; Set Attacker as usable actor reference
	float charHealthPercent = (playerRef.GetActorValue("health") / playerRef.GetBaseActorValue("health"))
	int maso = Math.Floor(5 * SuH_rank_masochist.GetValue())
	int x = Math.Floor(maso + SuH_PCSdesire.GetValue())
	SuH_PCSdesire.SetValue(x)
	int rank = SuH_rank_savingessence.GetValue() as int
	if charHealthPercent <= 0.3 && rank > 0
		int sperm = SuH_PCSstacksperm.GetValue() as int
		int milk = SuH_PCSstackmilk.GetValue() as int
		if sperm > 0 || milk > 0
			int tstack = sperm + milk
			if tstack <= 10
				x = (rank*2)*tstack
				playerRef.RestoreActorValue("health", x)
				SuH_PCSstacksperm.SetValue(0)
				SuH_PCSstackmilk.SetValue(0)
			elseIf sperm == milk
				x = (rank*2)*10
				playerRef.RestoreActorValue("health", x)
				sperm -= 5
				milk -= 5
				SuH_PCSstacksperm.SetValue(sperm)
				SuH_PCSstackmilk.SetValue(milk)
			elseIf sperm > milk
				x = (rank*2)*10
				playerRef.RestoreActorValue("health", x)
				int j = sperm - milk
				if j > 10
					sperm -= 10
					SuH_PCSstacksperm.SetValue(sperm)
				else
					int k = Math.Floor((10 - j)*0.5)
					if (k*2) == (10 - j)
						sperm -= (k+j)
						milk -= k
						SuH_PCSstacksperm.SetValue(sperm)
						SuH_PCSstackmilk.SetValue(milk)
					else
						sperm -= (k+j+1)
						milk -= k
						SuH_PCSstacksperm.SetValue(sperm)
						SuH_PCSstackmilk.SetValue(milk)
					endIf
				endIf
			else ; milk > sperm
				x = (rank*2)*10
				playerRef.RestoreActorValue("health", x)
				int j = milk - sperm
				if j > 10
					milk -= 10
					SuH_PCSstackmilk.SetValue(milk)
				else
					int k = Math.Floor((10 - j)*0.5)
					if (k*2) == (10 - j)
						milk -= (k+j)
						sperm -= k
						SuH_PCSstacksperm.SetValue(sperm)
						SuH_PCSstackmilk.SetValue(milk)
					else
						milk -= (k+j+1)
						sperm -= k
						SuH_PCSstacksperm.SetValue(sperm)
						SuH_PCSstackmilk.SetValue(milk)
					endIf
				endIf
			endIf        
		; X and Y must land as evenly as possible
		; X = RanInt1 Y = RanInt2 Z = Subtracted Value
		;if (X=Y) K = Math.Floor(Z*0.5)
		;	if ((K*2) =Z) X -= K , Y -= K
		;	else  X -= K+1 , Y -= K
		;elseif (X>Y) J = X-Y , K = Math.Floor((Z-J)*0.5)
		;	if ((K*2) = Z-j) X -= K+J , Y -= K
		;	else  X -= K+J+1 , Y -= K
		;elseif (Y>X) J = Y-X , K = Math.Floor((Z-J)*0.5)
		;	if ((k*2) = Z-j) X -= K , Y -= K+J
		;	else  X -= K+1 , Y -= K+J
		endIf
	endIf
	charHealthPercent = (playerRef.GetActorValue("health") / playerRef.GetBaseActorValue("health"))
	if attChar != playerRef
		if SuH_SETcombatseduction.GetValue()
			if SuH_CHKcsplayersubmit.GetValue()
				;do nothing
			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
					_Fun.ANIMcalc("0", animActor, playerRef) ; Send aggressive act to SuH_Fun
				endIf
				SuH_CHKcsplayersubmit.SetValue(0)
			endIf
		endIf
	endIf
endEvent