Scriptname _00E_MQ11c_Functions extends Quest  

import Utility
Import _00E_QuestFunctions
Import Math


;=====================================================================================
;              							 STATES                  					 
;=====================================================================================

State DefaultState 

;Do Nothing

EndState


State DogMinigame

Event OnBeginState()

	Game.RequestAutoSave()
	MQ11c_IAmInTheWayDogREF.Disable()
	_00E_MQ11c_DogMinigameMessagebox.Show()
	RyneusDogA.SetActorValue("Speedmult", (PlayerREF.GetActorValue("SpeedMult") + 50.0))
	RegisterForSingleUpdate(1.5)
	
EndEvent

Event OnUpdate()

	if GetStage() >= 110 && GetStage() < 130

		if (PlayerREF.GetDistance(RyneusDogA) >= DogMinigameWarningDistance) && (PlayerREF.GetDistance(RyneusDogA) < DogMinigameLosingDistance)
			if (Utility.RandomFloat(0, 1) <= 0.5)
				_00E_MQ11c_sThorusWarning.Show()
			EndIf
			RegisterForSingleUpdate(1)	
		ElseIf (PlayerREF.GetDistance(RyneusDogA) >= DogMinigameLosingDistance)
			RyneusDogA.SetActorValue("Speedmult", (PlayerREF.GetActorValue("SpeedMult") + 150.0))
			RegisterForSingleLOSLost(PlayerREF, RyneusDogA)
			;Game.QuitToMainMenu()
		Else	
			RegisterForSingleUpdate(1.5)
		EndIf
		
	EndIf

EndEvent

Event OnLostLOS(Actor akViewer, ObjectReference akTarget)
	
	if akViewer == PlayerREF && akTarget == RyneusDogA
		FailedDogMinigame()
	endif

EndEvent
EndState


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

;------------------------------------------Miscellaneous------------------------------------

Function UnlockKetaronHouseDoor()
	SC1_KetaronHouseDoor.Lock(False)
EndFunction

Function StartEscortScene()
	MQ11c_SC1_KeshanEscortsPlayer.ForceStart()
EndFunction

Function MovePlayerInFrontOfHouse()
	PlayerREF.MoveTo(SC1_PlayerMoveToMarker)
	SC1_KetaronHouseDoor.Lock(True)
	TimescaleDefault = Timescale.GetValue()
EndFunction


Function RemoveLetter()
	PlayerREF.RemoveItem(_00E_MQ11c_RyneusDiscardLetter)
	Wait(3)
	PlayerREF.MoveTo(MQ11c_D0_PlayerSilberhainEntrymarker)
EndFunction

Function AddSilence()

	_00E_SilenceTransitionLowPriority.Add()

EndFunction

Function RemoveSilences()

	Levelsystem.RemoveSilence()

EndFunction

Function PrepareForLockingGate()
	Game.DisablePlayerControls()

	MQ11c_LeverREF.BlockActivation(False)
	MQ11c_SilvergroveGateREF.BlockActivation(False)

	; Move the player to the warden if the player is too far away.
	; Move the warden into his proper position if he gets stuck on the way.
	; If we're going to move the player, do this with fade-in/fade-out.

	Bool bPlayerTooFar = False
	If PlayerREF.GetDistance(MQ11c_D4_PlayerFailsafeMarker) > 400.0 || MQ11c_PlayerInSilvergroveRadius.GetValue() == 0
		bPlayerTooFar = True
	EndIf

	If bPlayerTooFar
		FadeToBlackImod.Apply()
		Wait(1)
	EndIf

	If MQ11c_SilvergroveGateWardenREF.GetDistance(MQ11c_D4_GuardWaitMarker) >= 128.0
		MQ11c_SilvergroveGateWardenREF.Disable()
		MQ11c_SilvergroveGateWardenREF.MoveTo(MQ11c_D4_GuardWaitMarker)
		MQ11c_SilvergroveGateWardenREF.Enable()
	EndIf

	If bPlayerTooFar
		FadeToBlackHoldImod.Apply()
		FadeToBlackImod.Remove()
		PlayerREF.MoveTo(MQ11c_D4_PlayerFailsafeMarker)
		Wait(1)
		FadeToBlackBackImod.Apply()
		FadeToBlackHoldImod.Remove()
	EndIf

EndFunction

Function LockGateFailsave()
	MQ11c_LeverREF.BlockActivation(True)
	MQ11c_SilvergroveGateREF.SetOpen(False)
	MQ11c_SilvergroveGateREF.Lock(True)
	MQ11c_SilvergroveGateREF.BlockActivation(True)
EndFunction

Function StartSC03()

	SympathyRyneus.SetValueInt(0)
	MQ11c_SilvergroveGateREF.SetOpen(True)
	MQ11c_SilvergroveGateREF.BlockActivation(True)

	MQ11c_SC3_WardenClosesGate.ForceStart()
	
EndFunction

Function RemoveTeddy()

	PlayerREF.RemoveItem(_00E_MQ11c_RyneusTeddy, 1)

EndFunction


;----------------------------------Sphere minigame----------------------------------------------------

