scriptName magicAttachAshPileOnVampireDeathNPC extends ActiveMagicEffect
{Scripted effect for on death ash pile}

import debug
import FormList

;======================================================================================;
;  PROPERTIES  /
;=============/

float property fDelay = 1.00 auto
									{time to wait before Spawning Ash Pile}
; float property fDelayAlpha = 1.65 auto
; 									{time to wait before Setting alpha to zero.}
float property fDelayEnd = 1.65 auto
									{time to wait before Removing Base Actor}
float property ShaderDuration = 0.00 auto
									{Duration of Effect Shader.}
Activator property AshPileObject auto
									{The object we use as a pile.}
EffectShader property MagicEffectShader auto
									{The Effect Shader we want.}
Bool property bSetAlphaZero = True auto
									{The Effect Shader we want.}
FormList Property ImmunityList auto
									{If the target is in this list, they will not be disintegrated.}
Bool property bSetAlphaToZeroEarly = False Auto
									{Use this if we want to set the acro to invisible somewhere before the effect shader is done.}

Faction Property VampirePCFaction  Auto  
Faction Property VampirePCFamily  Auto
Faction Property PotentialFollowerFaction  Auto
Faction Property CurrentFollowerFaction Auto
Faction Property CrimeFactionEastmarch  Auto
Faction Property CrimeFactionFalkreath  Auto
Faction Property CrimeFactionHjaalmarch  Auto
Faction Property CrimeFactionImperial  Auto
Faction Property CrimeFactionOrcs  Auto
Faction Property CrimeFactionPale  Auto
Faction Property CrimeFactionReach  Auto
Faction Property CrimeFactionRift  Auto
Faction Property CrimeFactionSons  Auto
Faction Property CrimeFactionWhiterun  Auto
Faction Property CrimeFactionWinterhold  Auto
Faction Property VigilantOfStendarrFaction  Auto
Faction Property VampireHunter  Auto
Faction Property DummyFaction  Auto
Faction Property DLC1HunterFaction  Auto
Faction Property DLC1DawnguardFaction  Auto
Faction Property DLC1DawnguardExteriorGuardFaction  Auto

Faction Property WERoad02BodyguardFaction Auto
Faction Property MorthalGuardhouseFaction Auto
Faction Property dunDawnstarSanctuaryGuardianFaction Auto
Faction Property MS03ChaletGuardEnemyFaction Auto
Faction Property MQ201ExteriorGuardFaction Auto
Faction Property MQ201PartyGuardFaction Auto
Faction Property DragonsreachBasementGuards Auto
Faction Property CWWhiterunGuardNeutralFaction Auto
Faction Property GuardFactionWindhelm Auto
Faction Property GuardFactionRiften Auto
Faction Property CaravanGuard Auto
Faction Property OrcGuardFaction Auto
Faction Property DA02GuardsPlayerEnemy Auto
Faction Property DA02GuardFaction Auto
Faction Property IsGuardFaction Auto
Faction Property JobGuardCaptainFaction Auto
Faction Property KarthwastenSilverFishGuards Auto
Faction Property GuardFactionCidhnaMine Auto
Faction Property GuardFactionKolskeggr Auto
Faction Property GuardFactionSoljund Auto
Faction Property GuardFactionDawnstar Auto
Faction Property GuardFactionHaafingar Auto
Faction Property GuardFactionSolitude Auto
Faction Property GuardFactionDragonbridge Auto
Faction Property GuardFactionFalkreath Auto	
Faction Property GuardFactionKarthwasten Auto	
Faction Property GuardFactionMarkarth Auto
Faction Property GuardFactionWhiterun Auto
Faction Property CWSonsFaction Auto
Faction Property CWSonsFactionNPC Auto
Faction Property CWImperialFaction Auto
Faction Property CWImperialFactionNPC Auto
			
Faction Property CWMission08AllGiantsPlayerFriendFaction Auto
Faction Property DA02CulistsPlayerEnemy Auto
Faction Property DA02CultistsAreEnemies Auto
Faction Property DA02ElisifAfraidOfPlayer Auto
Faction Property DA07PlayerEnemyFaction Auto
Faction Property DA10PlayerEnemyFaction Auto
Faction Property DA11AttackPlayerFaction Auto
Faction Property DA16OrcAmbushFaction Auto
Faction Property DA16VaerminaHostileFaction Auto
Faction Property DB11KatariahCrewFaction Auto
Faction Property dunMarkarthWizard_SecureAreaFaction Auto
Faction Property dunYsgramorsTombGhostFaction Auto
Faction Property dunKarthwastenPlayerEnemyFaction Auto
Faction Property MarriageRivalAttackFaction Auto
Faction Property MG03CallerFaction Auto
Faction Property MGThalmorFaction Auto
Faction Property MS01PlayerEnemyFaction Auto
Faction Property RiftenRatwayFactionEnemy Auto
Faction Property RiftenSkoomaDealerFactionEnemy Auto
Faction Property T03HatePlayerFaction Auto
Faction Property TG02AringothPlayerEnemyFaction Auto
Faction Property TG04EastEmpireFactionHostile Auto
Faction Property TG07ValdFactionHatesPlayer Auto
Faction Property TG08BMercerHatesPlayerFaction Auto
Faction Property TG09NightingaleEnemyFaction Auto
Faction Property TGTQ04NiranyeAttacksFaction Auto
Faction Property WEAdventureHorseRiderFaction Auto
Faction Property WEPlayerEnemy Auto
Faction Property WEThalmorPlayerEnemyFaction Auto
Faction Property WIPlayerEnemyFaction Auto
Faction Property WIPlayerEnemySpecialCombatFaction Auto

