/* Lightly tweaks some of the Doom weapons.  Or not so lightly.
 */
 
// PISTOL
// The Doom pistol is so worthless that once you have literally anything else,
// it's not worth using any more.  So let's make it fire about 1/3 faster,
// do slightly more damage, and have a tighter spread than the chaingun.
//
// Actually, disregard all that, we're just gonna disable the pistol for now.
actor Anachrony_BulletCasing
{
	scale 0.1666
	radius 3
	height 3
	speed 6
	mass 1
	bouncefactor 0.5
	PROJECTILE
	-NOGRAVITY
	-NOBLOCKMAP
	+DOOMBOUNCE
	seesound "weapons/casing"

	states {
	Spawn:
		BCAS A 35
	Death:
		BCAS AAA 35
		BCAS A 7 A_FadeOut(0.05)
		wait
	}
}

/*
Unused: pistol that ejects bullet casings
TODO do that to the silenced pistol as well
actor Anachrony_Pistol : Pistol replaces Pistol
{
	// Not inherited
	weapon.slotnumber 2
	weapon.slotpriority 1
	decal BulletChip
	
	+WEAPON.CHEATNOTWEAPON
	
	states {
	Fire:
		PISG A 3
		PISG A 0 A_SpawnItemEx("Anachrony_BulletCasing", 0, -16, 32, frandom(-5, -1), frandom(-4, 0), frandom(0, 1), frandom(-75, -85))
		PISG B 0 A_FireBullets(2.8, 2.8, -1, 6, "BulletPuff")
		PISG B 0 A_PlaySound("weapons/pistol", CHAN_WEAPON)
		PISG B 5 A_GunFlash
		PISG C 3
		PISG B 5 A_ReFire
		goto Ready
	Flash:
		PISF A 6 Bright A_Light1
		goto LightDone
	}
}
*/


// Give the shotgun slightly more power: 9 pellets (up from 7) and 20% faster.
// Also spawn the OG shell casings.
actor AnachronyShellCasing
{
	scale 0.1666
	radius 3
	height 3
	speed 6
	mass 1
	bouncefactor 0.5
	PROJECTILE
	-NOGRAVITY
	-NOBLOCKMAP
	+DOOMBOUNCE
	seesound "weapons/casing"

	states {
	Spawn:
		SCAS A 35
	Death:
		SCAS AAA 35
		SCAS A 7 A_FadeOut(0.05)
		wait
	}
}
actor AnachronyShotgun : Shotgun replaces Shotgun
{
	// This stuff is not inherited, whoops
	weapon.slotnumber 3
	weapon.slotpriority 0
	decal BulletChip

	states {
	Fire:
		SHTG A 3
		SHTG A 0 A_SpawnItemEx("AnachronyShellCasing", -4, -16, 32, random(-5, -1), random(-3, 1), random(1, 2), random(-75, -85))
		SHTG A 0 A_FireBullets(5.6, 0, 9, 5, "BulletPuff")
		SHTG A 0 A_PlaySound("weapons/shotgf", CHAN_WEAPON)
		SHTG A 5 A_GunFlash

		goto super::Fire +2
	}
}

actor Anachrony_SuperShotgun : SuperShotgun replaces SuperShotgun
{
	// Not inherited
	weapon.slotnumber 3
	weapon.slotpriority 1
	decal BulletChip
	
	states {
	Fire:
		SHT2 A 3
		SHT2 A 0 A_SpawnItemEx("AnachronyShellCasing", -4, -16, 32, random(-5, -1), random(-3, 1), random(1, 2), random(-75, -85))
		SHT2 A 0 A_SpawnItemEx("AnachronyShellCasing", -4, -16, 32, random(-5, -1), random(-3, 1), random(1, 2), random(-75, -85))
		// Identical to default behavior
		SHT2 A 0 A_FireBullets(11.2, 7.1, 20, 5, "BulletPuff")
		SHT2 A 0 A_PlaySound("weapons/sshotf", CHAN_WEAPON)
		SHT2 A 7 A_GunFlash
		goto super::Fire+2
	}
}

