// 2.1 Egg Slimer - Ported from 2.1.25
// PLEASE LOAD LUA_CMMN, LUA_OJTF AND LUA_ENCM BEFORE THIS

freeslot(
	// Sprites come from LUA_ENCM
	// Objects
	"MT_21EGGMOBILE2",
	"MT_21EGGMOBILE2_POGO",
	"MT_21GOOP",
	// States
	"S_21EGGMOBILE2_STND",
	"S_21EGGMOBILE2_POGO1",
	"S_21EGGMOBILE2_POGO2",
	"S_21EGGMOBILE2_POGO3",
	"S_21EGGMOBILE2_POGO5",
	"S_21EGGMOBILE2_POGO6",
	"S_21EGGMOBILE2_POGO7",
	"S_21GOOP1",
	"S_21GOOP2",
	"S_21GOOP3"
)

// 2.1 Egg Slimer

// Make the 2.0 Egg Slimer spawn 33 FRACUNITS off the ground if its map thing's z-position hasn't been set
addHook("MapThingSpawn", function(mobj, mapthing)
	if not (mobj and mobj.valid
	and mapthing and mapthing.valid) then
		return
	end
	
	local offset
	
	if mapthing.z ~= 0 then
		offset = mapthing.z
	else
		offset = 33*FRACUNIT
	end
	
	if (mapthing.options & MTF_OBJECTFLIP) then
		if offset ~= 0 then
			mobj.z = mobj.z - offset
		else
			mobj.z = mobj.ceilingz
		end
	else
		if offset ~= 0 then
			mobj.z = mobj.z + offset
		else
			mobj.z = mobj.floorz
		end
	end
	
end, MT_21EGGMOBILE2)

// Allows the Egg Slimer to move in a circular path in its first phase
addHook("MobjSpawn", function(mo)
	if not (mo and mo.valid) then return end
	mo.watertop = mo.info.speed
end, MT_21EGGMOBILE2)

// Ported BossThinker behavior for 2.1 Egg Slimer from 2.1.25
addHook("BossThinker", function(mobj)
	if not (mobj and mobj.valid) then return end
	
	if mobj.movecount then mobj.movecount = $-1 end
	if not mobj.movecount then mobj.flags2 = $ & ~MF2_FRET end
	
	if not (mobj.tracer and mobj.tracer.valid) then
		//var1 = 0
		A_OldBossJetFumes(mobj, 0, 0)
	end
	
	if mobj.health <= mobj.info.damage 
	and (not (mobj.target and mobj.target.valid and (mobj.target.flags & MF_SHOOTABLE))) then
		if mobj.health <= 0 then
			// Look for a new target
			if P_BossTargetPlayer(mobj, false) and mobj.info.mass then // Bid farewell!
				S_StartSound(mobj, mobj.info.mass)
			end
			
			return true
		end
		
		// Look for a new target
		if P_BossTargetPlayer(mobj, false) and mobj.info.mass then // Bid farewell!
			S_StartSound(mobj, mobj.info.mass)
		end
		
		return true
	end
	
	if mobj.state == mobj.info.spawnstate and mobj.health > mobj.info.damage then
		A_Boss2Chase(mobj) // Replace this with A_21Boss2Chase if it changes in the future
	elseif mobj.health > 0 and mobj.state ~= mobj.info.painstate and mobj.state ~= mobjinfo[mobj.info.missilestate].raisestate then
		mobj.flags = $ & ~MF_NOGRAVITY
		A_Boss2Pogo(mobj) // Replace this with A_21Boss2Pogo if it changes in the future
		P_LinedefExecute(LE_PINCHPHASE, mobj, NULL)
	end
	
	return true
end, MT_21EGGMOBILE2)

// Ported from p_inter.c in 2.1.25
// Allows the 2.1 Egg Slimer to immediately pogo back up when hit, instead of stopping
addHook("MobjDamage", function(target, inflictor, source, damagetype)
	if not (target and target.valid) then return end
	
	if target.health > 1 then target.flags2 = $|MF2_FRET end
	
	target.health = $-1
	
	if target.health <= 0 then
		P_KillMobj(target, inflictor, source, damagetype)
		return true
	end
	
	if target.health < target.info.damage then
		target.state = target.info.meleestate
	else
		target.state = target.info.painstate
	end
	
	target.reactiontime = 0
	
	return true
end, MT_21EGGMOBILE2)

// Display the 2.1 Egg Slimer's "gibs" when it dies
addHook("BossDeath", function(mo)
	if not (mo and mo.valid) then return end
	A_21BossDeath(mo)
	A_20EggSlimerGibs(mo, 0, 0)
	return true
end, MT_21EGGMOBILE2)

