-- 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(mo, mthing)
	if not (mo and mo.valid and mthing and mthing.valid) then return end
	
	RB_MapThingHeightOffset(mo, mthing, 33*FRACUNIT)
	
-- 	if not udmf and (mthing.options & MTF_AMBUSH) then mo.flags2 = $|MF2_AMBUSH end -- This is already handled by SRB2
	if udmf and not mthing.args[5] then mo.flags2 = $|MF2_AMBUSH 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(mo)
	if not (mo and mo.valid) then return end
	
	if mo.movecount then mo.movecount = $-1 end
	if not mo.movecount then mo.flags2 = $ & ~MF2_FRET end
	
	if not (mo.tracer and mo.tracer.valid) then
		A_OldBossJetFumes(mo, 0, 0)
	end
	
	if mo.health <= mo.info.damage 
	and (not (mo.target and mo.target.valid and (mo.target.flags & MF_SHOOTABLE))) then
		if mo.health <= 0 then
			-- Look for a new target
			if P_BossTargetPlayer(mo, false) and mo.info.mass then -- Bid farewell!
				S_StartSound(mo, mo.info.mass)
			end
			
			return true
		end
		
		-- Look for a new target
		if P_BossTargetPlayer(mo, false) and mo.info.mass then -- Bid farewell!
			S_StartSound(mo, mo.info.mass)
		end
		
		return true
	end
	
	if mo.state == mo.info.spawnstate and mo.health > mo.info.damage then
		A_Boss2Chase(mo) -- Replace this with A_21Boss2Chase if it changes in the future
	elseif mo.health > 0 and mo.state ~= mo.info.painstate and mo.state ~= mobjinfo[mo.info.missilestate].raisestate then
		mo.flags = $ & ~MF_NOGRAVITY
		A_Boss2Pogo(mo) -- Replace this with A_21Boss2Pogo if it changes in the future
		-- UDMF support for pinch phase!
		local pinchtag = LE_PINCHPHASE
		if udmf and mo.spawnpoint and mo.spawnpoint.valid then pinchtag = mo.spawnpoint.args[4] end
		P_LinedefExecute(pinchtag, mo, nil)
	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
	--$Arg0 Boss ID
	--$Arg1 End level on death?
	--$Arg1Type 11
	--$Arg1Enum noyes
	--$Arg2 Death trigger tag
	--$Arg2Type 15
	--$Arg3 Victory trigger tag
	--$Arg3Type 15
	--$Arg4 Pinch trigger tag
	--$Arg4Type 15
	--$Arg5 Speed up when hit?
	--$Arg5Type 11
	--$Arg5Enum yesno
	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(mo)
	if not (mo and mo.valid) then return end
	
	if not (mo.target and mo.target.valid) or not mo.target.health 
	or mo.target.state == mo.target.info.spawnstate 
	or mo.target.state == mo.target.info.raisestate then
		P_RemoveMobj(mo)
		return
	end
	P_MoveOrigin(mo, mo.target.x, mo.target.y, mo.target.z - mo.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 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}
