ScriptName VampireSeductionBefriend extends ActiveMagicEffect
{Scripted addition to Vampire Seduction to charm the NPC and make them a teammate}

Keyword Property VampireHunterKeyword Auto
ReferenceAlias Property VampireDominationAlias Auto
Spell Property VampireCharm2 Auto
PlayerVampireQuestScript Property PlayerVampireQuest  Auto
GlobalVariable Property EnableVampireBloodPoints Auto
GlobalVariable Property VampireBloodPoints Auto
GlobalVariable Property RECASTVampireDomination Auto
GlobalVariable Property VampireAbilityTimers Auto
GlobalVariable Property VampireDominationToggle Auto
Sound Property QSTHalldirCairnFade Auto
	
Event OnEffectStart(Actor akCharmTarget, Actor Caster)

	Int instanceID1 = QSTHalldirCairnFade.Play(akCharmTarget)
	Sound.SetInstanceVolume(instanceID1, 0.6)
			
	If VampireDominationToggle.GetValue() == 0
	
		VampireDominationToggle.SetValue(1)	
	
		Float TargetWillpower = akCharmTarget.GetAV("Health") + akCharmTarget.GetAV("Stamina")
		Float PlayerWillpower = Game.GetPlayer().GetAV("Health") + Game.GetPlayer().GetAV("Stamina")

		Int VictimIsTooPowerful = 0
		VictimIsTooPowerful = 0
		Int VictimFightsBack = Utility.RandomInt(1, 10)
		
		If (TargetWillpower > PlayerWillpower) && VictimFightsBack >= 6
			VictimIsTooPowerful = 1
		EndIf		
		
		If (VictimIsTooPowerful == 0 && akCharmTarget.IsPlayerTeammate() == False && akCharmTarget.HasKeyword(VampireHunterKeyword) == False)

			Caster.StopCombat()
			akCharmTarget.StopCombat()
			
			If EnableVampireBloodPoints.GetValue() == 10000
				PlayerVampireQuest.VampireBloodPoints(40)
				RegisterForUpdate(5.0)
			EndIf			
			
			Actor OldCharmedTarget = VampireDominationAlias.GetReference() as Actor
			If OldCharmedTarget	
				OldCharmedTarget.DispelSpell(VampireCharm2)		
			EndIf
			
			Utility.Wait(2.0)
			
			VampireDominationAlias.ForceRefTo(akCharmTarget)
			akCharmTarget.SetActorValue("Assistance", 2)
			akCharmTarget.SetActorValue("Aggression", 1)
			akCharmTarget.SetActorValue("Confidence", 4)
			akCharmTarget.SetActorValue("Morality", 0)		
			akCharmTarget.EvaluatePackage()
			akCharmTarget.SetPlayerTeammate(True)
			akCharmTarget.SetRelationshipRank(Game.GetPlayer(), 3)
			akCharmTarget.SetActorValue("Assistance", 2)
			akCharmTarget.SetActorValue("Aggression", 1)
			akCharmTarget.SetActorValue("Confidence", 4)
			akCharmTarget.SetActorValue("Morality", 0)
		
		Else
		
			akCharmTarget.DispelSpell(VampireCharm2)
			Utility.Wait(1.0)						
			Debug.Notification("Your victim is too strong to be dominated ...")
			akCharmTarget.SendAssaultAlarm()
			akCharmTarget.StartCombat(Game.GetPlayer())					
			
		EndIf
		
	Else

		akCharmTarget.DispelSpell(VampireCharm2)
		VampireDominationToggle.SetValue(0)

	EndIf
	
EndEvent


Event OnUpdate()

		If EnableVampireBloodPoints.GetValue() == 10000
			If VampireBloodPoints.GetValue() > 0
				VampireBloodPoints.SetValue(VampireBloodPoints.GetValue() - 1)
			EndIf
		EndIf

EndEvent


Event OnEffectFinish(Actor akCharmTarget, Actor Caster)

	Actor OldCharmedTarget = VampireDominationAlias.GetReference() as Actor

	OldCharmedTarget.SetPlayerTeammate(False)
	OldCharmedTarget.SetRelationshipRank(Game.GetPlayer(), 0)
	VampireDominationAlias.Clear()
	OldCharmedTarget.EvaluatePackage()
	
	If RECASTVampireDomination.GetValue() <= 1
		RECASTVampireDomination.SetValue(1)
	EndIf	
	
	If RECASTVampireDomination.GetValue() == 1
	
		RECASTVampireDomination.SetValue(2)
		If VampireAbilityTimers.GetValue() == 0	
			Debug.Notification("You need to wait before attempting to dominate again")	
			Utility.WaitGameTime(5)
			Utility.Wait(0.1)
			If Game.GetPlayer().HasSpell(VampireCharm2)		
				Debug.Notification("Domination is available for use")
			EndIf	
		EndIf	
		RECASTVampireDomination.SetValue(0)
		
	EndIf	
	
	VampireDominationToggle.SetValue(0)
	UnregisterForUpdate()
	UnregisterForUpdate()
	
EndEvent
