Scriptname PAF_InventoryCheck extends activemagiceffect  


Keyword Property VendorItemPotion Auto
Keyword Property VendorItemWeapon Auto
Keyword Property VendorItemPoison Auto
GlobalVariable Property PAF_PotionAnimated Auto
GlobalVariable Property PAF_AllowPotionUse Auto
GlobalVariable Property PAF_ModelBugFix Auto
GlobalVariable Property PAF_DrinkSFX Auto
GlobalVariable Property PAF_CameraSwitch Auto
GlobalVariable Property PAF_AllowJump Auto
Actor Property PlayerRef Auto
Spell Property PAF_InventoryCheckSpell Auto
Spell Property PAF_ReduceSpeedSpell Auto
Weapon Property PAF_Unarmed1H Auto
Weapon Property PAF_Unarmed2H Auto
Sound property PAF_OriginalDrinkPotionSound Auto
Sound property PAF_CustomDrinkPotionSound Auto
FormList Property PAF_DrinkPotionList Auto

Form EquippedItemRight
Bool CameraSwitched = false
Static RightWeaponModel
Bool RightHandHasWeapon = false
Bool Unequipped = false
Bool Prevention = false


Event OnEffectStart(Actor akTarget, Actor akCaster)
      
      PAF_PotionAnimated.SetValue(1)
      PAF_AllowPotionUse.SetValue(0)

EndEvent

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
	GotoState("Busy")

	if akBaseItem == none || akItemReference || akDestContainer
		GotoState("")
		return
	elseif !akBaseItem.HasKeyword(VendorItemPotion) || akBaseItem.HasKeyword(VendorItemPoison) || akBaseItem.HasKeyword(VendorItemWeapon)
		GotoState("")
		return
	elseif PlayerRef.IsOnMount() || PlayerREF.GetAnimationVariableBool("bInJumpState") == true || PlayerREF.IsSwimming() == true || PlayerREF.getAnimationVariableBool("isStaggering") == true || PlayerREF.IsInKillMove() || PlayerREF.GetActorValue("Health") <= 0
		GotoState("")
		return
	elseif !Game.IsLookingControlsEnabled()
		GotoState("")
		return
	endif

	DrinkPotion(akBaseItem as Potion)
EndEvent

Function DrinkPotion(Potion DrinkPotion)

    Game.DisablePlayerControls(abMovement = false, abFighting = false, abCamSwitch = false, abMenu = True)
    CameraSwitched = false
    PAF_AllowJump.SetValue(0)
    If PlayerRef.IsWeaponDrawn() 
	   PAF_ReduceSpeedSpell.Cast(PlayerRef,PlayerRef)  
	    If PlayerRef.IsSprinting()
	       PlayerRef.SetDontMove(1)
        EndIf
    EndIf    
	If PlayerRef.GetActorValue("Health") > 0 && !PlayerRef.IsInKillMove() && PAF_CameraSwitch.GetValue() == 1 && PlayerRef.GetAnimationVariableInt("i1stPerson") == 1
	   Game.ForceThirdPerson()
	   CameraSwitched = true
	EndIf
	If PlayerRef.IsWeaponDrawn() 
	   Prevention = false
	   Unequipped = false
       RightHandHasWeapon = false
	   EquippedItemRight = PlayerRef.GetEquippedObject(1)
	   If (EquippedItemRight as Weapon) != none
	   	   RightHandHasWeapon = true
	   EndIf	
	   If PAF_ModelBugFix.GetValue() == 1 
	   	    If PlayerREF.GetEquippedShield() != none
	   	       PlayerREF.EquipItem(PAF_Unarmed2H,false,true)
               Unequipped = true
               Utility.Wait(0.1)
	   	    ElseIf PlayerREF.GetEquippedShield() == none && RightHandHasWeapon == true
	   	    	If (EquippedItemRight as Weapon).GetWeaponType() == 8
	   	    	    PlayerREF.EquipItem(PAF_Unarmed1H,false,true) 
	   	    	    Unequipped = true
	   	    	    Utility.Wait(0.1)
	   	    	EndIf    
	   	    EndIf	    
       EndIf  
	   If RightHandHasWeapon == true && Unequipped == false
	   	  RightWeaponModel = (EquippedItemRight as weapon).GetEquippedModel()
         (EquippedItemRight as Weapon).SetEquippedModel(none)
          Utility.Wait(0.1)
          PlayerRef.QueueNiNodeUpdate()
       EndIf
       RegisterForAnimationEvent(PlayerRef, "StaggerStop")
	   Debug.SendanimationEvent(PlayerRef, "DrinkPotionRun")
	   Game.DisablePlayerControls(abMovement = false, abFighting = True, abCamSwitch = True, abMenu = True)
	   PlayerRef.SetDontMove(0)
	   Utility.Wait(0.5)
	   If Prevention == true || PlayerRef.GetAnimationVariableBool("isStaggering") == true
		  Return 
	   EndIf
	   If PAF_DrinkPotionList.HasForm(DrinkPotion)
	      PAF_AllowPotionUse.SetValue(1)
	      PlayerREF.AddItem(DrinkPotion, 1, true)
          PlayerREF.EquipItem(DrinkPotion,false,true)
          PAF_AllowPotionUse.SetValue(0)
           If PAF_DrinkSFX.GetValue() == 0
              PAF_OriginalDrinkPotionSound.Play(PlayerRef)
           Else 
       	      PAF_CustomDrinkPotionSound.Play(PlayerRef)
           EndIf
       EndIf      
	   Utility.Wait(0.7)
       Debug.SendanimationEvent(PlayerRef, "offsetstop")
	   If Prevention == true || PlayerRef.GetAnimationVariableBool("isStaggering") == true
		  Return 
	   EndIf
	   UnregisterForAnimationEvent(PlayerRef, "StaggerStop")
	   PlayerRef.DispelSpell(PAF_ReduceSpeedSpell)
	   ResetCombatStance(EquippedItemRight)
	   PAF_AllowJump.SetValue(1)
	   If RightHandHasWeapon == true && Unequipped == false
	     (EquippedItemRight as Weapon).SetEquippedModel(RightWeaponModel)
	      Utility.Wait(0.1)
	      PlayerRef.QueueNiNodeUpdate()
	   ElseIf Unequipped == true
	   	    If EquippedItemRight.GetType() == 22 
	           PlayerREF.EquipSpell((EquippedItemRight as Spell), 1)
	        Else
	           PlayerREF.EquipItemEx(EquippedItemRight,1,false,false)
	        Endif	
	    Utility.Wait(0.1)
	    ResetCombatStance(EquippedItemRight)
	   EndIf   

    Else

       Debug.SendanimationEvent(PlayerRef, "DrinkPotionRun")
       Game.DisablePlayerControls(abMovement = false, abFighting = True, abCamSwitch = True, abMenu = True)
       Utility.Wait(0.5)
       If PAF_DrinkPotionList.HasForm(DrinkPotion)
          PAF_AllowPotionUse.SetValue(1)
          PlayerREF.AddItem(DrinkPotion, 1, true)
          PlayerREF.EquipItem(DrinkPotion,false,true)
          PAF_AllowPotionUse.SetValue(0)
           If PAF_DrinkSFX.GetValue() == 0
              PAF_OriginalDrinkPotionSound.Play(PlayerRef)
           Else 
       	      PAF_CustomDrinkPotionSound.Play(PlayerRef)
           EndIf
       EndIf    
       Utility.Wait(0.7)
	   Debug.SendanimationEvent(PlayerRef, "offsetstop")
	   PAF_AllowJump.SetValue(1)
	Endif   
    Utility.Wait(0.5)
    Game.EnablePlayerControls()
	If PlayerRef.GetActorValue("Health") > 0 && !PlayerRef.IsInKillMove() && CameraSwitched == true
	   Game.ForceFirstPerson()
	Endif
	CameraSwitched = false
	RightHandHasWeapon = false

