Scriptname _00E_MQ11b_Questfunctions extends Quest

Import Utility
Import Game

Event OnUpdate()

    GiveBox2()

EndEvent

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

function SetUp()

	AllowIdleChatter.SetValue(1)
	_00E_MC_JesparRef.Disable()

EndFunction 

Function GiveBox()

    ; Give Adila's map to the player. Called at stage 10.
    ; In order for this function to be latent, I'm redirecting it
    ; to GiveBox2() through a single-update event.
	
	RegisterForSingleUpdate(0.2)

EndFunction

Function GiveBox2()

	ObjectReference WirtRef = Wirt.GetReference()

    if !WirtRef.IsInDialogueWithPlayer()
        Done = true
        MQ11bMap.DisableNoWait()
        _00E_MQ11b_sBoxMessage.Show()
        
        Wait(0.4)
        MQ11bMap.Activate(PlayerREF)
        SetCurrentStageID(10)
        MQ11b_DoorC.Lock(0)
        MQ11b_DoorB.Lock(0)
        MQ11b_DoorA.Lock(0)
        SetObjectiveCompleted(10)
        SetObjectiveDisplayed(20)
        SetObjectiveDisplayed(30)
        SetObjectiveDisplayed(40)

        Wait(0.6)
        
        if PlayerREF.GetItemcount(_00E_MQ11b_Map) < 1 
            PlayerREF.AddItem(_00E_MQ11b_Map, 1)
        endif
    else
        RegisterForSingleUpdate(1)
    endif
	
EndFunction

Function UpdateObjectives()

	If IsObjectiveCompleted(20) && IsObjectiveCompleted(30) && IsObjectiveCompleted(40)
		SetCurrentStageID(20)
		SetObjectiveDisplayed(50)
	EndIf

EndFunction

Function ActivateJespar()

	_00E_MC_JesparRef.Enable()
	_00E_MC_JesparRef.MoveTo(MQ11bJesparPortMarker)
	_00E_MC_JesparRef.EquipItemEx(_01E_02Eisendolch, 1, True)
	_00E_MC_JesparRef.EquipItemEx(_01E_02Eisendolch, 2, True)
	
EndFunction

Function JesparPortDunehome()
	_00E_MC_JesparRef.MoveTo(MQ11bDunehomeJesparMarker)
EndFunction

Function StartSC01()

	SetObjectiveCompleted(81)

	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, False)

	MQ11b_SC01_Cave.ForceStart()

EndFunction


Function StartSC02()

	AudioCategoryAMB.SetVolume(0.75)
	PlayerREF.StartSneaking()
	MQ11EscapeDoor.BlockActivation(true)
    MQ11EscapeDoor.SetLockLevel(255)
    MQ11EscapeDoor.Lock(True)
	_00E_TeleportGlobal.SetValueInt(1)
	_00E_SilenceAbruptHighPriority.Add()
	
	_00E_MQ11b_AdilaREF.Enable()
	
	_00E_MC_JesparREF.MoveTo(MQ11b_SC02_JesparStartREF)
	
	MQ11b_SC02_Confrontation.ForceStart()
	
	MQ11SoundMarker.Disable()
	
EndFunction

Function EnableFollowTGBX()

	If MQ11b_FollowStartTGBX == None
		MQ11b_FollowStartTGBX = Game.GetForm(0x000333B3) as ObjectReference
	EndIf

	MQ11b_FollowStartTGBX.Enable()
	CompanionIsTalking.SetValueInt(0)
	
	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, True, 550, 600, 150)
	
EndFunction

Function ValenzioPrepareForSit()

	; Switch Valenzio to the sitting package
	_00E_MQ11b_PrisonerREF.SetActorValue("Variable01", 1.0)
	_00E_MQ11b_PrisonerREF.EvaluatePackage()

EndFunction

Function ValenzioSit()

	Debug.SendAnimationEvent(_00E_MQ11b_PrisonerREF, "IdleBoundKneesStart")

EndFunction

Function StartSC03()

	MQ11b_SC03_Interrupt.ForceStart()
	
EndFunction

Function KnockPlayer()
	
	;this function was there to knock down the player, but didn't work out that well, now a barrier is enabled instead
	MQ11b_SC02_AdilaBarrierREF.Enable()
	
EndFunction

Function DisableInvisibleBarrier()

	MQ11b_SC02_InvisibleBarrierREF.Disable()

EndFunction