Spell Property crVampireSunDamage Auto						
Spell Property VampireTurnToAshPile Auto
Spell Property ABVampireSkills Auto
Spell Property ABVampireSkills02 Auto
Spell Property AbVampire02 Auto
Spell Property AbVampire02b Auto
Spell Property VampireVampirism Auto
Spell Property crVampireDrain03 Auto	
Spell Property VampireCharm Auto
Spell Property VampireRaiseThrall02 Auto
Spell Property VampireStrength02 Auto
Spell Property VampireSunDamageNPC Auto
Spell Property VampireCharisma02 Auto
Spell Property VampireSleep Auto
Spell Property InvisibilityVampiric Auto
Spell Property VampireRankMistFormSpell Auto
Spell Property VampireRankFrostCloud04 Auto
Spell Property VampireRankIceFleshSpell04 Auto

Race Property ArgonianRace  Auto  
Race Property ArgonianRaceVampire  Auto  
Race Property BretonRace  Auto  
Race Property BretonRaceVampire  Auto  
Race Property DarkElfRace  Auto  
Race Property DarkElfRaceVampire  Auto  
Race Property ElderRace  Auto  
Race Property ElderRaceVampire  Auto  
Race Property HighElfRace  Auto  
Race Property HighElfRaceVampire  Auto  
Race Property ImperialRace  Auto  
Race Property ImperialRaceVampire  Auto  
Race Property KhajiitRace  Auto  
Race Property KhajiitRaceVampire  Auto  
Race Property NordRace  Auto  
Race Property NordRaceVampire  Auto  
Race Property OrcRace  Auto  
Race Property OrcRaceVampire  Auto  
Race Property RedguardRace  Auto  
Race Property RedguardRaceVampire  Auto  
Race Property FoxRace Auto
Race Property WoodElfRace  Auto  
Race Property WoodElfRaceVampire  Auto 

Float Property FeedingDayTime Auto
Int Property HoursSinceTurned Auto
Int Property CurrentlySearching Auto
Idle Property VampireFeedingBedrollLeft Auto
Idle Property VampireFeedingBedrollRight Auto
Idle Property VampireFeedingBedLeft Auto
Idle Property VampireFeedingBedRight Auto
Idle Property IdleVampireStandingBack Auto
Idle Property pa_HugA Auto
Spell Property ParalyzeNPCVampire Auto
Spell Property VampireRankBlinkAttackSpellNPC Auto
Spell Property VampireTurnToAshPileNPC Auto
Keyword Property ActorTypeNPC Auto
Keyword Property ActorTypeUndead Auto
Keyword Property Vampire Auto

ReferenceAlias Property TurnedVampireHungry Auto
ReferenceAlias Property TurnedVampireVictim Auto

Sound Property MagVampireTransform01  Auto  

MagicEffect Property InfluenceAggDownFFAimed Auto

GlobalVariable Property GameDaysPassed Auto
GlobalVariable Property VampireLastTimeFed Auto
GlobalVariable Property VampirePraestareRemoveAllFactions Auto
GlobalVariable Property VampireVictimSkills Auto
GlobalVariable Property VampireVictimAppearance Auto
GlobalVariable Property VampireTurnToAshes Auto
Globalvariable Property VampireDawnguardInstalled Auto
Globalvariable Property VampireVictimsWillFeed Auto
GlobalVariable Property VampireIdentifyingTurnedNPCs Auto
						
;======================================================================================;
;  VARIABLES   /
;=============/


actor Victim
race VictimRace
bool TargetIsImmune = True
; bool DeadAlready = FALSE

Float Function GetCurrentTimeOfDay() 
 
	FeedingDayTime = Utility.GetCurrentGameTime()
	FeedingDayTime -= Math.Floor(FeedingDayTime) ; Remove "previous in-game days passed" bit
	FeedingDayTime *= 24 ; Convert from fraction of a day to number of hours
	Return FeedingDayTime
 
EndFunction

;======================================================================================;
;   EVENTS     /
;=============/


