scriptName TrapExplosiveGas extends objectReference

formlist property flExplodesGasTrapOnHit auto
{projectiles that will set this off when they hit}

formlist property flExplodesGasTrapOnEnter auto
{objects that will set this off when they enter}

formlist property flExplodesGasTrapOnMagicEffectApply auto
{magicEffects that will set this off when applied}

formlist property flExplodesGasTrapEquippedSpell auto
{Spells that will set this off if equipped and drawn}

formlist property trapGasWeapon auto
{weapons that will set this off on hit, specifically explosions}

bool property checkActorEquippedItems = TRUE auto
{Whether or not to check items the player is carrying
	default = True on explosive gas
	set to off on oil pool}
bool property checkActorMagic = TRUE auto
{check the actors drawn magic
	default = true on explosive gas
	set to off on oil pool}

keyword property flameKeyword Auto
keyword property lightningKeyword auto	
bool property lightningIgnites = TRUE Auto
{if this is true lightning should ignite this trap
	default == true}
	
light property Torch01 auto
projectile property storedProjectile auto hidden
MagicEffect property storedEffect1 auto hidden
MagicEffect property storedEffect2 auto hidden
MagicEffect property storedEffect3 auto hidden
MagicEffect property storedEffect4 auto hidden
int property storedEffectIncrement =  1 auto hidden

auto state waiting
	;Start of the new script
	event onHit(objectReference akAggressor, form akWeapon, projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
		if !akProjectile || akProjectile != storedProjectile
			storedProjectile = akProjectile
			
			;USKP 1.3.2 added NONE checks due to numerous Papyrus errors.
			If( (trapGasWeapon != NONE && trapGasWeapon.hasForm(akWeapon)) || akweapon == torch01 )
				GasExplode(akAggressor)
			ElseIf( flExplodesGasTrapOnHit != NONE && flExplodesGasTrapOnHit.hasForm(akProjectile) )
				GasExplode(akAggressor)
			ElseIf akWeapon.HasKeyword(flameKeyword) || ( lightningIgnites && akWeapon.HasKeyword(lightningKeyword) )
				GasExplode(akAggressor)
			endif
		endif
	endEvent
	
	event OnMagicEffectApply(ObjectReference akCaster, MagicEffect akEffect)
		if akEffect == storedEffect1 || akEffect == storedEffect2 || akEffect == storedEffect3 || akEffect == storedEffect4
			;if the effect == any of the stored effects do nothing
		else
			if storedEffectIncrement == 1
				storedEffect1 = akEffect
				storedEffectIncrement = 2
			elseif storedEffectIncrement == 2
				storedEffect2 = akEffect
				storedEffectIncrement = 3
			elseif storedEffectIncrement == 3
				storedEffect3 = akEffect
				storedEffectIncrement = 4
			else
				storedEffect4 = akEffect
				storedEffectIncrement = 1
			endif
			if flExplodesGasTrapOnMagicEffectApply.hasForm(akEffect as form)
				GasExplode(akCaster)
			elseif akEffect.hasKeyword(flameKeyword)
				GasExplode(akCaster)
			elseif lightningIgnites && akEffect.hasKeyword(lightningKeyword)
				GasExplode(akCaster)
			endif
		endif
	endEvent
	

	event onTriggerEnter(objectReference triggerRef)
		if ! triggerRef
			return
		endif
		
		Actor triggerActor = triggerRef as Actor
	
		if !triggerActor
			if flExplodesGasTrapOnEnter.hasForm(triggerRef.GetBaseObject())
				GasExplode(triggerRef)
			endif
			return
		endif
	
		if checkActorEquippedItems && checkActorWeapons(triggerActor)
			return
		endif
		
		if checkActorMagic
			If triggerActor.hasMagicEffectWithKeyword(flameKeyword)
				GasExplode(triggerRef)
			ElseIf lightningIgnites
				if triggerActor.hasMagicEffectWithKeyword(lightningKeyword)
					GasExplode(triggerRef)
				endif
			endif
		endif
	endEvent
	
EndState

function GasExplode(objectReference causeActor)
	self.setActorCause(causeActor as actor)
	self.damageObject(5)
endFunction

bool function checkActorWeapons(actor triggerActor)
	; Checking if Torch is equipped
	if triggerActor.GetEquippedItemType(0) == 11 || triggerActor.GetEquippedItemType(1) == 11
		GasExplode(triggerActor)
		return true
	endif
	If triggerActor.IsWeaponDrawn()
		if IsIgnitingSpell(triggerActor.GetEquippedSpell(1)) || IsIgnitingSpell(triggerActor.GetEquippedSpell(0))
			GasExplode(triggerActor)
			return true
		endif
	endif
	return false
endFunction

bool function IsIgnitingSpell(Spell aSpell)
	return aSpell && ( aSpell.hasKeyword(flameKeyword) || ( lightningIgnites && aSpell.hasKeyword(lightningKeyword) ) || flExplodesGasTrapEquippedSpell.hasForm(aSpell as form) )
endfunction

event onReset()
	self.reset()
	self.clearDestruction()
	goToState("waiting")
endEvent

event OnDestructionStageChanged(int aiOldStage, int aiCurrentStage)
	;
endEvent