;BEGIN FRAGMENT CODE - Do not edit anything between this and the end comment
;NEXT FRAGMENT INDEX 2
Scriptname PF_BloodthirstFeeding_030034FD Extends Package Hidden

;BEGIN FRAGMENT Fragment_1
Function Fragment_1(Actor akActor)
;BEGIN CODE
Actor vampireActor = VampireAlias.GetActorReference()
Actor victimActor =  VictimAlias.GetActorReference()

if(  vampireActor != None ) && (  victimActor != None ) &&  \
	( ! vampireActor .IsDead() ) &&  (! victimActor .IsDead() ) &&  \
	 ( vampireActor.GetDistance( victimActor ) < 110 )

	int observeAttempts =  3
	if( victimActor.GetSleepState() >= 3)
		observeAttempts = 1
	endif

	while( observeAttempts > 0 ) 
		Actor observer = Game.FindRandomActorFromRef( vampireActor, 600 ) 
		if ( observer != victimActor  ) && vampireActor.IsDetectedBy(observer) && ( observer.GetRelationshipRank(vampireActor) < 3 ) && ( ! observer.HasKeyword(Vampire) )
			VampireAlias.ForceRefTo(Game.GetPlayer())
			VictimAlias.ForceRefTo(Game.GetPlayer())

			VampireAlias.ForceRefTo(None)
			VictimAlias.ForceRefTo(None)
			return
		endif

		observeAttempts -= 1
	endWhile

	bool playingAnimation = false
	if( victimActor.GetSleepState() >= 3)
		if( victimActor.GetHeadingAngle(vampireActor) > 0)
			if( victimActor.IsInInterior() )
				playingAnimation  = vampireActor.PlayIdle(VampireFeedingBedLeft_Loose)
			else
				playingAnimation  = vampireActor.PlayIdle(VampireFeedingBedrollLeft_Loose)
			endif
		else
			if( victimActor.IsInInterior() )
				playingAnimation  = vampireActor.PlayIdle(VampireFeedingBedRight_Loose)
			else
				playingAnimation  = vampireActor.PlayIdle(VampireFeedingBedrollRight_Loose)
			endif
		endif 
	else
		playingAnimation =   vampireActor.PlayIdleWithTarget( IdleVampireStandingBack, victimActor )

		int feedAttempts = 60
		while(feedAttempts > 0 ) && ( ! playingAnimation )
			playingAnimation =   vampireActor.PlayIdleWithTarget( IdleVampireStandingBack, victimActor )
			Utility.Wait(0.07)
			feedAttempts -= 1
		endWhile
	endif

	if( playingAnimation )
		; Can cause the alarm? vampireActor.StartVampireFeed(victimActor )

		vampireActor.StopCombat()

		; Alarm preventing Spike 1
		victimActor.StopCombatAlarm()
		victimActor.StopCombat()

		if( victimActor.GetSleepState() < 3)

			victimActor.SendStealAlarm(vampireActor) ; Alarm preventing Spike 2
						
			; Alarm preventing Spike
			victimActor.StopCombatAlarm()
			victimActor.StopCombat()

			Utility.wait(2)

			; Alarm preventing Spike
			victimActor.StopCombatAlarm()
			victimActor.StopCombat()
			
			if( Utility.RandomInt(0, 100) > vampireActor.GetAV("Sneak") )
				BloodthirstBleedFX.Cast(victimActor,victimActor)
			endif
					
			; Alarm preventing Spike
			victimActor.StopCombatAlarm()
			victimActor.StopCombat()
			vampireActor.StopCombat()

			Utility.wait(4)

			; Alarm preventing Spike
			victimActor.StopCombatAlarm()
			victimActor.StopCombat()
			vampireActor.StopCombat()

			if( victimActor== Game.GetPlayer() ) && ( ! vampireActor.IsInFaction(DLC1VampireCrimeFaction ) )  && ( ! vampireActor.IsInFaction(DLC1SeranaFaction) )
				victimActor.DoCombatSpellApply(DiseaseSanguinareVampiris, victimActor)
			endif
		endif

	endif
		
	vampireActor.ClearLookAt()


	vampireActor.SetAV("Variable08", 0)


		VampireAlias.ForceRefTo(Game.GetPlayer())
		VictimAlias.ForceRefTo(Game.GetPlayer())
	
		VampireAlias.ForceRefTo(None)
		VictimAlias.ForceRefTo(None)
endif
;END CODE
EndFunction
;END FRAGMENT

;END FRAGMENT CODE - Do not edit anything between this and the begin comment

ReferenceAlias Property VampireAlias  Auto  
ReferenceAlias Property VictimAlias  Auto  
Idle Property IdleVampireStandingBack Auto

Idle Property VampireFeedingBedrollRight_Loose Auto
Idle Property VampireFeedingBedrollLeft_Loose Auto
Idle Property VampireFeedingBedRight_Loose Auto
Idle Property VampireFeedingBedLeft_Loose Auto

Keyword Property Vampire Auto

SPELL Property BloodthirstBleedFX Auto

SPELL Property DiseaseSanguinareVampiris Auto
Faction Property DLC1VampireCrimeFaction Auto
Faction Property DLC1SeranaFaction Auto