Function StartSC04()

	_00E_SilenceAbruptHighPriority.Add()
	MQ11bSwitchMarker01.EnableNoWait()
	Utility.Wait(1)
	SetObjectiveDisplayed(150)
	
	MQ11b_SC04_AdilaCombat.ForceStart()
	
EndFunction

Function PlayJesparScream()

	_00E_MQ11b_SC02_JesparScreamM.Play(_00E_MC_JesparRef)

EndFunction

Function PlaceJespar()

	_00E_MC_JesparRef.MoveTo(MQ11b_SC02_JesparLayMarker)

EndFunction

Function SpawnBarrier()
	
	; MQ11b_SC02_AdilaBarrierREF.Enable() ; Gavrant: doesn't look so good when the barrier appears for a few seconds.
	_00E_MC_JesparREF.SetGhost(False)
	ObjectReference objSpawnMarker = _00E_MC_JesparREF.PlaceAtMe(XMarkerHeading, 1)
	objSpawnMarker.MoveTo(_00E_MC_JesparREF, 100.0 * Math.Sin(_00E_MC_JesparREF.GetAngleZ()), 100.0 * Math.Cos(_00E_MC_JesparREF.GetAngleZ()), _00E_MC_JesparREF.GetHeight())
	objSpawnMarker.PlaceAtMe(Fireball01ImpactExplosion)
	_00E_MQ11b_SC02_JesparScreamM.Play(_00E_MC_JesparRef)
	_00E_MQ11b_PrisonerREF.SetGhost(False)
	_00E_MQ11b_PrisonerREF.Kill(_00E_MQ11b_PrisonerREF)
	_00E_StaggerSelf.Cast(_00E_MC_JesparREF, _00E_MC_JesparREF)
	_00E_StaggerSelf.Cast(_00E_MQ11b_AdilaREF, _00E_MQ11b_AdilaREF)
	SetCurrentStageID(115)
	
EndFunction

Function CastFireSpell()

	_00E_MC_JesparREF.SetGhost(False)
	_00E_MQ11b_AdilaREF.AddSpell(AbFireStormCastBodyFX)
	Wait(1)
	_00E_MQ11b_AdilaREF.PlayIdle(_00E_IdleStomp)
	Wait(0.3)
	_00E_MQ11b_AdilaFireShout.Cast(_00E_MQ11b_AdilaREF, _00E_MC_JesparREF)
	MAGFireBallExpImod.Apply()
	_00E_MQ11b_PrisonerREF.KillEssential(_00E_MQ11b_PrisonerREF)
	FireFXShader.Play(_00E_MQ11b_PrisonerREF)
	_00E_MC_JesparREF.BlockActivation(True)
	
EndFunction

Function PlayImpact()

	MAGFireBallExpImod.Apply()
	MAGDestructionFireStormExplosionM.Play(_00E_MC_JesparREF)
	Game.ShakeCamera()
	_00E_MQ11b_SC02_JesparScream02M.Play(_00E_MC_JesparREF)
	_00E_MC_JesparREF.PlaceAtMe(Fireball01ImpactExplosion)
	_00E_MQ11b_AdilaREF.PushActorAway(_00E_MC_JesparREF, 5.0)
	FireFXShader.Play(_00E_MC_JesparREF)
	Wait(0.75)
	_00E_MC_JesparREF.KillEssential(_00E_MC_JesparREF)
	MQ11b_SC04_JesparSmokeREF.EnableNoWait()
	MQ11b_SC04_JesparSmokeREF.MoveTo(_00E_MC_JesparREF)
	_00E_MC_JesparREF.SetGhost(True)
	
EndFunction

Function DisableBarrier()

	If MQ11b_SC02_AdilaBarrierREF.IsEnabled()
		MQ11b_SC02_AdilaBarrierREF.PlayAnimation("PlayAnim01")
		Utility.Wait(0.95)
		MQ11b_SC02_AdilaBarrierREF.Disable()
	EndIf

EndFunction

Function StartCombatAdila()

	_00E_MQ11b_AdilaREF.EvaluatePackage()
	MQ11EscapeDoor.BlockActivation(true)
    MQ11EscapeDoor.SetLockLevel(255)
    MQ11EscapeDoor.Lock(True)
	Wait(0.5)	
	_00E_MQ11b_AdilaREF.SetGhost(0)
	_00E_MC_Adila.SetEssential(0)
	_00E_MQ11b_AdilaREF.SetActorValue("Aggression", 3)
	_00E_MQ11b_AdilaREF.StartCombat(PlayerREF)
	_00E_Music_Special_Character_Jespar_SturmUndDrang.Add()
	AudioCategoryAMB.SetVolume(1)
	_00E_PlayerFunctions.GetSoundControl().RemoveSilence()
	_00E_SilenceAbruptHighPriority.remove()
	