Event OnEffectStart(Actor VampTarget, Actor Caster)

	victim = VampTarget

	If victim.GetAV("Variable01") < 0
		victim.SetAV("Variable01", 0)
	EndIf	

	If victim.GetAV("Variable05") == 667
	
		victim.SetAV("Variable01", (victim.GetAV("Variable01")+1))

		; DeadAlready = Victim.IsDead()
		trace("victim == " + victim + ", is this right?")
	
			victim.StopCombat()

			;If VampirePraestareRemoveAllFactions.GetValue() == 10000
			;	victim.RemoveFromAllFactions()
			;EndIf
			
			;Remove From Crime Factions
			victim.RemoveFromFaction(CrimeFactionEastmarch)			
			victim.RemoveFromFaction(CrimeFactionFalkreath)	
			victim.RemoveFromFaction(CrimeFactionHjaalmarch)	
			victim.RemoveFromFaction(CrimeFactionImperial)	
			victim.RemoveFromFaction(CrimeFactionOrcs)	
			victim.RemoveFromFaction(CrimeFactionPale)	
			victim.RemoveFromFaction(CrimeFactionReach)	
			victim.RemoveFromFaction(CrimeFactionRift)	
			victim.RemoveFromFaction(CrimeFactionSons)	
			victim.RemoveFromFaction(CrimeFactionWhiterun)	
			victim.RemoveFromFaction(CrimeFactionWinterhold)	
			victim.RemoveFromFaction(VigilantOfStendarrFaction)
			victim.RemoveFromFaction(VampireHunter)
			victim.RemoveFromFaction(DLC1HunterFaction)
			victim.RemoveFromFaction(DLC1DawnguardFaction)
			victim.RemoveFromFaction(DLC1DawnguardExteriorGuardFaction)			
			
			;Remove From Guard Factions
			victim.RemoveFromFaction(WERoad02BodyguardFaction)
			victim.RemoveFromFaction(MorthalGuardhouseFaction)
			victim.RemoveFromFaction(dunDawnstarSanctuaryGuardianFaction)
			victim.RemoveFromFaction(MS03ChaletGuardEnemyFaction)
			victim.RemoveFromFaction(MQ201ExteriorGuardFaction)
			victim.RemoveFromFaction(MQ201PartyGuardFaction)
			victim.RemoveFromFaction(DragonsreachBasementGuards)
			victim.RemoveFromFaction(CWWhiterunGuardNeutralFaction)
			victim.RemoveFromFaction(GuardFactionWindhelm)
			victim.RemoveFromFaction(GuardFactionRiften)
			victim.RemoveFromFaction(CaravanGuard)
			victim.RemoveFromFaction(OrcGuardFaction)
			victim.RemoveFromFaction(DA02GuardsPlayerEnemy)
			victim.RemoveFromFaction(DA02GuardFaction)
			victim.RemoveFromFaction(IsGuardFaction)
			victim.RemoveFromFaction(JobGuardCaptainFaction)
			victim.RemoveFromFaction(KarthwastenSilverFishGuards)
			victim.RemoveFromFaction(GuardFactionCidhnaMine)
			victim.RemoveFromFaction(GuardFactionKolskeggr)
			victim.RemoveFromFaction(GuardFactionSoljund)
			victim.RemoveFromFaction(GuardFactionDawnstar)
			victim.RemoveFromFaction(GuardFactionHaafingar)
			victim.RemoveFromFaction(GuardFactionSolitude)
			victim.RemoveFromFaction(GuardFactionDragonbridge)
			victim.RemoveFromFaction(GuardFactionFalkreath)	
			victim.RemoveFromFaction(GuardFactionKarthwasten)	
			victim.RemoveFromFaction(GuardFactionMarkarth)
			victim.RemoveFromFaction(GuardFactionWhiterun)
			victim.RemoveFromFaction(CWSonsFaction)
			victim.RemoveFromFaction(CWSonsFactionNPC)
			victim.RemoveFromFaction(CWImperialFaction)
			victim.RemoveFromFaction(CWImperialFactionNPC)			
			
			;Remove From Player Hated Factions			
			victim.RemoveFromFaction(CWMission08AllGiantsPlayerFriendFaction)
			victim.RemoveFromFaction(DA02CulistsPlayerEnemy)
			victim.RemoveFromFaction(DA02CultistsAreEnemies)
			victim.RemoveFromFaction(DA02ElisifAfraidOfPlayer)
			victim.RemoveFromFaction(DA07PlayerEnemyFaction)
			victim.RemoveFromFaction(DA10PlayerEnemyFaction)
			victim.RemoveFromFaction(DA11AttackPlayerFaction)
			victim.RemoveFromFaction(DA16OrcAmbushFaction)
			victim.RemoveFromFaction(DA16VaerminaHostileFaction)
			victim.RemoveFromFaction(DB11KatariahCrewFaction)
			victim.RemoveFromFaction(dunMarkarthWizard_SecureAreaFaction)
			victim.RemoveFromFaction(dunYsgramorsTombGhostFaction)
			victim.RemoveFromFaction(dunKarthwastenPlayerEnemyFaction)
			victim.RemoveFromFaction(MarriageRivalAttackFaction)
			victim.RemoveFromFaction(MG03CallerFaction)
			victim.RemoveFromFaction(MGThalmorFaction)
			victim.RemoveFromFaction(MS01PlayerEnemyFaction)
			victim.RemoveFromFaction(RiftenRatwayFactionEnemy)
			victim.RemoveFromFaction(RiftenSkoomaDealerFactionEnemy)
			victim.RemoveFromFaction(T03HatePlayerFaction)
			victim.RemoveFromFaction(TG02AringothPlayerEnemyFaction)
			victim.RemoveFromFaction(TG04EastEmpireFactionHostile)
			victim.RemoveFromFaction(TG07ValdFactionHatesPlayer)
			victim.RemoveFromFaction(TG08BMercerHatesPlayerFaction)
			victim.RemoveFromFaction(TG09NightingaleEnemyFaction)
			victim.RemoveFromFaction(TGTQ04NiranyeAttacksFaction)
			victim.RemoveFromFaction(WEAdventureHorseRiderFaction)
			victim.RemoveFromFaction(WEPlayerEnemy)
			victim.RemoveFromFaction(WEThalmorPlayerEnemyFaction)
			victim.RemoveFromFaction(WIPlayerEnemyFaction)
			victim.RemoveFromFaction(WIPlayerEnemySpecialCombatFaction)

			;Many NPCs are actually part of the Fox Race
			;If (victim.GetActorBase().GetRace() == FoxRace)
			;	victim.SetRace(NordRaceVampire)
			;	victim.RemoveFromAllFactions()
			;EndIf
			
			victim.StopCombat()				
			
			utility.wait(0.5)			
			victim.AddtoFaction(VampirePCFamily)
			victim.SetFactionRank(VampirePCFamily, 0)
			victim.AddtoFaction(DummyFaction)
			victim.SetFactionRank(DummyFaction, 0)	
			
			If victim.IsInFaction(PotentialFollowerFaction) || victim.IsInFaction(CurrentFollowerFaction)
			Else
				victim.SetRelationshipRank(Game.GetPlayer(), 4)
				Game.GetPlayer().SetRelationshipRank(victim, 4)
				victim.AddtoFaction(PotentialFollowerFaction)
				victim.SetFactionRank(PotentialFollowerFaction, 0)
			EndIf

			victim.SetActorValue("Assistance", 2)
			victim.SetActorValue("Aggression", 1)
			victim.SetActorValue("Confidence", 4)
			victim.SetActorValue("Morality", 0)				

			;victim.SetActorValue("Variable05", 666)			
			
			If VampireVictimAppearance.GetValue() == 0
				if (victim.GetActorBase().GetRace() == ArgonianRace)
					victim.SetRace(ArgonianRaceVampire)
				elseif (victim.GetActorBase().GetRace() == BretonRace)
					victim.SetRace(BretonRaceVampire)
				elseif (victim.GetActorBase().GetRace() == DarkElfRace)
					victim.SetRace(DarkElfRaceVampire)
				elseif (victim.GetActorBase().GetRace() == ElderRace)
					victim.SetRace(ElderRaceVampire)				
				elseif (victim.GetActorBase().GetRace() == HighElfRace)
					victim.SetRace(HighElfRaceVampire)
				elseif (victim.GetActorBase().GetRace() == ImperialRace)
					victim.SetRace(ImperialRaceVampire)
				elseif (victim.GetActorBase().GetRace() == KhajiitRace)
					victim.SetRace(KhajiitRaceVampire)
				elseif (victim.GetActorBase().GetRace() == NordRace)
					victim.SetRace(NordRaceVampire)
				elseif (victim.GetActorBase().GetRace() == OrcRace)
					victim.SetRace(OrcRaceVampire)
				elseif (victim.GetActorBase().GetRace() == RedguardRace)
					victim.SetRace(RedguardRaceVampire)
				elseif (victim.GetActorBase().GetRace() == WoodElfRace)
					victim.SetRace(WoodElfRaceVampire)
				endif
			EndIf
			
			If VampireVictimSkills.GetValue() == 0
			
				Float PlayerHealth = Game.GetPlayer().GetBaseActorValue("Health")
				Float VictimHealth = PlayerHealth
				victim.SetActorValue("Health", VictimHealth)
				
				Float PlayerMagicka = Game.GetPlayer().GetBaseActorValue("Magicka")
				victim.SetActorValue("Magicka", PlayerMagicka)			
				
				victim.SetActorValue("Sneak", 100)				
				
				Float Player1Hand = Game.GetPlayer().GetActorValue("OneHanded")
				victim.SetActorValue("OneHanded", Player1Hand)
				
				Float Player2Hand= Game.GetPlayer().GetActorValue("TwoHanded")
				victim.SetActorValue("TwoHanded", Player2Hand)

				Float PlayerMarksman = Game.GetPlayer().GetActorValue("Marksman")
				victim.SetActorValue("Marksman", PlayerMarksman)	
				
				Float PlayerBlock = Game.GetPlayer().GetActorValue("Block")
				victim.SetActorValue("Block", PlayerBlock)
				
				Float PlayerDamageResist = Game.GetPlayer().GetActorValue("DamageResist")
				victim.SetActorValue("DamageResist", PlayerDamageResist)
				
			EndIf	
			
			victim.SetActorValue("Sneak", 100)				
			
			victim.SetAttackActorOnSight(False)	
			
			victim.AddSpell(ABVampireSkills, abVerbose = False)
			victim.AddSpell(ABVampireSkills02, abVerbose = False)
			victim.AddSpell(AbVampire02, abVerbose = False)
			victim.AddSpell(AbVampire02b, abVerbose = False)
			victim.AddSpell(VampireVampirism, abVerbose = False)
			victim.AddSpell(crVampireDrain03, abVerbose = False)
			;victim.AddSpell(VampireRankFrostCloud04, abVerbose = False)
			victim.AddSpell(VampireRankIceFleshSpell04, abVerbose = False)
			victim.AddSpell(VampireRankMistFormSpell, abVerbose = False)
			victim.AddSpell(VampireRaiseThrall02, abVerbose = False)
			victim.AddSpell(VampireStrength02, abVerbose = False)
			;victim.AddSpell(VampireSunDamageNPC, abVerbose = False)
			victim.AddSpell(crVampireSunDamage, abVerbose = False)				
			victim.AddSpell(VampireCharisma02, abVerbose = False)
			victim.AddSpell(VampireSleep, abVerbose = False)
			victim.AddSpell(InvisibilityVampiric, abVerbose = False)
			
			utility.wait(1.0)			
	
			victim.StopCombat()
			
			;CurrentlySearching = 0
			
			;victim.EvaluatePackage()
			If victim.IsOnMount()
			Else
				If SKSE.GetVersionRelease() > 0
					If VampireVictimAppearance.GetValue() == 0
						Utility.Wait(0.1)
						;ActorBase victimActorBase = victim.GetActorBase()
						;Float victimOrigWeight = victim.GetWeight()
						;Float victimNewWeight = Utility.RandomFloat(50.0, 60.0)
						;Float NeckDelta = (victimOrigWeight / 100) - (victimNewWeight / 100)			
						;victimActorBase.SetWeight(victimNewWeight)	
						;;victimActorBase.UpdateWeight(NeckDelta)
						String facegen = "bUseFaceGenPreprocessedHeads:General"
						Utility.SetINIBool(facegen, False)
						Utility.Wait(0.5)
						victim.QueueNiNodeUpdate()
						;victim.RegenerateHead()
						Utility.Wait(0.5)				
						Utility.SetINIBool(facegen, True)
					EndIf
				EndIf	
			EndIf				
			
			;Debug.Notification("Hours since NPC fed = "+(victim.GetAV("Variable01") as Int))			
			
			If victim.GetAV("Variable01") >= 4 && CurrentlySearching == 0
				LookForFeedingVictim()
			EndIf
			
	EndIf		
			
	Utility.Wait(30.0)
	victim.SetAV("Variable05", 666)	
	victim.SetAV("Variable05", 666)		
	
	VampireIdentifyingTurnedNPCs.SetValue(0)	
	
	;RegisterForUpdateGameTime(1)
	
