ACTOR ChestKey : CustomInventory
{
  Inventory.Icon "I_SPBKEY"
  Inventory.PickupMessage "Chest Key"
  Inventory.PickupSound "SupplyChest/Key"
  Inventory.UseSound "supplychest/open"
  Inventory.InterHubAmount 0x7FFFFFFF
  Inventory.MaxAmount 0x7FFFFFFF
  +INVBAR
  +NEVERRESPAWN
  Tag "Chest key"
  States
  {
  Spawn:
    SBKY A -1
    Stop
  Pickup:
	TNT1 A 0 A_JumpIfInventory("ChestKey", ACS_ExecuteWithResult(WOC_DECORATE,STATS,ITEMCAP,ACTIVATOR),"Failed")
	TNT1 A 0 ACS_ExecuteWithResult(WOC_DECORATE,STATS,ITEMCAP,ACTIVATOR)
	Stop
  Failed:
	TNT1 A 0
	Fail
  Use:
	TNT1 A 0
	Fail
  }
}

Actor SupplyChestRandomSpawning : RandomSpawner
{
	+DROPPED
	+NEVERRESPAWN
	DropItem "Money3" 128 5
	DropItem "Money2" 128 5
	DropItem "ArtiAquaFlask" 128 5
	DropItem "ArtiBlackFlask" 128 5
	DropItem "ArtiPurpleFlask" 128 5
	DropItem "ArtiWhiteFlask" 128 5
	DropItem "ArtiOrangeFlask" 128 5
	DropItem "NewArtiSuperHealth" 128 5
	DropItem "NewArtiHealingRadius" 128 5
	DropItem "NewArtiBoostMana" 128 4
	DropItem "NewArtiTeleport" 128 4
	DropItem "NewArtiSpeedBoots" 128 4
	DropItem "NewArtiPork" 128 3
	DropItem "NewArtiEgg" 128 3	
	DropItem "ForceCube" 128 3
	DropItem "NewArtiDarkServant" 128 2
	DropItem "AccessoryRandomSpawnerDropped" 128 2
	DropItem "NewArtiInvulnerability2" 128 1
}

Actor AccessoryRandomSpawnerDropped : AccessoryRandomSpawner { +DROPPED }

Actor SupplyChestSpawner : Inventory
{
  Inventory.RespawnTics 0x7FFFFFFF
  +NEVERRESPAWN
  +Inventory.Quiet
  +Inventory.AlwaysPickup
  Inventory.MaxAmount 0
  States
  {
  Spawn:
    TNT1 A 0
	TNT1 A 0 A_Jump(1,"Mimic")
	TNT1 A 0 A_Jump(32,"Gold")
	TNT1 A 0 A_SpawnItemEx("SupplyChestIron", 0, 0, 0, 0, 0, 0, 0, SXF_NOCHECKPOSITION)
    Goto PermaLoop
  Mimic:
	TNT1 A 0 A_SpawnItemEx("SupplyChestMimic", 0, 0, 0, 0, 0, 0, 0, SXF_NOCHECKPOSITION)
	Goto PermaLoop
  Gold:
	TNT1 A 0 A_SpawnItemEx("SupplyChestGold", 0, 0, 0, 0, 0, 0, 0, SXF_NOCHECKPOSITION)
	Goto PermaLoop
  PermaLoop:
    TNT1 A -1
    Loop
  }
}

Actor SupplyChestMapRandomSpawner : RandomSpawner 20010
{
	DropItem "SupplyChestMimic" 255 1
	DropItem "SupplyChestIron" 255 221
	DropItem "SupplyChestGold" 255 32
}

