// 2.1 Egg Slimer - Ported from 2.1.25

freeslot(
	// Sprites
	"SPR_21EN",
	"SPR_2TKA",
	"SPR_2TKB",
	"SPR_2SPK",
	"SPR_OGOP",
	// Objects
	"MT_21EGGMOBILE2",
	"MT_21EGGMOBILE2_POGO",
	"MT_20BOSSJUNK",
	"MT_21GOOP",
	// States
	"S_21EGGMOBILE2_STND",
	"S_21EGGMOBILE2_POGO1",
	"S_21EGGMOBILE2_POGO2",
	"S_21EGGMOBILE2_POGO3",
	"S_21EGGMOBILE2_POGO4",
	"S_21EGGMOBILE2_POGO5",
	"S_21EGGMOBILE2_POGO6",
	"S_21EGGMOBILE2_POGO7",
	"S_21EGGMOBILE2_PAIN",
	"S_21EGGMOBILE2_PAIN2",
	"S_21EGGMOBILE2_DIE1",
	"S_21EGGMOBILE2_DIE2",
	"S_21EGGMOBILE2_DIE3",
	"S_21EGGMOBILE2_DIE4",
	"S_21EGGMOBILE2_DIE5",
	"S_21EGGMOBILE2_DIE6",
	"S_21EGGMOBILE2_DIE7",
	"S_21EGGMOBILE2_DIE8",
	"S_21EGGMOBILE2_DIE9",
	"S_21EGGMOBILE2_DIE10",
	"S_21EGGMOBILE2_DIE11",
	"S_21EGGMOBILE2_DIE12",
	"S_21EGGMOBILE2_DIE13",
	"S_21EGGMOBILE2_DIE14",
	"S_21EGGMOBILE2_FLEE1",
	"S_21EGGMOBILE2_FLEE2",
	"S_20BOSSTANK1",
	"S_20BOSSTANK2",
	"S_20BOSSSPIGOT",
	"S_21GOOP1",
	"S_21GOOP2",
	"S_21GOOP3"
	)

/*
// Mimics the A_Boss2Chase behavior of versions 1.01-1.09.2
// Unused
function A_FDBoss2Chase(actor, var1, var2)
	local radius = 384*FRACUNIT
	local reversed = false
	
	// When reactiontime hits zero, he will go the other way
	if actor.reactiontime
		actor.reactiontime = $ - 1
	end
	
	if actor.reactiontime <= 0
		reversed = true
		actor.reactiontime = 2*TICRATE + P_RandomByte()
	end
	
	actor.target = P_GetClosestAxis(actor)
	
	if not actor.target // This should NEVER happen.
		CONS_Printf("Error: Boss2 has not target!\n")
		A_BossDeath(actor)
		return
	end
	
	if reversed
		actor.watertop = $ * -1
	end
	
	actor.target.angle = $ + FixedMul(actor.watertop, ANG1)
	local fa = actor.target.angle
	local fc = FixedMul(cos(fa), radius)
	local fs = FixedMul(sin(fa), radius)
	actor.angle = R_PointToAngle2(actor.x, actor.y, actor.target.x + fc, actor.target.y + fs)
	// Emulate the choppiness of the original boss
	if (leveltime & 1)
		P_TeleportMove(actor, actor.target.x + fc, actor.target.y + fs, actor.target.z + 64*FRACUNIT)
	end
	
	// Put goop spraying code here
	if leveltime % 14 == 1
		local ns = 3 * FRACUNIT
		local goop
		local fz = actor.z+actor.height+56*FRACUNIT
		local fa2
		// actor.movedir is used to determine the last
		// direction goo was sprayed in. There are 8 possible
		// directions to spray. (45-degree increments)
		
		actor.movedir = $+1
		actor.movedir = $%8 // 8 is represented by NUMDIRS in the original code
		fa2 = (actor.movedir*45)
		
		goop = P_SpawnMobj(actor.x, actor.y, fz, actor.info.painchance)
		goop.momx = FixedMul(sin(fa2), ns)
		goop.momy = FixedMul(cos(fa2), ns)
		goop.momz = 4*FRACUNIT
		goop.fuse = 30*TICRATE+P_RandomByte()
		if actor.info.attacksound
			S_StartSound(actor, actor.info.attacksound)
		end
		
		if (P_RandomByte() & 1)
			goop.momx = $*2
			goop.momy = $*2
		elseif (P_RandomByte() > 128)
			goop.momx = $*3
			goop.momy = $*3
		end
		
		actor.flags2 = $|MF2_JUSTATTACKED
	end
end
*/

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

