Scriptname SuH_spellES_petself extends activemagiceffect  

SuH_Fun property _f auto
SuH_Status property _s auto

Event OnInit()
	_f = Game.GetFormFromFile(0x814, "Succubus Heart.esp") as SuH_Fun
	_s = Game.GetFormFromFile(0x16E496, "Succubus Heart.esp") as SuH_Status
endEvent

; Pets Menu
; 0 Train Pet
; 1 Summon Pet
; 2 Summon Random Pet
; 3 Return Pet
; 4 Cancel

Event OnEffectStart(Actor akTarget, Actor akCaster)
	if akCaster == _s.PlayerRef
		Int m = _s.SuH_menu_petself.Show()
		;ent/ Train Pet
		if m == 0
			if _s.SuH_actor_activepet != none
				actor[] animActor = new actor[2]
				animActor[0] = _s.SuH_actor_activepet
				animActor[1] = _s.PlayerRef
				_f.ANIMcalc(animActor)
			else
				Debug.Notification("Train Pet - No active pets found!")
			endIf
		;end/		
		;ent/ Summon Random Pet
		elseif m == 1
			int x = _s.SuH_petactorbases.Length - 1
			ActorBase[] validarray = new ActorBase[25]
				validarray[0] = _s.SuH_petactorbases[0]
				validarray[1] = _s.SuH_petactorbases[1]
				validarray[2] = _s.SuH_petactorbases[2]
				validarray[3] = _s.SuH_petactorbases[3]
				validarray[4] = _s.SuH_petactorbases[4]
				validarray[5] = _s.SuH_petactorbases[5]
				validarray[6] = _s.SuH_petactorbases[6]
				validarray[7] = _s.SuH_petactorbases[7]
				validarray[8] = _s.SuH_petactorbases[8]
				validarray[9] = _s.SuH_petactorbases[9]
				validarray[10] = _s.SuH_petactorbases[10]
				validarray[11] = _s.SuH_petactorbases[11]
				validarray[12] = _s.SuH_petactorbases[12]
				validarray[13] = _s.SuH_petactorbases[13]
				validarray[14] = _s.SuH_petactorbases[14]
				validarray[15] = _s.SuH_petactorbases[15]
				validarray[16] = _s.SuH_petactorbases[16]
				validarray[17] = _s.SuH_petactorbases[17]
				validarray[18] = _s.SuH_petactorbases[18]
				validarray[19] = _s.SuH_petactorbases[19]
				validarray[20] = _s.SuH_petactorbases[20]
				validarray[21] = _s.SuH_petactorbases[21]
				validarray[22] = _s.SuH_petactorbases[22]
				validarray[23] = _s.SuH_petactorbases[23]
				validarray[24] = _s.SuH_petactorbases[24]
			int i = 0
			while i == 0 ;Try to find a random pet using temporary validarray
				int y = Utility.RandomInt(0, x)
				if validarray[y] != none && validarray[y] != _s.SuH_actor_activepet.GetActorBase()
					_f.SoulPetSummon(petbase=validarray[y])
					i = 1
				elseif x >= 0 ;collapse array on invalid slot, decrease random range to fit new valid range
					int z = y
					while z < x
						int k = z + 1
						validarray[z] = validarray[k]
						z += 1
					endWhile
					validarray[z] = none
					x -= 1
				else ; valid range fell below 0
					debug.notification("Soul Pets : No valid pets found!")
					i = 2
				endIf
			endWhile
		;end/
		;ent/ Return Pet
		elseif m == 2
			_f.SoulPetDeSummon()
		;end/
		;ent/ Cancel
		else
		;end/
		endIf
	endIf
endEvent