Scriptname aaaKimachuuCaptiveMain extends ActiveMagicEffect

Faction Property aaaKimachuuCaptiveFaction Auto
FormList Property aaaKimachuuCaptiveAllyList Auto
FormList Property aaaKimachuuCaptiveMarkerList Auto
Message Property aaaKimachuuCaptiveMes Auto
Package Property aaaKimachuuCaptivePackage Auto
Quest Property aaaKimachuuCaptiveQuest Auto
ReferenceAlias Property Follower Auto

Bool bSafety = false

Event OnEffectStart(Actor akTarget, Actor akCaster)

	int m = aaaKimachuuCaptiveMes.Show()
	If m == 1
		debug.notification("Canceled.")
		Return
	ElseIf bSafety == true
		debug.notification("Processing in progress.")
		Return		
	Else
		bSafety = true
	EndIf

	Int iMax = aaaKimachuuCaptiveAllyList.GetSize()
	Int i = 0
	Int iError
	Faction facTemp = none
	While i < iMax
		facTemp = aaaKimachuuCaptiveAllyList.GetAt(i) as Faction
		If facTemp == none
			iError += 1
		Else
			facTemp.SetAlly(aaaKimachuuCaptiveFaction)
		EndIf
		facTemp = none
		i += 1
	EndWhile
	If iError >= 1
		Debug.Notification(iError + "SetAlly process failure.")
	Else
		Debug.Notification(i + " SetAlly process completed.")
	EndIf

	int pCount = 200
	while pCount > 0
		aaaKimachuuCaptiveQuest.Stop()
		Utility.Wait(0.1)
		aaaKimachuuCaptiveQuest.Start()
		Actor targetF = Follower.GetActorRef()

		if targetF
			Self.moveExe(targetF)
		Else
			debug.MessageBox("Entire process completed !")
			pCount = 0
		Endif
	endwhile
	bSafety = false
EndEvent

Function moveExe(Actor aCaptive)
	Actor aPlayer = Game.GetPlayer()
	string name = aCaptive.GetLeveledActorBase().GetName()

	int n = aaaKimachuuCaptiveMarkerList.GetSize()
	n -= 1
	n = Utility.RandomInt(0, n)
	ObjectReference mapM = aaaKimachuuCaptiveMarkerList.GetAt(n) as ObjectReference

	aCaptive.Disable()
	aCaptive.AddToFaction(aaaKimachuuCaptiveFaction)
	aCaptive.MoveTo(aPlayer, 0.0, 0.0, 0.0, true)
	aCaptive.Enable()
	aCaptive.SetActorValue("Aggression", 0)
	aCaptive.SetActorValue("Confidence", 0)
	aCaptive.AddToFaction(aaaKimachuuCaptiveFaction)
	ActorUtil.AddPackageOverride(aCaptive, aaaKimachuuCaptivePackage, 100)
	aCaptive.EvaluatePackage()
	aCaptive.MoveTo(mapM as ObjectReference, 0.0, 0.0, 0.0, true)
	aCaptive.SetRestrained(true)
	Debug.SendAnimationEvent(aCaptive, "IdleBoundKneesStart")

	debug.notification(name + " Processed")
EndFunction