Scriptname _00E_MQ11c_PlayerAliasScript extends ReferenceAlias

Int iScriptVersion = 0
Int Property LATEST_SCRIPT_VERSION = 1 AutoReadOnly

Event OnPlayerLoadGame()
	If iScriptVersion < LATEST_SCRIPT_VERSION
		iScriptVersion = LATEST_SCRIPT_VERSION

		UpdateWeatherTrackingState()

		Int curStage = GetOwningQuest().GetStage()

		If curStage >= 260
			_00E_MQ11c_SilvergroveMainMusic.SetValue(0)
		EndIf

		If curStage >= 260 && _00E_MQ12a_MQ11cCleared.GetValueInt() == 0
			Location curLoc = GetActorReference().GetCurrentLocation()
			If LocationIsInnerSilberhain(curLoc)
				SetDeadWeather(False)
			ElseIf curLoc != _00E_MQ11c_SilberhainExteriorBorder
				ReleaseDeadWeather(False)
			EndIf
		Else
			ReleaseDeadWeather(False)
		EndIf
	EndIf
EndEvent

Function UpdateWeatherTrackingState()
	If _00E_MQ12a_MQ11cCleared.GetValueInt() == 0 && GetOwningQuest().GetStage() >= 260
		GoToState("TrackingWeather")
	Else
		GoToState("")
	EndIf
EndFunction

State TrackingWeather

	Event OnLocationChange(Location akOldLoc, Location akNewLoc)
		; The black stone has been given to Lexyl? Time to stop changing the weather in Silberhain
		If _00E_MQ12a_MQ11cCleared.GetValueInt() != 0
			GoToState("")
			ReleaseDeadWeather(False)
			Return
		EndIf

		If LocationIsInnerSilberhain(akNewLoc) ; In inner Silberhain
			If LocationIsInnerSilberhain(akOldLoc)
				; Do nothing
			ElseIf akOldLoc == _00E_MQ11c_SilberhainExteriorBorder
				SetDeadWeather(True)
			Else ; Fast travel?
				SetDeadWeather(False)
			EndIf
		ElseIf akNewLoc != _00E_MQ11c_SilberhainExteriorBorder ; Not in inner Silberhain and not in transition zone
			If LocationIsInnerSilberhain(akOldLoc)
				ReleaseDeadWeather(False)
			ElseIf akOldLoc == _00E_MQ11c_SilberhainExteriorBorder
				Actor playerRef = GetActorReference()
				If playerRef.GetWorldSpace() == Vyn && playerRef.GetDistance(SilberhainMapMarker) < 25000.0
					ReleaseDeadWeather(True)
				Else
					ReleaseDeadWeather(False)
				EndIf
			EndIf
		EndIf
	EndEvent

EndState

Bool Function LocationIsInnerSilberhain(Location lc)
	Return lc == SilberhaimLoc || lc == _00E_MQ11c_SilberhainExterior || lc == _00E_MQ11c_SilberhainInterior
EndFunction

Function SetDeadWeather(Bool bGradualTransition)
	(GetOwningQuest() as _00E_MQ11c_Functions).SetSilberhainDeadWeatherFX(bGradualTransition)
EndFunction

Function ReleaseDeadWeather(Bool bGradualTransition)
	(GetOwningQuest() as _00E_MQ11c_Functions).ResetSilberhainDeadWeatherFX(bGradualTransition)
EndFunction


Location Property SilberhaimLoc Auto
Location Property _00E_MQ11c_SilberhainExterior Auto
Location Property _00E_MQ11c_SilberhainInterior Auto
Location Property _00E_MQ11c_SilberhainExteriorBorder Auto

WorldSpace Property Vyn Auto

ObjectReference Property SilberhainMapMarker Auto

GlobalVariable Property _00E_MQ12a_MQ11cCleared Auto
GlobalVariable Property _00E_MQ11c_SilberhainDeadWeather Auto
GlobalVariable Property _00E_MQ11c_SilvergroveMainMusic Auto