// MINIGUN (chaingun replacement)
// Firing rate is dependent on the wielder's movement speed.  Running at full
// tilt will fire at the same rate as the chaingun; walking will fire 25%
// faster; standing still will fire 50% faster; and standing still for a
// couple seconds will fire at maximum possible speed.
// There's also a slight vertical spread, and a spin up/down delay.
// The main fire no longer works as a sniper rifle, but the alt fire will
// shoot several perfectly accurate (and slow) bullets.
actor AnachronyMinigun : Weapon replaces Chaingun 13399
{
	weapon.slotnumber 4
	weapon.slotpriority 10
	weapon.ammotype1 "Clip"
	weapon.ammotype2 "Clip"
	weapon.ammouse1 1
	weapon.ammouse2 4
	weapon.ammogive 20
	inventory.pickupsound "misc/w_pkup"
	inventory.pickupmessage "You got the minigun!"
	decal BulletChip
	obituary "%o was mowed down by %k's minigun."
	attacksound "weapons/minigunfire"
	states {
	Spawn:
		MNGN A -1
		loop
	Ready:
		MNGG A 1 A_WeaponReady
		loop
	Deselect:
		MNGG A 1 A_Lower
		loop
	Select:
		MNGG A 1 A_Raise
		loop
	Fire:
		MNGG A 0 A_SetUserVar("user_stood_still_for", 0)
		MNGG B 5
		MNGG A 3
		MNGG B 2
	Hold:
		MNGG A 0 A_SetArg(4, CallACS("anachrony_update_minigun_speed"))
		MNGG A 0 A_GunFlash
		MNGG A 0 A_SpawnItemEx("Anachrony_BulletCasing", 0, -16, 32, frandom(-5, -1), frandom(-4, 0), frandom(0, 1), frandom(-75, -85))
		MNGG A 0 A_FireBullets(5, 1, 1, 5)
		MNGG A 5 A_SetTics(args[4])
		MNGG A 0 A_SpawnItemEx("Anachrony_BulletCasing", 0, -16, 32, frandom(-5, -1), frandom(-4, 0), frandom(0, 1), frandom(-75, -85))
		MNGG B 0 A_FireBullets(5, 1, 1, 5)
		MNGG B 5 A_SetTics(args[4])
		MNGG B 0 A_ReFire
		MNGG B 2 A_WeaponReady(WRF_NOBOB|WRF_NOSWITCH|WRF_NOPRIMARY)
		MNGG A 3
		MNGG B 5
		goto Ready
	AltFire:
		MNGG A 0 A_SetArg(4, 3)
		MNGG A 0 A_GunFlash
		MNGG A 3 A_FireBullets(0, 0, 3, 5)
		MNGG B 4
		MNGG A 6
		MNGG B 9
		goto Ready
	Flash:
		MNGF A 0 BRIGHT A_Light1
		MNGF A 5 BRIGHT A_SetTics(args[4])
		MNGF B 0 BRIGHT A_Light0
		MNGF B 5 BRIGHT A_SetTics(args[4])
		stop
	}
}
/*
Preserved for posterity, and because it was surprisingly tricky to get
working: a minigun that slows you down while you're firing it, and can be
manually spun up with alt-fire, much like TF2's Heavy.
actor PowerAnachronyMinigunSlowdown : PowerSpeed
{
	powerup.duration 0x7FFFFFFD
	speed 0.6
	+POWERSPEED.NOTRAIL
}

actor AnachronyMinigunSlowdown : PowerupGiver
{
	powerup.type PowerAnachronyMinigunSlowdown
}
actor AnachronyMinigun : Weapon 13399
{
	weapon.slotnumber 4
	weapon.ammotype "Clip"
	weapon.ammogive 20
	weapon.ammouse 1
	inventory.pickupsound "misc/w_pkup"
	inventory.pickupmessage "Picked up a minigun!"
	decal BulletChip
	obituary "%o was shredded by %k's minigun."
	attacksound "weapons/minigunfire"
	states {
	Spawn:
		MNGN A -1
		loop
	Ready:
		MNGG A 1 A_WeaponReady
		loop
	Deselect:
		MNGG A 1 A_TakeInventory("PowerAnachronyMinigunSlowdown", 255)
		MNGG A 1 A_Lower
		wait
	Select:
		MNGG A 1 A_Raise
		loop
	Fire:
		MNGG A 0 A_JumpIfInventory("PowerAnachronyMinigunSlowdown", 1, "Hold")
		MNGG A 0 A_GiveInventory("PowerAnachronyMinigunSlowdown")
		MNGG B 8
		MNGG A 6
		MNGG B 3
	Hold:
		MNGG A 0 A_GunFlash
		MNGG A 0 A_SpawnItemEx("Anachrony_BulletCasing", 0, -16, 32, frandom(-5, -1), frandom(-4, 0), frandom(0, 1), frandom(-75, -85))
		MNGG A 3 A_FireBullets(5, 1, 1, 5)
		MNGG A 0 A_SpawnItemEx("Anachrony_BulletCasing", 0, -16, 32, frandom(-5, -1), frandom(-4, 0), frandom(0, 1), frandom(-75, -85))
		MNGG B 3 A_FireBullets(5, 1, 1, 5)
		MNGG B 0 A_ReFire
		MNGG B 3 A_WeaponReady(WRF_NOBOB|WRF_NOSWITCH|WRF_NOPRIMARY)
		MNGG A 6
		MNGG B 8
		MNGG A 0 A_TakeInventory("PowerAnachronyMinigunSlowdown", 255)
		goto Ready
	AltFire:
		MNGG A 0 A_JumpIfInventory("PowerAnachronyMinigunSlowdown", 1, "AltHold")
		MNGG A 0 A_GiveInventory("PowerAnachronyMinigunSlowdown")
		// Used to keep the weapon bobbing while it spins up; the bob lasts about
		// half a second
		MNGG B 8 A_WeaponReady(WRF_NOSWITCH|WRF_NOFIRE)
		MNGG A 6
		MNGG B 3
	AltHold:
		MNGG AAABBB 1 A_WeaponReady(WRF_NOSWITCH|WRF_NOSECONDARY)
		MNGG B 0 A_ReFire
		MNGG B 3
		// Used for bob
		MNGG A 6 A_WeaponReady(WRF_NOSWITCH|WRF_NOFIRE)
		MNGG B 8
		MNGG A 0 A_TakeInventory("PowerAnachronyMinigunSlowdown", 255)
		goto Ready
	Flash:
		MNGF A 3 BRIGHT A_Light1
		MNGF B 3 BRIGHT A_Light0
		stop
	}
}
*/

