Scriptname TW_SCRIPT_WeaponSetup extends ObjectReference  

Import Game

GLOBALVARIABLE Property GVDamage Auto

QUEST Property TW_Q01_ThrowQuest Auto

BOOL Property SneakGrenade Auto

EXPLOSION Property GrenadeExplosion Auto

SPELL Property GrenadeSpell Auto

MESSAGE Property ActorExplosionMessage Auto
MESSAGE Property ContainerExplosionMessage Auto

WEAPON Property SetupWeapon Auto
WEAPON Property SetupWeaponHidden Auto

;OBJECTREFERENCE ContainerSource

ACTOR Target

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)	

	If akNewContainer == Game.GetPlayer() && (GVDamage != None)
		if (SetupWeapon)
			SetupWeapon.SetBaseDamage(GVDamage.GetValue() as Int)
			SetupWeaponHidden.SetBaseDamage(GVDamage.GetValue() as Int)
		endif
	EndIf
	 
	If (SneakGrenade)
		PocketBomb(akOldContainer, akNewContainer)
	EndIf
EndEvent

Event OnEquipped(Actor akActor)

	If (!TW_Q01_ThrowQuest.IsRunning())
		TW_Q01_ThrowQuest.Start()
	EndIf
	
	If akActor == Game.GetPlayer() && (GVDamage != None)
		if (SetupWeapon)
			SetupWeapon.SetBaseDamage(GVDamage.GetValue() as Int)
			SetupWeaponHidden.SetBaseDamage(GVDamage.GetValue() as Int)
		endif
	EndIf
EndEvent

Function PocketBomb(ObjectReference OldContainer, ObjectReference NewContainer)
	If (OldContainer == Game.GetPlayer()) && (NewContainer != Game.GetPlayer()) && (NewContainer != None) 
		Target = Game.FindClosestActorFromRef(NewContainer, 0.1)
		If (Target != None) && (Target.IsDead() == False)
			Int WaitTime = ActorExplosionMessage.Show()
			;Wait 3 Seconds
			If (WaitTime == 0)
				Utility.Wait(3.0)
				PocketExplosion()
			;Wait 5 Seconds
			ElseIf (WaitTime == 1)
				Utility.Wait(5.0)
				PocketExplosion()
			;Wait 10 Seconds
			ElseIf (WaitTime == 2)
				Utility.Wait(10.0)
				PocketExplosion()
			;Wait 30 Seconds
			ElseIf (WaitTime == 3)
				Utility.Wait(30.0)
				PocketExplosion()
			;Wait 60 Seconds
			ElseIf (WaitTime == 4)
				Utility.Wait(60.0)
				PocketExplosion()
			;Do not explode
			ElseIf (WaitTime == 5)
				Return
			EndIf	
		Else
			Int Option1 = ContainerExplosionMessage.Show()
			If (Option1 == 0)
				Utility.Wait(1.0)
				ContainerExplosion(NewContainer)
			ElseIf (Option1 == 1)
				Return
			EndIf
		EndIf
	EndIf
EndFunction

Function ContainerExplosion(ObjectReference ContainerSource)
	Actor TriggerTarget = Game.FindClosestActorFromRef(ContainerSource, 160)
	If TriggerTarget == None || TriggerTarget == Game.GetPlayer()
		Utility.Wait(1.0)
		ContainerExplosion(ContainerSource)
	Else
		ContainerSource.PlaceAtMe(GrenadeExplosion)
		GrenadeSpell.Cast(TriggerTarget, TriggerTarget)
		ContainerSource.RemoveItem(SetupWeapon, 1)
	EndIf
EndFunction

Function PocketExplosion()
	Target.PlaceAtMe(GrenadeExplosion)
	GrenadeSpell.Cast(Target, Target)
	Target.RemoveItem(SetupWeapon, 1)
EndFunction