Scriptname _00E_PlayerFunctions extends Actor  

_00E_EnderalControls Property ControlRepository  Auto  

Float fDefaultHeadTrackTimerOldValue = 0.0
Float fStayHeadTrackTimerOldValue = 0.0

int function _GetScriptVersion() Global
	return 1
endFunction

Event OnInit()
	fDefaultHeadTrackTimerOldValue = Game.GetGameSettingFloat("fAIHoldDefaultHeadTrackTimer")
	fStayHeadTrackTimerOldValue = Game.GetGameSettingFloat("fAIStayonScriptHeadtrack")
	if ControlRepository == None
		ControlRepository = Game.GetForm(0x493C2) as _00E_EnderalControls
	endif
EndEvent

Event OnPlayerLoadGame()
	if ControlRepository == None
		ControlRepository = Game.GetForm(0x493C2) as _00E_EnderalControls
	endif
EndEvent

_00E_SkillControl function GetSkillControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.SkillControl
endfunction

_00E_SympathyControl function GetSympathyControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.SympathyControl
endfunction

_00E_SoundControl function GetSoundControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.SoundControl
endfunction

_00E_VisionControl function GetVisionControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.VisionControl
endfunction

_00E_CombatMusicControl function GetCombatMusicControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.CombatMusicControl
endfunction

_00E_FadeToBlackControl function GetFadeToBlackControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.FadeToBlackControl
endfunction

_00E_TeleportControl function GetTeleportControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.TeleportControl
endfunction

_00E_SafeItemRemove function GetSafeItemRemove() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.SafeItemRemove
endfunction

_00E_BrawlControl function GetBrawlControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.BrawlControl
endfunction

_00E_MiningControl function GetMiningControl() Global
	return (Game.GetForm(0x14) as _00E_PlayerFunctions).ControlRepository.MiningControl
endfunction


;=====================================================================================
; Stop/restart random headtracking
;=====================================================================================

Function StopRandomHeadTracking()
    ; Raise "change headtrack target" timers a lot so the NPCs would stare at what the scene tells them, without randomly switching to the player or other NPCs
	Game.SetGameSettingFloat("fAIHoldDefaultHeadTrackTimer", 1000.0)
	Game.SetGameSettingFloat("fAIStayonScriptHeadtrack", 1000.0)
EndFunction

Function ResumeRandomHeadTracking()
	Game.SetGameSettingFloat("fAIHoldDefaultHeadTrackTimer", fDefaultHeadTrackTimerOldValue)
	Game.SetGameSettingFloat("fAIStayonScriptHeadtrack", fStayHeadTrackTimerOldValue)
EndFunction


;=====================================================================================
; Equip/unequip torches
;=====================================================================================

Int Function _GetHandSlotEx(int iHand)
	; 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)
	Bool unequipping = False
	Int iHand = 0
	While iHand < equippedTorches.Length
		If GetEquippedItemType(iHand) == 11 ; A torch is in the hand?
			Form torch = GetEquippedObject(iHand)
			If torch
				UnequipItemEx(torch, _GetHandSlotEx(iHand))
				equippedTorches[iHand] = torch
				unequipping = True
			EndIf
		EndIf

		iHand += 1
	EndWhile

	Return unequipping
EndFunction

Function ReequipTorches(Form[] equippedTorches)
	Int iHand = 0
	While iHand < equippedTorches.Length
		Form torch = equippedTorches[iHand]
		If torch
			If GetItemCount(torch) > 0
				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