// ROCKET LAUNCHER
// Alt-fire launches a lazy homing rocket.  Costs three ammo, has recoil,
// refires more slowly, and does less damage.  Good for picking off jerks on
// faraway ledges.
actor Anachrony_SeekerRocket : Rocket
{
	+SEEKERMISSILE
	+SCREENSEEKER
	speed 16
	damage 16
	damagetype rocket
	maxtargetrange 32  // look for target up to 2048u away
	
	states {
	Spawn:
		MISL A 6 Bright A_SeekerMissile(0, 2, SMF_LOOK|SMF_PRECISE)
	}
}

actor Anachrony_RocketLauncher : RocketLauncher replaces RocketLauncher
{
	// Non-inherited
	weapon.slotnumber 5
	weapon.slotpriority 0

	weapon.ammotype2 RocketAmmo
	weapon.ammouse2 3
	
	states {
	AltFire:
		MISG B 8
		MISG B 8 A_GunFlash
		MISG B 0 A_Recoil(5 * cos(pitch))
		MISG B 32 A_FireCustomMissile("Anachrony_SeekerRocket")
		MISG B 0 A_ReFire
		goto Ready
	}
}

// PLASMA GUN
// Alt-fire is borrowed from the Ionspray: rapidly eject lots of tiny energy
// pellets.  The difference is that these ones are RIPPER, meaning they pass
// through enemies.  Cool way to clear a room, but eats ammo like mad.
actor Anachrony_IonBall
{
	radius 13
	height 8
	speed 26
	// RIPPER projectiles continue to do damage while passing through enemies,
	// and we fire a LOT of these, so we need them to do very little damage!
	damage (random(0, 3))
	projectile
	+RIPPER
	renderstyle Add
	alpha 0.45
	scale 0.35
	decal RailScorchLower
	obituary "%o was ionized by %k"
	states {
	Spawn:
		TNT1 A 2
		IONB A 1 Bright
		wait
	Death:
		IONB BCDEFGHIJKL 2 Bright
		stop
	}
}

