freeslot("sfx_egett", "S_PLAY_ALTFALL1", "S_PLAY_ALTFALL2")

states[S_PLAY_ALTFALL1] = {SPR_PLAY, SPR2_TIRE_1, 12, nil, 0, 0, S_PLAY_ALTFALL2}
states[S_PLAY_ALTFALL2] = {SPR_PLAY, SPR2_TIRE_2, 12, nil, 0, 0, S_PLAY_ALTFALL1}


//Adds the ability to detect bustable FOF's 
rawset(_G, "eggetteblockbuster", function(player, bustsec)
	for rover in bustsec.ffloors() // search through FOFs in sector
		if not (rover.flags & FF_EXISTS) continue end // FOF does not "exist" in sector!
		if not (rover.flags & FF_BUSTUP) continue end // FOF is not bustable, ignore it
		
		if player.mo.z >= rover.topheight+FRACUNIT // too high
		continue
		end

		if player.mo.z+player.mo.momz+player.mo.height < rover.bottomheight // too low
			continue
		end

		EV_CrumbleChain(bustsec, rover) // DESTROY IT
	end
end)


// "Spin" dust for Eggy boi. Made by MotdSpork and modified from ModernSonic's Slide.
addHook("ThinkFrame", do
	for player in players.iterate
		if player.mo and player.mo.skin == "eggette"
			//Eggette flying sounds
			if player.powers[pw_tailsfly]
			and player.powers[pw_super] == 0
			and not (player.mo.eflags & MFE_UNDERWATER)
				if (leveltime % 7 == 1)
					S_StartSound(player.mo, sfx_egett)
				end
			end
			//EggBurst
			if not (player.cmd.buttons & BT_USE)
				player.spindown = false
			end
			if P_IsObjectOnGround(player.mo)
			and player.cmd.buttons & BT_USE
				player.spindown = true
			end
			if player.pflags & PF_JUMPED
			and player.playerstate == PST_LIVE
			and player.cmd.buttons & BT_USE
			and player.spindown == false
			and not player.gotflagdebuff
			and not (player.pflags & PF_THOKKED)
			and not (P_SuperReady(player))
			and not (player.mo.state >= S_PLAY_SUPER_TRANS1 and player.mo.state <= S_PLAY_SUPER_TRANS6)
				player.spindown = true
				player.drawangle = player.mo.angle
				player.mo.state = S_PLAY_RUN
				if not (player.mo.eflags & MFE_UNDERWATER)
					if player.powers[pw_super]
					or player.powers[pw_sneakers]
						P_InstaThrust(player.mo, player.mo.angle, FixedMul(65*FRACUNIT, player.mo.scale))
					else
						P_InstaThrust(player.mo, player.mo.angle, FixedMul(36*FRACUNIT, player.mo.scale))
					end
					player.mo.momz = FixedMul(9*FRACUNIT*P_MobjFlip(player.mo), player.mo.scale)
				else
					if player.powers[pw_super]
					or player.powers[pw_sneakers]
						P_InstaThrust(player.mo, player.mo.angle, FixedMul(36*FRACUNIT, player.mo.scale))
					else
						P_InstaThrust(player.mo, player.mo.angle, FixedMul(36*FRACUNIT/2, player.mo.scale))
					end
					player.mo.momz = FixedMul(5*FRACUNIT*P_MobjFlip(player.mo), player.mo.scale)
				end
				if not (player.powers[pw_super])
					S_StartSound(player.mo, sfx_kc4d)
					player.pflags = $1|PF_THOKKED
					for i = 0,31
						local offset = FixedMul(1*FRACUNIT, player.mo.scale)
						local dust = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z + offset, MT_FLAMEJETFLAME)
						dust.target = player.mo
						dust.destscale = player.mo.scale/6
						dust.scalespeed = FRACUNIT/20
						if player.mo.eflags & MFE_VERTICALFLIP then
							dust.flags2 = $1|MF2_OBJECTFLIP // flip gravity!
						end
						local an = i*ANGLE_11hh
						local speed = FixedMul(dust.info.speed/FRACUNIT, player.mo.scale)
						dust.momx = speed*cos(an)
						dust.momy = speed*sin(an)
						dust.momz = 0
					end
				else
					P_SpawnGhostMobj(player.mo)
				end
			end
			if player.pflags & PF_THOKKED
				if player.mo.state == S_PLAY_RUN
				and player.mo.momz < FixedMul(0, player.mo.scale)
				and P_MobjFlip(player.mo) == 1
				or player.mo.state == S_PLAY_RUN
				and player.mo.momz > FixedMul(0, player.mo.scale)
				and P_MobjFlip(player.mo) == -1
				or player.mo.state == S_PLAY_WALK
					if not (player.mo.eflags & MFE_UNDERWATER)
						S_StartSound(player.mo, sfx_kc59)
						player.mo.state = S_PLAY_ALTFALL1
					else
						player.mo.state = S_PLAY_FALL
					end
				end
			end
			//Super Egg WallBreaker
			if player.powers[pw_super]
			or (player.pflags & PF_SPINNING and not (player.pflags & PF_JUMPED))
				local sectordetector = R_PointInSubsector(player.mo.x, player.mo.y).sector

				eggetteblockbuster(player, sectordetector) // check sector you're in
		
				if R_PointInSubsector(player.mo.x+(player.mo.momx/2), player.mo.y + (player.mo.momy/2)).sector != sectordetector
					sectordetector = R_PointInSubsector(player.mo.x+(player.mo.momx/2), player.mo.y + (player.mo.momy/2)).sector
					eggetteblockbuster(player, sectordetector) // sector half-way to next
				end
				if R_PointInSubsector(player.mo.x+(player.mo.momx), player.mo.y + (player.mo.momy)).sector != sectordetector
					sectordetector = R_PointInSubsector(player.mo.x+(player.mo.momx), player.mo.y + (player.mo.momy)).sector
					eggetteblockbuster(player, sectordetector) // sector at next frame
				end
				if R_PointInSubsector(player.mo.x+(player.mo.momx*2), player.mo.y + (player.mo.momy*2)).sector != sectordetector
					sectordetector = R_PointInSubsector(player.mo.x+(player.mo.momx*2), player.mo.y + (player.mo.momy*2)).sector
					eggetteblockbuster(player, sectordetector) // sector at frame after
				end	
			end
			//Special falling setup!
			if (player.mo.state == S_PLAY_FLY_TIRED) 
				if not (player.mo.eflags & MFE_UNDERWATER)
					S_StartSound(player.mo, sfx_kc59)
					player.mo.state = S_PLAY_ALTFALL1
				else
					player.mo.state = S_PLAY_FALL
				end
			end
			//Spin Dust!
			player.spinitem = MT_NULL
			if player.pflags & PF_SPINNING
			and not (player.pflags & PF_STARTDASH)
			and P_IsObjectOnGround(player.mo)
				if leveltime & 2
					S_StartSound(player.mo, sfx_s3k7e)
				end
				for i=(leveltime%7)/2,(leveltime%7) do
					local particle = P_SpawnMobj(player.mo.x, player.mo.y,
					player.mo.z + ((player.mo.eflags & MFE_VERTICALFLIP)/MFE_VERTICALFLIP * (player.mo.height - mobjinfo[MT_SPINDUST].height)), MT_SPINDUST)
					if (player.powers[pw_shield] == SH_ELEMENTAL and not (player.mo.eflags & MFE_TOUCHWATER or player.mo.eflags & MFE_UNDERWATER)) then
						particle.state = S_SPINDUST_FIRE1
						particle.bubble = false
					elseif (player.mo.eflags & MFE_TOUCHWATER or player.mo.eflags & MFE_UNDERWATER) then
						particle.state = S_SPINDUST_BUBBLE1
						particle.bubble = true
					else
						particle.state = S_SPINDUST1
						particle.bubble = false
					end
					particle.target = player.mo
					particle.eflags = $1 | (player.mo.eflags & MFE_VERTICALFLIP)
					particle.scale = player.mo.scale*2/3
					P_SetObjectMomZ(particle, player.speed/FRACUNIT*FRACUNIT/50+P_RandomByte()<<10, false)
					P_InstaThrust(particle, player.drawangle+(P_RandomRange(-ANG30/FRACUNIT, ANG30/FRACUNIT)*FRACUNIT), FixedMul(-player.speed/FRACUNIT*FRACUNIT/6-1*FRACUNIT-P_RandomByte()<<11, player.mo.scale))
					P_TryMove(particle, particle.x+particle.momx, particle.y+particle.momy, true)
				end
			end
		end
	end
end)	

