/* The Soulstealer, an ammoless weapon that fires a ripping projectile in
 * exchange for 10 health, but heals for the damage inflicted if it kills
 * something.
 *
 * Based on the Stunner Rifle from Realm 667.  Original DECORATE by
 * Tormentor667.  GLDEFS by Keksdose.  Sprites and sounds belong to Raven
 * Software (though I don't know the specific source), with sprite
 * modifications by Eriance.
 */

actor Anachrony_Soulstealer : Weapon 13398
{
	//$Category Weapons
	//$Title Soulstealer
	Weapon.AmmoType None
	Weapon.AmmoUse 0
	Weapon.SlotNumber 2
	Weapon.SlotPriority 1
	weapon.selectionorder 0  // really bad to autoswitch to
	Inventory.PickupMessage "You got the Soulstealer!"
	Obituary "%o had %p soul stolen by %k"

	states
	{
	Spawn:
		STPP A -1
		loop
	Ready:
		STPO E 0 A_JumpIf(health <= 10, "Ready_NoAmmo")
		STPO AAABBBCCC 1 Bright A_WeaponReady
		loop
	Ready_NoAmmo:
		STPO E 0 A_JumpIf(health > 10, "Ready")
		STPO E 1 A_WeaponReady(WRF_NOFIRE)
		loop
	Select:
		STPO D 0 A_JumpIf(health <= 10, "Select_NoAmmo")
		STPO D 1 A_Raise
		loop
	Select_NoAmmo:
		STPO E 0 A_JumpIf(health > 10, "Select")
		STPO E 1 A_Raise
		loop
	Deselect:
		STPO D 0 A_JumpIf(health <= 10, "Deselect_NoAmmo")
		STPO D 1 A_Lower
		loop
	Deselect_NoAmmo:
		STPO E 0 A_JumpIf(health > 10, "Deselect")
		STPO E 1 A_Lower
		loop
	Fire:
		STPO F 0 Bright A_GunFlash
		STPO F 3 Bright A_FireCustomMissile("StunnerBall", 0, 1, 0, 0)
		STPO GH 3 Bright
		STPO D 16 Bright A_JumpIf(health <= 10, "Ready_NoAmmo")
		STPO D 6 Bright A_ReFire
		goto Ready
	Flash:
		TNT1 A 1 A_Light1
		TNT1 A 1 A_Light2
		TNT1 A 1 A_Light1
		TNT1 A 1 A_Light0
		stop
	}
}

// TODO i feel pretty strongly that this is not worth using atm.  should probably ALWAYS give health back, and just give a bonus (bigger ball?) if you get a kill
// TODO also maybe have a limited range on the ring
actor StunnerBall
{
	Radius 6
  Height 8
  Speed 30
  // Sprite is about 44x44, so this makes it 20x20
  Scale 0.45
  Damagetype leech
  Damage 0
  Decal StunnerScorch
  RenderStyle Add
  SeeSound "Weapons/StunnerFire"
  DeathSound "Weapons/StunnerHit"
  Translation "192:207=250:254"
  Projectile
  // TODO *should* this have RIPPER?  seems more interesting (and potentially
  // worth using), but i don't see how to get access to the actors being passed
  // through...  i guess A_CheckBlock?
  +Ripper
  +HITTRACER
  
  states
  {
  Spawn:
	STPR A 0 Bright
	STPR A 0 Bright A_DamageTarget(10, "leech", DMSS_FOILINVUL)
  Flight:
	STPR A 1 Bright A_SpawnItemEx("StunnerBallFX", 0, 0, 0, 0, 0, 0, 0, 128)
	STPR A 0 Bright A_CheckBlock("Damage", CBF_NOLINES | CBF_SETTRACER, AAPTR_DEFAULT)
	loop
  Damage:
	STPR A 0 Bright ACS_NamedExecuteAlways("anachrony_souleater_on_hit")
	goto Flight
  Death:
	// This script will calculate damage and fire an Anachrony_SoulMorsel with
	// an appropriate amount of health bundled in it
	//PLSE A 4 Bright ACS_NamedExecute("anachrony_souleater_on_hit")
	PLSE ABCDE 4 Bright
	stop
  }
}

Actor StunnerBallFX
{
  +NoBlockMap
  +NoGravity
  Scale 0.45
  RenderStyle Add
  Radius 1
  Height 2
  States
  {
  Spawn:
	STPR A 1 Bright A_FadeOut(0.125)
	STPR A 0 Bright A_SetScale(0.45 * alpha)
	Loop
  }
}

actor Anachrony_SoulMorsel
{
	Projectile
	Speed 10
	Radius 1
	Height 1
	+SEEKERMISSILE
	+NOBLOCKMAP
	+NOCLIP
	
	// Amount to heal by, set by the script that spawns us
	var int user_heal;
	// Size of the particle trail
	var int user_size;
	
	states
	{
	Spawn:
		// we're spawned with our parent's same pointers, so copy our target
		// (the player who fired the original projectile) into our tracer
		TNT1 A 0 NoDelay A_TransferPointer(AAPTR_DEFAULT, AAPTR_DEFAULT, AAPTR_TARGET, AAPTR_TRACER)
	Flight:
		TNT1 A 0 A_JumpIfTracerCloser(16.0, "RestoreHealth")
		// Spawn particles back along the distance we moved this tic
		TNT1 A 0 A_SpawnParticle(
			"ff 00 ff", SPF_RELATIVE | SPF_FULLBRIGHT, 10, user_size, 0,
			0.0, 0.0, 0.0,
			0.0, frandom(-0.5, 0.5), frandom(-0.5, 0.5))
		TNT1 A 0 A_SpawnParticle(
			"ff 00 ff", SPF_RELATIVE | SPF_FULLBRIGHT, 10, user_size, 0,
			-0.25 * speed, 0.0, 0.0,
			0.0, frandom(-0.5, 0.5), frandom(-0.5, 0.5))
		TNT1 A 0 A_SpawnParticle(
			"ff 00 ff", SPF_RELATIVE | SPF_FULLBRIGHT, 10, user_size, 0,
			-0.5 * speed, 0.0, 0.0,
			0.0, frandom(-0.5, 0.5), frandom(-0.5, 0.5))
		TNT1 A 0 A_SpawnParticle(
			"ff 00 ff", SPF_RELATIVE | SPF_FULLBRIGHT, 10, user_size, 0,
			-0.75 * speed, 0.0, 0.0,
			0.0, frandom(-0.5, 0.5), frandom(-0.5, 0.5))
		// And a darker particle for, uh, flavor
		TNT1 A 0 A_SpawnParticle(
			"a0 00 a0", SPF_RELATIVE | SPF_FULLBRIGHT, 20, user_size / 2, 0,
			0.0, 0.0, 0.0,
			0.0, frandom(-1.0, 1.0), frandom(-1.0, 1.0))
		TNT1 A 1 A_SeekerMissile(360.0, 360.0, SMF_PRECISE)
		loop
	RestoreHealth:
		// Careful!  The script's activator is nothing if there's no delay
		// before the actor is destroyed
		TNT1 A 1 ACS_NamedExecute("anachrony_souleater_heal", 0, user_heal)
		stop
	}
}