EndEvent


;==============================================================================================


Event OnUpdateGameTime()


	If victim.GetAV("Variable05") == 666
	
		;victim.SetAV("Variable01", (victim.GetAV("Variable01")+1))

		; DeadAlready = Victim.IsDead()
		trace("victim == " + victim + ", is this right?")
	
			;victim.StopCombat()

			;If VampirePraestareRemoveAllFactions.GetValue() == 10000
			;	victim.RemoveFromAllFactions()
			;EndIf
			
			;Remove From Crime Factions
			victim.RemoveFromFaction(CrimeFactionEastmarch)			
			victim.RemoveFromFaction(CrimeFactionFalkreath)	
			victim.RemoveFromFaction(CrimeFactionHjaalmarch)	
			victim.RemoveFromFaction(CrimeFactionImperial)	
			victim.RemoveFromFaction(CrimeFactionOrcs)	
			victim.RemoveFromFaction(CrimeFactionPale)	
			victim.RemoveFromFaction(CrimeFactionReach)	
			victim.RemoveFromFaction(CrimeFactionRift)	
			victim.RemoveFromFaction(CrimeFactionSons)	
			victim.RemoveFromFaction(CrimeFactionWhiterun)	
			victim.RemoveFromFaction(CrimeFactionWinterhold)	
			victim.RemoveFromFaction(VigilantOfStendarrFaction)
			victim.RemoveFromFaction(VampireHunter)
			victim.RemoveFromFaction(DLC1HunterFaction)
			victim.RemoveFromFaction(DLC1DawnguardFaction)
			victim.RemoveFromFaction(DLC1DawnguardExteriorGuardFaction)			
			
			;Remove From Guard Factions
			victim.RemoveFromFaction(WERoad02BodyguardFaction)
			victim.RemoveFromFaction(MorthalGuardhouseFaction)
			victim.RemoveFromFaction(dunDawnstarSanctuaryGuardianFaction)
			victim.RemoveFromFaction(MS03ChaletGuardEnemyFaction)
			victim.RemoveFromFaction(MQ201ExteriorGuardFaction)
			victim.RemoveFromFaction(MQ201PartyGuardFaction)
			victim.RemoveFromFaction(DragonsreachBasementGuards)
			victim.RemoveFromFaction(CWWhiterunGuardNeutralFaction)
			victim.RemoveFromFaction(GuardFactionWindhelm)
			victim.RemoveFromFaction(GuardFactionRiften)
			victim.RemoveFromFaction(CaravanGuard)
			victim.RemoveFromFaction(OrcGuardFaction)
			victim.RemoveFromFaction(DA02GuardsPlayerEnemy)
			victim.RemoveFromFaction(DA02GuardFaction)
			victim.RemoveFromFaction(IsGuardFaction)
			victim.RemoveFromFaction(JobGuardCaptainFaction)
			victim.RemoveFromFaction(KarthwastenSilverFishGuards)
			victim.RemoveFromFaction(GuardFactionCidhnaMine)
			victim.RemoveFromFaction(GuardFactionKolskeggr)
			victim.RemoveFromFaction(GuardFactionSoljund)
			victim.RemoveFromFaction(GuardFactionDawnstar)
			victim.RemoveFromFaction(GuardFactionHaafingar)
			victim.RemoveFromFaction(GuardFactionSolitude)
			victim.RemoveFromFaction(GuardFactionDragonbridge)
			victim.RemoveFromFaction(GuardFactionFalkreath)	
			victim.RemoveFromFaction(GuardFactionKarthwasten)	
			victim.RemoveFromFaction(GuardFactionMarkarth)
			victim.RemoveFromFaction(GuardFactionWhiterun)
			victim.RemoveFromFaction(CWSonsFaction)
			victim.RemoveFromFaction(CWSonsFactionNPC)
			victim.RemoveFromFaction(CWImperialFaction)
			victim.RemoveFromFaction(CWImperialFactionNPC)			
			
			;Remove From Player Hated Factions			
			victim.RemoveFromFaction(CWMission08AllGiantsPlayerFriendFaction)
			victim.RemoveFromFaction(DA02CulistsPlayerEnemy)
			victim.RemoveFromFaction(DA02CultistsAreEnemies)
			victim.RemoveFromFaction(DA02ElisifAfraidOfPlayer)
			victim.RemoveFromFaction(DA07PlayerEnemyFaction)
			victim.RemoveFromFaction(DA10PlayerEnemyFaction)
			victim.RemoveFromFaction(DA11AttackPlayerFaction)
			victim.RemoveFromFaction(DA16OrcAmbushFaction)
			victim.RemoveFromFaction(DA16VaerminaHostileFaction)
			victim.RemoveFromFaction(DB11KatariahCrewFaction)
			victim.RemoveFromFaction(dunMarkarthWizard_SecureAreaFaction)
			victim.RemoveFromFaction(dunYsgramorsTombGhostFaction)
			victim.RemoveFromFaction(dunKarthwastenPlayerEnemyFaction)
			victim.RemoveFromFaction(MarriageRivalAttackFaction)
			victim.RemoveFromFaction(MG03CallerFaction)
			victim.RemoveFromFaction(MGThalmorFaction)
			victim.RemoveFromFaction(MS01PlayerEnemyFaction)
			victim.RemoveFromFaction(RiftenRatwayFactionEnemy)
			victim.RemoveFromFaction(RiftenSkoomaDealerFactionEnemy)
			victim.RemoveFromFaction(T03HatePlayerFaction)
			victim.RemoveFromFaction(TG02AringothPlayerEnemyFaction)
			victim.RemoveFromFaction(TG04EastEmpireFactionHostile)
			victim.RemoveFromFaction(TG07ValdFactionHatesPlayer)
			victim.RemoveFromFaction(TG08BMercerHatesPlayerFaction)
			victim.RemoveFromFaction(TG09NightingaleEnemyFaction)
			victim.RemoveFromFaction(TGTQ04NiranyeAttacksFaction)
			victim.RemoveFromFaction(WEAdventureHorseRiderFaction)
			victim.RemoveFromFaction(WEPlayerEnemy)
			victim.RemoveFromFaction(WEThalmorPlayerEnemyFaction)
			victim.RemoveFromFaction(WIPlayerEnemyFaction)
			victim.RemoveFromFaction(WIPlayerEnemySpecialCombatFaction)

			;Many NPCs are actually part of the Fox Race
			;If (victim.GetActorBase().GetRace() == FoxRace)
			;	victim.SetRace(NordRaceVampire)
			;	victim.RemoveFromAllFactions()
			;EndIf
			
			victim.StopCombat()				
			
			utility.wait(0.5)			
			victim.AddtoFaction(VampirePCFamily)
			victim.SetFactionRank(VampirePCFamily, 0)
			victim.AddtoFaction(DummyFaction)
			victim.SetFactionRank(DummyFaction, 0)	
			
			If victim.IsInFaction(PotentialFollowerFaction) || victim.IsInFaction(CurrentFollowerFaction)
			Else
				victim.SetRelationshipRank(Game.GetPlayer(), 4)
				Game.GetPlayer().SetRelationshipRank(victim, 4)
				victim.AddtoFaction(PotentialFollowerFaction)
				victim.SetFactionRank(PotentialFollowerFaction, 0)
			EndIf

			victim.SetActorValue("Assistance", 2)
			victim.SetActorValue("Aggression", 1)
			victim.SetActorValue("Confidence", 4)
			victim.SetActorValue("Morality", 0)				

			;victim.SetActorValue("Variable05", 666)			
			
			If VampireVictimAppearance.GetValue() == 0
				if (victim.GetActorBase().GetRace() == ArgonianRace)
					victim.SetRace(ArgonianRaceVampire)
				elseif (victim.GetActorBase().GetRace() == BretonRace)
					victim.SetRace(BretonRaceVampire)
				elseif (victim.GetActorBase().GetRace() == DarkElfRace)
					victim.SetRace(DarkElfRaceVampire)
				elseif (victim.GetActorBase().GetRace() == ElderRace)
					victim.SetRace(ElderRaceVampire)				
				elseif (victim.GetActorBase().GetRace() == HighElfRace)
					victim.SetRace(HighElfRaceVampire)
				elseif (victim.GetActorBase().GetRace() == ImperialRace)
					victim.SetRace(ImperialRaceVampire)
				elseif (victim.GetActorBase().GetRace() == KhajiitRace)
					victim.SetRace(KhajiitRaceVampire)
				elseif (victim.GetActorBase().GetRace() == NordRace)
					victim.SetRace(NordRaceVampire)
				elseif (victim.GetActorBase().GetRace() == OrcRace)
					victim.SetRace(OrcRaceVampire)
				elseif (victim.GetActorBase().GetRace() == RedguardRace)
					victim.SetRace(RedguardRaceVampire)
				elseif (victim.GetActorBase().GetRace() == WoodElfRace)
					victim.SetRace(WoodElfRaceVampire)
				endif
			EndIf
			
			If VampireVictimSkills.GetValue() == 0
			
				Float PlayerHealth = Game.GetPlayer().GetBaseActorValue("Health")
				Float VictimHealth = PlayerHealth
				victim.SetActorValue("Health", VictimHealth)
				
				Float PlayerMagicka = Game.GetPlayer().GetBaseActorValue("Magicka")
				victim.SetActorValue("Magicka", PlayerMagicka)			
				
				victim.SetActorValue("Sneak", 100)				
				
				Float Player1Hand = Game.GetPlayer().GetActorValue("OneHanded")
				victim.SetActorValue("OneHanded", Player1Hand)
				
				Float Player2Hand= Game.GetPlayer().GetActorValue("TwoHanded")
				victim.SetActorValue("TwoHanded", Player2Hand)

				Float PlayerMarksman = Game.GetPlayer().GetActorValue("Marksman")
				victim.SetActorValue("Marksman", PlayerMarksman)	
				
				Float PlayerBlock = Game.GetPlayer().GetActorValue("Block")
				victim.SetActorValue("Block", PlayerBlock)
				
				Float PlayerDamageResist = Game.GetPlayer().GetActorValue("DamageResist")
				victim.SetActorValue("DamageResist", PlayerDamageResist)
				
			EndIf	
			
			victim.SetActorValue("Sneak", 100)				
			
			victim.SetAttackActorOnSight(False)	
			
			victim.AddSpell(ABVampireSkills, abVerbose = False)
			victim.AddSpell(ABVampireSkills02, abVerbose = False)
			victim.AddSpell(AbVampire02, abVerbose = False)
			victim.AddSpell(AbVampire02b, abVerbose = False)
			victim.AddSpell(VampireVampirism, abVerbose = False)
			victim.AddSpell(crVampireDrain03, abVerbose = False)
			;victim.AddSpell(VampireRankFrostCloud04, abVerbose = False)
			victim.AddSpell(VampireRankIceFleshSpell04, abVerbose = False)
			victim.AddSpell(VampireRankMistFormSpell, abVerbose = False)
			victim.AddSpell(VampireRaiseThrall02, abVerbose = False)
			victim.AddSpell(VampireStrength02, abVerbose = False)
			;victim.AddSpell(VampireSunDamageNPC, abVerbose = False)
			victim.AddSpell(crVampireSunDamage, abVerbose = False)				
			victim.AddSpell(VampireCharisma02, abVerbose = False)
			victim.AddSpell(VampireSleep, abVerbose = False)
			victim.AddSpell(InvisibilityVampiric, abVerbose = False)
			
			utility.wait(1.0)			
	
			victim.StopCombat()
			
			;CurrentlySearching = 0
			
			;victim.EvaluatePackage()
			If victim.IsOnMount()
			Else
				If SKSE.GetVersionRelease() > 0
					If VampireVictimAppearance.GetValue() == 0
						Utility.Wait(0.1)
						;ActorBase victimActorBase = victim.GetActorBase()
						;Float victimOrigWeight = victim.GetWeight()
						;Float victimNewWeight = Utility.RandomFloat(50.0, 60.0)
						;Float NeckDelta = (victimOrigWeight / 100) - (victimNewWeight / 100)			
						;victimActorBase.SetWeight(victimNewWeight)	
						;;victimActorBase.UpdateWeight(NeckDelta)
						String facegen = "bUseFaceGenPreprocessedHeads:General"
						Utility.SetINIBool(facegen, False)
						Utility.Wait(0.5)
						victim.QueueNiNodeUpdate()
						Utility.Wait(0.5)				
						Utility.SetINIBool(facegen, True)
					EndIf
				EndIf	
			EndIf				
			
			;Debug.Notification("Hours since NPC fed = "+(victim.GetAV("Variable01") as Int))			
			
			If victim.GetAV("Variable01") >= 3 && CurrentlySearching == 0
				LookForFeedingVictim()
			EndIf
			
	EndIf		