// TODO what i really want is that the gun only changes types while you're holding altfire, but i need some kinda animation for that i think?
actor Anachrony_PlasmaRifle : PlasmaRifle replaces PlasmaRifle
{
	// Non-inherited
	weapon.slotnumber 6
	weapon.slotpriority 0

	weapon.ammotype2 Cell
	weapon.ammouse2 1

	//+WEAPON.CHEATNOTWEAPON

	states {
	Select:
		// Only count damage received while the plasma gun is out, i.e.,
		// reset the last damage tracker upon switching to it.
		PLSG A 0 A_SetUserVar("user_last_damage", 0)
		goto super::Select
	Deselect:
		PLSG A 0 A_StopSoundEx("SoundSlot7")
		goto super::Deselect
	Fire:
		// There's no way to read user variables from a weapon (?!), so we
		// have to dip into ACS and store the value in args[0].
		PLSG A 0 A_SetArg(0, CallACS("anachrony_retrieve_player_last_damage"))
		PLSG A 0 A_LogInt(args[0])
		PLSG A 0 A_JumpIf(args[0] == 1, "Fire.Fire")
		PLSG A 0 A_JumpIf(args[0] == 2, "Fire.Slime")
		PLSG A 0 A_JumpIf(args[0] == 3, "Fire.Electric")
		PLSG A 0 A_JumpIf(args[0] == 4, "Fire.Hitscan")
		PLSG A 0 A_JumpIf(args[0] == 5, "Fire.Ice")
	Fire.Normal:
		PLSG A 0 A_GunFlash("Flash.Plasma")
		PLSG A 3 A_FireCustomMissile("PlasmaBall")
		PLSG B 20 A_ReFire("Fire.Normal")
		goto Ready
	Flash.Plasma:
		PLSF A 0 Bright A_Jump(128, 1)
		PLSF A 4 Bright A_Light1
		stop
		PLSF B 4 Bright A_Light1
		stop
	Fire.Fire:
		PLSG A 0 A_GunFlash("Flash.Fire")
		PLSG A 3 A_FireCustomMissile("Anachrony_FirePlasmaBall")
		PLSG B 20 A_ReFire("Fire.Fire")
		goto Ready
	Flash.Fire:
		PLSF C 0 Bright A_Jump(128, 1)
		PLSF C 4 Bright A_Light1
		stop
		PLSF D 4 Bright A_Light1
		stop
	Fire.Slime:
		PLSG A 0 A_GunFlash("Flash.Slime")
		PLSG A 3 A_FireCustomMissile("Anachrony_SlimePlasmaBall")
		PLSG B 20 A_ReFire("Fire.Slime")
		goto Ready
	Flash.Slime:
		PLSF E 0 Bright A_Jump(128, 1)
		PLSF E 4 Bright A_Light1
		stop
		PLSF F 4 Bright A_Light1
		stop
	Fire.Electric:
		PLSG A 0 A_GunFlash("Flash.Electric")
		PLSG A 3 A_FireCustomMissile("Anachrony_ElectricPlasmaBall")
		PLSG B 20 A_ReFire("Fire.Electric")
		goto Ready
	Flash.Electric:
		PLSF G 0 Bright A_Jump(128, 1)
		PLSF G 4 Bright A_Light1
		stop
		PLSF H 4 Bright A_Light1
		stop
	Fire.Hitscan:
		PLSG A 0 A_GunFlash("Flash.Hitscan")
		PLSG A 0 A_PlaySoundEx("Weapons/IonFire", "SoundSlot7", 1)
	Hold.Hitscan:
		PLSG A 0 A_FireCustomMissile("Anachrony_HitscanPlasmaBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_HitscanPlasmaBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 0 A_FireCustomMissile("Anachrony_HitscanPlasmaBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_HitscanPlasmaBall", Random(-8, 8), 1, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG B 20 A_ReFire("Hold.Hitscan")
		PLSG B 0 A_StopSoundEx("SoundSlot7")
		PLSG B 0 A_PlaySoundEx("Weapons/IonCool", "SoundSlot7", 0)
		goto Ready
	Flash.Hitscan:
		PLSF I 0 Bright A_Jump(128, 1)
		PLSF I 4 Bright A_Light1
		stop
		PLSF J 4 Bright A_Light1
		stop
	Fire.Ice:
		PLSG A 0 A_GunFlash("Flash.Ice")
		PLSG A 3 A_FireCustomMissile("Anachrony_IcePlasmaBall")
		PLSG B 20 A_ReFire("Fire.Ice")
		goto Ready
	Flash.Ice:
		PLSF K 0 Bright A_Jump(128, 1)
		PLSF K 4 Bright A_Light1
		stop
		PLSF L 4 Bright A_Light1
		stop
	AltFire:
		PLSG A 0 A_GunFlash
		PLSG A 0 A_PlaySoundEx("Weapons/IonFire", "SoundSlot7", 1)
		PLSG A 0 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 1, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 0 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 1, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
	AltHold:
		PLSG A 0 A_GunFlash
		PLSG A 0 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 1, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 0 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 0, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 1 A_FireCustomMissile("Anachrony_IonBall", Random(-8, 8), 1, Random(-6, 6), Random(-4, 4), 0, random(-2, 2))
		PLSG A 0 A_ReFire
		PLSG A 4 A_StopSoundEx("SoundSlot7")
		PLSG A 3 A_PlaySoundEx("Weapons/IonCool", "SoundSlot7", 0)
		PLSG A 20
		goto Ready
	AltFlash:
		// A_FirePlasma does this wacky thing with picking a random Flash state;
		// emulate it clumsily
		PLSF A 0 Bright A_Jump(128, "AltFlash2")
		PLSF A 2 Bright A_Light1
		goto LightDone
	AltFlash2:
		PLSF B 2 Bright A_Light1
		goto LightDone
	}
}

// TODO should give these all custom obituaries

// Fire: 20% less damage, but it's done with a small explosion with three
// times the ball's radius (though it won't hurt the player), and also
// radiates damage to nearby actors as it flies
actor Anachrony_FirePlasmaBall : PlasmaBall
{
	Translation "192:207=208:223", "240:243=232:235"
	DamageType fireball
	Damage 0
	// This makes it less obvious that damage is inflicted around the ball,
	// alas, but without it you can trap monsters in a pain loop without even
	// hitting them, which just looks ridiculous
	+PAINLESS
	
	states {
	Spawn:
		PLSS AAABBB 1 A_Explode(random(1, 6), 40, 0)
		loop
	Death:
		PLSS A 0 A_ChangeFlag("PAINLESS", false)
		PLSS A 0 A_Explode(4 * random(1, 8), 40, 0)
		goto super::Death
	}
}
// Slime: The obvious thing is poison, but that really doesn't jive well
// with Doom, especially when it makes monsters just kinda flinch at random.
// Instead, the ball will split up to two times when it hits something it
// can't damage.
actor Anachrony_SlimePlasmaBall : PlasmaBall
{
	Translation "192:207=112:125", "240:243=126:127"
	DamageType slimeball
	Damage 4
	+HITMASTER

	states {
	Death:
		PLSS A 0 A_JumpIfMasterCloser(1024, "NoSplit")
		PLSS A 0 A_CustomMissile("Anachrony_SlimePlasmaBall2", 0, 0, (angle + 180 - random(20, 40)) % 360, CMF_TRACKOWNER | CMF_AIMDIRECTION | CMF_ABSOLUTEANGLE, pitch)
		PLSS A 0 A_CustomMissile("Anachrony_SlimePlasmaBall2", 0, 0, (angle + 180 + random(20, 40)) % 360, CMF_TRACKOWNER | CMF_AIMDIRECTION | CMF_ABSOLUTEANGLE, pitch)
	NoSplit:
		goto super::Death
	}
}
actor Anachrony_SlimePlasmaBall2 : PlasmaBall
{
	Translation "192:207=112:125", "240:243=126:127"
	DamageType slimeball
	Damage 2
	Scale 0.5
	+HITMASTER

	states {
	Death:
		PLSS A 0 A_JumpIfMasterCloser(1024, "NoSplit")
		PLSS A 0 A_CustomMissile("Anachrony_SlimePlasmaBall3", 0, 0, (angle + 180 - random(20, 40)) % 360, CMF_TRACKOWNER | CMF_AIMDIRECTION | CMF_ABSOLUTEANGLE, pitch)
		PLSS A 0 A_CustomMissile("Anachrony_SlimePlasmaBall3", 0, 0, (angle + 180 + random(20, 40)) % 360, CMF_TRACKOWNER | CMF_AIMDIRECTION | CMF_ABSOLUTEANGLE, pitch)
	NoSplit:
		goto super::Death
	}
}
actor Anachrony_SlimePlasmaBall3 : PlasmaBall
{
	Translation "192:207=112:125", "240:243=126:127"
	DamageType slimeball
	Damage 1
	Scale 0.25
}
// Electric: 20% less damage, but slight homing effect
actor Anachrony_ElectricPlasmaBall : PlasmaBall
{
	WeaveIndexZ 52
	+SEEKERMISSILE
	+SCREENSEEKER
	Translation "192:193=225:231", "194:195=212:215", "196:199=170:177", "200:207=251:253", "240:243=254:254"
	DamageType electric
	Damage 4
	states {
	Spawn:
		PLSS A 1 Bright A_SeekerMissile(0, 1, SMF_LOOK | SMF_PRECISE)
		PLSS AAAAA 1 A_Weave(0, 8, 0.0, 2.0)
		PLSS B 1 Bright A_SeekerMissile(0, 1, SMF_LOOK | SMF_PRECISE)
		PLSS BBBBB 1 A_Weave(0, 8, 0.0, 2.0)
		loop
	}
}
// Actually a tiny pellet, like the Ionspray
actor Anachrony_HitscanPlasmaBall
{
	projectile
	radius 13
	height 8
	speed 26
	// RIPPER projectiles continue to do damage while passing through enemies,
	// and we fire a LOT of these, so we need them to do very little damage!
	damage (randompick(0, 0, 1, 1, 2, 3))
	damagetype hitscan
	renderstyle Add
	alpha 0.45
	scale 0.35
	translation "192:207=80:111", "240:243=5:8"
	decal RailScorchLower
	obituary "%o was ionized by %k"
	+RIPPER
	+NOBOSSRIP

	states {
	Spawn:
		TNT1 A 2
		IONB A 1 Bright
		wait
	Death:
		IONB BCDEFGHIJKL 2 Bright
		stop
	}
}
// TODO no idea what to do here; thought about 5% chance of freezing what it
// hits, but the only way i know to do that is by inactivating it, and then it
// can't take damage any more
actor Anachrony_IcePlasmaBall : PlasmaBall
{
	+ICESHATTER
	+HITMASTER
	+FORCEPAIN
	Translation "192:192=4:4", "193:195=80:82", "196:207=192:197", "240:243=198:199"
	DamageType ice
	Damage 0

	states {
	Death:
		//PLSS A 0 ACS_NamedExecuteAlways("
		//PLSS A 0 A_Jump(243, "OrigDeath")
		//PLSS A 0 A_GiveInventory("Anachrony_DummyMonsterFreezer", 1, AAPTR_MASTER)
		PLSS A 0 A_GiveInventory("Anachrony_MonsterFreezerPower", 1, AAPTR_MASTER)
	OrigDeath:
		goto super::Death
	}
}

actor Anachrony_MonsterFreezerPower : PowerSpeed
{
	powerup.duration -5
	speed 0.125
	translation "ice"
	+INVENTORY.ADDITIVETIME
}
actor Anachrony_MonsterFreezer : PowerupGiver
{
	powerup.type Anachrony_MonsterFreezerPower
	translation "ice"
	states {
	Pickup:
		TNT1 A 0 A_Stop
		TNT1 A 0 A_Log("frozen!")
		goto super::Pickup
	}
}




// TODO get rid of this i think
actor Anachrony_Ionspray : Weapon
{
	Inventory.PickupMessage "You got the Ionspray"
	Obituary "%o was ionized by %k"
	Inventory.PickupSound "misc/w_pkup"
	Weapon.Kickback 1
	Weapon.AmmoType "Cell"
	Weapon.AmmoGive 40
	Weapon.AmmoUse 1
	Weapon.SlotNumber 6
	+WEAPON.CHEATNOTWEAPON
	States
	{
	Spawn:
		ISPP A -1
		Loop
	Ready:
		ISPG A 0 A_StopSoundEx("SoundSlot7")
		ISPG A 1 A_WeaponReady
		Loop
	Deselect:
		ISPG A 0 A_StopSoundEx("SoundSlot7")
		ISPG A 1 A_Lower
		Loop
	Select: 
		ISPG A 1 A_Raise
		Loop
	Fire:
		ISPG B 0 A_GunFlash
		ISPG B 0 A_PlaySoundEx("Weapons/IonFire", "SoundSlot7", 1)
		ISPG B 0 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG B 1 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG B 0 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG B 1 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 1, Random(-6, 6), Random(-4, 4))
	Hold:
		ISPG B 0 A_GunFlash
		ISPG C 0 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG C 1 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG C 0 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 0, Random(-6, 6), Random(-4, 4))
		ISPG C 1 A_FireCustomMissile("Anachrony_IonBall", Random(-2, 2), 1, Random(-6, 6), Random(-4, 4))
		ISPG C 0 A_ReFire
		ISPG C 4 A_StopSoundEx("SoundSlot7")
		ISPG B 3 A_PlaySoundEx("Weapons/IonCool", "SoundSlot7", 0)
		ISPG A 20
		Goto Ready
	Flash:
		TNT1 A 2 bright A_Light1
		TNT1 A 2 bright A_Light0
		Stop
	AltFire:
		ISPG A 0 A_SpawnItemEx("Anachrony_IonClump", 4.0, 4.0, 41.0, 0.0, 0.0, 0.0, 0.0, SXF_ISTRACER | SXF_SETTARGET | SXF_SETMASTER)
	AltHold:
		ISPG A 5
		ISPG A 0 A_ReFire
	AltFireFinish:
		ISPG A 5 A_TransferPointer(AAPTR_DEFAULT, AAPTR_TRACER, AAPTR_NULL, AAPTR_MASTER)
		goto Ready
	}
}

