Actor ResBFG1 : Weapon {
	Weapon.SelectionOrder 900
	Weapon.SlotNumber 7
    Inventory.PickupSound "weapons/pickup"
    Inventory.PickupMessage "You got the ion cannon!"	
	Weapon.AmmoType1 "IonAmmo"
	Weapon.AmmoType2 "IonOverheat"
	Weapon.AmmoUse1 0
	Weapon.AmmoGive 36
	Decal "BaronScorch"
	AttackSound ""
	+INVENTORY.UNDROPPABLE
	Tag "$WEP_7_4_TAG"
	Weapon.BobStyle InverseSmooth
	Weapon.BobSpeed 1.75
	Weapon.BobRangeY 0.5
	Weapon.BobRangeX 0.8
	+WEAPON.NOAUTOAIM
	States {
		Spawn:
			RW04 X -1
		Stop
		Ready:
			RW04 A 1 A_WeaponReady(WRF_NOSWITCH)
			TNT1 A 0 A_GiveInventory("DnD_OverheatCanReduce", 1)
			TNT1 A 0 ACS_NamedExecuteAlways("DnD Overheat Reduction", 0, DND_OVERHEAT_ION, 2 | (3 << 16))
		ReadyLoop:
			RW04 A 1 A_WeaponReady
		Loop
		Deselect:
			TNT1 A 0 A_StopSound(7)
			TNT1 A 0 A_TakeInventory("P_RedFire", 1)
			TNT1 A 0 A_TakeInventory("H_WeaponSlot7", 1)
			TNT1 A 0 A_TakeInventory("DnD_UsingEnergy", 1)
		DeselectLoop:
			RW04 A 1 A_Lower
			TNT1 A 0 A_Lower
		Loop
		Select:
			TNT1 A 0 A_GiveInventory("P_RedFire", 1)
			TNT1 A 0 A_GiveInventory("H_WeaponSlot7", 1)
			TNT1 A 0 A_GiveInventory("DnD_UsingEnergy", 1)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_0, 160, 0, DND_WEAPON_IONCANNON)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_1, 144, 0, DND_WEAPON_IONCANNON)
	    SelectLoop:
			RW04 A 1 A_Raise
			TNT1 A 0 A_Raise
		Loop
		Fire:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_IONCANNON, 0), "FireConfirmed")
			RW04 A 3 A_WeaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		FireConfirmed:
			RW04 A 1
			TNT1 A 0 A_TakeInventory("DnD_OverheatCanReduce", 1)
			RW04 B 9 A_PlayWeaponSound("IonCannon/Spin")
			RW04 C 8
			RW04 D 7
			RW04 A 6
			RW04 B 5
			RW04 C 4
			RW04 D 3
			TNT1 A 0 A_PlaySound("IonCannon/SpinLoop", 7, 1.0, 1)
		FireLoop:
			TNT1 A 0 A_JumpIfInventory("IonAttackCount", 3, "Fire4")
			TNT1 A 0 A_JumpIfInventory("IonAttackCount", 2, "Fire3")
			TNT1 A 0 A_JumpIfInventory("IonAttackCount", 1, "Fire2")
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_IONCANNON, 0), "FireCont1")
			RW04 A 1
		Goto WindDown4
		FireCont1:
			RW04 E 3 Bright A_GiveInventory("IonShooter", 1)
			RW04 F 2 Bright A_GiveInventory("IonShooter", 1)
			RW04 C 2
			TNT1 A 0 A_GiveInventory("IonAttackCount", 1)
			TNT1 A 0 A_Refire("FireLoop")
		Goto WindDown1
		Fire2:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_IONCANNON, 0), "FireCont2")
			RW04 D 1
		Goto WindDown1
		FireCont2:
			RW04 H 3 Bright A_GiveInventory("IonShooter", 1)
			RW04 E 2 Bright A_GiveInventory("IonShooter", 1)
			RW04 B 2
			TNT1 A 0 A_GiveInventory("IonAttackCount", 1)
			TNT1 A 0 A_Refire("FireLoop")
		Goto WindDown2
		Fire3:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_IONCANNON, 0), "FireCont3")
			RW04 C 1
		Goto WindDown2
		FireCont3:
			RW04 G 3 Bright A_GiveInventory("IonShooter", 1)
			RW04 H 2 Bright A_GiveInventory("IonShooter", 1)
			RW04 A 2
			TNT1 A 0 A_GiveInventory("IonAttackCount", 1)
			TNT1 A 0 A_Refire("FireLoop")
		Goto WindDown3
		Fire4:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_IONCANNON, 0), "FireCont4")
			RW04 B 1
		Goto WindDown3
		FireCont4:
			RW04 F 3 Bright A_GiveInventory("IonShooter", 1)
			RW04 G 2 Bright A_GiveInventory("IonShooter", 1)
			RW04 D 2
			TNT1 A 0 A_TakeInventory("IonAttackCount", 0)
			TNT1 A 0 A_Refire("FireLoop")
		Goto WindDown4
		WindDown1:
			TNT1 A 0 A_GiveInventory("IonCooldownGiver", 1)
			TNT1 A 0 A_GiveInventory("DnD_OverheatCanReduce", 1)
			TNT1 A 0 A_StopSound(7)
			TNT1 A 0 A_PlayWeaponSound("IonCannon/Slowdown")
			RW04 D 3 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 4 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 5 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 6 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 7 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 8 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 9 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 10 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 11 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
		Goto Ready
		WindDown2:
			TNT1 A 0 A_GiveInventory("IonCooldownGiver", 1)
			TNT1 A 0 A_GiveInventory("DnD_OverheatCanReduce", 1)
			TNT1 A 0 A_StopSound(7)
			TNT1 A 0 A_PlayWeaponSound("IonCannon/Slowdown")
			RW04 C 3 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 4 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 5 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 6 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 7 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 8 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 9 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 10 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 11 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 12 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
		Goto Ready
		WindDown3:
			TNT1 A 0 A_GiveInventory("IonCooldownGiver", 1)
			TNT1 A 0 A_GiveInventory("DnD_OverheatCanReduce", 1)
			TNT1 A 0 A_StopSound(7)
			TNT1 A 0 A_PlayWeaponSound("IonCannon/Slowdown")
			RW04 B 3 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 4 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 5 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 6 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 7 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 8 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 9 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 10 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
		Goto Ready
		WindDown4:
			TNT1 A 0 A_GiveInventory("IonCooldownGiver", 1)
			TNT1 A 0 A_GiveInventory("DnD_OverheatCanReduce", 1)
			TNT1 A 0 A_StopSound(7)
			TNT1 A 0 A_PlayWeaponSound("IonCannon/Slowdown")
			RW04 A 3 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 4 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 5 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 6 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 A 7 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 B 8 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 C 9 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
			RW04 D 10 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
		Goto Ready
		NoAction:
			RW04 A 3 A_WeaponReady(WRF_NOSWITCH | WRF_NOFIRE)
			TNT1 A 0 A_TakeInventory("IonOverheat", 2)
		Goto Ready
		Flash:
		  TNT1 A 4 A_Light1
		  TNT1 A 3 A_Light2
		  TNT1 A 0 A_Light0
		  stop
	}
}

