Scriptname zbfEffectBondage extends activemagiceffect  

zbfBondageShell Property zbf Auto

Actor kTarget

Bool bAutoRefresh
Bool bHasGagSound
Int iEffectMask

Float fUpdateFrequency
Float fNextGagSound

Bool Property bAllowBoundIdle = False Auto
Bool Property bAllowGeneral = False Auto

Function RegisterForAnotherUpdate()
	If bAutoRefresh
		RegisterForSingleUpdate(fUpdateFrequency)
	EndIf
EndFunction

Function ApplyEffects(Actor akTarget)
	zbf.ApplyModifiersMask(akTarget, iEffectMask)
	If bHasGagSound && (Utility.GetCurrentRealTime() >= fNextGagSound)
		zbf.PlayGagSound(akTarget)
		fNextGagSound = zbf.NextGagSound()
	EndIf
EndFunction

Event OnEffectStart(Actor akTarget, Actor akCaster)
	kTarget = akTarget

	; Update frequency
	fUpdateFrequency = zbf.GetUpdateIntervalForActor(kTarget)
	fNextGagSound = Utility.GetCurrentRealTime()

	; Get effect mask
	iEffectMask = zbf.WornEffectMask(akTarget)
	If !bAllowBoundIdle
		; 0x21 is zbfEffectRefresh || zbfEffectWrist
		iEffectMask = Math.LogicalAnd(iEffectMask, Math.LogicalNot(0x21))
	EndIf

	bAutoRefresh = bAllowGeneral || (Math.LogicalAnd(iEffectMask, 0x20) != 0)
	bHasGagSound = Math.LogicalAnd(iEffectMask, 0x40) != 0

	ApplyEffects(kTarget)
	RegisterForAnotherUpdate()
EndEvent

Auto State Default
	Event OnCellAttach()
		ApplyEffects(kTarget)
		RegisterForAnotherUpdate()
	EndEvent

	Event OnUpdate()
		ApplyEffects(kTarget)
		RegisterForAnotherUpdate()
	EndEvent

	Event OnEffectFinish(Actor akTarget, Actor akCaster)
		GoToState("Finished")
		ApplyEffects(kTarget)
	EndEvent
EndState

State Finished
	Function RegisterForAnotherUpdate()
	EndFunction
EndState
