Scriptname aaaKimachuuAGoodfriendNPCSex Extends ActiveMagicEffect

SexLabFramework property SexLab Auto

Actor Property PlayerRef Auto
Faction Property aaaKimachuuAGFNSfaction1 Auto
Faction Property aaaKimachuuAGFNSfaction2 Auto
Faction Property aaaKimachuuAGFNSfaction3 Auto
Faction Property aaaKimachuuAGFNSfactionS1 Auto
Faction Property aaaKimachuuAGFNSfactionS2 Auto
Faction Property aaaKimachuuAGFNSfactionS3 Auto
GlobalVariable Property aaaKimachuuAGFNSchanceG Auto
Keyword property LocTypePlayerHouse auto
Message Property aaaKimachuuAGFNSnoti01 Auto ;セックス中です
Message Property aaaKimachuuAGFNSnoti02 Auto ;スキャンを停止します
Message Property aaaKimachuuAGFNSnoti03 Auto ;検索を開始しませんでした
Message Property aaaKimachuuAGFNSnoti04 Auto ;マッチングを開始します
Message Property aaaKimachuuAGFNSnoti05 Auto ;マッチングに失敗しました
Message Property aaaKimachuuAGFNSnoti06 Auto ;対象はセックス中です
Message Property aaaKimachuuAGFNSnoti07 Auto ;マッチングに成功しました
Message Property aaaKimachuuAGFNSnoti15 Auto ;スキャンを開始します
Message Property aaaKimachuuAGFNSnoti16 Auto ;アクターが存在しないか検索が機能していません

Faction[] Property jCount Auto
Faction[] Property jCountS Auto

Actor aPartner ;ターゲット側
Actor aSearcher ;検索側
Float rndArm ;乱数
Location kCurrentLoc ;プレイヤーホーム判定

int c

Event OnLocationChange(Location akOldLoc, Location akNewLoc)
	jCount = new Faction[3]
	jCount[0] = aaaKimachuuAGFNSfaction1
	jCount[1] = aaaKimachuuAGFNSfaction2
	jCount[2] = aaaKimachuuAGFNSfaction3

	jCountS = new Faction[3]
	jCountS[0] = aaaKimachuuAGFNSfactionS1
	jCountS[1] = aaaKimachuuAGFNSfactionS2
	jCountS[2] = aaaKimachuuAGFNSfactionS3

	c = 0
	sexJudgement()
	c = 1
	sexJudgement()
	c = 2
	sexJudgement()
endEvent

Event OnEffectStart(Actor akTarget, Actor akCaster)
	aaaKimachuuAGFNSnoti15.Show()
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	aaaKimachuuAGFNSnoti02.Show()
EndEvent

Function sexJudgement()
	kCurrentLoc = PlayerRef.GetCurrentLocation()

	if c <= 1 && !kCurrentLoc.HasKeyword(LocTypePlayerHouse)
		Debug.Notification(c + "/2 not player house")
		return
	endif

	Debug.Notification("Process " + c + "/2")

		aPartner = None
		aSearcher = None
		aSearcher = SexLab.FindAvailableActorByFaction(FactionRef = jCountS[c] as Faction, CenterRef = Sexlab.PlayerRef as ObjectReference, Radius = 4096.0, IgnoreRef1 = PlayerRef, HasFaction = True)
		if aSearcher == None
			Return			
		EndIf

		aPartner = SexLab.FindAvailableActorByFaction(FactionRef = jCount[c] as Faction, CenterRef = aSearcher as ObjectReference, Radius = 4096.0, IgnoreRef1 = PlayerRef, HasFaction = True)
		if aPartner == None
			aaaKimachuuAGFNSnoti16.Show()
			Return
		Endif

	rndArm = Utility.RandomInt(0, 100)
	if aSearcher.HasKeyWordString("SexLabActive")
		aaaKimachuuAGFNSnoti01.Show()
		Return
	Elseif rndArm >= aaaKimachuuAGFNSchanceG.GetValue()
		aaaKimachuuAGFNSnoti03.Show()
		Return
	Else
		aaaKimachuuAGFNSnoti04.Show()
	endif

	if !aSearcher.HasKeyWordString("SexLabActive")
		if aPartner == None
			Debug.Notification(aSearcher.GetLeveledActorBase().GetName() + " search failed")
			aaaKimachuuAGFNSnoti16.Show()
			Return
		Elseif aPartner.HasKeyWordString("SexLabActive")
			aaaKimachuuAGFNSnoti06.Show()
			Return
		Elseif !aPartner.HasKeyWordString("SexLabActive")
			Debug.Notification(aSearcher.GetLeveledActorBase().GetName() + " & " + aPartner.GetLeveledActorBase().GetName())
			aaaKimachuuAGFNSnoti07.Show()
			actor[] sexActors = new actor[2]
			sexActors[0] = aSearcher
			sexActors[1] = aPartner
			sexActors = SexLab.SortActors(sexActors)
			sslBaseAnimation[] anims
			SexLab.StartSex(sexActors, Anims)
			Utility.Wait(0.1)
			sexActors[0] = None
			sexActors[1] = None
		Else
			Debug.Notification(aSearcher.GetLeveledActorBase().GetName() + " search failed")
			aaaKimachuuAGFNSnoti05.Show()
			Return
		Endif
	Endif
	Utility.Wait(1.0)

EndFunction

int Function StartSex(Actor[] Positions, sslBaseAnimation[] Anims, Actor Victim = None, Actor CenterOn = None)
	sslThreadModel Thread = SexLab.NewThread()
	if !Thread
		return -1
	elseIf !Thread.AddActors(Positions, Victim)
		return -1
	endIf

	; ここらへんから設定・もちろんどれかだけ指定が可能
	Thread.SetAnimations(Anims)
	Thread.DisableBedUse(False) ; ベッド使わない
	Thread.DisableLeadIn(True) ; 前戯なし
	Thread.CenterOnObject(CenterOn)

	if Thread.StartThread()
		return Thread.tid
	endIf
	return -1
EndFunction