Scriptname _00E_AutoSaveSystem_Functions extends Quest  

Event OnInit()
	
	fUpdateTime = Utility.GetIniFloat("fAutosaveEveryXMins:SaveGame")
	RegisterForSingleUpdate(fUpdateTime*60)
	
EndEvent

Event OnUpdate()

	If bAutosaveSystemStopped == false 
		If Utility.IsInMenuMode() == false && UI.IsTextInputEnabled() == false && UI.IsMenuOpen("Dialogue Menu") == false && PlayerREF.IsDead() == false && PlayerREF.IsInCombat() == false && Game.IsFightingControlsEnabled() == true
			Game.RequestAutoSave()
			RegisterForSingleUpdate(fUpdateTime*60)
		Else
			RegisterForSingleUpdate(5)
		EndIf
	EndIf

EndEvent

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

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

EndFunction

Function StopAutosaveSystem()
	bAutosaveSystemStopped = true
	UnregisterForUpdate()
EndFunction

Function ResumeAutosaveSystemInXMinutes(float _fMinutes = 0.0)

	If _fMinutes == 0.0
		_fMinutes = fUpdateTime
		If _fMinutes == 0.0 ; just in case
			_fMinutes = 60
		EndIf
	EndIf
	
	UnregisterForUpdate() ; just in case again
	RegisterForSingleUpdate(_fMinutes*60)
	bAutosaveSystemStopped = false

EndFunction

; not used yet btw / maybe will never be used
; called shortly before the end of every quest, parameter is always the quest name
Function RequestHardSave(string _sQuestName)

	Utility.Wait(0.1) ; in case we are in a menu (debug message boxes / dialogue etc)
	Game.SaveGame(_sQuestName)

EndFunction

bool bAutosaveSystemStopped = false
float fUpdateTime
Actor Property PlayerREF Auto