Actor IonCooldown : PowerDamage {
	powerup.duration -3
	damagefactor "normal", 1.0
}

Actor IonCooldownGiver : DnD_Activator {
	States {
		Pickup:
			TNT1 A 0 A_JumpIfInventory("IonOverHeat", 0, "Give")
		Stop
		Give:
			TNT1 A 0 A_GiveInventory("IonCooldown", 1)
		Stop
	}
}

Actor IonAttackCount : Inventory {
	inventory.maxamount 3
}

Actor IonShooter : DnD_Activator {
	States {
		Pickup:
			TNT1 A 0 A_PlayWeaponSound("IonCannon/Fire")
			TNT1 A 0 A_SpawnItemEx("GunSmokeSmaller", 18 * cos(-pitch), 0, 36 * (1 + sin(-pitch)), 0, 0, 1)
			TNT1 A 0 ACS_NamedExecuteWithResult("DnD Fire Weapon", DND_WEAPON_IONCANNON, DND_ATK_PRIMARY, DND_AMMOSLOT_CELL | (AMMO_ION << 16), DND_ATF_NOAMMOGAINCHECK)
		Stop
	}
}

Actor IonPuff : DnD_BasePuff {
	DamageType "EnergyExp"
	+PUFFONACTORS
	+PUFFGETSOWNER
	+MTHRUSPECIES
	+FOILINVUL
	+THRUGHOST
	VSpeed 0
	
	Stamina 4096
	
	var int user_exprad;
	States {
		Spawn:
		Crash:
		HitNoBlood:
			TNT1 AAAA 0
			TNT1 A 0 A_SpawnItem("RedParticleSpawner")
			TNT1 A 0 A_CustomMissile("IonExplosion", 0, 0, 0, CMF_TRACKOWNER | CMF_AIMDIRECTION)
			IOPF A 3 bright A_PlaySound("IonCannon/Hit")
			IOPF BC 3 Bright
			IOPF D 2 Bright
			IOPF EF 2 Bright
		Stop
	}
}

