/* Extend the Doom bestiary in various ways.  "replaces" can't stack, so all
 * of this has to live in the same file, alas.
 *
 * Here's what we've got going on:
 *
 * 1. Drops are changed to be a little more interesting.  Some monsters have
 * rare better drops; some have drops that didn't before.
 *
 * DEPENDS ON: actors/pickups/medium-ammo.txt
 *
 * 2. Monsters with hitscan weaponry are changed to use a "hitscan" damage
 * type, for compatibility with the commando armor.  The Archvile is similarly
 * changed to use "fireball" damage, for the sake of the ceramic suit.
 *
 * 3. Hell Knights and Barons of Hell get green blood.  Cacodemons get blue
 * blood, and also fall and splat to the floor.  Lost Souls lose their blood
 * entirely.  (Good idea, zpack, on that last one.)
 */


// ----------------------------------------------------------------------------

actor Anachrony__ZombieDrop : RandomSpawner
{
	dropitem "Anachrony_BulletKit", 255, 5
	dropitem "Clip", 255, 95
}

actor Anachrony_RandomZombie : RandomSpawner replaces ZombieMan
{
	dropitem "Anachrony_ZombieMan", 255, 1
	dropitem "Anachrony_ZombieWoman", 255, 1
}

actor Anachrony_ZombieMan : ZombieMan
{
	dropitem "Anachrony__ZombieDrop"
	damagetype "hitscan"
	damagefactor lightning, 1.5
}
actor Anachrony_StealthZombieMan : StealthZombieMan replaces StealthZombieMan
{
	dropitem "Anachrony__ZombieDrop"
	damagetype "hitscan"
	damagefactor lightning, 1.5
}
// Slightly modified from Female Zombie from Realm 667, by DavidRaven
actor Anachrony_ZombieWoman
{
	MONSTER
	+FLOORCLIP
	game Doom
	obituary "$OB_ZOMBIE"
	health 20
	mass 90
	speed 9
	radius 19
	height 52
	painchance 200
	seesound "fem/sight"
	attacksound "grunt/attack"
	painsound "fem/pain"
	deathsound "fem/death"
	activesound "fem/active"
 
	dropitem "Anachrony__ZombieDrop"
	damagetype "hitscan"
	damagefactor lightning, 1.5

	states
	{
	Spawn:
		ZOMW AB 10 A_Look
		loop
	See:
		ZOMW AABBCCDD 4 A_Chase
		loop
	Missile:
		ZOMW E 10 A_FaceTarget
		ZOMW F 8 A_PosAttack
		ZOMW E 8
		goto See
	Pain:
		ZOMW G 3
		ZOMW G 3 A_Pain
		goto See
	Death:
		ZOMW H 5
		ZOMW I 5 A_Scream
		ZOMW J 5 A_NoBlocking
		ZOMW K 5
		ZOMW L 5
		ZOMW M 5
		ZOMW N -1
		stop
	XDeath:
		ZOMW O 5
		ZOMW P 5 A_XScream
		ZOMW Q 5 A_NoBlocking
		ZOMW RSTUV 5
		ZOMW W -1
		stop
	Raise:
		ZOMW MLKJIH 5
		goto See
	}
}


actor Anachrony_RandomSergeant : RandomSpawner replaces ShotgunGuy
{
	dropitem Anachrony_ShotgunGuy, 255, 1
	dropitem Anachrony_ShotgunGal, 255, 1
}
actor Anachrony_ShotgunGuy : ShotgunGuy
{
	dropitem Shotgun
	dropitem Shell, 16
	dropitem Shell, 1
	damagetype hitscan
	damagefactor lightning, 1.5
}
actor Anachrony_StealthShotgunGuy : StealthShotgunGuy replaces StealthShotgunGuy
{
	dropitem Shotgun
	dropitem Shell, 16
	dropitem Shell, 1
	damagetype hitscan
	damagefactor lightning, 1.5
}
// Slightly modified from Female Sergeant from Realm 667, by DavidRaven
actor Anachrony_ShotgunGal
{
	+FLOORCLIP
	monster
	health 30
	mass 90
	speed 9
	radius 20
	height 52
	painchance 170
	obituary "$OB_SHOTGUY"
	seesound "fem/sight"
	attacksound "shotguy/attack"
	painsound "fem/pain"
	deathsound "fem/death"
	activesound "fem/active"

	dropitem "Shotgun"
	dropitem "Shell", 16
	dropitem "Shell", 1
	damagetype "hitscan"

	states
	{
	Spawn:
		CZOW AB 10 A_Look
		loop
	See:
		CZOW AABBCCDD 3 A_Chase
		loop 
	Missile: 
		CZOW E 10 A_FaceTarget
		CZOW F 10 Bright A_SPosAttackUseAtkSound
		CZOW E 10
		goto See
	Pain: 
		CZOW G 3
		CZOW G 3 A_Pain
		goto See
	Death: 
		CZOW H 5
		CZOW I 5 A_Scream
		CZOW J 5 A_Fall
		CZOW K 5
		CZOW L 5
		CZOW M 5
		CZOW N -1
		stop
	XDeath:
		CZOW O 5
		CZOW P 5 A_XScream
		CZOW Q 5 A_Fall
		CZOW RSTUV 5
		CZOW W -1
		stop
	Raise: 
		CZOW MLKJIH 5
		goto See 
	} 
}