Function MoveRyneusAndFatherToStart()
    Ryneus.GetReference().MoveTo(SC5_RyneusMoveMarker)
    Alias_Ulbar.GetReference().MoveTo(SC5_FatherMoveMarker)
EndFunction

Function RyneusInitiateMinigame()

	MQ11c_SC6_RyneusStartSphere.ForceStart()
	
EndFunction

Function ActivateSphere()
	SC6_Sphere.Activate(SC6_SphereButton)
EndFunction

Function GiveEPForHits()

	int RewardEPNormalHits = (_00E_MQ11c_Sphere_HitsLanded.GetValueInt()*30)
	int RewardEPEOTSHits = (_00E_MQ11c_Sphere_HitsLandedEOTS.GetValueInt()*10)
	Levelsystem.GiveEP(RewardEPNormalHits+RewardEPEOTSHits)

	
EndFunction

;------------------------------------------Stealth minigame------------------------------------

Function StartStealthMinigame()
	_00E_QuestshaderNew.Play(MQ11c_ScrollChest)
	_00E_MQ11c_StealthMinigameMessagebox.Show()
	_00E_MQ11c_StealthGame_Running.SetValue(1)
	GuardsHouseDoor.SetLockLevel(255)
	GuardsHouseDoor.Lock(True)
	Guard01 = DalGeyssGuard01.GetActorReference()
	Guard02 = DalGeyssGuard02.GetActorReference()
	Guard03 = DalGeyssGuard03.GetActorReference()
	Guard01.SetNotShowOnStealthMeter(True)
	Guard02.SetNotShowOnStealthMeter(True)
	Guard03.SetNotShowOnStealthMeter(True)
	RegisterForSingleUpdate(7)
	if !PlayerREF.IsSneaking()
		PlayerREF.StartSneaking()
	endif
	Game.RequestAutoSave()
	RegisterForLOS(Guard01, PlayerREF)
	RegisterForLOS(Guard02, PlayerREF)
	RegisterForLOS(Guard03, PlayerREF)
EndFunction

Function StopStealthMinigame()

	Guard01.UnregisterForLOS(Guard01, PlayerREF)
	Guard02.UnregisterForLOS(Guard02, PlayerREF)
	Guard03.UnregisterForLOS(Guard03, PlayerREF)
	Guard01.MoveTo(GuardsMoveToMarker)
	Guard02.MoveTo(GuardsMoveToMarker)
	Guard03.MoveTo(GuardsMoveToMarker)
	UnregisterForUpdate()
	Guard01.MoveTo(GuardsMoveToMarker)
	DalGeyssGuard01.GetActorReference().EvaluatePackage()
	DalGeyssGuard02.GetActorReference().EvaluatePackage()
	DalGeyssGuard03.GetActorReference().EvaluatePackage()
	_00E_MQ11c_StealthGame_Running.SetValue(0)
	
EndFunction

;------------------------------------------Dog minigame------------------------------------

Function StartDogChaseScene()
    RyneusDog.GetActorReference().SetOutfit(_00E_MQ11c_RyneusDogToyOutfit)
    NPCDogBark.Play(RyneusDog.GetReference())
    MQ11c_SC4_DogStealsPuppet.Start()
EndFunction

Function SetUpDogMinigame()
    RyneusDog.GetReference().MoveTo(SC4_DogChaseStartMarker)
    Ryneus.GetActorReference().EvaluatePackage()
EndFunction

Function StartDogMinigame()
    RyneusDogA = RyneusDog.GetActorReference()
    RyneusDogA.EvaluatePackage()
    GoToState("DogMinigame")
EndFunction

Function FailedDogMinigame()
	_00E_MQ11c_sThorusGone.Show()
	bDogMinigameFailed = true
	UnregisterForUpdate()
	UnregisterForLOS(PlayerREF, RyneusDogA)
	SetObjectiveFailed(40)
	SetObjectiveDisplayed(43)
	RyneusDogA.MoveTo(MQ11c_DogChaseMarker027)
	SetStage(120)
EndFunction

Function EndDogMinigame()
    _00E_NPCDogIdleWhineM.Play(RyneusDogA)
	MQ11c_IAmInTheWayDogREF.Enable()
    UnregisterForUpdate()
    if bDogMinigameFailed == false
		SetObjectiveCompleted(40)
	endif
	GoToState("DefaultState")
EndFunction

;---------------------------------------Butterfly minigame----------------------------------
Function SetUpMinigame()
	PlayerRef.AddSpell(_00E_MQ11c_ChaosohrSpell)
	PlayerRef.AddItem(_00E_MQ11cSchmetterlingsglas, 15)
	PlaceCritter(_00E_MQ11_CritterSpawnmarkerList)
	_00E_MQ11c_CrittersoundCategoryDontUse.Mute()	
	MQ11c.SetObjectiveDisplayed(45)
EndFunction

