Scriptname _00E_TorchControl Hidden

Int Function _GetHandSlotEx(int iHand) global
	; By default, iHand = 0 for left hand, 1 for right hand
	; In SKSE's "Equip Ex" functions though: iHand = 2 for left hand, 1 for right hand
	Return 2 - iHand
EndFunction

Bool Function UnequipTorches(Form[] equippedTorches) global
	Actor PlayerREF = Game.GetForm(0x14) as Actor
	
	if ! SKSE.GetVersion()
		if PlayerREF.GetEquippedItemType(0) == 11 || PlayerREF.GetEquippedItemType(1) == 11
			Form torchDefault = Game.GetForm(0x1D4EC)
			equippedTorches[0] = torchDefault
			PlayerREF.UnequipItem(torchDefault as Light, false, true)
			Utility.Wait(0.1)
			return true
		endif
		return false
	endif
	
	Bool unequipping = False
	Int iHand = 0
	
	While iHand < equippedTorches.Length
		If PlayerREF.GetEquippedItemType(iHand) == 11 ; A torch is in the hand?
			Form torch = PlayerREF.GetEquippedObject(iHand)
			If torch
				PlayerREF.UnequipItemEx(torch, _GetHandSlotEx(iHand))
				equippedTorches[iHand] = torch
				unequipping = True
			EndIf
		EndIf

		iHand += 1
	EndWhile

	Return unequipping
EndFunction

Function ReequipTorches(Form[] equippedTorches) global
	Actor PlayerREF = Game.GetForm(0x14) as Actor
	
	if ! SKSE.GetVersion()
		if equippedTorches[0] || equippedTorches[1]
			equippedTorches[0] = None
			equippedTorches[1] = None
			if PlayerREF.GetItemCount(Game.GetForm(0x1D4EC)) > 0
				Debug.SendAnimationEvent(PlayerREF, "equipTorch")
			endif
		endif
		return
	endif
	
	Int iHand = 0
	While iHand < equippedTorches.Length
		Form torch = equippedTorches[iHand]
		If torch
			If PlayerREF.GetItemCount(torch) > 0
				PlayerREF.EquipItemEx(torch, _GetHandSlotEx(iHand), False, True)
			EndIf
			; "Forget" about the form to not keep it persistent if equippedTorches is a script's property or variable
			equippedTorches[iHand] = None 
		EndIf
		iHand += 1
	EndWhile
EndFunction