Actor IonBall
{
	Radius 13
	Height 8
	Speed 26
	Damage (Random(3, 6))
	Projectile
	RenderStyle Add
	Alpha 0.45
	Scale 0.35
	Decal RailScorchLower
	States
	{
	Spawn:
		IONB A 1 Bright
		Loop
	Death:
		IONB BCDEFGHIJKL 2 Bright
		stop
	}
}

actor Anachrony_IonClump
{
	Projectile
	Speed 20
	RenderStyle Translucent
	Alpha 0.5
	// TODO surely this shouldn't itself be a ripper, since we want it to explode on contact with a monster too, right
	//+RIPPER

	var int user_charge;

	Damage (user_charge / 5)

	states
	{
	Spawn:
		IONB A 0 A_JumpIf(IsPointerEqual(AAPTR_MASTER, AAPTR_NULL), "BeginFlight")
		IONB A 1 A_Warp(AAPTR_TARGET, 24.0 * cos(-pitch), 0.0, 36.0 + 24.0 * sin(-pitch), 0.0, WARPF_COPYPITCH | WARPF_COPYINTERPOLATION)
		IONB A 0 A_SetScale(0.5 + 0.01 * user_charge)
		IONB A 0 A_JumpIfInventory("Cell", 2, "MaybeCharge", AAPTR_TARGET)
		goto Spawn

	MaybeCharge:
		IONB A 0 A_JumpIf(user_charge < 50, "Charge")
		goto Spawn
	Charge:
		IONB A 0 A_TakeInventory("Cell", 2, TIF_NOTAKEINFINITE, AAPTR_TARGET)
		IONB A 0 A_SetUserVar("user_charge", user_charge + 1)
		goto Spawn

	BeginFlight:
		IONB A 0 A_ChangeVelocity(speed * cos(-pitch), 0.0, speed * sin(-pitch), CVF_RELATIVE)
	Flight:
		IONB A 3
		loop
	Death:
		// TODO maybe this should fire a different kind of ball that does more damage, since it's hard to aim?
		IONB AAAAA 0 A_SpawnItemEx("Anachrony_IonBall", 0.0, 0.0, 0.0, 1.0, 0.0, frandom(-0.25, 0.25), frandom(0.0, 360.0), SXF_MULTIPLYSPEED)
		IONB A 0 A_SetUserVar("user_charge", user_charge - 1)
		IONB A 0 A_JumpIf(user_charge <= 0, "FinishDeath")
		goto Death
	FinishDeath:
		IONB BCDEFGHIJKL 2 Bright
		stop
	}
}