Function PlaceCritter(Formlist _00E_MQ11_CritterSpawnmarkerList)
	Int iIndex = _00E_MQ11_CritterSpawnmarkerList.GetSize() 
	
	While iIndex 
		iIndex -= 1
		ObjectReference kMarker = _00E_MQ11_CritterSpawnmarkerList.GetAt(IIndex) as ObjectReference
		kMarker.PlaceAtMe(_00E_MQ11_critterSpawnInsects_Single)
	endwhile
	
EndFunction

Function GiveEPForButterflies()

	int RewardEPButterfly = PlayerREF.GetItemCount(_00E_MQ11cSchmetterling)
	Levelsystem.GiveEP(RewardEPButterfly*50)
	
	if PlayerREF.GetItemCount(_00E_MQ11cSchmetterling) > 7
	
		int iSympathyCount = (RewardEPButterfly - 7)*2
		
		if iSympathyCount > 10
			iSympathyCount = 10
		endif
	
		Levelsystem.ModSympathyNG(Ryneus.GetActorReference(), iSympathyCount)
		
		If PlayerREF.GetItemCount(_00E_MQ11cSchmetterling) >= 15 && _00E_AchievementsEnabled.GetValueInt() == 1
			Game.UnlockAchievement("END_BUTTERFLIES_01")
		EndIf
		
	EndIf
	
	PlayerREF.RemoveItem(_00E_MQ11cSchmetterling, 15)
	PlayerREF.RemoveItem(_00E_MQ11cSchmetterlingsglas, 15)
	
	AudioCategoryMuteSubmerged.UnMute()
	AudioCategoryAMB.UnMute()
	AudioCategoryAMBr.UnMute()
	AudioCategoryAMB2x.UnMute()
	AudioCategoryPausedDuringMenuFade.UnMute()
	
EndFunction

Function FadeToBlackAndBack()

	PlayerREF.RemoveSpell(_00E_MQ11c_ChaosohrSpell)
	Game.DisablePlayerControls()
	FadeToBlackImod.Apply()
	Wait(2.8)
	FadeToBlackImod.PopTo(FadeToBlackHoldImod)
	Ryneus.GetReference().MoveTo(MQ11c_D10_RyneusSitLedgeMarker)
	Wait(3)
	FadeToBlackHoldImod.PopTo(FadeToBlackBackImod)
	Ryneus.GetActorReference().EvaluatePackage()
	SC7_RyneusButterflyHouseLinker.Disable()
	Game.EnablePlayerControls()

EndFunction

;--------------------------------------------OORBAYA FIGHT----------------------------------------------------

Function MoveRyneusToCaveEntrance()
    Ryneus.GetReference().MoveTo(MQ11c_D11_RyneusCaveEntrance)

    if (PlayerREF.GetActorBase().GetSex() == 0)
        MQ11c_SC10_PaintingMale.Enable()
    Else ;If (PlayerREF.GetActorBase().GetSex() == 1)
        MQ11c_SC10_PaintingFemale.Enable()
    EndIf
EndFunction

Function DebugGetIntoPosition()

    AudioCategoryMuteSubmerged.SetVolume(0.3)

EndFunction


Function PrepareTransformScene()

	Actor RyneusActor = Ryneus.GetActorReference()
	MQ11c_SC10_BarrierREF.MoveTo(MQ11c_SC10_BarrierMarker)
	MQ11c_SC10_BarrierREF.PlayAnimation("playanim02")
	MQ11c_SC10_BarrierColRef.Enable()
	_00E_MQ11c_SpecialDialogueSad.Remove()
	int WindLoop = AMBrWindMountainHeavyLP.Play(RyneusActor)
	_00E_MQ11c_SpecialDialogueSad.Remove()
	Sound.SetInstanceVolume(WindLoop, 1.5)
	_00E_FXCameraAttachJUngleLeavesWindyUpEffect.Play(PlayerREF)
	MQ11c_SC10_RyneusTransforms.ForceStart()
	
EndFunction

