freeslot("MT_AQUABUZZ", "S_BBUZZSWIM", "S_BBUZZSWIM2", "S_BBUZZBUBLEDEATH", "SPR_BGUL")
//Made by Glaber with help from amperbee and Kays.
addHook("MobjThinker", function(mobj)
	if (mobj.tracer and mobj.tracer.player and mobj.tracer.health > 0
					and P_AproxDistance(P_AproxDistance(mobj.tracer.x - mobj.x, mobj.tracer.y - mobj.y), mobj.tracer.z - mobj.z) <= mobj.radius * 16)
					// Home in on the target.
					P_HomingAttack(mobj, mobj.tracer)

					if (mobj.z < mobj.floorz)
						mobj.z = mobj.floorz
					end
					if (leveltime % mobj.info.painchance == 0)
						S_StartSound(mobj, mobj.info.activesound)
				end
				else
					// Try to find a player
					P_LookForPlayers(mobj, mobj.radius * 16, true, true )
					mobj.momx = $ / 2
					mobj.momy = $ / 2
					mobj.momz = $ / 2
				end
end, MT_AQUABUZZ)

//Bubble Spawner for death
addHook("MobjDeath", function(target, inflictor)
local Bubble
		if (inflictor and inflictor.player // did a player kill you? Spawn relative to the player so he's bound to get it
		and FixedHypot(inflictor.x - target.x, inflictor.y - target.y) <= inflictor.radius + target.radius + FixedMul(8*FRACUNIT, inflictor.scale) // close enough?
		and inflictor.z <= target.z + target.height + FixedMul(8*FRACUNIT, inflictor.scale)
		and inflictor.z + inflictor.height >= target.z - FixedMul(8*FRACUNIT, inflictor.scale))
			Bubble = P_SpawnMobj(inflictor.x + inflictor.momx, inflictor.y + inflictor.momy, inflictor.z + (inflictor.height / 2) + inflictor.momz, MT_EXTRALARGEBUBBLE)
		else
		
			Bubble = P_SpawnMobj(target.x, target.y, target.z, MT_EXTRALARGEBUBBLE)
		Bubble.destscale = target.scale
		P_SetScale(Bubble, Bubble.destscale)
		end
end, MT_AQUABUZZ)


mobjinfo[MT_AQUABUZZ] = {
		//$Name "Bubble Buzz"
		//$Sprite BGULA1
		//$Category Retro Enemies
        doomednum = 145,
        spawnstate = S_BBUZZSWIM,
        spawnhealth = 1,
        seestate = S_BBUZZSWIM,
        seesound = sfx_None,
        reactiontime = 3*TICRATE,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = TICRATE,
        painsound = sfx_dmpain,
		meleestate = S_NULL,
		missilestate = S_NULL,
        deathstate = S_XPLD_FLICKY,
        xdeathstate = S_NULL,
        deathsound = sfx_pop,
        speed = 6*FRACUNIT,
        radius = 20*FRACUNIT,
        height = 24*FRACUNIT,
        dispoffset = 0,
        mass = 100,
        damage = 0,
        activesound = sfx_gbeep,
		raisestate = S_NULL,
        flags = MF_SLIDEME|MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}

states[S_BBUZZSWIM] = {SPR_BGUL, A, 2, nil, 0, 0, S_BBUZZSWIM2}
states[S_BBUZZSWIM2] = {SPR_BGUL, B, 2, nil, 0, 0, S_BBUZZSWIM}
//states[S_BBUZZBUBLEDEATH] = {SPR_BGUL, A, 0, A_SpawnObjectRelative, 0, (16<<16)+MT_EXTRALARGEBUBBLE, S_XPLD_FLICKY}//for bubble spawning made easy. Might replace later, or not.
