ScriptName SuH_PCSsubmitScript extends ActiveMagicEffect

;ent/ Global Variables
GlobalVariable Property SuH_PCSheartXP auto
GlobalVariable Property SuH_PCShpabsorb auto
GlobalVariable Property SuH_PCSmpabsorb auto
GlobalVariable Property SuH_PCSspabsorb auto
GlobalVariable Property SuH_PCSheartXPgain auto
GlobalVariable Property SuH_NPChdry auto
GlobalVariable Property SuH_NPCmdry auto
GlobalVariable Property SuH_NPCsdry auto
GlobalVariable Property SuH_NPCgenders auto
GlobalVariable Property SuH_PCScapup auto
GlobalVariable Property SuH_skill_essence auto
GlobalVariable Property SuH_skill_earned auto
GlobalVariable Property SuH_skill_points auto
GlobalVariable Property SuH_SETmultivictim auto
;end/
;ent/ Magic Effects
MagicEffect Property SuH_sp_humandesireseffect auto
MagicEffect Property SuH_sp_animaldesireseffect auto
;end/
;ent/ Actors
Actor Property playerRef Auto
;end/
int xpUP
int hUP
int mUP
int sUP
int eUP
int pHa
int pMa
int pSa
int plvl
int NPCgenders

float XPgain
float XPmod
float aMOD

Event OnEffectStart(Actor akTarget, Actor akCaster)
	;ent/ Set Variables
	aMOD = SuH_SETmultivictim.GetValue()
	pHa = Math.Floor(SuH_PCShpabsorb.GetValue())
	pMa = Math.Floor(SuH_PCSmpabsorb.GetValue())
	pSa = Math.Floor(SuH_PCSspabsorb.GetValue())
	XPgain = SuH_PCSheartXPgain.GetValue()
	plvl = playerRef.GetLevel()
	NPCgenders = Math.Floor(SuH_NPCgenders.GetValue())
	;end/
	;ent/ Check Passives
	If playerRef.HasMagicEffect(SuH_sp_humandesireseffect)
		if NPCgenders >= 1 && NPCgenders <= 2 || NPCgenders == 12
			XPmod += 0.5
		endIf
	endIf
	If playerRef.HasMagicEffect(SuH_sp_animaldesireseffect) && NPCgenders == 3
		XPmod += 0.5
	endIf
	;end/
	
	RegisterForSingleUpdate(5.0)
EndEvent

Event OnUpdate()
	hUP += pHa
	mUP += pMa
	sUP += pSa
	xpUP += 10
	RegisterForSingleUpdate(5.0)
endEvent

Event onEffectFinish(Actor akTarget, Actor akCaster)
	;ent/ Variables
	int XP = Math.Floor(SuH_PCSheartXP.GetValue())
	int pH = Math.Floor(playerRef.GetBaseActorValue("health"))
	int pM = Math.Floor(playerRef.GetBaseActorValue("magicka"))
	int pS = Math.Floor(playerRef.GetBaseActorValue("stamina"))
	int pE = Math.Floor(SuH_skill_essence.GetValue())
	int statCapUp = Math.Floor(SuH_PCScapup.GetValue())
	int statCap = Math.Floor(100+(5*plvl)+(statCapUp))
	;end/
	;ent/ Apply and Overflow Check
	int h = pH + Math.Floor(hUP*aMOD)
	int m = pM + Math.Floor(mUP*aMOD)
	int s = pS + Math.Floor(sUP*aMOD)
	int x = 0
	int rh = 0
	int rm = 0
	int rs = 0
	
	
	if h <= statCap
		rh = hUP
		playerRef.SetActorValue("health", h)
	elseIf pH >= statCap
		eUP += Math.Floor(hUP*aMOD)
	else
		x = h - statCap
		rh = hUP - x
		eUP += x
		playerRef.SetActorValue("health", statCap)
	endIf
	
	if m <= statCap
		rm = mUP
		playerRef.SetActorValue("magicka", m)
	elseIf pM >= statCap
		eUP += Math.Floor(mUP*aMOD)
	else
		x = m - statCap
		rm = mUP - x
		eUP += x
		playerRef.SetActorValue("magicka", statCap)
	endIf
	
	if s <= statCap
		rs = sUP
		playerRef.SetActorValue("stamina", s)
	elseIf pS >= statCap
		eUP += Math.Floor(sUP*aMOD)
	else
		x = s - statCap
		rs = sUP - x
		eUP += x
		playerRef.SetActorValue("stamina", statCap)
	endIf
	;end/
	int rxp = Math.Floor(xpUP*aMOD*XPgain*0.01)
	XP += rxp
	pE += eUP
	SuH_PCSheartXP.SetValue(XP)
	SuH_skill_essence.SetValue(pE)
	;ent/ Act Report
	if	rh == 0
		if rm == 0
			if rs == 0
				if eUP == 0
					debug.Notification("Act Report : "+rxp+"XP  ")
				else
					debug.Notification("Act Report : "+rxp+"XP "+eUP+"Essence ")
				endIf
			elseIf eUP == 0
				debug.Notification("Act Report : "+rxp+"XP "+rs+"Stamina ")
			else
				debug.Notification("Act Report : "+rxp+"XP "+rs+"Stamina "+eUP+"Essence ")
			endIf
		elseIf rs == 0
			if eUP == 0
				debug.Notification("Act Report : "+rxp+"XP "+rm+"Magicka ")
			else
				debug.Notification("Act Report : "+rxp+"XP "+rm+"Magicka "+eUP+"Essence ")
			endIf
		elseIf eUP == 0
			debug.Notification("Act Report : "+rxp+"XP "+rm+"Magicka "+rs+"Stamina ")
		else
			debug.Notification("Act Report : "+rxp+"XP "+rm+"Magicka "+rs+"Stamina "+eUP+"Essence ")
		endIf
	elseIf rm == 0
		if rs == 0
			if eUP == 0
				debug.Notification("Act Report : "+rxp+"XP "+rh+"Health ")
			else
				debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+eUP+"Essence ")
			endIf
		elseif eUP == 0
			debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rs+"Stamina ")
		else
			debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rs+"Stamina "+eUP+"Essence ")
		endIf
	elseIf rs == 0
		if eUP == 0
			debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rm+"Magicka ")
		else
			debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rm+"Magicka "+eUP+"Essence ")
		endIf
	elseIf eUP == 0
		debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rm+"Magicka "+rs+"Stamina ")
	else
		debug.Notification("Act Report : "+rxp+"XP "+rh+"Health "+rm+"Magicka "+rs+"Stamina "+eUP+"Essence ")
	endIf
	;end/
	;end/
endEvent