EndFunction

Function RemoveCombatMusic()

	_00E_Music_Special_Character_Jespar_SturmUndDrang.Remove()

EndFunction

Function StartSC05()

	MQ11b_SC05_ResurrectMarkerREF.MoveTo(MQ11b_SC05_ResurrectMarker)
	DisableInvisibleBarrier()
	_00E_MC_VeiledWomanMQ11bREF.AddSpell(CandlelightLong)
	_00E_MC_VeiledWomanMQ11bREF.SetGhost(True)
	_00E_MC_VeiledWomanMQ11bREF.MoveTo(MQ11b_SC05_SpawnVeiledWomanREF)
	_00E_MC_VeiledWomanMQ11bREF.Enable()
	_00E_MC_VeiledWomanMQ11bREF.SetAlpha(0.9)
	_00E_Music_Special_Character_Jespar_SturmUndDrang.Remove()
	_00E_SilenceAbruptHighPriority.Add()
	
	MQ11b_SC05_VeiledWoman.ForceStart()

EndFunction

Function IgniteTorch()

	EffectShader _00E_FS_NQ02_VeiledWomanAppearFXS = Game.GetFormFromFile(0x0002ED3C, "Enderal - Forgotten Stories.esm") as EffectShader
	_00E_FS_NQ02_VeiledWomanAppearFXS.Play(_00E_MC_VeiledWomanMQ11bREF)
	
	MQ11b_SC05_LightREF.Enable()
	MAGNightingaleSubterfugeFire2DM.Play(PlayerREF)
	_00E_MAGFXFireOilIgniteLow.Play(PlayerREF)
	
EndFunction

Function AddVeiledWomanMusic()

	_00E_PlayerFunctions.GetSoundControl().RemoveSilence()
	_00E_Music_Character_VeiledWomanT.Add()
	
EndFunction

Function ReanimateJespar()

    if _00E_MC_VeiledWomanMQ11bREF.GetDistance(_00E_MC_JesparREF) > 200
        ; Failsave if the player blocks the Veiled Woman's path.
        _00E_MC_VeiledWomanMQ11bREF.MoveTo(_00E_MC_JesparREF)
    endif
	_00E_SilenceAbruptHighPriority.Add()
	Debug.SendAnimationEvent(_00E_MC_VeiledWomanMQ11bREF, "IdleKneelingEnter")
	Wait(1)
	_00E_MC_JesparREF.SetGhost(False)
	_00E_MagConjureReanimate.Play(_00E_MC_JesparREF)
	Actor akSpawnActor = _00E_MC_JesparREF.PlaceActorAtMe(_00E_NPCOorbayaInvisibeActor)
	MQ11b_SC05_ResurrectMarker.SetPosition(_00E_MC_JesparREF.GetPositionX(), _00E_MC_JesparREF.GetPositionY(), akSpawnActor.GetPositionZ())
	MQ11b_SC05_ResurrectMarker.SetAngle(_00E_MC_JesparREF.GetAngleX(), _00E_MC_JesparREF.GetAngleY(), _00E_MC_JesparREF.GetAngleZ())
	akSpawnActor.Disable()
	
	if MQ11b_SC04_JesparSmokeREF.IsEnabled()
		MQ11b_SC04_JesparSmokeREF.Disable()
	EndIf
	
	FireFXShader.Stop(_00E_MC_JesparREF)
	MAGRestorationConcentrationFire2D.Play(_00E_MC_JesparREF)
	_00E_FireBlueFXS.Play(_00E_MC_JesparREF)
	_00E_FireBlueFXS.Play(_00E_MC_VeiledWomanMQ11bREF)
	Wait(1)
	dunReanimateSelfNoShader.Cast(_00E_MC_JesparREF, _00E_MC_JesparREF)
	Wait(4)
	_00E_BlendToWhite.Apply()
	FadeToWhiteHoldImod.ApplyCrossFade(1.0)
	Wait(2)
		
	If PlayerREF.GetActorBase().GetSex() == 0
		_00E_MQ11b_VeiledWomanGoodbye_MaleM.Play(PlayerREF)
	Else
		_00E_MQ11b_VeiledWomanGoodbye_FemaleM.Play(PlayerREF)
	EndIf
	
	_00E_MC_JesparREF.PlaceAtMe(ExplosionTurnUndeadMass01VertOffset)
	_00E_MC_JesparREF.DispelSpell(dunReanimateSelfNoShader)
	_00E_MC_Jespar.SetEssential(true)
	Wait(3)
	;Jojo: Habe der Scene mehr Zeit gegeben. Manchmal hing Jespar noch in der Luft (weil er nach dem Reanimatespell herunterfallen muss)und hängt dann durch das AnimationEvent über dem Boden und fällt nicht mehr
	_00E_MC_JesparREF.Resurrect()
	_00E_MC_JesparREF.SetGhost(True)
	Debug.SendAnimationEvent(_00E_MC_JesparREF, "IdleBedRollFrontEnterInstant")
	_00E_FireBlueFXS.Stop(_00E_MC_JesparREF)
	_00E_MC_JesparREF.SetLookAt(_00E_MC_JesparREF)
	JesparUnblockActivation()
	_00E_MC_VeiledWomanMQ11bREF.DisableNoWait()
	MQ11b_SC05_LightREF.DisableNoWait()
	_00E_Music_Character_VeiledWomanT.Remove()
	Wait(5)
	
	FadeToWhiteBackImod.ApplyCrossFade(afFadeDuration = 2.0)
	SetObjectiveDisplayed(170)
	SetCurrentStageID(230)
		