Function RyneusTransforms()

	Actor RyneusActor = Ryneus.GetActorReference()
	Debug.SendAnimationEvent(RyneusActor, "IdleReturnToDefault")
	Levelsystem.RemoveSilence()
	_00E_Music_Combat_Epic_Transition.Add()
	Debug.SendAnimationEvent(RyneusActor, "IdleDA02BoethiahPostEnter")
	Utility.Wait(1)
	RyneusActor.Say(MQ11c_D12_RyneusTransformTopic)
	EnchPurpleFXShader.Play(RyneusActor)
	Wait(2)
	_00E_MQ11c_RyneusTransformSoundM.Play(RyneusActor)
	FXCameraAttachTurbulentFogEffect.Play(PlayerREF)
	_00E_MQ11c_RyneusTransformsIMOD.Apply()
	_00E_MQ11c_FXRyneusParticleAttach.Play(RyneusActor)
	Wait(3)
	_00E_MQ11c_RyneusChestVFX.Play(RyneusActor)
	Utility.Wait(2)
	RyneusActor.Say(MQ11c_D12_RyneusTransformTopic02)
	Wait(2)

	if (_00E_MQ11c_RyneusPeacefulSolution.GetValueInt() == 0)
		AtronachUnsummonDeathFXS.Play(RyneusActor)
		Wait(3.6)
		Game.ShakeCamera(afStrength = 1)
		RyneusActor.SetAlpha(0)
		RyneusActor.PlaceAtMe(_00E_MQ11c_RyneusTransformExplosion, 1)
		EnchPurpleFXShader.Stop(RyneusActor)
		_00E_MQ11c_FXRyneusParticleAttach.Stop(RyneusActor)
		_00E_MQ11c_RyneusChestVFX.Stop(RyneusActor)
		RyneusOorbaya = RyneusActor.PlaceActorAtMe(_30E_MQ11c_RyneusOorbaya, 1)
		RyneusOorbaya.SetAlpha(0.0)
		RyneusOorbaya.MoveTo(MQ11c_SC10_OorbayaSpawnMarker)
		RyneusOorbaya.SetAlpha(0.90)
	ElseIf (_00E_MQ11c_RyneusPeacefulSolution.GetValueInt() == 1)
		Wait(3.6)
		Game.ShakeCamera(afStrength = 1)
		MQ11c_SC10_RyneusTransformOriginMarker.PushActorAway(RyneusActor, 4.0)
		RyneusActor.KillEssential(RyneusActor)
		EnchPurpleFXShader.Stop(RyneusActor)
		RyneusActor.Disable()
		_00E_MQ11c_FXRyneusParticleAttach.Stop(RyneusActor)
		_00E_MQ11c_RyneusChestVFX.Stop(RyneusActor)
		RyneusActor.PlaceAtMe(_00E_MQ11c_RyneusTransformExplosion, 1)
		RyneusOorbaya = RyneusActor.PlaceActorAtMe(_30E_MQ11c_RyneusOorbayaInkarnation, 1)
		RyneusOorbaya.SetAlpha(0.0)
		RyneusOorbaya.MoveTo(MQ11c_SC10_OorbayaSpawnMarker)
		RyneusOorbaya.SetAlpha(0.90)
	EndIf

	Debug.SendAnimationEvent(PlayerREF, "StaggerPlayer")
	Wait(0.5)
	Debug.SendAnimationEvent(RyneusOorbaya, "summonStart")
	Wait(1.8)
	_00E_MQ11c_RyneusTransformsIMOD.PopTo(_00E_MQ11c_RyneusTransformsIMODLoop)
	AudioCategoryMuteSubmerged.SetVolume(0.5)
	FXCameraAttachTurbulentFogEffect.Stop(PlayerREF)
	MQ11c.SetObjectiveDisplayed(100)
	Wait(1)
	Debug.SendAnimationEvent(RyneusOorbaya, "IdleForceDefaultState")
	Debug.SendAnimationEvent(RyneusOorbaya, "returnToDefault")
	RyneusOorbaya.StartCombat(PlayerREF)
	
EndFunction

Function StartMusic()

	Levelsystem.RemoveSilence()
	_00E_MQ11c_SpecialDialogueSad.Add()

EndFunction

Function StartTalkingScene()
    MQ11c_SC9_RyneusTalks.Start()
EndFunction

;======================================EPILOGUE======================================

Function CrashDownEverything()
    QSTGreybeardRumble.Play(PlayerREF)
    Game.ShakeCamera(afstrength = 0.2)
    Wait(2)
    QSTGreybeardRumble.Play(PlayerREF)
    Game.ShakeCamera(afstrength = 0.5)
    Wait(2.3)
    QSTTG09NocturnalFloorRumble.Play(PlayerREF)
    Game.ShakeCamera(afstrength = 0.5)
    Debug.SetGodMode(True)
    MQ11c_SC10_RockStartMarker01.Activate(MQ11c_SC10_RockStartMarker01)
    _00E_MQ11c_RyneusTransformSoundM.Play(PlayerREF)
    MQ11c_SC10_RockStartMarker02.Activate(MQ11c_SC10_RockStartMarker02)
    Wait(2)
    _00E_MQ11c_DreamCollapseIMOD.Apply()
    _00E_AMBHorrorVoicesNoLoopM.Play(PlayerREF)
    Wait(1)
    QSTDA10Rumble.Play(PlayerREF)
    Wait(1.5)
    QSTDA10Rumble.Play(PlayerREF)
    Wait(1)
    _00E_AMBHorrorVoicesNoLoopM.Play(PlayerREF)
    Wait(2.5)
    QSTDA10Rumble.Play(PlayerREF)
    Wait(2)
    QSTTG09NocturnalFloorRumble.Play(PlayerREF)
    Wait(2)
    _00E_MQ11c_DreamCollapseIMOD.PopTo(FadeToWhiteHoldImod)
    Wait(2)
    SetUpRealSilvergrove()
EndFunction

