ScriptName SuH_PCSmasteryScript extends ActiveMagicEffect

SexLabFramework property SexLab auto

;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_SETmultiattacker auto
GlobalVariable Property SuH_SEThcap auto
GlobalVariable Property SuH_SETmcap auto
GlobalVariable Property SuH_SETscap auto
GlobalVariable Property SuH_PCSactstacks auto
GlobalVariable Property SuH_PCSmaxactstacks auto
GlobalVariable Property SuH_PCSspermstacks auto
GlobalVariable Property SuH_PCSmaxspermstacks auto
GlobalVariable Property SuH_PCSmilkstacks auto
GlobalVariable Property SuH_PCSmaxmilkstacks auto
GlobalVariable Property SuH_PCSgenders auto
GlobalVariable Property SuH_PCSinactive auto
GlobalVariable Property SuH_PCSdesireOrg auto
;end/
;ent/ Magic Effects
MagicEffect Property SuH_sp_humandesireseffect auto
MagicEffect Property SuH_sp_animaldesireseffect auto
MagicEffect Property SuH_sp_selfstackereffect auto
;end/

Spell Property SuH_PCSmastery auto

;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
int XPearn
int switch
int invalid
int XPbase
int SETh
int SETm
int SETs
int itimer
int aminutes
float XPgain
float aMOD

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

Event OnUpdate()
	hUP += pHa
	mUP += pMa
	sUP += pSa
	xpUP += XPearn
	switch += 1
	if switch >= 6
		if SexLab.IsValidActor(playerRef)
			invalid = 1
			playerRef.RemoveSpell(SuH_PCSmastery)
		else
			switch = 0
		endIf
	endIf
	RegisterForSingleUpdate(5.0)
endEvent

Event onEffectFinish(Actor akTarget, Actor akCaster)
if invalid == 0
	;ent/ Variables
	int XP = SuH_PCSheartXP.GetValue() as int
	int pH = playerRef.GetBaseActorValue("health") as int
	int pM = playerRef.GetBaseActorValue("magicka") as int
	int pS = playerRef.GetBaseActorValue("stamina") as int
	int pE = SuH_skill_essence.GetValue() as int
	int statCapUp = SuH_PCScapup.GetValue() as int
	int statCap = Math.Floor(100+(5*plvl)+(statCapUp))
	SETh = SuH_SEThcap.GetValue() as int
	SETm = SuH_SETmcap.GetValue() as int
	SETs = SuH_SETscap.GetValue() as int
	;end/
	;ent/ Apply Desire Bonus
	int pDesireOrg = SuH_PCSdesireOrg.GetValue() as int
	aMOD += (pDesireOrg*0.0001)
	SuH_PCSdesireOrg.SetValue(0)
	;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 SETh == 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
	else
		if h <= SETh
			rh = hUP
			playerRef.SetActorValue("health", h)
		elseIf pH >= SETh
			eUP += Math.Floor(hUP*aMOD)
		else
			x = h - SETh
			rh = hUP - x
			eUP += x
			playerRef.SetActorValue("health", SETh)
		endIf
	endIf
	
	if SETm == 0
		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
	else
		if m <= SETm
			rm = mUP
			playerRef.SetActorValue("magicka", m)
		elseIf pM >= SETm
			eUP += Math.Floor(mUP*aMOD)
		else
			x = m - SETm
			rm = mUP - x
			eUP += x
			playerRef.SetActorValue("magicka", SETm)
		endIf
	endIf
	
	if SETs == 0
		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
	else
		if s <= SETs
			rs = sUP
			playerRef.SetActorValue("stamina", s)
		elseIf pS >= SETs
			eUP += Math.Floor(sUP*aMOD)
		else
			x = s - SETs
			rs = sUP - x
			eUP += x
			playerRef.SetActorValue("stamina", SETs)
		endIf
	endIf
	;end/
	int rxp = Math.Floor(xpUP*aMOD*XPgain*0.01)
	XP += rxp
	pE += eUP
	SuH_PCSheartXP.SetValue(XP)
	SuH_skill_essence.SetValue(pE)
	SuH_PCSinactive.SetValue(0)
	;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/
	;ent/ Act Stack
	int aStack = SuH_PCSactstacks.GetValue() as int
	int maStack = SuH_PCSmaxactstacks.GetValue() as int
	if aStack < maStack
		aStack += 1
		SuH_PCSactstacks.SetValue(aStack)
	endIf
	if playerRef.HasMagicEffect(SuH_sp_selfstackereffect)
		int pGender = SuH_PCSgenders.GetValue() as int
		if pGender == 1
			int mStack = SuH_PCSmilkstacks.GetValue() as int
			int mmStack = SuH_PCSmaxmilkstacks.GetValue() as int
			if mStack < mmStack
				mStack += 1
				SuH_PCSmilkstacks.SetValue(mStack)
			endIf
		elseIf pGender == 2
			int sStack = SuH_PCSspermstacks.GetValue() as int
			int msStack = SuH_PCSmaxspermstacks.GetValue() as int
			if sStack < msStack
				sStack += 1
				SuH_PCSspermstacks.SetValue(sStack)
			endIf
		endIf
	endIf
	;end/ 
endIf
endEvent