Actor IonPuff_GhostHitter : IonPuff {
	-THRUGHOST
}

Actor IonExplosion : DnD_ExplosiveBase {
	Speed 0
	+EXTREMEDEATH
	+FORCERADIUSDMG
	+NODAMAGETHRUST
	var int user_count;
	States {
		SpawnState:
			TNT1 A 0
		Death:
			TNT1 A 0 A_SetUserVar("user_expdmg", 
				DND_WEAPON_IONCANNON |
				(DND_DMGID_1 << CACHE_SHIFT) |
				(DND_DAMAGETYPE_ENERGYEXPLOSION << DTYPE_SHIFT) |
				(100 << DPCT_SHIFT)
			)
			TNT1 A 0 A_SetUserVar("user_expradius", 160)
			TNT1 A 0 A_SetUserVar("user_fullexpradius", 0)
			TNT1 A 0 A_SetUserVar("user_expflags", DND_DAMAGEFLAG_BLASTSELF)
		ExplosionLoop:
			TNT1 A 0
		Goto DoExplosionDamage
		ContinueFromExplosion:
			TNT1 A 0 A_SetUserVar("user_expradius", user_expradius - 50)
			TNT1 A 3 A_SetUserVar("user_count", user_count - 1)
			TNT1 A 1 A_JumpIf(user_count > 0, "ExplosionLoop")
		Stop
	}
}

const int thunderbolt_range = 420;

