Scriptname _00E_ChestAndDoorLockScript extends ObjectReference  

;=====================================================================================
;              							GOLDBUFF                 					 
;=====================================================================================

; Use this to control how much the lockpicking skill increments the gold found in chest. When changing, remember to change in _00E_LootContainer.psc as well!
; Current calculation: Gold in chest*((Lockpicking/iGoldMultiplicator)/100)
int iGoldMultiplicator = 5
int iCurrentGoldCount
float fPlayerLockpicking

bool bDone

GlobalVariable Property _00E_FS_IsForgottenStoriesActivated Auto

Message Property _00E_FS_LockpickingGoldBuffMSG Auto

MiscObject Property Gold001 Auto

Event OnOpen(ObjectReference akActionRef)
	
	If _00E_FS_IsForgottenStoriesActivated == None ; just in case, all properties in all doors and containers should be filled by now
		_00E_FS_IsForgottenStoriesActivated = Game.GetForm(0x0004320E) as GlobalVariable
	EndIf

	if !bDone && (Self.GetBaseObject().GetType() == 28) && _00E_FS_IsForgottenStoriesActivated.GetValueInt() == 1 ; check if it's a container otherwise the script tries to call this stuff on doors
	
		bDone = True
		iCurrentGoldCount = Self.GetItemCount(Gold001)
		fPlayerLockpicking = PlayerREF.GetActorValue("Lockpicking")
		if iCurrentGoldCount >= 5 && fPlayerLockpicking >= 15
			IncrementGold()
		EndIf
			
	EndIf
	
EndEvent

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

Function IncrementGold()

	float fIncrementPercentage = (fPlayerLockpicking/iGoldMultiplicator)/100
	int iGoldBuffAmount = (iCurrentGoldCount*fIncrementPercentage) as Int
	
	Self.AddItem(Gold001, iGoldBuffAmount)
	
	if iGoldBuffAmount > 1
		_00E_FS_LockpickingGoldBuffMSG.Show(iGoldBuffAmount as Int)
	EndIf
	
EndFunction

;=====================================================================================
;              							ORIGINAL SCRIPT                 					 
;=====================================================================================

Event OnLoad()
	
	If _00E_FS_IsForgottenStoriesActivated == None ; just in case, all properties in all doors and containers should be filled by now
		_00E_FS_IsForgottenStoriesActivated = Game.GetForm(0x0004320E) as GlobalVariable
		Gold001 = Game.GetFormFromFile(0x0000000F, "Skyrim.esm") as MiscObject
		_00E_FS_LockpickingGoldBuffMSG =  Game.GetFormFromFile(0x0001ED74, "Enderal - Forgotten Stories.esm") as Message
		_00E_sDoorLocked = Game.GetForm(0x00141570) as Message
		_00E_Game_UnlockNeedsSkill = Game.GetForm(0x00042AE0) as Message
	EndIf

	BlockActivation(True)
	
EndEvent

Int Property LOCK_LEVEL_NOVICE 	   = 1 AutoReadOnly
Int Property LOCK_LEVEL_APPRENTICE = 25 AutoReadOnly
Int Property LOCK_LEVEL_ADEPT 	   = 50 AutoReadOnly
Int Property LOCK_LEVEL_EXPERT 	   = 75 AutoReadOnly
Int Property LOCK_LEVEL_MASTER     = 254 AutoReadOnly

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

	If akAggressor != PlayerREF || IsLocked() == False
		Return
	EndIf

	Int projectileLevel = 0

	If akProjectile == _00E_UnlockProjectile_Novice
		projectileLevel = LOCK_LEVEL_NOVICE
	ElseIf akProjectile == _00E_UnlockProjectile_Apprentice
		projectileLevel = LOCK_LEVEL_APPRENTICE
	ElseIf akProjectile == _00E_UnlockProjectile_Adept
		projectileLevel = LOCK_LEVEL_ADEPT
	ElseIf akProjectile == _00E_UnlockProjectile_Expert
		projectileLevel = LOCK_LEVEL_EXPERT
	ElseIf akProjectile == _00E_UnlockProjectile_Master
		projectileLevel = LOCK_LEVEL_MASTER
	ElseIf akSource
		Formlist unlockSpells = Game.GetFormFromFile(0x00044EE4, "Skyrim.esm") as Formlist
		If unlockSpells.HasForm(akSource)
			If akSource == unlockSpells.GetAt(0) as Explosion
				projectileLevel = LOCK_LEVEL_APPRENTICE
			ElseIf akSource == unlockSpells.GetAt(1) as Explosion
			projectileLevel = LOCK_LEVEL_ADEPT
			ElseIf akSource == unlockSpells.GetAt(2) as Explosion
				projectileLevel = LOCK_LEVEL_EXPERT
			ElseIf akSource == unlockSpells.GetAt(3) as Explosion
				projectileLevel = LOCK_LEVEL_MASTER
			EndIf
		EndIf
	EndIf

	If projectileLevel > 0
		Int lockLevel = self.GetLockLevel()
		If lockLevel > LOCK_LEVEL_MASTER
			_00E_sDoorLocked.Show()
			WPNTG06ControlStaffShootFailM.Play(Self)
		ElseIf projectileLevel >= lockLevel
			UnlockLock()
		EndIf
	EndIf