EndFunction

Function JesparGetUp()

	Debug.SendAnimationEvent(_00E_MC_JesparREF, "_00E_BedrollFrontExit")

EndFunction

Function StartSC06()

	_00E_MC_JesparREF.ClearLookAt()

	MQ11b_SC06_JesparFinal.ForceStart()

EndFunction

Function JesparUnblockActivation()

	_00E_MC_JesparREF.BlockActivation(False)

EndFunction

Function ActivateBlackStone()

	Alias_BlackStone.GetRef().Enable()
	MQ11EscapeDoor.Lock(0)
	_00E_SilenceAbruptHighPriority.remove()
	
EndFunction

Function FadeToBlackAndBack()

	FadeToBlackAndBackIMOD.Apply()

EndFunction

Function CompleteEverything()

	PlayerREF.RemoveItem(_00E_MQ11b_TargetABook)
	PlayerREF.RemoveItem(_00E_MQ11b_TargetBBook)
	PlayerREF.RemoveItem(_00E_MQ11b_TargetCBook)
	
	; Move the grieve marker to the side of Adila, and turn it to her
	Float adilaAngleZ = _00E_MQ11b_AdilaREF.GetAngleZ()
	Float offsetAngleZ = adilaAngleZ + 90.0
	Float offsetDistance = 70.0
	MQ11b_SC07_JesparGrieveMarkerREF.MoveTo(_00E_MQ11b_AdilaREF, offsetDistance * Math.Sin(offsetAngleZ), offsetDistance * Math.Cos(offsetAngleZ), 0, False)
	MQ11b_SC07_JesparGrieveMarkerREF.SetAngle(0, 0, adilaAngleZ - 90.0)

	_00E_PlayerFunctions.GetSoundControl().RemoveSilence()
	_00E_EPHandler.GiveEP(1500)
    MQ11EscapeDoor.BlockActivation(False)
    MQ11EscapeDoor.Lock(False)
	MQ11bAdilasCaveDoorREF.BlockActivation(False)
	ResetTeleportGlobal()
	Self.Stop()
	
EndFunction

Function ResetTeleportGlobal()

	_00E_TeleportGlobal.SetValueInt(0)

EndFunction

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

Bool Done = false

int Property __Config_iRewardEXP = 1500 Auto

; JESPAR ACTIVATION UNBLOCKEN

GlobalVariable Property AllowIdleChatter Auto

;String Property sMQ11bBoxMessage Auto
Message Property _00E_MQ11b_sBoxMessage Auto

Actor Property _00E_MQ11b_AdilaREF Auto
Actor Property _00E_MC_JesparREF Auto
Actor Property PlayerREF Auto
Actor Property _00E_MQ11b_PrisonerREF Auto
Actor Property _00E_MC_VeiledWomanMQ11bREF Auto

Quest Property CQJ02 Auto
Quest Property CQJ04 Auto

ActorBase Property _00E_NPCOorbayaInvisibeActor Auto