addHook("BossThinker", function(mobj)
	if mobj.movecount
		mobj.movecount = $ - 1
	end
	
	if not mobj.movecount
		mobj.flags2 = $ & ~MF2_FRET
	end
	
	if not mobj.tracer
		//var1 = 0
		A_21BossJetFume(mobj, 0, 0)
	end
	
	if mobj.health <= mobj.info.damage and (not mobj.target or not (mobj.target.flags & MF_SHOOTABLE))
		if mobj.health <= 0
			// Look for a new target
			if P_BossTargetPlayer(mobj, false) and mobj.info.mass // 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 // Bid farewell!
			S_StartSound(mobj, mobj.info.mass)
		end
		
		return true
	end
	
	if mobj.state == mobj.info.spawnstate and mobj.health > mobj.info.damage
		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
		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 target.health > 1
		target.flags2 = $|MF2_FRET
	end
	
	target.health = target.health - 1
	
	if target.health <= 0
		P_KillMobj(target, inflictor, source, damagetype)
		return true
	end
	
	if target.health < target.info.damage
		target.state = target.info.meleestate
	else
		target.state = target.info.painstate
	end
	
	target.reactiontime = 0
	
	return true
end, MT_21EGGMOBILE2)

// Ported from p_mobj.c in 2.1.25
// Gives the invisible pogo barrier a brain
addHook("MobjThinker", function(mobj)
	if not mobj.target or not mobj.target.health or mobj.target.state == mobj.target.info.spawnstate or mobj.target.state == mobj.target.info.raisestate
		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)
	// sanity checks
	if not special.target or not special.target.health
		return
	end
	// Goomba Stomp'd!
	if special.target.momz < 0
		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)