EndEvent

Bool Function _IsInSuntemple()
	If _00E_SuntempleLocations
		Location curLoc = GetCurrentLocation()
		If curLoc && _00E_SuntempleLocations.HasForm(curLoc)
			Return True
		EndIf
	EndIf

	Return False
EndFunction

Bool Function _IsInSiege()
	Quest qSiegeQuest = Game.GetForm(0x0002EBAD) as Quest
	Int siegeStage = qSiegeQuest.GetStage()
	Return (siegeStage >= 5 && siegeStage <= 125)
EndFunction

Bool Function _PlayerHasKey()
	Key myKey = GetKey()
	Return (myKey && PlayerREF.GetItemCount(myKey) > 0)
EndFunction

Event OnActivate(ObjectReference akActionRef)

	If akActionRef == PlayerREF
		If _IsInSiege() && (Self.GetBaseObject().GetType() != 29 || _IsInSuntemple() == False)
			_00E_sDoorLocked.Show()
		ElseIf IsActivationBlocked()
			If IsLocked() && _PlayerHasKey() == False
				Bool bPlayerCanActivate = False
				Int iLockLevel = GetLockLevel()
				If iLockLevel <= LOCK_LEVEL_APPRENTICE
					bPlayerCanActivate = True
				ElseIf iLockLevel <= LOCK_LEVEL_ADEPT
					bPlayerCanActivate = (PlayerREF.HasPerk(_00E_Class_Trickery_P04b_) || PlayerREF.GetAV("Lockpicking") >= 25)
				ElseIf iLockLevel <= LOCK_LEVEL_EXPERT
					bPlayerCanActivate = (PlayerREF.HasPerk(_00E_Class_Trickery_P07_) || PlayerREF.GetAV("Lockpicking") >= 50)
				ElseIf iLockLevel <= LOCK_LEVEL_MASTER
					bPlayerCanActivate = (PlayerREF.HasPerk(_00E_Class_Trickery_P10_) || PlayerREF.GetAV("Lockpicking") >= 75)
				Else
					bPlayerCanActivate = True
				EndIf

				If bPlayerCanActivate
					Activate(PlayerREF, True)
				Else
					_00E_Game_UnlockNeedsSkill.Show()
				EndIf
			Else
				BlockActivation(False)
				Activate(PlayerREF, True)
			EndIf
		EndIf
	Else
		Activate(akActionRef, True)
	EndIf

EndEvent

Function UnlockLock()

	Self.Lock(False)
	_00E_UILockpickingUnlockM.Play(Self)
	_00E_Ability_Antimagic_FXSShader.Play(Self)
	self.PlaceAtMe(_00E_UnlockLockExplosion, 1)

EndFunction

Formlist Property _00E_SuntempleLocations Auto

Projectile Property _00E_UnlockProjectile_Novice Auto
Projectile Property _00E_UnlockProjectile_Apprentice Auto
Projectile Property _00E_UnlockProjectile_Adept Auto
Projectile Property _00E_UnlockProjectile_Expert Auto
Projectile Property _00E_UnlockProjectile_Master Auto

Message Property _00E_sDoorLocked Auto

Perk Property _00E_Class_Trickery_P04b_ Auto ; Unlock adept locks perk
Perk Property _00E_Class_Trickery_P07_ Auto ; Unlock expert locks
Perk Property _00E_Class_Trickery_P10_ Auto ; Unlock master locks

Sound Property _00E_UILockpickingUnlockM Auto
Sound Property WPNTG06ControlStaffShootFailM Auto

EffectShader Property _00E_Ability_Antimagic_FXSShader Auto
Explosion Property _00E_UnlockLockExplosion Auto

Actor Property PlayerREF Auto

Message Property _00E_Game_UnlockNeedsSkill Auto