Scriptname _00E_CQC06_Functions extends Quest  

Import _00E_QuestFunctions

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

Event OnUpdateGameTime()

	if !bCleanedUp
	
		bCleanedUp = True
		CleanUp()
		
	EndIf
	
	If !PlayerREF.GetParentCell() == CapitalCityBar01
		CQC06_DoorREF.lock(50)
		CQC06_SC01_BedREF2.SetActorOwner(None)
	Else
		RegisterForSingleUpdateGameTime(6)
	EndIf

EndEvent


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

Function SetUp()

	_00E_MC_CaliaREF.UnequipItem(_01E_20Hueterschwert, True)
	_00E_MC_CaliaREF.RemoveItem(_01E_20Hueterschwert)
	_00E_MC_CaliaREF.UnequipItem(_01E_20Huetergrossschwert, True)
	_00E_MC_CaliaREF.RemoveItem(_01E_20Huetergrossschwert)
	_00E_MC_CaliaREF.SetOutfit(_00E_CQC06_CaliaDanceOutfit)
	MoveAndEnableCalia()
	
	CQC06_SC01_CaliaGoToTavern.ForceStart()
	
EndFunction

Function MoveAndEnableCalia()

	If _00E_MC_CaliaREF.IsDisabled()
		_00E_MC_CaliaREF.Enable()
	EndIf
	
	_00E_MC_CaliaREF.MoveTo(CQC06_SC01_CaliaRailMarker)

EndFunction

Function EnableBand()

	SetObjectiveCompleted(5)
	
	PlayerREF.EquipItem(_00E_InvisibleHelmet_Armor, false, true)
	Utility.Wait(0.1)
	PlayerREF.RemoveItem(_00E_InvisibleHelmet_Armor, 1, true)
	
	CQC06_SC01_EnableMarker.Disable()
	CQJ01_SC01_BreadREF.Disable()
	CQC06_SC01_MusicMarker.Enable()
	CQC06_SC01_Dancer01REF.SetAnimationVariableBool("bIsSynced", false)
	CQC06_SC01_Dancer02REF.SetAnimationVariableBool("bIsSynced", false)
	
EndFunction

Function StartAnimationsFailsave()

	Debug.SendAnimationEvent(CQC06_Lute_Player, "IdleLuteStart")
	Debug.SendAnimationEvent(CQC06_Drum_Player_01, "IdleDrumStart")
	Debug.SendAnimationEvent(CQC06_Drum_Player_02, "IdleDrumStart")
	Debug.SendAnimationEvent(CQC06_Bagpipe_Player, "Bagpipe")
	Debug.SendAnimationEvent(CQC06_SC01_SingerREF, "IdleFluteStart")

EndFunction

Function FadeOutAndPort()

	FadeToBlackIMOD.Apply()
	FadeToBlackHoldIMOD.ApplyCrossFade(afFadeDuration = 3.0)
	Utility.Wait(3)
	Sound.StopInstance(iTavernSong)
	_00E_MC_CaliaREF.MoveTo(CQC06_SC01_PortREF2)
	_00E_MC_CaliaREF.MoveTo(CQC06_SC01_CaliaBedMarker)
	PlayerREF.MoveTo(CQC06_SC01_BedREF2)
	CQC06_SC01_BedREF2.SetActorOwner(PlayerREF.GetActorBase())
	CQC06_DoorREF.Lock(False)
	_00E_MC_CaliaREF.UnequipAll()
	CQC06_SC01_MusicMarker.Disable()
	CQC06_SC01_EnableMarker.Enable()
	PlayerREF.UnequipAll()
	PlayerAIWalk()
	Debug.SendAnimationEvent(_00E_MC_CaliaREF, "returnToDefault")
	Debug.SendAnimationEvent(PlayerREF, "returnToDefault")
	
	CQC06_SC02_Room.ForceStart()
	
EndFunction

Function FadeBack()

	PlayerAIWalkStop()
	FadeToBlackBackIMOD.ApplyCrossFade()
	CQC06_SC01_BedREF2.Activate(PlayerREF, true)
	_00E_MC_CaliaREF.SetAnimationVariableBool("bIsSynced", false)
	
EndFunction

Function StartPlayerDance()

	Game.ForceThirdPerson()
	Game.DisablePlayerControls(true, true, true, false, true, true, true, true)
	Utility.Wait(1)
	PlayerREF.PlayIdle(IdleCiceroDance2)
	_00E_MC_CaliaREF.SetAnimationVariableBool("bIsSynced", true)
	
EndFunction

Function StartDancing()
	
	StartAnimationsFailsave()

EndFunction

Function CompleteAndGiveEP()

	Utility.Wait(3)
	Levelsystem.GiveEP(__Config_iRewardEXP)
	If _00E_AchievementsEnabled.GetValueInt() == 1
		Steam.UnlockAchievement("END_ROMANCE_CALIA_01")
	EndIf
	
EndFunction

Function RegisterForCleanUp()

	RegisterForSingleUpdateGameTime(6)

EndFunction

Function CleanUp()
	
	If PlayerREF.GetParentCell() == CapitalCityCommonTavern
		FadeToBlackIMOD.Apply()
		FadeToBlackHoldIMOD.ApplyCrossFade(afFadeDuration = 3.0)
		CQC06_SC01_EnableMarker.Enable()
		FadeToBlackBackIMOD.ApplyCrossFade(1)
	Else
		CQC06_SC01_EnableMarker.Enable()
	EndIf
	
EndFunction

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

bool bCleanedUp

int iTavernSong

int Property __Config_iRewardEXP = 500 Auto

_00E_QuestFunctions Property Levelsystem Auto

Armor Property _00E_InvisibleHelmet_Armor Auto

Actor Property _00E_MC_CaliaREF Auto
Actor Property PlayerREF Auto
Actor Property CQC06_SC01_Dancer01REF Auto
Actor Property CQC06_SC01_Dancer02REF Auto
Actor Property CQC06_Drum_Player_01 Auto
Actor Property CQC06_Drum_Player_02 Auto
Actor Property CQC06_Bagpipe_Player Auto
Actor Property CQC06_Lute_Player Auto

Cell Property CapitalCityCommonTavern Auto
Cell Property CapitalCityBar01 Auto

Weapon Property _01E_20Hueterschwert Auto
Weapon Property _01E_20Huetergrossschwert Auto

Idle Property IdleCiceroDance2 Auto
Idle Property IdleDrumStart Auto
Idle Property IdleLuteStart Auto

ObjectReference Property CQC06_SC01_CaliaRailMarker Auto
ObjectReference Property CQC06_SC01_EnableMarker Auto
ObjectReference Property CQC06_SC01_SingerREF Auto
ObjectReference Property CQC06_SC01_MusicMarker Auto
ObjectReference Property CQC06_SC01_BedREF2 Auto
ObjectReference Property CQC06_SC01_PortREF2 Auto
ObjectReference Property CQC06_SC01_CaliaBedMarker Auto
ObjectReference Property CQJ01_SC01_BreadREF Auto
ObjectReference Property CQC06_DoorREF Auto

Scene Property CQC06_SC01_CaliaGoToTavern Auto
Scene Property CQC06_SC02_Room Auto

Outfit Property _00E_CQC06_CaliaDanceOutfit Auto

ImageSpaceModifier Property FadeToBlackIMOD Auto
ImageSpaceModifier Property FadeToBlackHoldIMOD Auto
ImageSpaceModifier Property FadeToBlackBackIMOD Auto

GlobalVariable Property _00E_AchievementsEnabled Auto