Actor ResBFG2 : Weapon {
	Weapon.SelectionOrder 900
	Weapon.AmmoUse1 0
	Weapon.AmmoUse2 0
	Weapon.AmmoGive1 0
	Weapon.AmmoGive2 0
	Weapon.AmmoType "ThunderAmmo"
	Weapon.AmmoType2 "ThunderRangeCounter"
	Inventory.PickupMessage "you got the thunder staff!"
	Weapon.SlotNumber 7
	Tag "$WEP_7_5_TAG"
	Weapon.BobStyle InverseSmooth
	Weapon.BobSpeed 1.75
	Weapon.BobRangeY 0.4
	Weapon.BobRangeX 0.6
	+WEAPON.NOAUTOAIM
	States {
		Spawn:
			APOP A -1
		Stop
		Ready:
			APOF BB 1 Bright A_WeaponReady
			TNT1 A 0 A_PlaySound("ThunderStaff/Loop", 7, 1.0, 1)
			APOF CCDDEE 1 Bright A_WeaponReady
		Loop
		Deselect:
			TNT1 A 0 A_TakeInventory("P_GrayFire", 1)
			TNT1 A 0 A_TakeInventory("H_WeaponSlot7", 1)
		DeselectLoop:
			TNT1 A 0 A_StopSound(7)
			APOF A 1 A_Lower
			TNT1 A 0 A_Lower
		Loop
		Select:
			TNT1 A 0 A_GiveInventory("P_GrayFire", 1)
			TNT1 A 0 A_GiveInventory("H_WeaponSlot7", 1)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_0, 50, 5 | (10 << 16), DND_WEAPON_THUNDERSTAFF)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_1, 100, 0, DND_WEAPON_THUNDERSTAFF)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_2, 250, 0, DND_WEAPON_THUNDERSTAFF)
			TNT1 A 0 ACS_NamedExecuteWithResult("DND Weapon Damage Cache", DND_DMGID_3, 750, 0, DND_WEAPON_THUNDERSTAFF)
		SelectLoop:
			APOF A 1 A_Raise
			TNT1 A 0 A_Raise
		Loop
		Fire:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_THUNDERSTAFF, 0), "FireConfirmed")
			APOF B 2 Bright A_WeaponReady(WRF_NOFIRE)
			TNT1 A 0 A_PlaySound("ThunderStaff/Loop", 7, 1.0, 1)
			APOF CDE 2 Bright A_WeaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		FireConfirmed:
			APOF A 1 ACS_NamedExecuteWithResult("DnD Fire Weapon", DND_WEAPON_THUNDERSTAFF, DND_ATK_PRIMARY, DND_AMMOSLOT_CELL | (AMMO_THUNDER << 16), DND_ATF_NOAMMOGAINCHECK)
			TNT1 A 0 A_PlayWeaponSound("ThunderStaff/Fire")
			TNT1 A 0 A_SetBlend("LightBlue", 0.25, 18)
			APOF A 1 Offset(0, 36)
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Check Unique Player Property", PUP_KNOCKBACKIMMUNE), "NoRecoil")
			TNT1 A 0 A_Recoil(2.4)
		NoRecoil:
			APOF B 1 Bright Offset(0, 41)
			APOF B 1 Bright Offset(0, 47)
			APOF C 1 Bright Offset(0, 54)
			APOF C 1 Bright Offset(0, 58)
			APOF D 1 Bright Offset(0, 61)
			APOF D 1 Bright Offset(0, 63)
			APOF E 1 Bright Offset(0, 64)
			APOF E 1 Bright Offset(0, 65)
			APOF BCDBCDBCDBCD 2 Bright Offset(0, 66)
			APOF CC 1 Bright Offset(0, 65)
			APOF DD 1 Bright Offset(0, 59)
			APOF B 1 Bright Offset(0, 52)
			APOF B 1 Bright Offset(0, 44)
			APOF C 1 Bright Offset(0, 37)
			APOF C 1 Bright Offset(0, 35)
			APOF D 1 Bright Offset(0, 34)
			APOF D 1 Bright Offset(0, 33)
			APOF BC 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
			TNT1 A 0 A_Refire
			APOF D 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
			TNT1 A 0 A_Refire
			APOF E 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		DoNothing:
			TNT1 A 0 A_JumpIfInventory("ThunderHoldFrames", 2, "DoNothing_3")
			TNT1 A 0 A_JumpIfInventory("ThunderHoldFrames", 1, "DoNothing_2")
			TNT1 A 0 A_GiveInventory("ThunderHoldFrames", 1)
			APOF C 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		DoNothing_2:
			TNT1 A 0 A_GiveInventory("ThunderHoldFrames", 1)
			APOF D 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		DoNothing_3:
			TNT1 A 0 A_TakeInventory("ThunderHoldFrames", 2)
			APOF B 2 Bright A_weaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		AltFire:
			TNT1 A 0 A_JumpIf(ACS_NamedExecuteWithResult("DnD Can Fire Weapon", DND_WEAPON_THUNDERSTAFF, 1), "AltFireConfirmed")
			APOF B 2 Bright A_WeaponReady(WRF_NOFIRE)
			TNT1 A 0 A_PlaySound("ThunderStaff/Loop", 7, 1.0, 1)
			APOF CDE 2 Bright A_WeaponReady(WRF_NOFIRE | WRF_NOSWITCH)
		Goto Ready
		AltFireConfirmed:
			TNT1 A 0 A_TakeInventory("ThunderStopRing", 1)
			APOF A 1 ACS_NamedExecuteAlways("DND Thunder Ring", 0, 128)
		HoldAlt:
			TNT1 A 0 A_JumpIfInventory("ThunderRangeCounter", 0, "LetGoAlt")
			APOF A 2 A_GiveInventory("ThunderRangeCounter", 1)
			APOF A 1 A_Refire("HoldAlt")
		Goto LetGoAlt
		LetGoAlt:
			TNT1 A 0 A_PlayWeaponSound("ThunderStaff/Alt")
			TNT1 A 0 A_GiveInventory("ThunderStopRing", 1)
			TNT1 A 0 A_SetBlend("Gray", 0.33, 24)
			APOF A 1 Offset(0, 41)
			APOF A 1 Offset(0, 47)
			APOF A 1 Offset(0, 54)
			APOF A 1 Offset(0, 58)
			APOF A 1 Offset(0, 61)
			APOF A 1 Offset(0, 63)
			APOF A 1 Offset(0, 64)
			APOF A 1 Offset(0, 65)
			TNT1 A 0 A_TakeInventory("ThunderStopRing", 1)
			TNT1 A 0 ACS_NamedExecuteWithResult("DnD Fire Weapon", DND_WEAPON_THUNDERSTAFF, DND_ATK_SECONDARY, DND_AMMOSLOT_CELL | (AMMO_THUNDER << 16), DND_ATF_NOAMMOGAINCHECK)
			TNT1 A 0 A_Quake(6, 17, 0, 384)
			APOF A 1 Offset(0, 65)
			TNT1 A 0 ACS_NamedExecuteAlways("DND Thunderstaff Lightning", 0)
		ContinueAltAnim:
			APOF A 35 A_TakeInventory("ThunderRangeCounter", 0)
			APOF AAA 1 Offset(0, 59)
			APOF AA 1 Offset(0, 52)
			APOF A 1 Offset(0, 44)
			APOF A 1 Offset(0, 37)
			APOF A 1 Offset(0, 35)
			APOF A 1 Offset(0, 34)
			APOF A 1 Offset(0, 33)
			APOF BCD 2 Bright Offset(0, 32) A_WeaponReady(WRF_NOFIRE | WRF_NOSWITCH)
			APOF BCDBCDBCD 2 Bright A_WeaponReady(WRF_NOSECONDARY | WRF_NOSWITCH)
		Goto Ready
	}
}