// Display the 2.1 Egg Slimer's "gibs" when it dies
addHook("BossDeath", function(mo)
	A_21BossDeath(mo)
	
	local mo2 = P_SpawnMobj(mo.x + P_ReturnThrustX(mo, mo.angle - ANGLE_90, FixedMul(32*FRACUNIT, mo.scale)),
							mo.y + P_ReturnThrustY(mo, mo.angle - ANGLE_90, FixedMul(32*FRACUNIT, mo.scale)),
							mo.z + mo.height/2 + ((mo.eflags & MFE_VERTICALFLIP) and FixedMul(8*FRACUNIT, mo.scale) - mobjinfo[MT_20BOSSJUNK].height or -FixedMul(8*FRACUNIT, mo.scale)),
							MT_20BOSSJUNK)
	mo2.angle = mo.angle
	mo2.scale = mo.scale
	if (mo.eflags & MFE_VERTICALFLIP)
		mo2.eflags = $|MFE_VERTICALFLIP
		mo2.flags2 = $|MF2_OBJECTFLIP
	end
	P_InstaThrust(mo2, mo2.angle - ANGLE_90, FixedMul(4*FRACUNIT, mo2.scale))
	mo2.momz = 4*FRACUNIT
	mo2.state = S_20BOSSTANK1
	
	mo2 = P_SpawnMobj(mo.x + P_ReturnThrustX(mo, mo.angle + ANGLE_90, FixedMul(32*FRACUNIT, mo.scale)),
							mo.y + P_ReturnThrustY(mo, mo.angle + ANGLE_90, FixedMul(32*FRACUNIT, mo.scale)),
							mo.z + mo.height/2 + ((mo.eflags & MFE_VERTICALFLIP) and FixedMul(8*FRACUNIT, mo.scale) - mobjinfo[MT_20BOSSJUNK].height or -FixedMul(8*FRACUNIT, mo.scale)),
							MT_20BOSSJUNK)
	mo2.angle = mo.angle
	mo2.scale = mo.scale
	if (mo.eflags & MFE_VERTICALFLIP)
		mo2.eflags = $|MFE_VERTICALFLIP
		mo2.flags2 = $|MF2_OBJECTFLIP
	end
	P_InstaThrust(mo2, mo2.angle + ANGLE_90, FixedMul(4*FRACUNIT, mo2.scale))
	mo2.momz = 4*FRACUNIT
	mo2.state = S_20BOSSTANK2
	
	mo2 = P_SpawnMobj(mo.x, mo.y,
					mo.z + ((mo.eflags & MFE_VERTICALFLIP) and mobjinfo[MT_20BOSSJUNK].height-FixedMul(32*FRACUNIT, mo.scale) or mo.height + FixedMul(32*FRACUNIT, mo.scale)),
					MT_20BOSSJUNK)
	mo2.angle = mo.angle
	mo2.scale = mo.scale
	if (mo.eflags & MFE_VERTICALFLIP)
		mo2.eflags = $|MFE_VERTICALFLIP
		mo2.flags2 = $|MF2_OBJECTFLIP
	end
	mo2.momz = 4*FRACUNIT
	mo2.state = S_20BOSSSPIGOT
	
	return true
end, MT_21EGGMOBILE2)
// 2.1 Egg Slimer

mobjinfo[MT_21EGGMOBILE2] = {
	//$Name 2.1 Egg Slimer
	//$Sprite 21ENA1
	//$Category Bosses
	doomednum = 4041,
	spawnstate = S_21EGGMOBILE2_STND,
	spawnhealth = 8,
	reactiontime = -666,
	attacksound = sfx_gspray,
	painstate = S_21EGGMOBILE2_PAIN,
	painchance = MT_21GOOP, // Replace with MT_21GOOP
	painsound = sfx_odmpai,
	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_STND] = {
	sprite = SPR_21EN,
	frame = A,
	tics = -1
}

states[S_21EGGMOBILE2_POGO1] = {
	sprite = SPR_21EN,
	frame = B,
	tics = 4,
	nextstate = S_21EGGMOBILE2_POGO2
}

states[S_21EGGMOBILE2_POGO2] = {
	sprite = SPR_21EN,
	frame = A,
	tics = 2,
	action = A_Boss2PogoTarget, // Replace this with A_21Boss2PogoTarget?
	var1 = 9*FRACUNIT,
	var2 = 8*FRACUNIT,
	nextstate = S_21EGGMOBILE2_POGO3
}

states[S_21EGGMOBILE2_POGO3] = {
	sprite = SPR_21EN,
	frame = B,
	tics = 2,
	nextstate = S_21EGGMOBILE2_POGO4
}

states[S_21EGGMOBILE2_POGO4] = {
	sprite = SPR_21EN,
	frame = C,
	tics = -1
}

states[S_21EGGMOBILE2_POGO5] = {
	sprite = SPR_21EN,
	frame = B,
	tics = 4,
	nextstate = S_21EGGMOBILE2_POGO6
}

states[S_21EGGMOBILE2_POGO6] = {
	sprite = SPR_21EN,
	frame = A,
	tics = 2,
	action = A_Boss2PogoTarget, // Replace this with A_21Boss2PogoTarget?
	var1 = 7*FRACUNIT,
	var2 = 8*FRACUNIT,
	nextstate = S_21EGGMOBILE2_POGO7
}

