Scriptname aaaKimachuuAGoodfriendNPCSex Extends ActiveMagicEffect

SexLabFramework property SexLab Auto

Actor Property PlayerRef Auto
Faction Property aaaKimachuuAGFNSfaction Auto
GlobalVariable Property aaaKimachuuAGFNSchanceG Auto
Spell Property aaaKimachuuAGFNSSpellCT Auto
Spell Property aaaKimachuuAGFNSspell Auto
MagicEffect Property aaaKimachuuAGFNSmeCT 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 aaaKimachuuAGFNSnoti16 Auto ;アクターが存在しないか検索が機能していません

Actor aPartnerI ;派閥が付与された相手側初期化処理
Actor aSearcherI ;魔法が付与された検索側初期化処理
Actor aPartner ;派閥が付与された相手側
Actor aSearcher ;魔法が付与された検索側
Float rndArm ;乱数

Event OnEffectStart(Actor akTarget, Actor akCaster)
	aPartner = None
	aPartnerI = None
	aSearcherI = akCaster
	aPartnerI = SexLab.FindAvailableActorByFaction(FactionRef = aaaKimachuuAGFNSfaction as Faction, CenterRef = aSearcherI as ObjectReference, Radius = 4096.0, IgnoreRef1 = PlayerRef, HasFaction = True)
	if aPartnerI == None
		aaaKimachuuAGFNSnoti16.Show()
		Return
	Else
		RegisterForSingleLOSGain(aSearcherI, aPartnerI)
		Debug.Notification(aSearcherI.GetLeveledActorBase().GetName() + " Register")
		GotoState("Active")
	Endif
endEvent

State Active
	Event OnGainLOS(Actor akViewer, ObjectReference akTarget)
		aSearcher = akViewer
		Debug.Notification(akViewer.GetLeveledActorBase().GetName() + " GainLOS")
    	exeSex()
    	Utility.Wait(0.1)
    	GotoState("Busy")
	EndEvent

	Event OnEffectFinish(Actor akTarget, Actor akCaster)
		Debug.Notification(akCaster.GetLeveledActorBase().GetName() + " EffectFinish")
		Utility.Wait(1.0)
		Return
	endEvent
EndState

State Busy
	Event OnLostLOS(Actor akViewer, ObjectReference akTarget)
		Utility.Wait(0.1)
		GotoState("Active")
	endEvent

	Event OnEffectFinish(Actor akTarget, Actor akCaster)
		Debug.Notification(akCaster.GetLeveledActorBase().GetName() + " EffectFinish")
		Utility.Wait(1.0)
		Return
	endEvent
EndState

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

	if !aSearcher.HasKeyWordString("SexLabActive")
		aPartner = aPartnerI
		Utility.Wait(1.0)
		if aPartner == None
			Debug.Notification(aSearcher.GetLeveledActorBase().GetName() + " search failed")
			aaaKimachuuAGFNSnoti16.Show()
			;aaaKimachuuAGFNSSpellCT.Cast(aSearcher)
			;aSearcher.RemoveSpell(aaaKimachuuAGFNSspell)
		Elseif aPartner.HasKeyWordString("SexLabActive")
			aaaKimachuuAGFNSnoti06.Show()
			;aaaKimachuuAGFNSSpellCT.Cast(aSearcher)
			;aSearcher.RemoveSpell(aaaKimachuuAGFNSspell)
		Elseif !aPartner.HasKeyWordString("SexLabActive")
			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)
			;aaaKimachuuAGFNSSpellCT.Cast(aSearcher)
			;aSearcher.RemoveSpell(aaaKimachuuAGFNSspell)
			sexActors[0] = None
			sexActors[1] = None
		Else
			Debug.Notification(aSearcher.GetLeveledActorBase().GetName() + " search failed")
			aaaKimachuuAGFNSnoti05.Show()
			;aaaKimachuuAGFNSSpellCT.Cast(aSearcher)
			;aSearcher.RemoveSpell(aaaKimachuuAGFNSspell)
		Endif
	Endif
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