Actor ThunderHoldFrames : Inventory {
	inventory.maxamount 2
}

Actor ThunderStopRing : DnD_Boolean { }

Actor ThunderRangeCounter : Ammo {
	inventory.amount 1
	inventory.maxamount 45
	ammo.backpackamount 0
	ammo.backpackmaxamount 45
	+IGNORESKILL
}

Actor ThunderRingMaker : DnD_Activator {
	States {
		Pickup:
			TNT1 A 0 A_RadiusGive("ThunderStrike", ACS_NamedExecuteWithResult("DND Thunder Range"), RGF_MONSTERS, 1)
		Stop
	}
}

Actor ThunderTarget : PowerProtection {
	damagefactor "normal", 1.0
	powerup.duration 3
}

Actor ThunderStrike : DnD_Info { }

Actor BallLightning : DnD_ExplosiveBase {
	Radius 8
	Height 16
	Speed 10
	Renderstyle Add
	Scale 0.75
	Decal Scorch
	ReactionTime 13
	
	Damage(
		DND_WEAPON_THUNDERSTAFF |
		(DND_DMGID_0 << CACHE_SHIFT) |
		(DND_DAMAGETYPE_LIGHTNING << DTYPE_SHIFT) |
		(100 << DPCT_SHIFT)
	)
	
	States {
		SpawnState:
			TNT1 A 0 A_CountDown
			ZAP7 A 1 Bright A_PlaySoundEx("ThunderStaff/Loop", "SoundSlot7", 1, -1)
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160)
			ZAP7 A 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 B 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160)
			ZAP7 B 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 C 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 C 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 A 0 A_RadiusGive("ThunderTarget", thunderbolt_range, RGF_MONSTERS, 1)
			TNT1 A 0 ACS_NamedExecuteAlways("DND Thunderstaff Bolts", 0, 0)
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 C 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160)
			ZAP7 D 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 D 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 E 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 E 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 AAA 0 Bright A_SpawnItemEx("SmallZapPuff", 0, Random(-12, 12), Random(-12, 12), 0, 0, 0, 0, 160) 
			ZAP7 E 1 Bright A_JumpIf(WaterLevel > 2, "Submerged")
			TNT1 A 0 A_RadiusGive("ThunderTarget", thunderbolt_range, RGF_MONSTERS, 1)
			TNT1 A 0 ACS_NamedExecuteAlways("DND Thunderstaff Bolts", 0)
		Loop
		Death:
			TNT1 A 0 A_StopSoundEx("SoundSlot7")
			TNT1 A 0 A_PlayWeaponSound("ThunderStaff/Impact")
			TNT1 AAAAA 0 A_SpawnItemEx("ZapPuff", Random(-16, 16), Random(-16, 16), Random(-16, 16), 0, 0, 0, 0, 160)
		
			TNT1 A 0 A_SetUserVar("user_expdmg", 
				DND_WEAPON_THUNDERSTAFF |
				(DND_DMGID_2 << CACHE_SHIFT) |
				(DND_DAMAGETYPE_LIGHTNING << DTYPE_SHIFT) |
				(100 << DPCT_SHIFT)
			)
			TNT1 A 0 A_SetUserVar("user_expradius", 128)
			TNT1 A 0 A_SetUserVar("user_fullexpradius", 0)
			TNT1 A 0 A_SetUserVar("user_expflags", DND_DAMAGEFLAG_BLASTSELF)
		Explosion:
			TNT1 A 0
		Goto DoExplosionDamage
		ContinueFromExplosion:
			ZAP7 ABCDE 2 Bright A_FadeOut(0.1)
		Loop
		Submerged:
			TNT1 A 0 A_Stop
			TNT1 A 0 A_StopSoundEx("SoundSlot7")
			TNT1 A 0 A_PlayWeaponSound("ThunderStaff/Impact")
			TNT1 AAAAA 0 A_SpawnItemEx("ZapPuff", Random(-16, 16), Random(-16, 16), Random(-16, 16), 0, 0, 0, 0, 160)
			
			TNT1 A 0 A_SetUserVar("user_expdmg", 
				DND_WEAPON_THUNDERSTAFF |
				(DND_DMGID_2 << CACHE_SHIFT) |
				(DND_DAMAGETYPE_LIGHTNING << DTYPE_SHIFT) |
				(500 << DPCT_SHIFT)
			)
			TNT1 A 0 A_SetUserVar("user_expradius", 256)
			TNT1 A 0 A_SetUserVar("user_expflags", DND_DAMAGEFLAG_BLASTSELF)
			TNT1 A 0 A_SetUserVar("user_fullexpradius", 0)
		Goto Explosion
	}
}