states[S_21EGGMOBILE2_POGO7] = {
	sprite = SPR_21EN,
	frame = B,
	tics = 2,
	nextstate = S_21EGGMOBILE2_POGO4
}

states[S_21EGGMOBILE2_PAIN] = {
	sprite = SPR_21EN,
	frame = D,
	tics = 24,
	action = A_Boss2TakeDamage, // Replace this with A_21Boss2TakeDamage if it changes in the future
	var1 = 24+TICRATE,
	var2 = 0,
	nextstate = S_21EGGMOBILE2_STND
}

states[S_21EGGMOBILE2_PAIN2] = {
	sprite = SPR_21EN,
	frame = E,
	tics = 24,
	action = A_Boss2TakeDamage, // Replace this with A_21Boss2TakeDamage if it changes in the future
	var1 = 24+TICRATE,
	var2 = 0,
	nextstate = S_21EGGMOBILE2_POGO4
}

states[S_21EGGMOBILE2_DIE1] = {
	sprite = SPR_21EN,
	frame = F,
	tics = 8,
	action = A_Fall,
	nextstate = S_21EGGMOBILE2_DIE2
}

states[S_21EGGMOBILE2_DIE2] = {
	sprite = SPR_21EN,
	frame = F,
	tics = 8,
	action = A_BossScream,
	var1 = 0,
	var2 = MT_BOSSEXPLODE,
	nextstate = S_21EGGMOBILE2_DIE3
}

states[S_21EGGMOBILE2_DIE3] = {
	sprite = SPR_21EN,
	frame = F,
	tics = 8,
	action = A_Repeat,
	var1 = 12,
	var2 = S_21EGGMOBILE2_DIE2,
	nextstate = S_21EGGMOBILE2_DIE4
}

states[S_21EGGMOBILE2_DIE4] = {
	sprite = SPR_21EN,
	frame = F,
	tics = -1,
	action = A_BossDeath
}

states[S_21EGGMOBILE2_FLEE1] = {
	sprite = SPR_21EN,
	frame = G,
	tics = 5,
	nextstate = S_21EGGMOBILE2_FLEE2
}

states[S_21EGGMOBILE2_FLEE2] = {
	sprite = SPR_21EN,
	frame = H,
	tics = 5,
	nextstate = S_21EGGMOBILE2_FLEE1
}

// 2.1 Egg Slimer Invisible Pogo Barrier

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

// 2.1 Egg Slimer "Gibs" (Compatible with 2.0 Egg Slimer)

mobjinfo[MT_20BOSSJUNK] = {
	doomednum = -1,
	spawnstate = S_INVISIBLE, // Invisible unless its state is altered
	spawnhealth = 1,
	reactiontime = 8,
	radius = 8*FRACUNIT,
	height = 64*FRACUNIT,
	flags = MF_NOBLOCKMAP
}

states[S_20BOSSTANK1] = {
	sprite = SPR_2TKA,
	frame = A,
	tics = 35
}

states[S_20BOSSTANK2] = {
	sprite = SPR_2TKB,
	frame = A,
	tics = 35
}

states[S_20BOSSSPIGOT] = {
	sprite = SPR_2SPK,
	frame = A,
	tics = 35
}

// 2.1 Egg Slimer Goop

addHook("MobjThinker", function(mo)
	if mo.z <= mo.floorz and not (mo.state == mo.info.meleestate)
		mo.state = mo.info.meleestate
		mo.momx = 0
		mo.momy = 0
		mo.momz = 0
		mo.z = mo.floorz
		if mo.info.painsound
			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] = {
	sprite = SPR_OGOP,
	frame = A,
	tics = 2,
	nextstate = S_21GOOP2
}

states[S_21GOOP2] = {
	sprite = SPR_OGOP,
	frame = B,
	tics = 2,
	nextstate = S_21GOOP1
}

states[S_21GOOP3] = {
	sprite = SPR_OGOP,
	frame = C,
	tics = -1,
}
