// Egghead - Ported by Glaber (original SOC) and MIDIMan (accuracy fixes and conversion to Lua)
// Return of the Egghead

freeslot(
	"MT_EGGHEAD",
	"S_EGGH_FLOAT",
	"S_EGGH_FLY",
	"S_EGGH_SHOOT",
	"S_EGGH_SHOOT2",
	"S_EGGH_SHOOT3",
	"S_EGGH_SHOOT4",
	"S_EGGH_SHOOT5",
	"S_EGGH_OW",
	"SPR_EGGH",
	"sfx_firsht"
)

local function Egghead_Attack(actor, var1, var2)
	if not (actor and actor.valid) then return end
	if not (actor.target and actor.target.valid) then return end
	
	A_FaceTarget(actor)
	
	// Originally, Egghead also had a melee attack, but I took it out for gameplay purposes --MIDIMan
	if actor.info.raisestate then
		if actor.info.attacksound then S_StartSound(actor, actor.info.attacksound) end
		P_SpawnMissile(actor, actor.target, actor.info.raisestate)
	end
end

addHook("MobjThinker", function(mo)
	if not (mo and mo.valid) then return end
	
	if (mo.flags2 & MF2_FRET) and mo.state ~= mo.info.painstate then
		mo.flags2 = $ & ~MF2_FRET
	end
end, MT_EGGHEAD)

mobjinfo[MT_EGGHEAD] = {
	//$Name Egg Head
	//$Sprite EGGHA1
	//$Category TGF/Concept Enemies
	doomednum = 4029,
	spawnstate = S_EGGH_FLOAT,
	spawnhealth = 20,
	seestate = S_EGGH_FLY,
	reactiontime = 8,
	attacksound = sfx_firsht,
	painstate = S_EGGH_OW,
	painchance = 128,
	painsound = sfx_dmpain,
	missilestate = S_EGGH_SHOOT,
	deathstate = S_XPLD_FLICKY,
	deathsound = sfx_pop,
	speed = 8,
	radius = 28*FRACUNIT,
	height = 45*FRACUNIT,
	raisestate = MT_JETTBULLET,
	flags = MF_SPECIAL|MF_ENEMY|MF_SHOOTABLE|MF_FLOAT|MF_NOGRAVITY
}

states[S_EGGH_FLOAT] =	{SPR_EGGH,	A,	10,	A_Look,		0,	0,	S_EGGH_FLOAT}
states[S_EGGH_FLY] =	{SPR_EGGH,	A,	3,	A_Chase,	0,	0,	S_EGGH_FLY}

states[S_EGGH_SHOOT] =	{SPR_EGGH,	B,	5,	A_FaceTarget,	0,	0,	S_EGGH_SHOOT2}
states[S_EGGH_SHOOT2] =	{SPR_EGGH,	C,	5,	A_FaceTarget,	0,	0,	S_EGGH_SHOOT3}
states[S_EGGH_SHOOT3] =	{SPR_EGGH,	D,	10,	Egghead_Attack,	0,	0,	S_EGGH_SHOOT4}
states[S_EGGH_SHOOT4] =	{SPR_EGGH,	E,	5,	A_FaceTarget,	0,	0,	S_EGGH_SHOOT5}
states[S_EGGH_SHOOT5] =	{SPR_EGGH,	F,	5,	A_FaceTarget,	0,	0,	S_EGGH_FLY}

states[S_EGGH_OW] =		{SPR_EGGH,	A,	3,	A_Pain,			0,	0,	S_EGGH_FLY}