actor Anachrony_ChaingunGuy : ChaingunGuy replaces ChaingunGuy
{
	dropitem "Chaingun"
	dropitem "Anachrony_BulletKit", 4
	dropitem "CommandoArmor", 1
	damagetype hitscan
	damagefactor lightning, 1.5
}
actor Anachrony_StealthChaingunGuy : StealthChaingunGuy replaces StealthChaingunGuy
{
	dropitem Chaingun
	dropitem CommandoArmor, 1
	damagetype hitscan
	damagefactor lightning, 1.5
}

actor Anachrony_HellKnight : HellKnight replaces HellKnight
{
	bloodcolor darkgreen
	damagefactor hitscan, 1.5
}

actor Anachrony_BaronOfHell : BaronOfHell replaces BaronOfHell
{
	bloodcolor darkgreen
	damagefactor hitscan, 1.5
}

// Cacodemon splat is from:
// http://zdoom.org/wiki/Improving_original_monsters_with_DECORATE
actor Anachrony_Cacodemon : Cacodemon replaces Cacodemon
{
	bloodcolor blue
	damagefactor fire, 1.5
	damagefactor fireball, 1.5
	damagefactor rocket, 1.25

	states {
	Death:
		HEAD G 4 A_SetSolid
		HEAD G 4 A_SetShootable
		HEAD H 8 A_Scream
		HEAD H -1
		stop
	Crash:
		HEAD I 4 A_PlaySound("*fist")
		HEAD J 4
		HEAD K 2 A_NoBlocking
		HEAD K 1 A_UnsetSolid
		HEAD K 1 A_UnSetShootable
		HEAD L -1 A_SetFloorClip
		stop
	Raise:
		HEAD L 8 A_UnSetFloorClip
		HEAD KJIHG 8
		goto See
	}
}

actor Anachrony_LostSoul : LostSoul replaces LostSoul
{
	+NOBLOOD
	// mostly just an easter egg
	dropitem ArmorBonus, 12

}

// Just like the regular Cell, but using the beta sprite
actor Anachrony_ArachnotronCell : Cell
{
	inventory.pickupmessage "Picked up an odd energy cell."
	inventory.amount 30
	states {
	Spawn:
		CEL0 A -1
		stop
	}
}

actor Anachrony_Arachnotron : Arachnotron replaces Arachnotron
{
	dropitem Anachrony_ArachnotronCell, 32
}

// Mancubus's angles are too predictable; keep the player on their toes by
// randomizing the number and direction of fireballs.
actor Anachrony_Fatso : Fatso replaces Fatso
{
	states {
	Missile:
		// Spawn height of 32 is baked into P_SpawnMissile.
		// The original functions always fire two missiles 90/8 (11.25) degrees
		// apart.  We add some modest variation of 6 degrees, and add a possible
		// fourth shot that aims /both/ fireballs at the player.
		FATT G 20 A_FatRaise
		FATT H 0 Bright A_CustomMissile("FatShot", 32, -8, frandom(6, 18))
		FATT H 10 Bright A_CustomMissile("FatShot", 32, 8, frandom(-6, 6))
		FATT IG 5 A_FaceTarget
		FATT H 0 Bright A_CustomMissile("FatShot", 32, -8, frandom(-18, -6))
		FATT H 10 Bright A_CustomMissile("FatShot", 32, 8, frandom(-6, 6))
		FATT H 0 Bright A_Jump(48, "StopMissile")
		FATT IG 5 A_FaceTarget
		FATT H 0 Bright A_CustomMissile("FatShot", 32, -8, frandom(0, 12))
		FATT H 10 Bright A_CustomMissile("FatShot", 32, 8, frandom(-12, 0))
		FATT H 0 Bright A_Jump(192, "StopMissile")
		FATT IG 5 A_FaceTarget
		FATT H 0 Bright A_CustomMissile("FatShot", 32, -8, frandom(-9, 3))
		FATT H 10 Bright A_CustomMissile("FatShot", 32, 8, frandom(-3, 9))
	StopMissile:
		FATT IG 5
		Goto See
	}
}