// BFG 9000
// Alt-fire throws it and all your ammo, creating a BFG that will explode and
// basically nuke everything.
actor Anachrony_BFG9000 : BFG9000 replaces BFG9000
{
	// Not inherited
	weapon.slotnumber 7
	weapon.slotpriority 0
	
	states {
	/*
	Fire:
		BFGG A 20 A_BFGSound
		BFGG B 10 A_GunFlash
		BFGG B 0 A_FireCustomMissile("Anachrony_BFGBall")
		BFGG B 10 A_FireCustomMissile("Anachrony_BFGBall2")
		BFGG B 20 A_ReFire
		goto Ready
	*/
	AltFire:
		BFGG A 0 A_FireCustomMissile("Anachrony_PrimedBFG9000")
		BFGG A 0 A_TakeInventory("Anachrony_BFG9000", 255)
		stop
	}
}

actor Anachrony_PrimedBFG9000
{
	radius 1
	height 20
	speed 20
	mass 200
	damagetype "nuclearbfg"
	
	// The "power" of the resulting explosion.  Roughly equal to 200 plus the
	// number of cells the player had remaining, so ranges from 200 to 800.
	// The radius of the effect is this value in map units (200 - 800); the
	// maximum damage inflicted is half of this value (100 - 400); and the
	// extra thrust force is twenty times this value (a lot).
	var int user_power;

	states {
	Spawn:
		BFUG A 0 Bright
		// We can't actually do math with the number of cells the player has, so
		// do this ridiculous loop to approximate it instead.
		BFUG A 0 Bright A_SetUserVar("user_power", 200)
	StupidAmmoLoop:
		BFUG A 0 Bright A_JumpIfInTargetInventory("Cell", 20, "TakeAmmo")
		// Take any remaining ammo, just in case
		BFUG A 35 Bright A_TakeInventory("Cell", 65535, TIF_NOTAKEINFINITE, AAPTR_TARGET)
		BFUG A 80 Bright A_Quake(user_power / 100, 80, 0, user_power)
		// TODO this doesn't work; it has to be called by a weapon (which we
		// just took away, oops!) or a custom inventory item
		BFUG A 0 Bright A_SetBlend("FF FF FF", 1.0, 35)
		BFUG A 0 Bright A_Explode(user_power / 2, user_power, XF_HURTSOURCE, true, user_power / 16)
		BFUG A 0 Bright A_RadiusThrust(user_power * 20, user_power, RTF_AFFECTSOURCE)
		// Switch to barrel sprites
		// TODO this is kinda unimpressive; should launch some decorative sprites in every direction too
		// TODO leave fire around the center?
		// TODO attenuation (last value) should scale with user_power, but unclear exactly how to do that
		BEXP D 0 Bright A_PlaySound("world/barrelx", CHAN_WEAPON, 1.0, false, 1.0)
		BEXP D 5 Bright A_SetScale(1.25)
		BEXP D 5 Bright A_SetScale(1.5)
		BEXP E 5 Bright A_SetScale(2.0)
		BEXP E 5 Bright A_SetScale(3.0)
		BEXP E 5 Bright A_SetScale(4.0)
		stop
	TakeAmmo:
		BFUG A 0 Bright A_TakeInventory("Cell", 20, TIF_NOTAKEINFINITE, AAPTR_TARGET)
		BFUG A 0 Bright A_SetUserVar("user_power", user_power + 20)
		goto StupidAmmoLoop
	}
}