EndEvent


;==============================================================================================


Function LookForFeedingVictim()

			GetCurrentTimeOfDay()
			
			If VampireVictimsWillFeed.GetValue() == 0 && victim.GetAV("Variable01") >= 8 && CurrentlySearching == 0 && (victim.IsInInterior() == 1 || (victim.IsInInterior() == 0 && FeedingDayTime < 5) || (victim.IsInInterior() == 0 && FeedingDayTime > 20))

				Utility.Wait(0.1)

				Int SearchForVictim = 40				
			
				;Debug.Notification("NPC Vampire is searching!")
			
				While SearchForVictim > 0
					
					Actor RandomFeedTarget = Game.FindRandomActorFromRef(victim, 1200)
					CurrentlySearching = 1
					Race RandomFeedTargetRace = RandomFeedTarget.GetRace()
					
					If (RandomFeedTargetRace == ArgonianRace) || (RandomFeedTargetRace == BretonRace) || (RandomFeedTargetRace == DarkElfRace) || (RandomFeedTargetRace == HighElfRace) || (RandomFeedTargetRace == ImperialRace) || (RandomFeedTargetRace == KhajiitRace) || (RandomFeedTargetRace == NordRace) || (RandomFeedTargetRace == OrcRace) || (RandomFeedTargetRace == RedguardRace) || (RandomFeedTargetRace == FoxRace) || (RandomFeedTargetRace == WoodElfRace)
					
						If RandomFeedTarget.HasKeyword(ActorTypeNPC) && RandomFeedTarget.HasKeyword(ActorTypeUndead) == False && RandomFeedTarget.HasKeyword(Vampire) == False && RandomFeedTarget != victim && RandomFeedTarget != Game.GetPlayer() && RandomFeedTarget.IsChild() == False && RandomFeedTarget.IsGhost() == False  && RandomFeedTarget.IsDead() == False

							;Debug.Notification("NPC Vampire has found " + RandomFeedTarget)
							TurnedVampireHungry.Clear()
							TurnedVampireVictim.Clear()							
							TurnedVampireHungry.ForceRefTo(victim)
							TurnedVampireVictim.ForceRefTo(RandomFeedTarget)
							victim.EvaluatePackage()
							
							victim.SetLookAt(RandomFeedTarget)
							
							Int SneakingToVictim = 60

							While victim.GetDistance(RandomFeedTarget) > 100 && SneakingToVictim > 0
								;Debug.Notification("NPC Vampire is sneaking to target")
								SneakingToVictim -= 1
								Utility.Wait(1.0)
							EndWhile
							
							Int TryingToFeed = 10
							Bool NPCIsFeeding = False
							
							;Debug.Notification("NPC Vampire trying to feed ...")							
							
							While victim.GetDistance(RandomFeedTarget) < 400 && TryingToFeed > 0 && NPCIsFeeding == False
						
								If RandomFeedTarget.GetSleepState() >= 3
									If RandomFeedTarget.IsInInterior() == True
										If RandomFeedTarget.GetHeadingAngle(victim) > 0
											NPCIsFeeding = victim.PlayIdle(VampireFeedingBedLeft)
											;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
											;Sound.SetInstanceVolume(instanceID1, 0.5)
											If NPCIsFeeding == True
												;Debug.Notification("Bed Feeding")
												victim.SetAV("Variable01", 0)
											EndIf	
										Else
											NPCIsFeeding = victim.PlayIdle(VampireFeedingBedRight)
											;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
											;Sound.SetInstanceVolume(instanceID1, 0.5)
											If NPCIsFeeding == True
												;Debug.Notification("Bed Feeding")
												victim.SetAV("Variable01", 0)
											EndIf	
										EndIf			
									ElseIf RandomFeedTarget.IsInInterior() == False
										If RandomFeedTarget.GetHeadingAngle(victim) > 0	
											NPCIsFeeding = victim.PlayIdle(VampireFeedingBedrollLeft)
											;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
											;Sound.SetInstanceVolume(instanceID1, 0.5)
											If NPCIsFeeding == True
												;Debug.Notification("Bedroll Feeding")
												victim.SetAV("Variable01", 0)
											EndIf	
										Else
											NPCIsFeeding = victim.PlayIdle(VampireFeedingBedrollRight)
											;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
											;Sound.SetInstanceVolume(instanceID1, 0.5)
											If NPCIsFeeding == True
												;Debug.Notification("Bedroll Feeding")
												victim.SetAV("Variable01", 0)
											EndIf	
										EndIf	
									EndIf
								ElseIf RandomFeedTarget.GetSleepState() < 3
									If VampireDawnguardInstalled.GetValue() == 10000
										If !RandomFeedTarget.HasMagicEffect(InfluenceAggDownFFAimed) 
											VampireCharm.Cast(victim, RandomFeedTarget)
										EndIf	
										Utility.Wait(3.0)
										NPCIsFeeding = victim.PlayIdleWithTarget(IdleVampireStandingBack, RandomFeedTarget as ObjectReference)
										;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
										;Sound.SetInstanceVolume(instanceID1, 0.5)
										If NPCIsFeeding == True
											;Debug.Notification("Grapple Feeding")
											victim.SetAV("Variable01", 0)
										EndIf										
									ElseIf VampireDawnguardInstalled.GetValue() == 0
										If !RandomFeedTarget.HasMagicEffect(InfluenceAggDownFFAimed) 
											VampireCharm.Cast(victim, RandomFeedTarget)
										EndIf
										Utility.Wait(3.0)
										NPCIsFeeding = victim.PlayIdleWithTarget(pa_HugA, RandomFeedTarget as ObjectReference)
										;Int instanceID1 = MAGVampireTransform01.Play(RandomFeedTarget)
										;Sound.SetInstanceVolume(instanceID1, 0.5)
										If NPCIsFeeding == True
											;Debug.Notification("Hug Feeding")
											victim.SetAV("Variable01", 0)
										EndIf		
									EndIf		
								EndIf
								
								TryingToFeed -= 1
								
							EndWhile
							
							victim.ClearLookAt()
							CurrentlySearching = 0
							SearchForVictim = 0
							
							;Debug.Notification("NPC Vampire is now finished searching.")
							TurnedVampireHungry.Clear()
							TurnedVampireVictim.Clear()
							
							If RandomFeedTarget.GetSleepState() >= 3
							Else
								Utility.Wait(4.0)
								If victim.GetDistance(RandomFeedTarget) < 200
									InvisibilityVampiric.Cast(victim, victim)
								EndIf	
							EndIf	
							
						Else
						
							;Debug.Notification("No specific target...")
							
						EndIf
					
						;Debug.Notification("No racial target...")
					
					EndIf
				
					SearchForVictim -= 1
				
					Utility.Wait(1.0)				
				
				EndWhile
			
				CurrentlySearching = 0
			
			EndIf
			
			;Debug.Notification("NPC Vampire will not feed at this time.")
			
			CurrentlySearching = 0

EndFunction

;==============================================================================================

Event OnDying(Actor Killer)

		utility.wait(0.1)
			 
		If VampireTurnToAshes.GetValue() == 0			 
             victim.SetCriticalStage(Victim.CritStage_DisintegrateStart)
             ;victim.SetAlpha (0.99,False)
             MagicEffectShader.play(Victim,ShaderDuration)
             if bSetAlphaToZeroEarly
                   victim.SetAlpha (0.0,True)
             endif
             utility.wait(fDelay)
             Victim.AttachAshpile(AshPileObject)
             ;AshPileRef = AshPileObject
             ;AshPileRef.SetAngle(0.0,0.0,(Victim.GetAngleZ()))
             utility.wait(fDelayEnd)
             MagicEffectShader.stop(Victim)
             if bSetAlphaZero == True
                    victim.SetAlpha (0.0,True)
             endif
             utility.wait(fDelayEnd)
		EndIf	 
			 
		victim.RemoveSpell(VampireTurnToAshPileNPC)
		
		UnRegisterForUpdateGameTime()		
			 
EndEvent

;==============================================================================================

Event OnEffectFinish(Actor VampTarget, Actor Caster)
	


EndEvent
