Scriptname _00E_PlaceableGrammophonPlay extends ObjectReference  

Sound Property _00E_MUS_Placeable_Koppophon Auto
MusicType Property _00E_SilenceTransitionHighPriority Auto ; OBSOLETE. Left for version update on older saves.
MusicType Property _00E_SilenceKoppophon Auto
GlobalVariable Property _00E_AchievementsEnabled Auto
Message Property _00E_PlayerhousingKoppophonVolume Auto
Actor Property PlayerREF Auto

Int playbackId = -1
Float volume = 0.5
Bool bDone ; Unlocked END_KOPPOPHON_01 achievement
Bool bMusicLocked = False
Bool bOldSilenceUpdated = False

Float Property MUSIC_LENGTH = 87.2 AutoReadOnly


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

Function StopMusic()
	bMusicLocked = True

	UnregisterForModEvent("BardsLoadGame")
	UnregisterForUpdate()

	If bOldSilenceUpdated == False
		bOldSilenceUpdated = True
		If playbackId != -1
			_00E_SilenceTransitionHighPriority.Remove()
		EndIf
	EndIf

	If playbackId != -1
		Sound.StopInstance(playbackId)
		playbackId = -1
	EndIf

	_00E_SilenceKoppophon.Remove()

	bMusicLocked = False
EndFunction

Function TryStopMusic()
	If bMusicLocked == False
		StopMusic()
	Else
		RegisterForSingleUpdate(0.5)
	EndIf
EndFunction

Function Cleanup()
	GoToState("DoneCleanup")
	TryStopMusic()
EndFunction


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

Event OnInit()
	GoToState("DoneCleanup")
EndEvent

Event OnActivate(ObjectReference triggerRef)
	If triggerRef == PlayerREF && _00E_PlayerhousingMaster.ReferenceCanBeActivated(Self) && bMusicLocked == False
		bMusicLocked = True

		If playbackId == -1 ; Start playing
			bOldSilenceUpdated = True
			RegisterForModEvent("BardsLoadGame", "OnBardsLoadGame")

			_00E_SilenceKoppophon.Add()

			playbackId = _00E_MUS_Placeable_Koppophon.Play(Self)
			If volume <= 0.0 || volume > 1.0
				volume = 0.5
			EndIf
			Sound.SetInstanceVolume(playbackId, volume)
			RegisterForSingleUpdate(_00E_QuestFunctions.AdjustTimePeriodByEngineTimerError(MUSIC_LENGTH))

			GoToState("")

			_00E_PlayerhousingKoppophonVolume.Show(volume * 100.0)

			If _00E_AchievementsEnabled.GetValueInt() == 1 && (bDone == False)
				bDone = True
				Steam.UnlockAchievement("END_KOPPOPHON_01")
			EndIf

		ElseIf bOldSilenceUpdated == False 
			; Player just loaded an older save with the Koppophon playing. The sound is dead by now.
			StopMusic()

		Else ; Volume control
			Float fNewVolume = volume + 0.25
			If fNewVolume > 1.0
				fNewVolume = 0.0
			EndIf

			Sound.SetInstanceVolume(playbackId, fNewVolume)
			If fNewVolume == 0.0 ; On mute
				_00E_SilenceKoppophon.Remove()
			ElseIf volume == 0.0 ; On unmute
				_00E_SilenceKoppophon.Add()
			EndIf
			volume = fNewVolume

			_00E_PlayerhousingKoppophonVolume.Show(volume * 100.0)

		EndIf

		bMusicLocked = False
	EndIf
EndEvent

Event OnUpdate()
	TryStopMusic()
EndEvent

Event OnBardsLoadGame(string eventName, string strArg, float numArg, Form sender)
	; Player just loaded a save with the Koppophon playing. The sound is dead by now.
	TryStopMusic()
EndEvent

Event OnCellAttach()
	Cleanup()
EndEvent

Event OnLoad()
	Cleanup()
EndEvent

Event OnCellDetach()
	Cleanup()
EndEvent

Event OnUnload()
	Cleanup()
EndEvent

State DoneCleanup
	Event OnCellAttach()
		; Do nothing
	EndEvent

	Event OnLoad()
		; Do nothing
	EndEvent

	Event OnCellDetach()
		; Do nothing
	EndEvent

	Event OnUnload()
		; Do nothing
	EndEvent
EndState