//Hi :]
local function propel()
	for player in players.iterate
        if player.mo.skin == "propeller_box" then
			if player.mo.state == S_PLAY_DEAD 
			or (player.mo.state == S_PLAY_PAIN and (not (player.pflags & PF_SLIDING))) then
				return
			end
			
			if (maptol & TOL_NIGHTS) then
				return
			end
			
			if not player.propelled then
				player.propelled = false
			end
			
			if not player.propelcount then
				player.propelcount = 0
			end
			
			if P_IsObjectOnGround(player.mo)
			or (player.powers[pw_justsprung]) then
				player.propelled = false
				player.propelcount = 0
				player.pflags = $ & ~PF_CANCARRY
			end
			
			if player.mo.state == S_PLAY_FLY then
				player.pflags = $|PF_CANCARRY|PF_JUMPED
				player.pflags = $ & ~PF_STARTJUMP
				
				
				if (not (player.mo.eflags & MFE_UNDERWATER))
				and not S_SoundPlaying(player.mo, 508) then
					S_StartSound(player.mo, 508)
				end
			end
				
			
			
			if (player.cmd.buttons & BT_JUMP) then
				if player.mo.state != S_PLAY_FLY then
					if (not (player.mo.eflags & MFE_UNDERWATER)) then
						S_StartSound(player.mo, 195)
					end
					player.mo.state = S_PLAY_FLY
				end
				
				if player.propelled == false then
					player.mo.momz = $ + 1*FRACUNIT*P_MobjFlip(player.mo)
					player.propelcount = $ + 1
					if player.propelghost and player.propelghost.valid then // Prevents the first ghost from showing, preventing weird visuals. Thanks to L_CloneFighter-v2.pk3 on Discord for helping with this.
						player.propelghost.flags2 = $ & ~MF2_DONTDRAW
					end
					player.propelghost = P_SpawnGhostMobj(player.mo) //spawns afterimages
					player.propelghost.fuse = 4 //amount of afterimages
					player.propelghost.flags2 = $|MF2_DONTDRAW // Part of preventing the first ghost from showing
					if player.propelcount == 24 then
						player.propelled = true
					end
				else
					if (player.mo.momz < -1*FRACUNIT)
					or ((player.mo.eflags & MFE_VERTICALFLIP) and (player.mo.momz > 1*FRACUNIT)) then
						player.mo.momz = $ + 1*FRACUNIT*P_MobjFlip(player.mo)
					end
				end
			end
			
			if (player.propelcount > 0)
			and (player.cmd.buttons & BT_USE) then
				player.mo.momz = $ - 1*FRACUNIT*P_MobjFlip(player.mo)
				if (not (player.mo.eflags & MFE_UNDERWATER))
				and not S_SoundPlaying(player.mo, 354) then
					S_StartSoundAtVolume(player.mo, 354, 100)
				end
				if player.propelghost and player.propelghost.valid then // Prevents the first ghost from showing, preventing weird visuals. Thanks to L_CloneFighter-v2.pk3 on Discord for helping with this.
					player.propelghost.flags2 = $ & ~MF2_DONTDRAW
				end
				player.propelghost = P_SpawnGhostMobj(player.mo) //spawns afterimages
				player.propelghost.fuse = 4 //amount of afterimages
				player.propelghost.flags2 = $|MF2_DONTDRAW // Part of preventing the first ghost from showing
				if player.propelcount == 20 then
					player.propelled = true
				end
			else
				S_StopSoundByID(player.mo, 354)
			end
			
		end
	end
end

addHook("ThinkFrame", propel)


//function that checks if a wall is bustable, then crumbles it, by Tatsuru
local function CheckAndCrumble(mo, sec)
  for fof in sec.ffloors()
    if not (fof.flags & FF_EXISTS) then continue end
    if not (fof.flags & FF_BUSTUP) then continue end
    
    if mo.z + mo.height < fof.bottomheight - 10*FRACUNIT then continue end 
    if mo.z > fof.topheight + 10*FRACUNIT then continue end

    EV_CrumbleChain(fof)
  end
end

//calls the function every time you launch down
addHook("PlayerThink", function(player)
  if not player.mo return end
  if player.mo.skin != "propeller_box" return end
  if player.mo.state != S_PLAY_FLY then return end
  if not (player.cmd.buttons & BT_USE) then return end

  CheckAndCrumble(player.mo, player.mo.subsector.sector)
end)

//also required
addHook("MobjLineCollide", function(mo, line)
  if not mo.player return end
  if mo.skin != "propeller_box" return end
  if mo.state != S_PLAY_FLY then return end
  if not (mo.player.cmd.buttons & BT_USE) then return end

  for _, sec in ipairs({line.frontsector, line.backsector})
    CheckAndCrumble(mo, sec)
  end
end, MT_PLAYER)


addHook("JumpSpecial", function(player) 
	if player.mo.skin == "propeller_box" then
		if (player.powers[pw_super])
		or player.mo.state == S_PLAY_ROLL 
		or player.mo.state == S_PLAY_RIDE
		or player.mo.state == S_PLAY_SPRING then
			return false
		else
			return true
		end
	end
end)		

addHook("PlayerCanDamage", function(player, mohit)
	if player.mo.skin == "propeller_box" then
		if player.mo.state == S_PLAY_FLY then
			if mohit.flags & MF_ENEMY
			or mohit.flags & MF_MONITOR
			or mohit.flags & MF_SHOOTABLE
			or mohit.flags & MF_BOSS then
				return true
			end
		end
	end
end)			
					
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			