Scriptname _00E_MQ07bAftermath_Function extends Quest  

Import _00E_QuestFunctions

;=====================================================================================
;              							EVENTS                  					 
;=====================================================================================

Float Function _CalcNextUpdate(Float newUpdateDay, Float oldUpdateHours)
	Float newUpdateHours = (newUpdateDay - GameDaysPassed.GetValue()) * 24.0 + 0.025
	If newUpdateHours < 0.25
		newUpdateHours = 0.25
	EndIf
	If oldUpdateHours <= 0.0 || newUpdateHours < oldUpdateHours
		Return newUpdateHours
	Else
		Return oldUpdateHours
	EndIf
EndFunction

Function _RegisterForNextUpdate()
	Float fNextUpdateTime = 0.0

	If bBeaconErrected && bLishariMoved == False
		fNextUpdateTime = 0.25
	EndIf

	If bBeaconErrected == False
		fNextUpdateTime = _CalcNextUpdate(fBeaconEnableDay, fNextUpdateTime)
	EndIf

	Int curStage = GetStage()
	If curStage < 10
		fNextUpdateTime = _CalcNextUpdate(fConsequencesStartDay, fNextUpdateTime)
	ElseIf curStage >= 20 && fConsequencesEndDay > 0.0
		fNextUpdateTime = _CalcNextUpdate(fConsequencesEndDay, fNextUpdateTime) 
	EndIf

	If fNextUpdateTime > 0.0
		RegisterForSingleUpdateGameTime(fNextUpdateTime)
	EndIf
EndFunction

Event OnUpdateGameTime()

	; Version update
	If bDoneTimeSetup == False
		bDoneTimeSetup = True
		fBeaconEnableDay = GameDaysPassed.GetValue()
		If GetStage() < 10 && bBeaconErrected == False
			fConsequencesStartDay = fBeaconEnableDay + 1.0
		Else
			fConsequencesStartDay = fBeaconEnableDay
		EndIf
		If GetStage() >= 20 && _00E_MQ07b_HallysFree.GetValueInt() == 0 
			fConsequencesEndDay = fBeaconEnableDay
		EndIf
	EndIf

	; Enable construction version of the beacon
	If bBeaconErrected == False && GameDaysPassed.GetValue() >= fBeaconEnableDay
		If MQ10b.GetStage() >= 5
			bBeaconErrected = True ; Too late
		ElseIf PlayerREF.GetWorldSpace() != CapitalCityUpperTemple
			bBeaconErrected = True
			MQ07b_Suntemple_Beacon_InConstruction.Enable()
		EndIf 
	EndIf

	; Teleport Lishari to the beacon
	If bBeaconErrected && bLishariMoved == False
		If MQ08Prologue.GetStage() >= 10
			bLishariMoved = True ; No point to move her anymore, MQ08Prologue has done this already
		ElseIf _00E_MC_LishariREF.Is3DLoaded() == False && PlayerREF.GetWorldSpace() != CapitalCityUpperTemple
			bLishariMoved = True
			_00E_MC_LishariREF.MoveTo(LishariSuntempleMarker)
			_00E_MC_LishariREF.EvaluatePackage()
		EndIf
	EndIf

	; Consequences
	Int curStage = GetStage()
	If curStage < 15 && GameDaysPassed.GetValue() >= fConsequencesStartDay
		Bool bHallysIsReady
		If _00E_MQ07b_HallysFree.GetValueInt() == 0
			bHallysIsReady = MQ07b_Aftermath_HallysCorpseREF.IsEnabled()
			If bHallysIsReady == False && PlayerREF.GetWorldSpace() != CapitalCityLowerCity
				MQ07b_Aftermath_HallysCorpseREF.Enable()
				bHallysIsReady = True
			EndIf
		Else
			bHallysIsReady = True
		EndIf

		If bHallysIsReady
			
			If NQ_G_07Dijaam_SC03_EscapeThroughTomb == None
				NQ_G_07Dijaam_SC03_EscapeThroughTomb = Game.GetFormFromFile(0x00147063, "Skyrim.esm") as Scene
			EndIf
			
			If (PlayerREF.GetCurrentLocation() == CapitalCityLocation && NQ_G_07Dijaam_SC03_EscapeThroughTomb.IsPlaying() == false)
				SetStage(15)
			Else
				SetStage(10)
			EndIf
		EndIf
	ElseIf curStage >= 20 && fConsequencesEndDay > 0.0 && GameDaysPassed.GetValue() >= fConsequencesEndDay
		DeleteDeadHallys()
	EndIf

	_RegisterForNextUpdate()
