Scriptname SuH_spellES_randompet extends activemagiceffect  

SuH_Fun property _Fun auto
DialogueFollowerScript Property DialogueFollower Auto
Quest Property pDialogueFollower  Auto
ObjectReference Property SuH_realm_petreturn Auto 

GlobalVariable property SuH_PETactive auto
GlobalVariable property SuH_PETactivedog auto
GlobalVariable property SuH_PETactivewolf auto
GlobalVariable property SuH_PETactivehorse auto
GlobalVariable property SuH_PETactiveskeever auto
GlobalVariable property SuH_PETactivefamiliar auto
GlobalVariable property SuH_PETactivebear auto
GlobalVariable property SuH_PETactivesabrecat auto
GlobalVariable property SuH_PETactivedraugr auto
GlobalVariable property SuH_PETactivetroll auto
GlobalVariable property SuH_PETactivespriggan auto
GlobalVariable property SuH_PETactivehorker auto
GlobalVariable property SuH_PETactivefalmer auto
GlobalVariable property SuH_PETdogunlock auto
GlobalVariable property SuH_PETwolfunlock auto
GlobalVariable property SuH_PEThorseunlock auto
GlobalVariable property SuH_PETskeeverunlock auto
GlobalVariable property SuH_PETfamiliarunlock auto
GlobalVariable property SuH_PETbearunlock auto
GlobalVariable property SuH_PETsabrecatunlock auto
GlobalVariable property SuH_PETdraugrunlock auto
GlobalVariable property SuH_PETtrollunlock auto
GlobalVariable property SuH_PETsprigganunlock auto
GlobalVariable property SuH_PEThorkerunlock auto
GlobalVariable property SuH_PETfalmerunlock auto
GlobalVariable property SuH_PETdogdesire auto
GlobalVariable property SuH_PETwolfdesire auto
GlobalVariable property SuH_PEThorsedesire auto
GlobalVariable property SuH_PETskeeverdesire auto
GlobalVariable property SuH_PETfamiliardesire auto
GlobalVariable property SuH_PETbeardesire auto
GlobalVariable property SuH_PETsabrecatdesire auto
GlobalVariable property SuH_PETdraugrdesire auto
GlobalVariable property SuH_PETtrolldesire auto
GlobalVariable property SuH_PETspriggandesire auto
GlobalVariable property SuH_PEThorkerdesire auto
GlobalVariable property SuH_PETfalmerdesire auto

