Scriptname SuH_Main extends Quest  

;ent/ Scripts
SexLabFramework property SexLab auto
SuH_Fun property _Fun auto
;end/
;ent/ Globals
GlobalVariable property SuH_MODversion auto
GlobalVariable property SuH_SETallowdrainkill auto
GlobalVariable property SuH_SETessentialacts auto
GlobalVariable property SuH_SETcleanactor auto
GlobalVariable property SuH_PCSstackmaxsex auto
GlobalVariable property SuH_PCSstackmaxsperm auto
GlobalVariable property SuH_PCSstackmaxmilk auto
GlobalVariable property SuH_PCSstacksex auto
GlobalVariable property SuH_PCSstacksperm auto
GlobalVariable property SuH_PCSstackmilk auto
GlobalVariable property SuH_PCSxpearn auto
GlobalVariable property SuH_rank_perfectorgasm auto
GlobalVariable property SuH_rank_selfstacker auto
GlobalVariable property SuH_rank_valuestacker auto
;end/
;ent/ Factions
Faction property SuH_Tracker auto
;end/
;ent/ Spells
Spell property SuH_sex_PCmastery auto
Spell property SuH_sex_PCtraining auto
Spell property SuH_sex_PCsubmit auto
Spell property SuH_sex_NPCattacker auto
Spell property SuH_sex_NPCconsent auto
Spell property SuH_sex_NPCvictim auto
Spell property SuH_stat_essential auto
Spell property SuH_dyna_player auto
Spell property SuH_sex_inact auto
Spell property SuH_spell_selfpleasure auto
Spell property SuH_spell_nightsembrace auto

MagicEffect Property SuH_spellE_cursedblood_hate auto
;end/
;ent/ Items
Ingredient Property SuH_ing_milk auto
Ingredient Property SuH_ing_sperm auto
Ingredient Property SuH_ing_vitalitystone auto
Ingredient Property SuH_ing_empowermentstone auto
Ingredient Property SuH_ing_specialtystone auto
;end/
Actor Property PlayerRef auto

;ent/ MODupdate
Function MODupdate()
	float MODversion = SuH_MODversion.GetValue()
	;Always Run
	; NOTE : Succubus Heart MUST reset the player Dynamic Effect spell on each load because Skyrim doesn't store spell values properly between saves
	PlayerRef.RemoveSpell(SuH_dyna_player)
	PlayerRef.AddSpell(SuH_dyna_player, false)
	;Previous Patches
	if MODversion < 0.1
		SexLab.TrackFaction(SuH_tracker,"NPCtracker")
		RegisterForModEvent("NPCtracker_Added","anim_NPC_Added")
		RegisterForModEvent("NPCtracker_Start","anim_NPC_Start")
		RegisterForModEvent("NPCtracker_Orgasm","anim_NPC_Orgasm")
		RegisterForModEvent("NPCtracker_End","anim_NPC_End")
		RegisterForModEvent("PlayerTrack_Added","anim_PC_Added")
		RegisterForModEvent("PlayerTrack_Start","anim_PC_Start")
		RegisterForModEvent("PlayerTrack_Orgasm","anim_PC_Orgasm")
		RegisterForModEvent("PlayerTrack_End","anim_PC_End")
		SuH_MODversion.SetValue(0.1)
		debug.Notification("Succubus Heart : Patch 0.1")
	endIf
	if MODversion < 0.6
		SuH_MODversion.SetValue(0.6)
		playerRef.AddSpell(SuH_spell_nightsembrace)
		PlayerRef.AddSpell(SuH_spell_selfpleasure)
	endIf
	;Current Patch
	if MODversion < 0.7
		SuH_MODversion.SetValue(0.7)
		SuH_PCSxpearn.SetValue(25)
		debug.Notification("Succubus Heart : 3.x BETA Installed (0.7)")
	endIf
endFunction
;end/

Event OnInit()
	MODupdate()
endEvent

;ent/ NPC Anim Start
Function anim_NPC_Start(form ActorRef, int threadId)
	actor char = ActorRef as actor ; Get Actor
	char.addSpell(SuH_sex_inact, false) ; Add InAct spell tag
	if SuH_SETcleanactor.GetValue()
		char.ClearExtraArrows()
	endIf
	if SuH_SETessentialacts.GetValue()
		if char.GetActorBase().IsEssential()
			;do nothing
		else
			char.GetActorBase().setEssential() ; Make character essential
			char.addSpell(SuH_stat_essential) ; Add Essential spell tag
		endIF
	endIf
endFunction
;end/