ACTOR SupplyChestMimic 20012
{
	Height 32
	Radius 16
	Health 1500
	+USESPECIAL
	+NOBLOOD
	+NOTARGETSWITCH
	+NONSHOOTABLE
	+NODAMAGETHRUST
	+USEKILLSCRIPTS
	MeleeRange 52
	Activation THINGSPEC_ThingTargets
	Tag "Mimic"
	States
	{
	Spawn:
		SUBX A 0
		SUBX A 0 ACS_NamedExecuteAlways("WOC Monster Init",0,32,0)//increase health on higher difficulties
	Idle:
		SUBX A 0 A_ChangeFlag("ISMONSTER",0)
		SUBX A 0 A_ChangeFlag("USESPECIAL",1)
		SUBX A 0 A_JumpIfInTargetInventory("ChestKey",1,"Activated")
		SUBX A 0 A_GiveToTarget("SupplyChestActivator")
		SUBX A 1 A_ClearTarget
		Loop
	Activated:
		SUBX A 0 A_ChangeFlag("USESPECIAL",0)
	    SUBX A 3
		SUBX B 5
		SUBX A 0 A_SetShootable
		SUBX A 0 A_ChangeFlag("ISMONSTER",1)
		SUBX A 2 A_PlaySound("SupplyChest/Close", 4.0)
	See:
		SUBX A 0 A_PlaySound("SupplyChest/Close", 4.0)
		SUBX A 1 A_Chase
		Goto See+1
	Missile:
		SUBX B 6 A_FaceTarget
		SUBX B 0 A_FaceTarget
		SUBX B 0 ThrustThingZ(0,20,0,1)
		SUBX B 0 A_FaceTarget
		SUBX B 0 A_SkullAttack(25)
		SUBX BBBBBBBBBBBBBBBBBBBB 1 A_JumpIfTargetInsideMeleeRange("MeleeStrike")
		SUBX B 0 A_Gravity
		SUBX B 0 A_Stop
		Goto See
	MeleeStrike:
		SUBX A 0 A_FaceTarget
		SUBX A 0 A_PlaySound("SupplyChest/Close", 4.0)
		SUBX A 15 A_CustomMeleeAttack(ACS_NamedExecuteWithResult("WOC Monster Damage Scale",ACTIVATOR,random(26,46)))
		Goto See+1
	Death:
	    SUBX B 0 A_PlaySound("SupplyChest/Break", 4.0)
		SUBX BBBBBBBBBBBB 0 A_SpawnItemEx("SupplyChestRandomSpawning",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
		SUBX BBBBBBBBBBBB 0 A_SpawnItemEx("Money1",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,128)
		SUBX BBBBB 0 A_SpawnItemEx("Money2",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
		SUBX B 0 A_UnSetSolid
		SUBX B 1 A_FadeOut
		Wait
	}
}

ACTOR SupplyChestIron 20011
{
	Height 32
	Radius 16
	Tag "Supply chest"
	+USESPECIAL
	Activation THINGSPEC_ThingTargets
	States
	{
	Spawn:
	    SUBX A 0
		SUBX A 0 A_JumpIfInTargetInventory("ChestKey",1,"Open")
		SUBX A 0 A_GiveToTarget("SupplyChestActivator")
		SUBX A 1 A_ClearTarget
		Loop
	Open:
		SUBX B 0 A_TakeFromTarget("ChestKey",1)
		SUBX A 3
		SUBX B 5 A_PlaySound("SupplyChest/Open")
		SUBX B 0 A_Jump(32,"Arrow","Gas","Fireballs")
	Reward:
		SUBX BBBBBBBBBBBB 0 A_SpawnItemEx("SupplyChestRandomSpawning",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
		SUBX BBBBBBBBBBBB 0 A_SpawnItemEx("Money1",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,128)
		SUBX BBB 0 A_SpawnItemEx("Money2",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
		SUBX B -1 A_UnSetSolid
		Stop
	Gas:
		SUBX B 0 A_SpawnItemEx ("ChestGasTrap", 16.0, 8.0, 36.0, 0.0, 0.0, 0.0, 0.0, 32, 0)
		SUBX B 0 A_SpawnItemEx ("ChestGasTrap", 24.0, 0.0, 36.0, 0.0, 0.0, 0.0, 0.0, 32, 0)
		SUBX B 0 A_SpawnItemEx ("ChestGasTrap", 16.0,-8.0, 36.0, 0.0, 0.0, 0.0, 0.0, 32, 0)
		Goto Reward
	Arrow:
		SUBX B 0 A_CustomMissile("ChestArrowTrap",28,0,-45,1)
		SUBX B 0 A_CustomMissile("ChestArrowTrap",28,0,45,1)
		SUBX B 0 A_CustomMissile("ChestArrowTrap",28,0,135,1)
		SUBX B 0 A_CustomMissile("ChestArrowTrap",28,0,225,1)
		Goto Reward
	Fireballs:
		SUBX BBBBBBBBBB 0 A_CustomMissile("ChestFireballTrap",28,0,random(-180,180))
		Goto Reward
	}
}


Actor ChestArrowTrap : Dart replaces Dart
{
	+THRUGHOST
	+WINDTHRUST
	+NODAMAGETHRUST
	Damagetype "Physical"
	Speed 20
	Radius 6
	Height 4
	Scale 0.5
	Damage (random(1,8)*6*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)))
	states
	{
	Spawn:
		ARRO A 1
		Loop
	Death:
		ARRO A 1
		Stop
	}
}

Actor ChestGasTrap
{
	Radius 20
	Height 30
	Mass 0x7fffffff
	+NOBLOCKMAP
	+NOGRAVITY
	+DROPOFF
	+NODAMAGETHRUST
	+DONTSPLASH
	+FOILINVUL
	+CANBLAST
	+BLOODLESSIMPACT
	+FLOATBOB
	//+MISSILE
	+RIPPER
	+BLOODLESSIMPACT
	+DONTBLAST
	PoisonDamage 1000000
	RenderStyle Translucent
	Alpha 0.6
	DeathSound "PoisonShroomDeath"
	DamageType Poison
	States
	{
	Spawn:
		PSBG D 3 A_ChangeFlag("DONTBLAST", 0)
		PSBG D 1 A_Scream
		PSBG DEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		PSBG EEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		PSBG EEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		PSBG EEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		PSBG EEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		PSBG EEEFFFGGGHHHIIIEEEFFFGGGHHHIIIEEEFFFGGGHHHIII 2 A_Explode (4*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)), 40, 1)
		Goto Death
	Death:
    	PSBG HG 7
    	PSBG FD 6
    	Stop
	}
}