EndEvent

;=====================================================================================
;              							FUNCTIONS                  					 
;=====================================================================================

Function SetUp()

	bDoneTimeSetup        = True
	fBeaconEnableDay      = GameDaysPassed.GetValue() + 0.5
	fConsequencesStartDay = fBeaconEnableDay + 1.0
	fConsequencesEndDay   = 0.0

	_RegisterForNextUpdate()

EndFunction

Function SetUpConsequences()

	ObjectReference objSpawnMarker

	If _00E_MQ07b_HallysFree.GetValueInt() == 0
		bDoneTimeSetup = True ; Version update failsave
		fConsequencesEndDay = GameDaysPassed.GetValue() + 7.0
		MQ07b_Aftermath_HallysCorpseREF.Enable()
		SetStage(20)
		_RegisterForNextUpdate()
	Else
		objSpawnMarker = PlayerREF.PlaceAtMe(XMarkerHeading, 1)
		objSpawnMarker.MoveTo(PlayerRef, -250.0 * Math.Sin(PlayerRef.GetAngleZ()), -250.0 * Math.Cos(PlayerRef.GetAngleZ()), PlayerRef.GetHeight())
		akHallys = objSpawnMarker.PlaceActorAtMe(_00E_MQ07b_Hallys, 1)
		Hallys.ForceRefTo(akHallys)
		
		MQ07bAftermath_SC01_HallysThanksPlayer.ForceStart()

		objSpawnMarker.Delete()
		
	EndIf
	
EndFunction

Function DeleteHallys()

	akHallys.Delete()
	Cleanup()

EndFunction

Function DeleteDeadHallys()

	MQ07b_Aftermath_HallysCorpseREF.Disable()
	MQ07b_HallysWifeREF.Disable()
	fConsequencesEndDay = 0.0
	Cleanup()

EndFunction

Function Cleanup()
    Player.Clear()

    ; Release all references
	akHallys = None
	MQ07b_Aftermath_HallysCorpseREF = None
	MQ07b_HallysWifeREF = None
	MQ07b_Suntemple_Beacon_InConstruction = None
	_00E_MC_LishariREF = None
	LishariSuntempleMarker = None
EndFunction

Function GiveReward()

	PlayerREF.AddItem(_00E_Lehrbuch_Plus2Skillpoints, 1)

EndFunction


;=====================================================================================
;              							PROPERTIES                  					 
;=====================================================================================

bool bBeaconErrected
Bool bLishariMoved
Bool bDoneTimeSetup
Float fBeaconEnableDay
Float fConsequencesStartDay
Float fConsequencesEndDay

Actor akHallys

Actor Property PlayerREF Auto

ActorBase Property _00E_MQ07b_Hallys Auto

Potion Property _00E_Lehrbuch_Plus2Skillpoints Auto

Location Property CapitalCityLocation Auto

GlobalVariable Property _00E_MQ07b_HallysFree Auto
GlobalVariable Property GameDaysPassed Auto

ObjectReference Property MQ07b_Aftermath_HallysCorpseREF Auto
ObjectReference Property MQ07b_HallysWifeREF Auto
ObjectReference Property MQ07b_Suntemple_Beacon_InConstruction Auto
Actor Property _00E_MC_LishariREF Auto
ObjectReference Property LishariSuntempleMarker Auto

ReferenceAlias Property Hallys Auto
ReferenceAlias Property Player Auto

Static Property XMarkerHeading Auto

Scene Property MQ07bAftermath_SC01_HallysThanksPlayer Auto
Scene Property NQ_G_07Dijaam_SC03_EscapeThroughTomb Auto

Quest Property MQ10b Auto
Quest Property MQ08Prologue Auto

Worldspace Property CapitalCityUpperTemple Auto
Worldspace Property CapitalCityLowerCity Auto