Function SetUpRealSilvergrove()

	if (_00E_MQ11c_RyneusPeacefulSolution.GetValueInt() == 1)
		MQ11c.SetObjectiveDisplayed(105)
		If _00E_AchievementsEnabled.GetValueInt() == 1
			Game.UnlockAchievement("END_RYNEUS_SILVERGROVE_01")
		EndIf
	Else	
		MQ11c.SetObjectiveDisplayed(110)
	EndIf

	If MQ11c_FatherREF.IsEnabled()
		MQ11c_FatherREF.Disable()
	EndIf

	RyneusDog.GetReference().Disable()
	Levelsystem.SkipTimeToHour(13.3)
	_00E_FXCameraAttachJUngleLeavesWindyUpEffect.Stop(PlayerREF)
	MQ11_D12_SilberhainDeadLinker.Disable()
	MQ11cSilberhainDestroyLinker.Disable()
	_00E_MQ11c_SilvergroveMainMusic.SetValue(0)

	; Prepare the gate to Powder Desert for the player
	MQ11c_LeverREF.BlockActivation(False)
	MQ11c_SilvergroveGateREF.Lock(False)
	MQ11c_SilvergroveGateREF.BlockActivation(False)

	_00E_MQ11c_SilberhainDeadWeather.SetValueInt(0)
	SetSilberhainDeadWeatherFX(False)
	PlayerREF.MoveTo(MQ11c_D12_SilberhainPlayerMarker)
	PlayerREF.RemoveItem(_00E_MQ11c_SphereBow)
    int SphereArrowCount = PlayerREF.GetItemCount(_00E_MQ11c_SphereArrow)
    If SphereArrowCount > 0
		PlayerREF.RemoveItem(_00E_MQ11c_SphereArrow, SphereArrowCount)
	EndIf
	_00E_SilenceAbruptHighPriority.Add()
	FadeToWhiteHoldImod.PopTo(FadeToWhiteBackImod)
	(Alias_Player as _00E_MQ11c_PlayerAliasScript).UpdateWeatherTrackingState()
	Debug.SetGodMode(False)
	
EndFunction

Function ToggleHorseObjective()
    if (_00E_MQ11c_RyneusPeacefulSolution.GetValueInt() == 1)
        Mq11c.SetObjectiveCompleted(105)
        Mq11c.SetObjectiveDisplayed(115)
    EndIf
EndFunction

Function LaunchInvisibleRyneus()

    if (_00E_MQ11c_RyneusPeacefulSolution.GetValueInt() == 1)
        RyneusRealInvisible.GetReference().Activate(PlayerREF)
    EndIf	
EndFunction

Function AddDyingMusic()
    _00E_MQ11c_SpecialDialogueRyneusDeath.Add()
EndFunction

Function AddAmulet()

	PlayerREF.AddItem(_00E_MQ11_BlackStone_Ryneus, 1)
	SetObjectiveCompleted(110)
	SetObjectiveCompleted(115)
	SetObjectiveCompleted(120)
	SetObjectiveCompleted(105)
	Timescale.SetValue(10)
	MQ11cBlockMarker.disable()
	_00E_TeleportGlobal.SetValue(0)
	setstage(295)
	PostTransformCleanup()
	
EndFunction
	
Function BuryRyneus()

	If (_00E_MQ11c_RyneusBuried.GetValueInt() == 1)										
		If (PlayerREF.GetItemCount(_00E_MQ11_BlackStone_Ryneus) == 0)
			PlayerREF.AddItem(_00E_MQ11_BlackStone_Ryneus, 1)
			SetObjectiveCompleted(110)
			SetObjectiveCompleted(115)
			SetObjectiveCompleted(120)
			SetObjectiveCompleted(105)
			Timescale.SetValue(10)
			MQ11cBlockMarker.disable()
			_00E_TeleportGlobal.SetValue(0)
			setstage(295)
		EndIf
		
		MQ11c_D13_RyneusBurialMarker.Disable()
		Levelsystem.FadeToBlack()
		PlayerREF.MoveTo(MQ11c_D13_RyneusBurialPlayerMarker)
		MQ11c_RyneusRefReal.Disable()
		Levelsystem.FadeToBlackBack()

		Wait(3)
		PostTransformCleanup()
	EndIf

EndFunction

Function SetSilberhainDeadWeatherFX(Bool bGradualTransition)

	If _00E_MQ11c_SilberhainDeadWeather.GetValueInt() > 0
		Return
	EndIf
	_00E_MQ11c_SilberhainDeadWeather.SetValueInt(1)

	If bGradualTransition
		_00E_MQ11c_SilberhainRealWeather.SetActive(True)
	Else
		_00E_MQ11c_SilberhainRealWeather.ForceActive(True)
	EndIf
	AudioCategoryAMBr.Mute()
	AudioCategoryAMB.Mute()
	AudioCategoryAMB2x.SetVolume(0.5)

	If bGradualTransition
		_00E_MQ11c_RyneusTransformsIMODTo.Apply()
		_00E_MQ11c_RyneusTransformsIMODLoop.Remove()
		_00E_MQ11c_RyneusTransformsIMODFrom.Remove()
		Wait(4.9)
		If _00E_MQ11c_SilberhainDeadWeather.GetValueInt() == 1
			_00E_MQ11c_RyneusTransformsIMODTo.PopTo(_00E_MQ11c_RyneusTransformsIMODLoop)
		Else
			_00E_MQ11c_RyneusTransformsIMODTo.Remove()
		EndIf
	Else
		_00E_MQ11c_RyneusTransformsIMODLoop.Apply()
		_00E_MQ11c_RyneusTransformsIMODTo.Remove()
		_00E_MQ11c_RyneusTransformsIMODFrom.Remove()
	EndIf