Actor property PlayerRef auto
Actor property SuH_actor_dog auto
Actor property SuH_actor_wolf auto
Actor property SuH_actor_horse auto
Actor property SuH_actor_skeever auto
actor property SuH_actor_familiar auto
actor property SuH_actor_bear auto
actor property SuH_actor_sabrecat auto
actor property SuH_actor_draugr auto
actor property SuH_actor_troll auto
actor property SuH_actor_spriggan auto
actor property SuH_actor_horker auto
actor property SuH_actor_falmer auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
	int PETactive = SuH_PETactive.GetValue() as int
	string summonPET = "none"
	int chkdog = 0 ;0 Pet Values (unchecked/rolled/invalid)
	int chkwolf = 0 ;1
	int chkhorse = 0 ;2
	int chkskeever = 0 ;3
	int chkfamiliar = 0 ;4
	int chkbear = 0 ;5
	int chksabrecat = 0 ;6
	int chkdraugr = 0 ;7
	int chktroll = 0 ;8
	int chkspriggan = 0 ;9
	int chkhorker = 0 ;10
	int chkfalmer = 0 ;11
	
	int chkcount = 0 ; Times we've 'checked' for a valid pet
	int chkavail = 0 ; Valid Pet checks we have available
	int chkfail = 0 ; Invalid Pet checks
	int chkrand = 0 ; Have we checked the rolled pet?
	int petFound = 0 ; Have we found a pet to summon?
	int reroll = 1 ; Should we reroll for a pet?
	int x = 0 ; Common Variable
	
	; NOTES : chkpet has 3 values [0=unchecked][1=checked but rolled out][2=unavailable]
	while reroll != 0
		chkavail = 11 - chkfail ; # of pets (0 counts as 1)
		chkcount = 0
		chkrand = 0
		int rand = Utility.RandomInt(0,chkavail) ; Select Random Pet
		
		if chkrand == 0 && chkdog != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETdogunlock.GetValue()
					if SuH_PETactivedog.GetValue()
						chkdog = 2
						chkfail += 1
					else
						x = SuH_PETdogdesire.GetValue() as int
						if x > 99
							summonPET = "dog"
							petFound = 1
						else
							chkdog = 2
							chkfail += 1
						endIf
					endIf
				else
					chkdog = 2
					chkfail += 1
				endIf
			else
				chkdog = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkwolf != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETwolfunlock.GetValue()
					if SuH_PETactivewolf.GetValue()
						chkwolf = 2
						chkfail += 1
					else
						x = SuH_PETwolfdesire.GetValue() as int
						if x > 99
							summonPET = "wolf"
							petFound = 1
						else
							chkwolf = 2
							chkfail += 1
						endIf
					endIf
				else
					chkwolf = 2
					chkfail += 1
				endIf
			else
				chkwolf = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkhorse != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PEThorseunlock.GetValue()
					if SuH_PETactivehorse.GetValue()
						chkhorse = 2
						chkfail += 1
					else
						x = SuH_PEThorsedesire.GetValue() as int
						if x > 99
							summonPET = "horse"
							petFound = 1
						else
							chkhorse = 2
							chkfail += 1
						endIf
					endIf
				else
					chkhorse = 2
					chkfail += 1
				endIf
			else
				chkhorse = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkskeever != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETskeeverunlock.GetValue()
					if SuH_PETactiveskeever.GetValue()
						chkskeever = 2
						chkfail += 1
					else
						x = SuH_PETskeeverdesire.GetValue() as int
						if x > 99
							summonPET = "skeever"
							petFound = 1
						else
							chkskeever = 2
							chkfail += 1
						endIf
					endIf
				else
					chkskeever = 2
					chkfail += 1
				endIf
			else
				chkskeever = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkfamiliar != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETfamiliarunlock.GetValue()
					if SuH_PETactivefamiliar.GetValue()
						chkfamiliar = 2
						chkfail += 1
					else
						x = SuH_PETfamiliardesire.GetValue() as int
						if x > 99
							summonPET = "familiar"
							petFound = 1
						else
							chkfamiliar = 2
							chkfail += 1
						endIf
					endIf
				else
					chkfamiliar = 2
					chkfail += 1
				endIf
			else
				chkfamiliar = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkbear != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETbearunlock.GetValue()
					if SuH_PETactivebear.GetValue()
						chkbear = 2
						chkfail += 1
					else
						x = SuH_PETbeardesire.GetValue() as int
						if x > 99
							summonPET = "bear"
							petFound = 1
						else
							chkbear = 2
							chkfail += 1
						endIf
					endIf
				else
					chkbear = 2
					chkfail += 1
				endIf
			else
				chkbear = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chksabrecat != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETsabrecatunlock.GetValue()
					if SuH_PETactivesabrecat.GetValue()
						chksabrecat = 2
						chkfail += 1
					else
						x = SuH_PETsabrecatdesire.GetValue() as int
						if x > 99
							summonPET = "sabrecat"
							petFound = 1
						else
							chksabrecat = 2
							chkfail += 1
						endIf
					endIf
				else
					chksabrecat = 2
					chkfail += 1
				endIf
			else
				chksabrecat = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkdraugr != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETdraugrunlock.GetValue()
					if SuH_PETactivedraugr.GetValue()
						chkdraugr = 2
						chkfail += 1
					else
						x = SuH_PETdraugrdesire.GetValue() as int
						if x > 99
							summonPET = "draugr"
							petFound = 1
						else
							chkdraugr = 2
							chkfail += 1
						endIf
					endIf
				else
					chkdraugr = 2
					chkfail += 1
				endIf
			else
				chkdraugr = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chktroll != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETtrollunlock.GetValue()
					if SuH_PETactivetroll.GetValue()
						chktroll = 2
						chkfail += 1
					else
						x = SuH_PETtrolldesire.GetValue() as int
						if x > 99
							summonPET = "troll"
							petFound = 1
						else
							chktroll = 2
							chkfail += 1
						endIf
					endIf
				else
					chktroll = 2
					chkfail += 1
				endIf
			else
				chktroll = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkspriggan != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETsprigganunlock.GetValue()
					if SuH_PETactivespriggan.GetValue()
						chkspriggan = 2
						chkfail += 1
					else
						x = SuH_PETspriggandesire.GetValue() as int
						if x > 99
							summonPET = "spriggan"
							petFound = 1
						else
							chkspriggan = 2
							chkfail += 1
						endIf
					endIf
				else
					chkspriggan = 2
					chkfail += 1
				endIf
			else
				chkspriggan = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkhorker != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PEThorkerunlock.GetValue()
					if SuH_PETactivehorker.GetValue()
						chkhorker = 2
						chkfail += 1
					else
						x = SuH_PEThorkerdesire.GetValue() as int
						if x > 99
							summonPET = "horker"
							petFound = 1
						else
							chkhorker = 2
							chkfail += 1
						endIf
					endIf
				else
					chkhorker = 2
					chkfail += 1
				endIf
			else
				chkhorker = 1
				chkcount += 1
			endIf
		endIf
		if chkrand == 0 && chkfalmer != 2
			if chkcount == rand
				chkrand = 1
				if SuH_PETfalmerunlock.GetValue()
					if SuH_PETactivefalmer.GetValue()
						chkfalmer = 2
						chkfail += 1
					else
						x = SuH_PETfalmerdesire.GetValue() as int
						if x > 99
							summonPET = "falmer"
							petFound = 1
						else
							chkfalmer = 2
							chkfail += 1
						endIf
					endIf
				else
					chkfalmer = 2
					chkfail += 1
				endIf
			else
				chkfalmer = 1
				chkcount += 1
			endIf
		endIf
		
		if petFound == 1
			reroll = 0
		elseif chkfail >= 12
			reroll = 0
			debug.Notification("No valid pets found!")
		endIf
	endWhile
	
	
	if summonPET != "none"
		if SuH_PETactivedog.getValue()
			(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
			SuH_actor_dog.MoveTo(SuH_realm_petreturn)
			SuH_PETactivedog.SetValue(0)
		elseif SuH_PETactivewolf.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_wolf.MoveTo(SuH_realm_petreturn)
				SuH_PETactivewolf.SetValue(0)
		elseif SuH_PETactivehorse.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_horse.MoveTo(SuH_realm_petreturn)
				SuH_PETactivehorse.SetValue(0) 
		elseif SuH_PETactiveskeever.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_skeever.MoveTo(SuH_realm_petreturn)
				SuH_PETactiveskeever.SetValue(0) 
		elseif SuH_PETactivefamiliar.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_familiar.MoveTo(SuH_realm_petreturn)
				SuH_PETactivefamiliar.SetValue(0) 
		elseif SuH_PETactivebear.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_bear.MoveTo(SuH_realm_petreturn)
				SuH_PETactivebear.SetValue(0) 
		elseif SuH_PETactivesabrecat.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_sabrecat.MoveTo(SuH_realm_petreturn)
				SuH_PETactivesabrecat.SetValue(0) 
		elseif SuH_PETactivedraugr.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_draugr.MoveTo(SuH_realm_petreturn)
				SuH_PETactivedraugr.SetValue(0) 
		elseif SuH_PETactivetroll.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_troll.MoveTo(SuH_realm_petreturn)
				SuH_PETactivetroll.SetValue(0) 
		elseif SuH_PETactivespriggan.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_spriggan.MoveTo(SuH_realm_petreturn)
				SuH_PETactivespriggan.SetValue(0) 
		elseif SuH_PETactivehorker.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_horker.MoveTo(SuH_realm_petreturn)
				SuH_PETactivehorker.SetValue(0) 
		elseif SuH_PETactivefalmer.getValue()
				(pDialogueFollower as DialogueFollowerScript).DismissAnimal()
				SuH_actor_falmer.MoveTo(SuH_realm_petreturn)
				SuH_PETactivefalmer.SetValue(0) 
		endIf
	endIf
	
	if summonPET != "none"
		if summonPET == "dog"
			SuH_actor_dog.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_dog)
			SuH_PETactivedog.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Dog [Incubus] has been summoned!")
		elseif summonPET == "wolf"
			SuH_actor_wolf.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_wolf)
			SuH_PETactivewolf.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Wolf [Incubus] has been summoned!")
		elseif summonPET == "horse"
			SuH_actor_horse.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_horse)
			SuH_PETactivehorse.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Horse [Incubus] has been summoned!")
		elseif summonPET == "skeever"
			SuH_actor_skeever.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_skeever)
			SuH_PETactiveskeever.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Skeever [Incubus] has been summoned!")
		elseif summonPET == "familiar"
			SuH_actor_familiar.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_familiar)
			SuH_PETactivefamiliar.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Familiar [Incubus] has been summoned!")
		elseif summonPET == "bear"
			SuH_actor_bear.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_bear)
			SuH_PETactivebear.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Bear [Incubus] has been summoned!")
		elseif summonPET == "sabrecat"
			SuH_actor_sabrecat.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_sabrecat)
			SuH_PETactivesabrecat.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Sabre Cat [Incubus] has been summoned!")
		elseif summonPET == "draugr"
			SuH_actor_draugr.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_draugr)
			SuH_PETactivedraugr.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Draugr [Incubus] has been summoned!")
		elseif summonPET == "spriggan"
			SuH_actor_spriggan.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_spriggan)
			SuH_PETactivespriggan.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Spriggan [Succubus] has been summoned!")
		elseif summonPET == "horker"
			SuH_actor_horker.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_horker)
			SuH_PETactivehorker.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Horker [Incubus] has been summoned!")
		elseif summonPET == "falmer"
			SuH_actor_falmer.MoveTo(PlayerRef)
			(DialogueFollower as DialogueFollowerScript).SetAnimal(SuH_actor_falmer)
			SuH_PETactivefalmer.SetValue(1)
			x += 1
			SuH_PETactive.SetValue(1)
			debug.Notification("Falmer [Incubus] has been summoned!")
		endIf
	endIf
endEvent