//Uses the first argument to spawn a specific corpse, as fitting to the area.
//If 0, it spawns a random corpse. If 1, spawns a dead player. If 2, spawns a dead Golem.
//If 3, spawns a dead Weredragon. If 4, spawns a dead Ophidian. If 5, does not spawn a corpse.
//I might add more corpses, but I think this is good for now.

ACTOR Swarm 20013
{
	//$Category Monsters
	//$Title Swarm Corpse
	//$Sprite PLAYP0
	MONSTER
	+FLOAT
	+NOGRAVITY
	+LOOKALLAROUND
	-SOLID
	+CANTSEEK
    +Ghost
    +NoBloodDecals
    BloodType "SwarmBlood"
	Health 80
	Mass 0x7FFFFFFF
	Radius 24
	Height 48
	Speed 10
	DeathSound "Swarm/Death"
	RenderStyle None
	States
	{
	Spawn:
		TNT1 A 0
		TNT1 A 0 A_JumpIfInventory ("SwarmGibbed", 1, "SpawnedAlready")
		TNT1 A 0 A_GiveInventory("SwarmBodyDumper", 1)
	SpawnedAlready:
		TNT1 AA 4 A_Look
		goto SpawnedAlready
	See:
		TNT1 A 0 A_JumpIfInventory ("SwarmGibbed", 1, "SeeGibbed")
		TNT1 A 0 A_JumpIfCloser (256, "SeeGib")
		TNT1 A 4
		goto See
	SeeGib:
		TNT1 A 0 A_SpawnItemEX ("SwarmGibber", 0, 0, 0, 0, 0, 0, 0, SXF_NOCHECKPOSITION)
        TNT1 A 0 A_PlaySoundEx("Swarm/Fly", "SoundSlot7", 1)
		TNT1 AAAAAAAAAAAAAAAAAAAAAAA 1 A_CustomMissile("SwarmFly", 15, 0, random (0, 360), 2, random (0, 60))
		TNT1 A 0 A_GiveInventory ("SwarmGibbed", 1)
		goto SeeGibbed
	SeeGibbed:
		TNT1 AAAA 4 A_Chase
		Loop
	Melee:
		TNT1 A 4 A_CustomMeleeAttack (3, "", "", Normal, 1)
		goto SeeGibbed
	Death:
		TNT1 A 0 A_StopSoundEx("SoundSlot7")
		TNT1 A 0 A_Fall
		TNT1 A 0 A_Scream
		TNT1 A 0 A_TakeInventory ("SwarmGibbed", 1)
		TNT1 A 35
		Stop
	}
}

//Corpses the Swarm spawns

Actor SwarmBodyDumper : CustomInventory
{
  +Inventory.AutoActivate
  Inventory.MaxAmount 0
  States
  {
  Spawn:
    TNT1 A 1
    Fail
  Use:
    TNT1 A 0 A_JumpIf(Args[0] == 1, "DeadPlayer")
    TNT1 A 0 A_JumpIf(Args[0] == 2, "DeadGolem")
    TNT1 A 0 A_JumpIf(Args[0] == 3, "DeadWeredragon")
    TNT1 A 0 A_JumpIf(Args[0] == 4, "DeadOphidian")
    TNT1 A 0 A_JumpIf(Args[0] > 4, "NoCorpse")
  DeadRandom:
    TNT1 A 0 A_SpawnItemEx("SwarmBodyRandom", 0, 0, 0, 0, 0, 0, 0, 128)
    Stop
  DeadPlayer:
    TNT1 A 0 A_SpawnItemEx("SwarmBodyPlayer", 0, 0, 0, 0, 0, 0, 0, 128)
    Stop
  DeadGolem:
    TNT1 A 0 A_SpawnItemEx("SwarmBodyGolem", 0, 0, 0, 0, 0, 0, 0, 128)
    Stop
  DeadWeredragon:
    TNT1 A 0 A_SpawnItemEx("SwarmBodyWeredragon", 0, 0, 0, 0, 0, 0, 0, 128)
    Stop
  DeadOphidian:
    TNT1 A 0 A_SpawnItemEx("SwarmBodyOphidian", 0, 0, 0, 0, 0, 0, 0, 128)
    Stop
  NoCorpse:
    TNT1 A 0
    Stop
  }
}

Actor SwarmBodyRandom
{
  Radius 1
  Height 1
  +NoClip
  States
  {
  Spawn:
    TNT1 A 0
    TNT1 A 0 A_Jump (256, "deadPlayer", "DeadGolem", "DeadWeredragon", "DeadOphidian")
  DeadPlayer:
    PLAY P -1
    Stop
  DeadGolem:
    MUMM P -1
    Stop
  DeadWeredragon:
    BEAS Z -1
    Stop
  DeadOphidian:
    SNKE P -1
    Stop
  }
}

Actor SwarmBodyPlayer : SwarmBodyRandom
{
  States
  {
  Spawn:
    PLAY P -1
    Stop
  }
}

Actor SwarmBodyGolem : SwarmBodyRandom
{
  States
  {
  Spawn:
    MUMM P -1
    Stop
  }
}

Actor SwarmBodyWeredragon : SwarmBodyRandom
{
  States
  {
  Spawn:
    BEAS Z -1
    Stop
  }
}

Actor SwarmBodyOphidian : SwarmBodyRandom
{
  States
  {
  Spawn:
    SNKE P -1
    Stop
  }
}

//Swarm's fly stuff

ACTOR SwarmFly
{
	Radius 1
	Height 1
	Speed 5
	PROJECTILE
	+NOCLIP
    +ClientSideOnly
	ReactionTime 4
	States
	{
	Spawn:
		SFLY AB 2 ThrustThingZ(0, random (-1, 1), random (1, 0), 1)
		SFLY A 0 A_CountDown
 		Loop
	Death:
		SFLY A 0
		SFLY A 0 A_JumpIfInTargetInventory ("SwarmGibbed", 1, "SpawnNewFly")
		TNT1 A 35 A_SpawnItemEX ("DeadFly", 0, 0, 0)
		Stop
	Crash:
		SFLY A 0 A_JumpIfInTargetInventory ("SwarmGibbed", 1, "SpawnNewFly")
		TNT1 A 35 A_SpawnItemEX ("DeadFly", 0, 0, 0)
		Stop
	SpawnNewFly:
		SFLY A 2 A_CustomMissile("SwarmFly", 0, 0, random (-60, 60))
		stop
	}
}

ACTOR DeadFly
{
  Radius 1
  Height 1
  States
  {
  Spawn:
    TNT1 A 0
    TNT1 A 0 A_Jump(128, "Spawn2")
    SFLY A 1 A_FadeOut(0.01)
    Loop
  Spawn2:
    SFLY B 1 A_FadeOut(0.01)
    Loop
  }
}

ACTOR SwarmGibbed : Inventory
{
	Inventory.MaxAmount 1
}

ACTOR SwarmGibber
{
	+NOCLIP
    +ClientSideOnly
	Radius 1
	Height 1
	States
	{
	Spawn:
		TNT1 A 0
		TNT1 A 35 A_SpawnDebris ("SwarmGibs")
		Stop
	}
}

ACTOR SwarmGibs
{
	Health 20
	Radius 1
	Height 1
	States
	{
	Spawn:
		SGIB A -1
		SGIB B -1
		SGIB C -1
		SGIB D -1
		SGIB E -1
		SGIB F -1

	}
}

Actor SwarmBlood //This is just to prevent a console error :P
{
  +NoInteraction
  States
  {
  Spawn:
    TNT1 A 1
    Stop
  }
}