;ent/ NPC Anim Orgasm
Function anim_NPC_Orgasm(form ActorRef, int threadId)
	sslThreadController Thread = SexLab.GetController(threadId) ; Get Thread
	actor char = ActorRef as actor ; Get Actor (We'll exclude char from results)
	actor[] animActor = SexLab.HookActors(threadId) ; Create Actor Array
	; Hand out sex drops
	if animActor.Length > 1 ; If multiple actors are present
		int charGender = SexLab.GetGender(char) ; Get char gender
		if charGender == 0 || charGender == 2 ; If char is male or male creature
			int i = 0 ; Set array position to 0
			while i < animActor.Length
				if animActor[i] != char
					animActor[i].AddItem(SuH_ing_sperm, 2, true) ; Give actor 2 items
					animActor[i].EquipItem(SuH_ing_sperm, false, true) ; Use 1 item
				endIf
				i += 1
			endWhile
		elseIf charGender == 1 || charGender == 3 ; If char is female or female creature
			int i = 0 ; Set array position to 0
			while i < animActor.Length
				if animActor[i] != char
					animActor[i].AddItem(SuH_ing_milk, 2, true)
					animActor[i].EquipItem(SuH_ing_milk, false, true)
				endIf
				i += 1
			endWhile
		endIf
	else ; If actor is alone
		int charGender = SexLab.GetGender(char) ; Get char gender
		if charGender == 0 || charGender == 2 ; If char is male or male creature
			char.AddItem(SuH_ing_sperm, 2, true)
			char.EquipItem(SuH_ing_sperm, false, true)
		elseIf charGender == 1 || charGender == 3 ; If char is female or female creature
			int i = 0 ; Set array position to 0
			char.AddItem(SuH_ing_milk, 2, true)
			char.EquipItem(SuH_ing_milk, false, true)
		endIf
	endIf
endFunction
;end/

;ent/ NPC Anim End
Function anim_NPC_End(form ActorRef, int threadId)
	actor char = ActorRef as actor ; Get Actor
	; Remove sex effects
	char.RemoveSpell(SuH_sex_PCmastery)
	char.RemoveSpell(SuH_sex_PCtraining)
	char.RemoveSpell(SuH_sex_PCsubmit)
	char.RemoveSpell(SuH_sex_NPCattacker)
	char.RemoveSpell(SuH_sex_NPCconsent)
	char.RemoveSpell(SuH_sex_NPCvictim)
	char.RemoveSpell(SuH_sex_inact)
	if char.hasSpell(SuH_stat_essential) ; if char has essential spell tag
		char.GetActorBase().setEssential(FALSE) ; Remove essential
		char.removeSpell(SuH_stat_essential) ; Remove essential spell tag
	endIf
	if SuH_SETallowdrainkill.GetValue()
		float x = char.GetActorValue("Health")
		if x < 1.0
			char.DamageActorValue("Health", 1.0)
		endIf
	endIf
	if char.HasMagicEffect(SuH_spellE_cursedblood_hate)
		char.Kill(none)
	endIf
endFunction
;end/

;ent/ PC Anim Start
Function anim_PC_Start(form ActorRef, int threadId)
	sslThreadController Thread = SexLab.GetController(threadId) ; Get Thread
	actor[] animActor = SexLab.HookActors(threadId) ; Create Actor Array
	actor char = ActorRef as actor ; Get Actor (Looking for player)
	char.AddSpell(SuH_sex_inact, false) ; Add InAct spell tag
	if SuH_SETcleanactor.GetValue()
		char.ClearExtraArrows()
	endIf
	if char == PlayerRef && animActor.Length > 1 ; If actor is player then execute and distribute effect spells
		actor victim = SexLab.HookVictim(threadId) ; Get Victim
		int i = 0 ;Set array position to 0
		while i < animActor.Length ; Scan through all actors and apply effects
			if animActor[i] == PlayerRef ; If character is the PC
				if PlayerRef == victim ; If PC is victim
					PlayerRef.AddSpell(SuH_sex_PCsubmit, false) ; Add Submit Effect
				elseif victim != none ; If PC is aggressor
					PlayerRef.AddSpell(SuH_sex_PCmastery, false) ; Add Mastery Effect
				else ; If PC is consent
					PlayerRef.AddSpell(SuH_sex_PCtraining, false) ; Add Training Effect
				endIf
			else ; If character is an NPC
				if animActor[i] == victim ; If NPC is a victim
					animActor[i].AddSpell(SuH_sex_NPCvictim, false) ; Add Victim Effect
				elseif victim != none ; If NPC is aggressor
					animActor[i].AddSpell(SuH_sex_NPCattacker, false) ; Add Attacker Effect
				else ; If NPC is consent
					animActor[i].AddSpell(SuH_sex_NPCconsent, false) ; Add Consent Effect
				endIf
			endIf
			i+=1
		endWhile
	endIf
endFunction
;end/

;ent/ PC Anim Orgasm
Function anim_PC_Orgasm(form ActorRef, int threadId)
	actor char = ActorRef as actor ; Get Actor (We'll exclude char from results)
	; Apply player/character specific effects
	if char == PlayerRef
		_Fun.XPcalc("PCorgasm", 0.0, 0, 0.0, FALSE)
	endIf
	sslThreadController Thread = SexLab.GetController(threadId) ; Get Thread
	actor[] animActor = SexLab.HookActors(threadId) ; Create Actor Array
	; Hand out sex drops
	if animActor.Length > 1 ; If multiple actors are present
		int charGender = SexLab.GetGender(char) ; Get char gender
		if charGender == 0 || charGender == 2 ; If char is male or male creature
			int i = 0 ; Set array position to 0
			while i < animActor.Length
				if animActor[i] != char
					animActor[i].AddItem(SuH_ing_sperm, 2, true) ; Give actor 2 items
					animActor[i].EquipItem(SuH_ing_sperm, false, true) ; Use 1 item
				endIf
				i += 1
			endWhile
		elseIf charGender == 1 || charGender == 3 ; If char is female or female creature
			int i = 0 ; Set array position to 0
			while i < animActor.Length
				if animActor[i] != char
					animActor[i].AddItem(SuH_ing_milk, 2, true)
					animActor[i].EquipItem(SuH_ing_milk, false, true)
				endIf
				i += 1
			endWhile
		endIf
		; Calculate rare drop chance and handout
		if char == PlayerRef
			int i = 0
			while i < animActor.Length
				if animActor[i] != char
					int ran = Utility.RandomInt(0,99) + SuH_rank_perfectorgasm.GetValue() as int
					if ran >= 94
						int rare = Utility.RandomInt(0,2)
						if rare == 0
							char.AddItem(SuH_ing_vitalitystone, 1, true)
							if char == PlayerRef ; Notify player of rare drop
								debug.Notification("Succubus Heart - RARE DROP - Vitality Stone")
							endIf
						elseif rare == 1
							char.AddItem(SuH_ing_empowermentstone, 1, true)
							if char == PlayerRef
								debug.Notification("Succubus Heart - RARE DROP - Empowerment Stone")
							endIf
						elseif rare == 2
							char.AddItem(SuH_ing_specialtystone, 1, true)
							if char == PlayerRef
								debug.Notification("Succubus Heart - RARE DROP - Specialty Stone")
							endIf
						endIf
					endIf
				endIf
				i += 1
			endWhile
		endIf
	else ; If actor is alone
		int charGender = SexLab.GetGender(char) ; Get char gender
		if charGender == 0 || charGender == 2 ; If char is male or male creature
			char.AddItem(SuH_ing_sperm, 2, true)
			char.EquipItem(SuH_ing_sperm, false, true)
		elseIf charGender == 1 || charGender == 3 ; If char is female or female creature
			int i = 0 ; Set array position to 0
			char.AddItem(SuH_ing_milk, 2, true)
			char.EquipItem(SuH_ing_milk, false, true)
		endIf
	endIf
	
endFunction
;end/

;ent/ PC Anim End
Function anim_PC_End(form ActorRef, int threadId)
	actor char = ActorRef as actor ; Get Actor
	; Remove sex effects
	char.RemoveSpell(SuH_sex_PCmastery)
	char.RemoveSpell(SuH_sex_PCtraining)
	char.RemoveSpell(SuH_sex_PCsubmit)
	char.RemoveSpell(SuH_sex_NPCattacker)
	char.RemoveSpell(SuH_sex_NPCconsent)
	char.RemoveSpell(SuH_sex_NPCvictim)
	char.RemoveSpell(SuH_sex_inact)
	if char == PlayerRef
		; Give Sex Stack
		int sex = SuH_PCSstacksex.GetValue() as int
		int msex = SuH_PCSstackmaxsex.GetValue() as int
		if sex < msex
			int valuestacker = SuH_rank_valuestacker.GetValue() as int
			int tstack = sex + valuestacker + 1
			if tstack < msex
				SuH_PCSstacksex.SetValue(tstack)
			else
				SuH_PCSstacksex.SetValue(msex)
			endIf
		endIf
		;Self Stacker
		int selfstacker = SuH_rank_selfstacker.GetValue() as int
		if selfstacker != 0
			int pg = SexLab.GetGender(char)
			if pg == 1 ;female
				int y = SuH_PCSstackmaxmilk.GetValue() as int
				int z = SuH_PCSstackmilk.GetValue() as int
				int k = z + selfstacker
				if k < y
					SuH_PCSstackmilk.SetValue(k)
				else
					SuH_PCSstackmilk.SetValue(y)
				endIf
			else ;male
				int y = SuH_PCSstackmaxsperm.GetValue() as int
				int z = SuH_PCSstacksperm.GetValue() as int
				int k = z + selfstacker
				if k < y
					SuH_PCSstacksperm.SetValue(k)
				else
					SuH_PCSstacksperm.SetValue(y)
				endIf
			endIf
		endIf
	; Kill if HP is lower than 1 and Kill Drain is enabled
	elseif SuH_SETallowdrainkill.GetValue()
		float x = char.GetBaseActorValue("Health")
		if x < 1.0
			char.DamageActorValue("Health", 1.0)
		endIf
	endIf
endFunction
;end/