actor Anachrony_Archvile : Archvile replaces Archvile
{
	states {
	Missile:
		// All inherited from Archvile, except the damage type for A_VileAttack
		VILE G 0 Bright A_VileStart
		VILE G 10 Bright A_FaceTarget
		VILE H 8 Bright A_VileTarget
		VILE IJKLMN 8 Bright A_FaceTarget
		VILE O 8 Bright A_VileAttack("vile/stop", 20, 70, 70, 1.0, "fireball")
		VILE P 20 Bright
		goto See
	}
}

actor Anachrony_SpiderMastermind : SpiderMastermind replaces SpiderMastermind
{
	dropitem "Clip", 64
	dropitem "Clip", 64
	dropitem "Clip", 64
	dropitem "Clip", 64
	dropitem "Anachrony_BulletKit", 32
	dropitem "Anachrony_BulletKit", 32
	dropitem "ClipBox", 16
	damagetype "hitscan"
}

actor Anachrony_Cyberdemon : Cyberdemon replaces Cyberdemon
{
	dropitem "RocketAmmo", 64
	dropitem "RocketAmmo", 64
	dropitem "RocketAmmo", 64
	dropitem "RocketAmmo", 64
	dropitem "Anachrony_RocketPile", 32
	dropitem "Anachrony_RocketPile", 32
	dropitem "Anachrony_RocketPile", 32
	dropitem "Anachrony_RocketPile", 32
}

actor Anachrony_MarinePistol : MarinePistol replaces MarinePistol
{
	damagetype hitscan
}
actor Anachrony_MarineShotgun : MarineShotgun replaces MarineShotgun
{
	damagetype hitscan
}
actor Anachrony_MarineSSG : MarineSSG replaces MarineSSG
{
	damagetype hitscan
}
actor Anachrony_MarineChaingun : MarineChaingun replaces MarineChaingun
{
	damagetype hitscan
}





// ANGUISH -- based on Pain Elemental
// Projectile sprites are taken from the Hell's Fury monster on Realm 667,
// created by Tormentor667 and Vader based on id sprites.
actor Anachrony_Anguish 13503
{
	health 400
	radius 31
	height 56
	mass 400
	speed 4  // half a PE
	painchance 128
	bloodcolor yellow
	monster
	+AVOIDMELEE
	+FLOAT
	+NOGRAVITY
	+NOTARGET
	seesound "pain/sight"
	painsound "pain/pain"
	deathsound "pain/death"
	activesound "pain/active"
	translation "13:15=152:159", "64:79=96:111", "128:143=80:103", "144:151=96:111", "32:47=232:235", "168:179=224:231", "180:191=160:167", "160:167=112:127", "162:163=162:163", "232:235=162:165", "236:239=156:159"

	var int user_stashed_tid;
	var int user_closest_target;

	states {
	// TODO interesting!  this means it doesn't need to be woken up at all.
	Spawn:
	See:
		PAIN AABBCC 3 A_Wander
		TNT1 A 0 A_Jump(32, "Resurrect")
		// TODO this stuff is so weird and clumsy
		TNT1 A 0 A_JumpIfCloser(256.0, "Backoff")
		TNT1 A 0 A_JumpIfCloser(512.0, "See")
		TNT1 A 0 A_ChangeFlag("FRIGHTENED", false)
		loop
	Backoff:
		TNT1 A 0 A_ChangeFlag("FRIGHTENED", true)
		goto See
	// TODO probably shouldn't chase the player at all, right?  should maybe go
	// after nearest living monster, but also keep a healthy distance?
	// how do i do that?  A_Wander?  JumpIfTargetCloser?
	// TODO also maybe be a little less eager with the missiles; how do i control that
	// TODO actually "missile" is entirely the wrong thing to be using here; i don't care if the player is within line of sight
	Resurrect:
		TNT1 A 0 A_ClearTarget
		TNT1 A 0 A_SetUserVar("user_stashed_tid", tid)
		TNT1 A 0 A_SetUserVar("user_closest_target", -1)
		TNT1 A 0 Thing_ChangeTID(0, 13333)
		// TODO this script doesn't check for things in LOS only, oops
		TNT1 A 0 A_RadiusGive("Anachrony__SoulMiteTargeter", 1024.0, RGF_CORPSES)
		TNT1 A 0 Thing_ChangeTID(0, user_stashed_tid)
		// TODO need plan B if there's no viable corpse nearby
		// TODO this is a stupid hack to check for whether i have a target at all
		TNT1 A 0 A_JumpIfCloser(1024.0, "Resurrect2")
		goto See
	Resurrect2:
		SHBA A 0 bright ACS_NamedExecuteWithResult("anachrony_soulmite_debug2")
		TNT1 A 0 Thing_ChangeTID(0, user_stashed_tid)
		//PAIN A 0 bright ACS_NamedExecuteWithResult("anachrony_target_corpse")
		PAIN D 5 A_FaceTarget
		PAIN E 5 A_FaceTarget
		PAIN F 5 bright A_FaceTarget
		PAIN F 0 bright A_FaceTarget
		PAIN F 0 bright A_CustomMissile("Anachrony_SoulMite", 32.0, 0, 0, CMF_ABSOLUTEPITCH, 0)
		goto See
	Pain:
		PAIN G 6
		PAIN G 6 A_Pain
		goto See
	Death:
		PAIN H 8 bright
		PAIN I 8 bright A_Scream
		PAIN JK 8 bright
		PAIN L 8 bright // TODO do...  something...  interesting?
		PAIN M 8 bright
		stop
	Raise:
		stop
	}
}

