Scriptname DCF_ManuallyUnlockLockScript extends ObjectReference

Bool Property InsideActivator Auto ;'Enable property only for the activator inside the facility.'
ObjectReference xDoor01 ;'This variable is empty until the script is initialized the fist time, so you need a saved game where you never loaded this mod before'
Int OpenDoorTimer = 111 ;'This variable stores a random number to identify the timer I used to automatically close and lock the door after 5 seconds of being open by the player'
Sound Property xSoundFX01 Auto
Sound Property xSoundFX02 Auto
Message Property xMessage01 Auto
Message Property xMessage02 Auto

Event OnInit() ;'this event is triggered the first time this script is loaded, which occurs when the cell that contains the activator is loaded for the first time'
	xDoor01 = GetLinkedRef() ; 'this function stores in to the variable the ObjectReference of the door linked to this activator'
	If(xDoor01) ;'this is checking that the variable contains an actual ObjectReference, if a None value is stored then the "else" statement is executed.'
		xDoor01.BlockActivation(abBlocked = True, abHideActivateText = True)
		xDoor01.Lock(abLock = True, abAsOwner = False)
	Else
		Debug.Trace("ERROR: The activator has no linked door")
	EndIf
EndEvent

Event OnActivate(ObjectReference akAction)
	If(xDoor01);'If theres a linked reference'
		If(xDoor01.IsLocked());'if the reference is locked'
			If(Game.GetPlayer().GetItemCount(xDoor01.GetKey()) >= 1 || InsideActivator) ;'Checks if the player has the key of the linked door in the inventory'
				xDoor01.Unlock(abAsOwner = False);'Unlock the door'
				xDoor01.BlockActivation(abBlocked = False, abHideActivateText = False);'enable activation for the door'
					Debug.Notification("Unlocked with the Factory Secret Room Key")
					SetActivateTextOverride(xMessage01)
					int instanceID = xSoundFX02.play(self)
					Sound.SetInstanceVolume(instanceID, 0.3)
			Else
					Debug.Notification("Only Factory Secret Room Key can unlock")
					int instanceID = xSoundFX01.play(self)
					Sound.SetInstanceVolume(instanceID, 0.3)
			EndIf
		Else ;'if the reference is unlocked, as oposite to the if statement being true.'
				If !(xDoor01.GetOpenState() > 2) ;'If NOT(closed(3) or closing(4))'
					xDoor01.SetOpen(abOpen = False);'Close it'
				EndIf
				xDoor01.Lock(abLock = True, abAsOwner = False);'Lock it'
				xDoor01.BlockActivation(abBlocked = True, abHideActivateText = True);'Avoid activation and hide activation text.'
					Debug.Notification("Locked with the Factory Secret Room Key")
					SetActivateTextOverride(xMessage02)
					int instanceID = xSoundFX02.play(self)
					Sound.SetInstanceVolume(instanceID, 0.3)
		EndIf
	Else
		Debug.Notification("ERROR: The activator has no linked door")
	EndIf
EndEvent