EffectShader Property _00E_FireBlueFXS Auto

ObjectReference Property MQ11bMap Auto
ObjectReference Property MQ11b_DoorC Auto
ObjectReference Property MQ11b_DoorB Auto
ObjectReference Property MQ11b_DoorA Auto
ObjectReference Property MQ11bJesparPortMarker Auto
ObjectReference Property MQ11bDunehomeJesparMarker Auto
ObjectReference Property MQ11bSwitchMarker002 Auto
ObjectReference Property MQ11bSwitchMarker01 Auto
ObjectReference Property MQ11SoundMarker Auto
ObjectReference Property MQ11EscapeDoor Auto
ObjectReference Property MQ11bAdilasCaveDoorREF Auto
ObjectReference Property MQ11b_SC02_JesparStartREF Auto
ObjectReference Property MQ11b_SC02_JesparLayMarker Auto
ObjectReference Property MQ11b_SC02_InvisibleBarrierREF Auto
ObjectReference Property MQ11b_SC05_SpawnVeiledWomanREF Auto
ObjectReference Property MQ11b_SC05_ResurrectMarker Auto
ObjectReference Property MQ11b_SC05_LightREF Auto
ObjectReference Property MQ11b_SC05_ResurrectMarkerREF Auto
ObjectReference Property MQ11b_SC02_AdilaBarrierREF Auto
ObjectReference Property MQ11b_SC07_JesparGrieveMarkerREF Auto
ObjectReference Property MQ11b_FollowStartTGBX Auto

MusicType Property _00E_Music_Character_VeiledWomanT Auto

Static Property XMarkerHeading Auto

Spell Property _00E_StaggerSelf Auto
Spell Property AbFireStormCastBodyFX Auto
Spell Property _00E_MQ11b_AdilaFireShout Auto
Spell Property dunReanimateSelfNoShader Auto

GlobalVariable Property _00E_TeleportGlobal Auto
GlobalVariable Property CompanionIsTalking Auto

ReferenceAlias Property Wirt Auto
ReferenceAlias Property Alias_BlackStone Auto

Book Property _00E_MQ11b_Map Auto
Book Property _00E_MQ11b_TargetABook Auto
Book Property _00E_MQ11b_TargetBBook Auto
Book Property _00E_MQ11b_TargetCBook Auto

Idle Property _00E_IdleStomp Auto

Weapon Property _01E_02Eisendolch Auto

Explosion Property Fireball01ImpactExplosion Auto
Explosion Property ExplosionTurnUndeadMass01VertOffset Auto

Sound Property _00E_MQ11b_SC02_JesparScreamM Auto
Sound Property _00E_MQ11b_SC02_JesparScream02M Auto
Sound Property MAGRestorationConcentrationFire2D Auto
Sound Property	MAGNightingaleSubterfugeFire2DM Auto
Sound Property	_00E_MAGFXFireOilIgniteLow Auto
Sound Property	_00E_MQ11b_VeiledWomanGoodbye_FemaleM Auto
Sound Property	_00E_MQ11b_VeiledWomanGoodbye_MaleM Auto
Sound Property	_00E_MagConjureReanimate Auto
Sound Property	MAGDestructionFireStormExplosionM Auto

SoundCategory Property AudioCategoryAMB Auto

EffectShader Property FireFXShader Auto

Spell Property CandlelightLong Auto

Scene Property MQ11b_SC01_Cave Auto
Scene Property MQ11b_SC02_Confrontation Auto
Scene Property MQ11b_SC04_AdilaCombat Auto
Scene Property MQ11b_SC05_VeiledWoman Auto
Scene Property MQ11b_SC06_JesparFinal Auto
Scene Property MQ11b_SC03_Interrupt Auto

ActorBase Property _00E_MC_Jespar Auto
ActorBase Property _00E_MC_Adila Auto
MusicType Property _00E_Music_Special_Character_Jespar_SturmUndDrang Auto
MusicType Property _00E_SilenceAbruptHighPriority Auto
ImageSpaceModifier Property _00E_BlendToWhite Auto
ImageSpaceModifier Property FadeToBlackAndBackIMOD Auto
ObjectReference Property MQ11b_SC04_JesparSmokeREF Auto

ImageSpaceModifier Property MAGFireBallExpImod Auto
ImageSpaceModifier Property FadeToWhiteHoldImod Auto
ImageSpaceModifier Property FadeToWhiteBackImod Auto