EndFunction

Function ResetSilberhainDeadWeatherFX(Bool bGradualTransition)

	If _00E_MQ11c_SilberhainDeadWeather.GetValueInt() <= 0
		Return
	EndIf
	_00E_MQ11c_SilberhainDeadWeather.SetValueInt(0)

	Weather.ReleaseOverride()
	AudioCategoryAMBr.UnMute()
	AudioCategoryAMB.UnMute()
	AudioCategoryAMB2x.SetVolume(1)

	If bGradualTransition
		PostTransformCleanup()

		_00E_MQ11c_RyneusTransformsIMODLoop.PopTo(_00E_MQ11c_RyneusTransformsIMODFrom)
		_00E_MQ11c_RyneusTransformsIMODTo.Remove()
		Wait(4.9)
		_00E_MQ11c_RyneusTransformsIMODFrom.Remove()
	Else
		_00E_MQ11c_RyneusTransformsIMODLoop.Remove()
		_00E_MQ11c_RyneusTransformsIMODTo.Remove()
		_00E_MQ11c_RyneusTransformsIMODFrom.Remove()

		PostTransformCleanup()
	EndIf

EndFunction

Bool bPostTransformCleanupDone = False

Function PostTransformCleanup()
	If bPostTransformCleanupDone
		Return
	EndIf

	bPostTransformCleanupDone = True

	Debug.SetGodMode(False)

	MUSTavernSILENCE.Remove()
	_00E_MQ11c_SilberhainRealInterior.Remove()
	Levelsystem.RemoveSilence()
	_00E_FXCameraAttachJUngleLeavesWindyUpEffect.Stop(PlayerREF)
	_00E_MQ11c_DreamCollapseIMOD.Remove()
	FadeToWhiteHoldImod.Remove()
	FadeToBlackHoldImod.Remove()
	FadeToBlackBackImod.Remove()
	_00E_MQ11c_RyneusTransformsIMOD.Remove()
EndFunction

Function ResetMusic()
	
	Levelsystem.RemoveSIlence()

EndFunction

Function StopCoughingSound()

	If MQ11c_RyneusSoundBoxREF == None
		MQ11c_RyneusSoundBoxREF = (Game.GetFormFromFile(0x00096D53, "Skyrim.esm") as ObjectReference) as _00E_MQ11c_RyneusSoundBoxSc
	EndIf

	MQ11c_RyneusSoundBoxREF.StopCoughing()

EndFunction

Function FlagRyneusAsDead()

	If MQ11c_RyneusSoundBoxREF == None
		MQ11c_RyneusSoundBoxREF = (Game.GetFormFromFile(0x00096D53, "Skyrim.esm") as ObjectReference) as _00E_MQ11c_RyneusSoundBoxSc
	EndIf

	MQ11c_RyneusSoundBoxREF.FlagRyneusAsDead()

EndFunction

Function MoveRyneusToTavern()

	Ryneus.GetReference().MoveTo(MQ11cPortMarker)
	
EndFunction

Function SilvergroveTimeFailsave()

	if Timescale.GetValue() > 0.1
		Timescale.SetValue(0.1)
	EndIf
	
	if GameHour.GetValue() != 6.48
		; Minor correction of the time. Normally, _00E_MQ11c_RyneusEntryHeadache must take care about proper jump to 6.48 for the first time in MQ11c.
		GameHour.SetValue(6.48)
	EndIf

EndFunction

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

Event OnGainLOS(Actor akViewer, ObjectReference akTarget)
	;added a global as condition to prevent multiple resets when the player was detected by multiple guards at the same time
    If (((akViewer == Guard01) || (akViewer == Guard02) || (akViewer == Guard03)) && (MQ11c.GetStage() < 50) && (_00E_MQ11c_PlayerDetected.GetValueInt() == 0))
		If !PlayerREF.HasMagicEffectWithKeyword(MagicInvisibility)
			_00E_MQ11c_PlayerDetected.SetValueInt(1)
			Self.UpdateCurrentInstanceGlobal(_00E_MQ11c_PlayerDetected)
			UIActivateFailM.Play(PlayerREF)
			_00E_MQ11c_StealthMinigame_PlayerCaught02.Show() ;Debug only
			PlayerREF.MoveTo(DebugPlayerStartMarker)
			If !PlayerREF.IsSneaking()
				PlayerREF.StartSneaking()
			EndIf
		EndIf
	Endif

EndEvent

Event OnUpdate()

	If !(PlayerREF.IsSneaking()) && (MQ11c.GetStage() < 50) && (PlayerREF.IsInLocation(_00E_MQ11c_KetaronsHouse))
		UIActivateFailM.Play(PlayerREF)
		_00E_MQ11c_StealthMinigame_PlayerCaught.Show()
		PlayerREF.MoveTo(DebugPlayerStartMarker)
		If !PlayerREF.IsSneaking()
			PlayerREF.StartSneaking()
		EndIf
		RegisterForSingleUpdate(7)
	Else
		RegisterForSingleUpdate(7)
	EndIf
	
