Scriptname SuH_Main extends Quest  

;ent/ Scripts
SexLabFramework property SexLab auto
;end/
;ent/ Globals
GlobalVariable property SuH_MODversion auto
GlobalVariable property SuH_SETallowdrainkill auto
GlobalVariable property SuH_PCSstackmaxsex auto
GlobalVariable property SuH_PCSstacksex 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
;end/
;ent/ Items
Ingredient Property SuH_ing_milk auto
Ingredient Property SuH_ing_sperm auto
;end/
Actor Property PlayerRef auto

;ent/ MODupdate
Function MODupdate()
	float MODversion = SuH_MODversion.GetValue()
	;Previous Patches
	
	;Current Patch
	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 : vR 0.1 Installed")
	endIf

endFunction
;end/

Event OnInit()
	MODupdate()
endEvent

;ent/ NPC Anim Start
Function anim_NPC_Start(form ActorRef, int threadId)
endFunction
;end/

;ent/ NPC Anim Orgasm
Function anim_NPC_Orgasm(form ActorRef, int threadId)
	sslThreadController Thread = SexLab.GetController(threadId) ; Get Thread
	if Thread.HasPlayer
		; Do Nothing (Handled by player hook)
	else
		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
	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)
	if SuH_SETallowdrainkill.GetValue()
		float x = char.GetBaseActorValue("Health")
		if x < 1.0
			char.DamageActorValue("Health", 1.0)
		endIf
	endIf
endFunction
;end/

;ent/ PC Anim Start
Function anim_PC_Start(form ActorRef, int threadId)
	actor char = ActorRef as actor ; Get Actor (Looking for player)
	if char == PlayerRef ; If actor is player then exicute and distribute effect spells
		sslThreadController Thread = SexLab.GetController(threadId) ; Get Thread
		actor[] animActor = SexLab.HookActors(threadId) ; Create Actor Array
		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)
	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
	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)
	if char == PlayerRef
		; Give Sex Stack
		float x = SuH_PCSstacksex.GetValue()
		if x < SuH_PCSstackmaxsex.GetValue()
			SuH_PCSstacksex.SetValue(x+1)
		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/