addHook("PlayerThink", function(p)
	if p.sjtimer == nil
		p.sjtimer = 0
	end
	if p.superjumpcharge == nil
		p.superjumpcharge = false
	end
	if p.sjtimer > 40
		p.sjtimer = 40
	end
end)

local function resetsuperjump(p)
	p.sjtimer = 0
	p.superjumpcharge = false
end

rawset(_G, "CheckCel", function(player, dist)
	local cel = player.mo.ceilingz
	local celcheck = player.mo.z >= (cel - (player.mo.height+dist))
	if P_MobjFlip(player.mo) == 1
		cel = player.mo.ceilingz
		celcheck = player.mo.z >= (cel - (player.mo.height+dist))
	else
		cel = player.mo.floorz
		celcheck = player.mo.z+player.mo.height <= (cel + (player.mo.height+dist))
	end
	if celcheck
		return true
	else
		return false
	end
end)

freeslot("S_PLAY_CELHIT")

states[S_PLAY_CELHIT] = {
	sprite = SPR_PLAY,
	frame = SPR2_MLEL|FF_ANIMATE,
	tics = 20,
	var1 = 2,
	var2 = 2,
	nextstate = S_PLAY_PP_ROLLIN
}

freeslot("sfx_pchrge", "sfx_prlse","S_PLAY_SUPERCHARGE","S_PLAY_PP_SJSTART","S_EXPLOSIONEFFECT","SPR_EPLO")
sfxinfo[sfx_pchrge].caption = "Charging"
sfxinfo[sfx_prlse].caption = "Release" --the sfxs fked up somehow had to switch the captions --fixed it

states[S_PLAY_SUPERCHARGE] = {
	sprite = SPR_PLAY,
	frame = SPR2_LAND,
	tics = 2,
	nextstate = S_PLAY_SUPERCHARGE
}

states[S_PLAY_PP_SJSTART] = {
	sprite = SPR_PLAY,
	frame = SPR2_BNCE|FF_ANIMATE,
	tics = 10,
	var1 = 2,
	var2 = 2,
	nextstate = S_PLAY_SUPERCHARGE
}

states[S_EXPLOSIONEFFECT] = {
	sprite = SPR_EPLO,
	frame = A|FF_ANIMATE,
	tics = 18,
	var1 = 9,
	var2 = 2,
	nextstate = S_DEATHSTATE
}

local function idk(p)
	local idk = P_SpawnMobj(p.mo.x,p.mo.y,p.mo.z,MT_THOK)
	idk.state = S_EXPLOSIONEFFECT
	if (p.mo.eflags & MFE_VERTICALFLIP)
		idk.eflags = $|MFE_VERTICALFLIP
		idk.flags2 = $|MF2_OBJECTFLIP
	end
end

local function pain(p)
	if P_PlayerInPain(p)
		p.superjumpcharge = false
		p.mo.state = S_PLAY_PAIN
	end
end

addHook("ThinkFrame", do
	for p in players.iterate
		if p.c2tics == nil
			p.c2tics = 0
		end
		if (p.cmd.buttons & BT_CUSTOM2)
			p.c2tics = $1+1
		else
			p.c2tics = 0
		end
		if p.mo.skin == "prpeppino"
		and P_IsObjectOnGround(p.mo)
		and (p.mo.state == S_PLAY_PP_MACH3 or p.mo.state == S_PLAY_PP_MACH4)
		and p.c2tics == 1
			p.superjumpcharge = true
			p.mo.state = S_PLAY_PP_SJSTART
		end
		if p.superjumpcharge
			pain(p)
			p.machcounter = 0*FRACUNIT
			p.machspeed = 0
			p.pflags = ($|PF_STASIS|PF_JUMPSTASIS)
			if p.mo.state ~= S_PLAY_PP_SJSTART
			and not P_PlayerInPain(p)
				p.mo.state = S_PLAY_SUPERCHARGE
			end
			if not S_SoundPlaying(p.mo, sfx_pchrge)
				S_StartSound(p.mo, sfx_pchrge)
			end
		end
		if p.superjumpcharge
		and p.mo.state ~= S_PLAY_PP_SJSTART
		and not (p.cmd.buttons & BT_CUSTOM2)
			p.mo.state = S_PLAY_PP_SUPERJUMP
			idk(p)
			p.pflags = ($ & ~PF_STASIS|PF_JUMPSTASIS)
			p.superjumpcharge = false
			S_StartSound(p.mo, sfx_prlse)
			if S_SoundPlaying(p.mo, sfx_pchrge)
				S_StopSoundByID(p.mo, sfx_pchrge)
			end
		end
	end
end)

addHook("PlayerThink", function(p)
	if p.mo.state == S_PLAY_PP_SUPERJUMP
		P_SetObjectMomZ(p.mo, 40*FRACUNIT)
		p.sjtimer = $1+1
		p.mo.momx = 0
		p.mo.momy = 0
	end
	if p.sjtimer == 40
		p.mo.state = S_PLAY_PP_ROLLIN
		p.mo.momy = p.mo.momy
		p.mo.momx = p.mo.momx
		P_SetObjectMomZ(p.mo, 9*FRACUNIT)
		p.sjtimer = 0
	end
end)

addHook("PlayerThink", function(p)
	if p.celhit == nil
		p.celhit = false
	end
	if p.mo.state == S_PLAY_PP_SUPERJUMP
	and CheckCel(p, FRACUNIT)
		p.celhit = true
		S_StartSound(p.mo, sfx_bonk)
	end
	if p.celhit == true
		p.shouldmach3 = false
		p.mo.momx = 0
		p.mo.momy = 0
		p.mo.momz = 0
		resetsuperjump(p)
		p.powers[pw_nocontrol] = 150000000000000000
		if p.mo.state ~= S_PLAY_CELHIT
		and p.mo.state ~= S_PLAY_PP_ROLLIN
			p.mo.state = S_PLAY_CELHIT
		end
		if p.mo.state == S_PLAY_PP_ROLLIN
		and p.celhit
			p.celhit = false
			p.mo.momx = p.mo.momx
			p.mo.momz = p.mo.momz
			p.mo.momy = p.mo.momy
			p.powers[pw_nocontrol] = 0
		end
	end
end)