EndEvent

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

bool bDogMinigameFailed = false
float Property DogMinigameWarningDistance Auto
float Property DogMinigameLosingDistance Auto

Scene Property MQ11c_SC1_KeshanEscortsPlayer Auto
Scene Property MQ11c_SC3_WardenClosesGate Auto
Scene Property MQ11c_SC4_DogStealsPuppet Auto
Scene Property MQ11c_SC6_RyneusStartSphere Auto
Scene Property MQ11c_SC9_RyneusTalks Auto
Scene Property MQ11c_SC10_RyneusTransforms Auto

_00E_MQ11c_RyneusSoundBoxSc Property MQ11c_RyneusSoundBoxREF Auto

ObjectReference Property SC1_PlayerMoveToMarker Auto
ObjectReference Property SC1_KetaronHouseDoor Auto
ObjectReference Property GuardsMoveToMarker Auto
ObjectReference Property GuardsHouseDoor Auto
ObjectReference Property DebugPlayerStartMarker Auto
ObjectReference Property SC4_DogChaseStartMarker Auto
ObjectReference Property SC5_FatherMoveMarker Auto
ObjectReference Property SC5_RyneusMoveMarker Auto
ObjectReference Property SC6_Sphere Auto
ObjectReference Property SC6_SphereButton Auto
ObjectReference Property SC7_RyneusButterflyHouseLinker Auto
ObjectReference Property MQ11c_SC10_PaintingMale Auto
ObjectReference Property MQ11c_SC10_PaintingFemale Auto
ObjectReference Property MQ11c_D10_RyneusSitLedgeMarker Auto
ObjectReference Property MQ11c_D11_RyneusCaveEntrance Auto
ObjectReference Property MQ11c_SC10_OorbayaSpawnMarker Auto
ObjectReference Property MQ11c_SC10_BarrierREF Auto
ObjectReference Property MQ11c_SC10_BarrierColREF Auto
ObjectReference Property MQ11c_SC10_BarrierMarker Auto
ObjectReference Property MQ11c_SC10_RyneusTransformOriginMarker Auto
ObjectReference Property MQ11c_SC10_RockStartMarker01 Auto
ObjectReference Property MQ11c_SC10_RockStartMarker02 Auto
ObjectReference Property MQ11_D12_SilberhainDeadLinker Auto
ObjectReference Property MQ11cSilberhainDestroyLinker Auto
ObjectReference Property MQ11c_D12_SilberhainPlayerMarker Auto
ObjectReference Property MQ11c_D13_RyneusBurialMarker Auto
ObjectReference Property MQ11c_D13_RyneusBurialPlayerMarker Auto
ObjectReference Property MQ11c_D0_PlayerSilberhainEntrymarker Auto
ObjectReference Property MQ11c_ScrollChest Auto
ObjectReference Property MQ11cPortMarker Auto
ObjectReference Property MQ11c_SilvergroveGateREF Auto
ObjectReference Property MQ11c_D4_GuardWaitMarker Auto
ObjectReference Property MQ11c_D4_PlayerFailsafeMarker Auto
ObjectReference Property MQ11c_LeverREF Auto
ObjectReference Property MQ11c_DogChaseMarker027 Auto

Weapon Property _00E_MQ11c_SphereBow Auto
Ammo Property _00E_MQ11c_SphereArrow Auto

Activator Property _00E_MQ11_critterSpawnInsects_Single Auto

ReferenceAlias Property DalGeyssGuard01 Auto
ReferenceAlias Property DalGeyssGuard02 Auto
ReferenceAlias Property DalGeyssGuard03 Auto
ReferenceAlias Property RyneusDog Auto
ReferenceAlias Property Ryneus Auto
ReferenceAlias Property Alias_Ulbar Auto
ReferenceAlias Property RyneusRealInvisible Auto
ReferenceAlias Property Alias_Player Auto

Quest Property MQ11c Auto
GlobalVariable Property _00E_MQ11c_StealthGame_Running Auto
GlobalVariable Property _00E_MQ11c_PlayerDetected Auto
GlobalVariable Property _00E_MQ11c_Sphere_HitsLanded Auto
GlobalVariable Property  _00E_MQ11c_Sphere_HitsLandedEOTS Auto
GlobalVariable Property _00E_MQ11c_RyneusPeacefulSolution Auto
GlobalVariable Property _00E_MQ11c_SilberhainDeadWeather Auto
GlobalVariable Property _00E_MQ11c_RyneusBuried Auto
GlobalVariable Property _00E_AchievementsEnabled Auto
GlobalVariable Property GameHour Auto
GLobalVariable Property Timescale Auto
float TimescaleDefault = 10.0

_00E_QuestFunctions Property Levelsystem Auto