GoToState("")
EndFunction	

Function Prevention()

	PlayerRef.DispelSpell(PAF_ReduceSpeedSpell)
	If RightHandHasWeapon == true && Unequipped == false
	(EquippedItemRight as weapon).SetEquippedModel(RightWeaponModel)
	utility.Wait(0.1)
	PlayerRef.QueueNiNodeUpdate()
    ElseIf Unequipped == true
	   	    If EquippedItemRight.GetType() == 22 
	           PlayerREF.EquipSpell((EquippedItemRight as Spell), 1)
	        Else
	           PlayerREF.EquipItemEx(EquippedItemRight,1,false,false)
	        Endif
	Utility.Wait(0.1)
	ResetCombatStance(EquippedItemRight)        	
	EndIf
	Game.EnablePlayerControls()
	if PlayerRef.GetActorValue("Health") >= 0 && CameraSwitched == true
	   Game.ForceFirstPerson()
	endIf
	CameraSwitched = false
    
    GotoState("")
EndFunction
    
Function ResetCombatStance(Form RightHand)

    If RightHand == none 
       Debug.SendAnimationEvent(PlayerRef, "WeapEquip")
    Else    
	    If RightHand.GetType() == 22 || (RightHand as Weapon).GetWeaponType() == 8
		   Debug.SendAnimationEvent(PlayerRef, "Magic_Equip")    
		Else
		   Debug.SendAnimationEvent(PlayerRef, "WeapEquip") 
		Endif 
	EndIf	 

EndFunction

Event OnAnimationEvent(ObjectReference akSource, String asEventName)

	If asEventName == "StaggerStop"
	   Prevention = true
	   ResetCombatStance(EquippedItemRight)
       Prevention()
       PAF_AllowJump.SetValue(1)
       UnregisterForAnimationEvent(PlayerRef, "StaggerStop")
	EndIf
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
    
    If RightHandHasWeapon == true	
	    If ((PlayerRef.GetEquippedObject(1)) as Weapon).GetEquippedModel() == none
           ((PlayerRef.GetEquippedObject(1)) as Weapon).SetEquippedModel(RightWeaponModel)
       EndIf   
    EndIf   
    PAF_PotionAnimated.SetValue(0)
    PAF_AllowPotionUse.SetValue(1)
    PAF_AllowJump.SetValue(1)
    PlayerRef.SetDontMove(0)
	Game.EnablePlayerControls()

EndEvent



State Busy 


	Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
 
    EndEvent


EndState

