Scriptname _00E_AutoSaveSystem_Functions extends Quest

; Save Name Structure (from NQS NamedQuicksaves by Ryan McKenzie)

; Save3_0C2D58E2_0_507269736F6E6572_Tamriel_000002_20180503063315_4_1.ess
;   Save3: Type and index of save
;   0C2D58E2: Unique hash used to identify your save profile. Regenerated on closing racemenu.
;   0: Flag for modded game.
;   507269736F6E6572: Character name in hex.
;   Tamriel: coc code.
;   000002: Days, hours, minutes played.
;   20180503063315: Year, month, day, hour, minute, second in GMT + 0.
;   4: Player level.
;   1: Unknown flag.

Event OnUpdate()
	
	Actor PlayerREF = Game.GetForm(0x14) as Actor
	
	If PlayerREF.IsInCombat() || PlayerREF.IsOnMount() || PlayerREF.IsSwimming() || ! Game.IsFightingControlsEnabled() || PlayerREF.IsDead() || Utility.IsInMenuMode() || UI.IsTextInputEnabled() || UI.IsMenuOpen("Dialogue Menu")
		RegisterForSingleUpdate(5)
		return
	endif
	
	; Eddoursul: Index prefixed with 0 ensures the engine does not rotate these saves out
	Game.SaveGame("Autosave0" + iAutosaveIndex + "_" + EnderalFunctions.GetPlayerHash() + "_0_" + EnderalFunctions.StringToHex(PlayerREF.GetActorBase().GetName()) + "_EnderalSE_000000_00000000000000_1_1")
	
	iAutosaveIndex += 1
	if iAutosaveIndex >= Utility.GetIniInt("iAutoSaveCount:SaveGame")
		iAutosaveIndex = 0
	endif
	
	RegisterForSingleUpdate(fUpdateTime*60)
	
EndEvent

; called from _00E_AutosaveIntervalAlias to update ini setting fAutosaveEveryXMins in case it was changed
Function UpdateAutoSaveInterval()

	if SKSE.GetVersion() <= 0
		UnregisterForUpdate()
		return
	endif

	fUpdateTime = Utility.GetIniFloat("fAutosaveEveryXMins:SaveGame")
	
	If fUpdateTime <= 0.0 ; just in case
		fUpdateTime = 30
	EndIf
	
	RegisterForSingleUpdate(fUpdateTime*60)

EndFunction

int iAutosaveIndex = 0
float fUpdateTime