SoundCategory Property AudioCategoryMuteSubmerged Auto
SoundCategory Property AudioCategoryAMB Auto
SoundCategory Property AudioCategoryAMBr Auto
SoundCategory Property AudioCategoryAMB2x Auto
SoundCategory Property AudioCategoryPausedDuringMenuFade Auto

Armor Property _00E_MQ11_BlackStone_Ryneus Auto

Actor Property PlayerREF Auto
Actor Property MQ11c_FatherREF Auto
Actor Property Guard01 Auto
Actor Property Guard02 Auto
Actor Property Guard03 Auto
Actor Property MQ11c_RyneusRefReal Auto
Actor Property RyneusDogA Auto Hidden
Actor Property RyneusOorbaya Auto Hidden
Actor Property MQ11c_SilvergroveGateWardenREF Auto
Actor Property MQ11c_IAmInTheWayDogREF Auto

ActorBase Property _30E_MQ11c_RyneusOorbaya Auto
ActorBase Property _30E_MQ11c_RyneusOorbayaInkarnation Auto

Keyword Property MagicInvisibility Auto

Spell Property _00E_MQ11c_ChaosohrSpell Auto 
SoundCategory Property _00E_MQ11c_CrittersoundCategoryDontUse Auto
Book Property _00E_MQ11c_RyneusDiscardLetter Auto

MiscObject Property _00E_MQ11cSchmetterlingsglas Auto
MiscObject Property _00E_MQ11c_RyneusTeddy Auto
MiscObject Property _00E_MQ11cSchmetterling Auto

Topic Property MQ11c_D12_RyneusTransformTopic Auto
Topic Property MQ11c_D12_RyneusTransformTopic02 Auto

Sound Property NPCDogBark Auto
Sound Property _00E_NPCDogIdleWhineM Auto
Sound Property _00E_MQ11c_RyneusTransformSoundM Auto
Sound Property AMBrWindMountainHeavyLP Auto
Sound Property QSTGreybeardRumble Auto
Sound Property QSTTG09NocturnalFloorRumble Auto
Sound Property QSTDA10Rumble Auto
Sound Property _00E_AMBHorrorVoicesNoLoopM Auto
Sound Property UIActivateFailM Auto

Location  Property _00E_MQ11c_KetaronsHouse Auto

Outfit Property _00E_MQ11c_RyneusDogToyOutfit Auto

Message Property _00E_MQ11c_StealthMinigameMessagebox Auto
Message Property _00E_MQ11c_DogMinigameMessagebox Auto
Message Property _00E_MQ11c_StealthMinigame_PlayerCaught Auto
Message Property _00E_MQ11c_StealthMinigame_PlayerCaught02 Auto
Message Property _00E_MQ11c_sThorusWarning Auto
Message Property _00E_MQ11c_sThorusGone Auto

ImageSpaceModifier Property FadeToBlackImod Auto
ImageSpaceModifier Property FadeToBlackHoldImod Auto
ImageSpaceModifier Property FadeToBlackBackImod Auto
ImageSpaceModifier Property _00E_MQ11c_RyneusTransformsIMOD Auto
ImageSpaceModifier Property _00E_MQ11c_RyneusTransformsIMODLoop Auto
ImageSpaceModifier Property _00E_MQ11c_RyneusTransformsIMODTo Auto
ImageSpaceModifier Property _00E_MQ11c_RyneusTransformsIMODFrom Auto
ImageSpaceModifier Property _00E_MQ11c_DreamCollapseIMOD Auto
ImageSpaceModifier Property FadeToWhiteHoldImod Auto
ImageSpaceModifier Property FadeToWhiteBackImod Auto
ImageSpaceModifier Property _00E_MQ11c_SilberhainRealInterior Auto

FormList Property _00E_MQ11_CritterSpawnmarkerList Auto

Explosion Property _00E_MQ11c_RyneusTransformExplosion Auto

VisualEffect Property FXCameraAttachTurbulentFogEffect Auto
VisualEffect Property _00E_FXCameraAttachJUngleLeavesWindyUpEffect Auto
VisualEffect Property _00E_MQ11c_FXRyneusParticleAttach Auto
VisualEffect Property _00E_MQ11c_RyneusChestVFX Auto

EffectShader Property AtronachUnsummonDeathFXS Auto
EffectShader Property EnchPurpleFXShader Auto
EffectShader Property _00E_QuestshaderNew Auto

Weather Property _00E_MQ11c_SilberhainRealWeather Auto

MusicType Property _00E_MQ11c_SpecialDialogueRyneusDeath Auto
MusicType Property _00E_MQ11c_SpecialDialogueSad Auto
MusicType Property _00E_Music_Combat_Epic_Transition Auto
MusicType Property MUSTavernSILENCE Auto
MusicType Property _00E_SilenceTransitionLowPriority Auto
MusicType Property _00E_SilenceAbruptHighPriority Auto

ObjectReference Property MQ11cBlockMarker Auto
GlobalVariable Property _00E_TeleportGlobal Auto
GlobalVariable Property SympathyRyneus Auto
GlobalVariable Property MQ11c_PlayerInSilvergroveRadius Auto
GlobalVariable Property _00E_MQ11c_SilvergroveMainMusic Auto