ACTOR ChestFireballTrap
{
	Speed 14
	Radius 8
	Height 8
	Damage (random(15,30)*0.025*(40+acs_executewithresult(WOC_DECORATE,STATS,GLOBALLEVEL,0)))
	DamageType Fire
	PROJECTILE
	-NOGRAVITY
	RenderStyle Add
	DeathSound "Fireball"
	States
	{
	Spawn:
		FBL1 AB 4 Bright
		Loop
	Death:
		XPL1 ABCDEF 4 Bright
		Stop
	}
}

Actor SupplyChestGoldSpawner : Inventory
{
  Inventory.RespawnTics 0x7FFFFFFF
  +NEVERRESPAWN
  +Inventory.Quiet
  +Inventory.AlwaysPickup
  Inventory.MaxAmount 0
  States
  {
  Spawn:
    TNT1 A 0
	TNT1 A 0 A_SpawnItemEx("SupplyChestGold", 0, 0, 0, 0, 0, 0, 0, SXF_NOCHECKPOSITION)
  PermaLoop:
    TNT1 A 1
    Loop
  }
}

ACTOR SupplyChestGold 20013
{
  Height 32
  Radius 16
  Tag "Supply chest"
  +USESPECIAL
  Activation THINGSPEC_ThingTargets
  States
  {
  Spawn:
    SUBX C 0
    SUBX C 0 A_JumpIfInTargetInventory("ChestKey",1,"Open")
	SUBX C 0 A_GiveToTarget("SupplyChestActivator")
	SUBX C 1 A_ClearTarget
	Loop
  Open:
    SUBX C 0 
    SUBX C 0 A_TakeFromTarget("ChestKey",1)
    SUBX C 3
	SUBX D 5 A_PlaySound("SupplyChest/Open")
	SUBX CCCCCCCCCCCCCCCCCCCCCCCC 0 A_SpawnItemEx("SupplyChestRandomSpawning",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
	SUBX CCCCCCCCCCCCCCCCCCCCCCCC 0 A_SpawnItemEx("Money2",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,128)
	SUBX CCCCCC 0 A_SpawnItemEx("Money3",0,0,0,Random(100,400)/100.0,Random(100,400)/100.0,Random(375,900)/100.0,Random(1,360),0,0)
	SUBX D -1 A_UnSetSolid
    Stop
  }
}

Actor SupplyChestActivator : CustomInventory
{
	Inventory.MaxAmount 0
	+Inventory.AutoActivate
	Inventory.Icon "TNT1A0"
	States
	{
	Use:
		TNT1 A 0 A_PlaySound("SupplyChest/Locked")
		TNT1 A 0 A_Print("You need a Chest Key to open this!")
		Stop
	}
}