addHook("PlayerThink", function(player)
	if player.mo.skin == "knuckles" and player.playerstate == PST_LIVE then
		if (player.custom1tic == 1) and player.control then
			if not (player.mo.eflags & MFE_UNDERWATER) and (player.pflags & PF_JUMPED) then
				player.pflags = $ & ~PF_JUMPED
					if player.powers[pw_super] then
						P_SetObjectMomZ(player.mo,-40*FRACUNIT)
					else
						P_SetObjectMomZ(player.mo,-20*FRACUNIT)
					end
				player.mo.state = S_PLAY_FALL
				player.pflags = $ | PF_THOKKED
			end
		end
	end
end)

addHook("PlayerThink", function(player)
	if player.isdiging == nil then
		player.isdiging = false
	end
	
	if player.mo.skin == "knuckles" and player.playerstate == PST_LIVE then
		if (player.cmd.buttons & BT_CUSTOM1) and player.control and player.isdiging == false then
			if not (player.mo.eflags & MFE_UNDERWATER) and P_IsObjectOnGround(player.mo) then
				player.isdiging = true
				//S_StartSound(player.mo, sfx_s3k4c)
				P_StartQuake(11, 11)
			end
		end
	end
	
	if player.isdiging == true then
		player.mo.state = S_INVISIBLE
		player.mo.height = 1
		player.pflags = $|PF_JUMPSTASIS
		player.mo.flags = $|MF_NOGRAVITY
		player.pflags = $ & ~PF_SPINNING
		if not S_SoundPlaying(player.mo, sfx_s3kd3l) then
			S_StartSound(player.mo, sfx_s3kd3l)
		end
		if leveltime%4 == 0 then
			local ground = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_MINUSDIRT)	
			
			ground.scale = player.mo.scale
			
			if player.mo.eflags & MFE_VERTICALFLIP then
				ground.eflags = $|MFE_VERTICALFLIP
				ground.flags2 = $|MF2_OBJECTFLIP
			else
				ground.eflags = $ & ~MFE_VERTICALFLIP
				ground.flags2 = $ & ~MF2_OBJECTFLIP
			end
		end
	end
	
	if player.isdiging == true and (player.jumptic == 1) and player.control then
		player.mo.flags = $ & ~MF_NOGRAVITY
		player.isdiging = false
		player.mo.height = skins[player.skin].height
		player.mo.state = S_PLAY_SPRING
		player.mo.momz = 12*FRACUNIT*P_MobjFlip(player.mo)
		player.pflags = $ & ~PF_JUMPSTASIS
		S_StopSoundByID(player.mo, sfx_s3kd3l)
		S_StartSound(mo, sfx_crumbl)
	end
	
	if player.isdiging == true and ((player.mo.eflags & MFE_UNDERWATER) or (P_IsObjectOnGround(player.mo) == false)) then
		player.mo.flags = $ & ~MF_NOGRAVITY
		player.isdiging = false
		player.mo.height = skins[player.skin].height
		player.mo.state = S_PLAY_STND
		player.pflags = $ & ~PF_JUMPSTASIS
		S_StopSoundByID(player.mo, sfx_s3kd3l)
	end
end)


addHook("ShouldDamage", function(target, inf, src, dmg, dmgType)
	if target.player and target.player.isdiging == true then
		return false
	end
end, MT_PLAYER)

addHook("PlayerCanDamage", function(player, target)
	if player and player.isdiging == true then
		return false
	end
end, MT_PLAYER)

addHook("PlayerCanEnterSpinGaps", function(player, target)
	if player and player.isdiging == true then
		return true
	end
end, MT_PLAYER)