actor ZapPuff {
	Radius 2
	Height 2
	Renderstyle Add
	DamageType "Lightning"
	+NOINTERACTION
	+CLIENTSIDEONLY
	+PUFFONACTORS
	+ALWAYSPUFF
	States {
		Spawn:
			TNT1 A 1
			TNT1 A 0 A_Jump(256, "Puff1", "Puff2", "Puff3", "Puff4")
		Puff1:
			ZAP1 C 1 Bright A_FadeOut(0.1)
		Loop
		Puff2:
			ZAP1 D 1 Bright A_FadeOut(0.1)
		Loop
		Puff3:
			ZAP1 E 1 Bright A_FadeOut(0.1)
		Loop
		Puff4:
			ZAP1 F 1 Bright A_FadeOut(0.1)
		Loop
	}
}

actor SmallZapPuff : ZapPuff {
	Scale 0.5
	States {
		Spawn:
			TNT1 A 0 NoDelay Thing_ChangeTID(0, SPECIAL_FX_TID)
		Goto Super::Spawn
	}
}

Actor ThunderStaffLightningFX {
	Renderstyle Add
	Alpha 0.01
	xScale 1.25
	YScale 2.5
	Height 1
	Radius 1
	+NOINTERACTION
	+CLIENTSIDEONLY
	States {
		Spawn:
			TNT1 A 0 NoDelay A_PlaySound("ThunderStaff/Thunder")
			TNT1 AAAAAAAA 0 A_SpawnItemEx("RocketSmoke", 0, 0, 0, frandom(-2, 2), frandom(-2, 2), frandom(-2 ,2), 0, SXF_NOCHECKPOSITION)
			TNT1 A 1
			TNT1 A 0 A_Jump(256, "1", "2", "3", "4", "5")
		1:
			ZOT1 A 0
		Goto Vanish
		2:
			ZOT1 B 0
		Goto Vanish
		3:
			ZOT1 C 0
		Goto Vanish
		4:
			ZOT1 D 0
		Goto Vanish
		5:
			ZOT1 E 0
		Goto Vanish
		Vanish:
			"####" "###" 1 Bright A_FadeIn(0.33)
			"####" "#" 1 Bright
		VanishLoop:
			"####" "#" 1 Bright A_FadeOut(0.085)
		Loop
	}
}

Actor ThunderStaffRingAfterFX {
	Scale 0.35
	Renderstyle Add
	Alpha 1.0
	+NOINTERACTION
	+CLIENTSIDEONLY
	States {
		Spawn:
			TNT1 A 0 NoDelay Thing_ChangeTID(0, SPECIAL_FX_TID)
			TNT1 A 2
			ZAP7 AABBCCDDEE 1 A_FadeOut(0.1)
		Stop
	}
}

Actor ThunderStaffRingFX {
	Scale 0.35
	Renderstyle Add
	Alpha 1.0
	Projectile
	Speed 0
	Damage 0
	+THRUACTORS
	+CLIENTSIDEONLY
	+NOCLIP
	States {
		Spawn:
			TNT1 A 2
		SpawnLoop:
			ZAP7 AABBCCDDEE 2 Bright A_SpawnItemEx("ThunderStaffRingAfterFX")
		Loop
		Fade:
			ZAP7 AAABBBCCCDDDEEE 1 Bright A_FadeOut(0.075)
		Stop
	}
}