// Equivalent to Doom's berserk pack, but uses the smaller beta sprite.
actor Anachrony_Steroids : Berserk replaces Berserk
{
	inventory.pickupmessage "Steroids!"
	states
	{
	Spawn:
		TNT0 A -1
		stop
	}
}


actor Anachrony_TrueBerserk : CustomInventory 13302
{
	+COUNTITEM
	+INVENTORY.ALWAYSPICKUP
	inventory.pickupmessage "$GOTBERSERK"  // Berserk!
	inventory.pickupsound "*evillaugh"  // TODO i think i need to snag this from heretic?

	states {
	Spawn:
		PSTR A -1
		stop
	Pickup:
		TNT1 A 0 HealThing(100, 0)
		TNT1 A 0 A_GiveInventory("Anachrony_TrueBerserkPower")
		TNT1 A 0 A_GiveInventory("Anachrony_TrueBerserkFist")
		TNT1 A 0 A_SelectWeapon("Anachrony_TrueBerserkFist")
		stop
	}
}

// This just gives the speed; the increased damage is a property of the true
// berserk fist weapon.
// TODO this could break maps that assume you can't jump x distance!
// TODO do i want damage resistance instead?  in addition??  can i remove pain chance or something similar?
actor Anachrony_TrueBerserkPower : PowerSpeed
{
	powerup.duration -30
	// Same fade as the original berserk, but a little fainter since it's
	// on screen the whole time
	powerup.color 255, 0, 0, 0.33
	// inherited: 1.5x speed, no tele freeze
}

// A fist that is considerably more powerful, but cannot be switched away
// from.  Removes itself from the holder's inventory once the berserk
// powerup wears off.
actor Anachrony_TrueBerserkFist : Fist
{
	states
	{
	Select:
		TNT1 A 0 A_ChangeFlag("NOPAIN", true)
		goto super::Select
	Ready:
		TNT1 A 0 A_JumpIfInventory("Anachrony_TrueBerserkPower", 1, "Ready2")
		goto Selfdestruct
	Ready2:
		PUNG A 1 A_WeaponReady(WRF_DISABLESWITCH)
		goto Ready
	Selfdestruct:
		// Not sure this is necessary, but just in case, un-disable weapon switch
		TNT1 A 0 A_WeaponReady
		// TODO strictly speaking, should restore original value
		TNT1 A 0 A_ChangeFlag("NOPAIN", false)
		TNT1 A 0 A_TakeInventory("Anachrony_TrueBerserkFist")
		stop
	Fire:
		PUNG B 4
		// Equivalent to berserk fist, but with 1.5x damage (matching speed boost)
		PUNG C 4 A_CustomPunch(30 * random(1, 10), true)
		PUNG D 5
		PUNG C 4
		PUNG B 5 A_ReFire
		Goto Ready
	}
}