mobjinfo[MT_21EGGMOBILE2] = {
	//$Name 2.1 Egg Slimer
	//$Sprite 21ENA1
	//$Category Bosses
	//$Flags4Text End level on death
	//$Flags8Text Speed up when hit
	doomednum = 4041,
	spawnstate = S_21EGGMOBILE2_STND,
	spawnhealth = 8,
	reactiontime = -666,
	attacksound = sfx_gspray,
	painstate = S_21EGGMOBILE2_PAIN,
	painchance = MT_21GOOP,
	painsound = sfx_dmpain,
	meleestate = S_21EGGMOBILE2_PAIN2,
	missilestate = MT_21EGGMOBILE2_POGO,
	deathstate = S_21EGGMOBILE2_DIE1,
	xdeathstate = S_21EGGMOBILE2_FLEE1,
	deathsound = sfx_cybdth,
	speed = 2*FRACUNIT,
	radius = 24*FRACUNIT,
	height = 76*FRACUNIT,
	damage = 3,
	activesound = sfx_pogo,
	flags = MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY|MF_BOSS,
	raisestate = S_21EGGMOBILE2_POGO1
}

states[S_21EGGMOBILE2_POGO1] = {SPR_21EN,	B,	4,	nil,				0,			0,			S_21EGGMOBILE2_POGO2}
// Replace A_Boss2PogoTarget with A_21Boss2PogoTarget if it changes in the future
states[S_21EGGMOBILE2_POGO2] = {SPR_21EN,	A,	2,	A_Boss2PogoTarget,	9*FRACUNIT,	8*FRACUNIT,	S_21EGGMOBILE2_POGO3}
states[S_21EGGMOBILE2_POGO3] = {SPR_21EN,	B,	2,	nil,				0,			0,			S_21EGGMOBILE2_POGO4}
states[S_21EGGMOBILE2_POGO5] = {SPR_21EN,	B,	4,	nil,				0,			0,			S_21EGGMOBILE2_POGO6}
// Replace A_Boss2PogoTarget with A_21Boss2PogoTarget if it changes in the future
states[S_21EGGMOBILE2_POGO6] = {SPR_21EN,	A,	2,	A_Boss2PogoTarget,	7*FRACUNIT,	8*FRACUNIT,	S_21EGGMOBILE2_POGO7}
states[S_21EGGMOBILE2_POGO7] = {SPR_21EN,	B,	2,	nil,				0,			0,			S_21EGGMOBILE2_POGO4}

// 2.1 Egg Slimer Invisible Pogo Barrier

// Ported from p_mobj.c in 2.1.25
// Gives the invisible pogo barrier a brain
addHook("MobjThinker", function(mobj)
	if not (mobj and mobj.valid) then return end
	
	if not (mobj.target and mobj.target.valid) or not mobj.target.health 
	or mobj.target.state == mobj.target.info.spawnstate 
	or mobj.target.state == mobj.target.info.raisestate then
		P_RemoveMobj(mobj)
		return
	end
	P_TeleportMove(mobj, mobj.target.x, mobj.target.y, mobj.target.z - mobj.info.height)
end, MT_21EGGMOBILE2_POGO)

// Hurt anything that touches the 2.1 Egg Slimer's pogo barrier
addHook("TouchSpecial", function(special, toucher)
	if not (special and special.valid
	and toucher and toucher.valid) then
		return
	end
	
	// sanity checks
	if not (special.target and special.target.valid and special.target.health) then
		return
	end
	// Goomba Stomp'd!
	if special.target.momz < 0 then
		P_DamageMobj(toucher, special, special.target, 1, 0)
		special.target.momx = 0
		special.target.momy = 0
		special.target.momz = 0
		special.target.flags = $|MF_NOGRAVITY
		special.target.state = special.info.raisestate
		S_StartSound(special.target, special.info.activesound)
		P_RemoveMobj(special)
	end
	return true
end, MT_21EGGMOBILE2_POGO)

mobjinfo[MT_21EGGMOBILE2_POGO] = {
	doomednum = -1,
	spawnstate = S_INVISIBLE,
	spawnhealth = 1000,
	radius = 15*FRACUNIT,
	height = 28*FRACUNIT,
	activesound = sfx_s24c,
	flags = MF_SPECIAL|MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT,
	raisestate = S_21EGGMOBILE2_POGO5
}

// 2.1 Egg Slimer Goop

addHook("MobjThinker", function(mo)
	if not (mo and mo.valid) then return end
	
	if mo.z <= mo.floorz and not (mo.state == mo.info.meleestate) then
		mo.state = mo.info.meleestate
		mo.momx = 0
		mo.momy = 0
		mo.momz = 0
		mo.z = mo.floorz
		if mo.info.painsound then
			S_StartSound(mo, mo.info.painsound)
		end
	end
end, MT_21GOOP)

mobjinfo[MT_21GOOP] = {
	doomednum = -1,
	spawnstate = S_21GOOP1,
	spawnhealth = 1000,
	reactiontime = 8,
	painsound = sfx_ghit,
	meleestate = S_21GOOP3,
	speed = 1,
	radius = 4*FRACUNIT,
	height = 4*FRACUNIT,
	mass = DMG_WATER,
	flags = MF_PAIN
}

states[S_21GOOP1] = {SPR_OGOP,	A,	2,	nil,	0,	0,	S_21GOOP2}
states[S_21GOOP2] = {SPR_OGOP,	B,	2,	nil,	0,	0,	S_21GOOP1}
states[S_21GOOP3] = {SPR_OGOP,	C,	-1,	nil,	0,	0,	S_NULL}