actor Anachrony__SoulMiteTargeter : CustomInventory
{
	states {
	Pickup:
		TNT1 A 0 ACS_NamedExecuteWithResult("anachrony_soulmite_become_target")
		stop
	}
}

// TODO also want a slight effect on the monster so it's clear something is going on here
// TODO prevent resurrecting twice?  destroy the corpse or something
actor Anachrony_AnguishWeaknessPower : PowerProtection
{
	damagefactor normal, 2
}

// TODO don't need givers when placing directly in inventory
actor Anachrony_AnguishWeaknessGiver : PowerupGiver
{
	+INVENTORY.AUTOACTIVATE
	+INVENTORY.ALWAYSPICKUP
	inventory.maxamount 0
	powerup.type Anachrony_AnguishWeaknessPower
	powerup.duration 0x7FFFFFFD
	powerup.color "A02000", 0.3
}


actor Anachrony_SoulMite
{
	projectile
	radius 8
	height 6
	scale 0.4
	speed 9
	damage 0
	renderstyle Add
	seesound "harvester/scream"
	deathsound "imp/shotx"
	decal DoomImpScorch
	+SEEKERMISSILE
	+HITMASTER
	+RIPPER  // pass through live monsters
	+PAINLESS
	+BLOODLESSIMPACT
	+DONTREFLECT
	//+FLOORHUGGER
	States
	{
	Spawn:
		SHBA A 0 nodelay ACS_NamedExecuteWithResult("anachrony_soulmite_debug")
		SHBA A 0 bright A_SpawnItemEx("Anachrony_SoulMiteTrail", 0, 0, 0, 0, 0, 0, 180, 128)
		// TODO argh, A_SeekerMissile clears the tracer field (and doesn't seek!) if the tracer is dead
		//SHBA AB 2 bright A_SeekerMissile(10,20)
		SHBA A 0 bright A_FaceTracer(5, 5, 0, 0, FAF_TOP, 16.0)
		TNT1 A 0 bright A_JumpIfTracerCloser(16.0, "ForceDeath", true)
		SHBA A 2 bright A_ChangeVelocity(speed * cos(pitch), 0, speed * sin(pitch), CVF_RELATIVE | CVF_REPLACE)
		SHBA B 0 bright A_FaceTracer(5, 5, 0, 0, FAF_TOP, 16.0)
		SHBA B 0 bright A_JumpIfTracerCloser(16.0, "ForceDeath", true)
		SHBA B 2 bright A_ChangeVelocity(speed * cos(pitch), 0, speed * sin(pitch), CVF_RELATIVE | CVF_REPLACE)
		loop
	ForceDeath:
		TNT1 A 0 A_Die
	Death:
		// we HAVE to become non-blocking here, or we block the resurrection!
		TNT1 A 0 A_Stop
		TNT1 A 0 A_NoBlocking
		TNT1 A 0 bright ACS_NamedExecuteAlways("anachrony_soulmite_collide")
		SHBA CDEFGHIJKL 4 bright
		stop
	}
}


actor Anachrony_SoulMiteTrail
{
	projectile
	radius 1
	height 1
	speed 0
	renderstyle Add
	alpha 0.5
	+NOCLIP
	states
	{
	Spawn:
	Death:
		SBTR H 6 
		SBTR ABCDEFG 4 Bright
		stop
	}
}



