Scriptname JR_JailQuestScript extends Quest  

ReferenceAlias[] Property raActors Auto
Message Property JR_RapeMsg  Auto
SexLabFramework Property sexlab Auto
bool Property LastRaped Auto

Actor[] actors
int nActors
float[] posX
float[] posY
float[] posZ

Event OnStoryJail(ObjectReference akGuard, Form akCrimeGroup, Location akLocation, int aiCrimeGold)
	LastRaped = false

	actors = new Actor[3]
	posX = new float[3]
	posY = new float[3]
	posZ = new float[3]

	nActors = 2
	if raActors[2] && Utility.RandomInt(0, 100) < 30
		nActors += 1
	endif

	int i = 0
	while i < nActors
		actors[i] = raActors[i].GetActorRef()
		posX[i] = actors[i].GetPositionX()
		posY[i] = actors[i].GetPositionY()
		posZ[i] = actors[i].GetPositionZ()
		i += 1
	endWhile

	i = 10
	sslBaseAnimation[] anims
	while !anims && i
		i -= 1
		anims = sexlab.GetAnimationsByType(nActors, Males = nActors - 1, Females = 1, Aggressive = true)
		if anims
			anims = sexlab.RemoveTagged(anims, "Guro,Necro,Sleeping")
		endif
	endWhile

	sexlab.TrackActor(actors[1], "JailRape")
	RegisterForModEvent("JailRape_End", "OnJailRapeEnd")

	Actor[] positions = SexLabUtil.MakeActorArray(actors[0], actors[1], actors[2])
	if sexlab.StartSex(positions, anims, Victim = positions[0]) != -1
		JR_RapeMsg.Show()
		LastRaped = true
	else
		sexlab.UntrackActor(actors[1], "JailRape")
	endif

	if !LastRaped
		Stop()
	endif
endEvent

Event OnJailRapeEnd(Form FormRef, int tid)
	int i = 0
	while i < nActors
		actors[i].SetPosition(posX[i], posY[i], posZ[i])
		i += 1
	endWhile
	sexlab.UntrackActor(actors[1], "JailRape")
	Stop()
endEvent

