freeslot("sfx_shbst","S_PLAY_SHOULDERBASHSTART","SPR_SHBS")
sfxinfo[sfx_shbst].caption = "Super Jump Cancel"

states[S_PLAY_SHOULDERBASHSTART] = {
	sprite = SPR_SHBS,
	frame = A|FF_ANIMATE,
	tics = 24,
	var1 = 12,
	var2 = 2,
	nextstate = S_PLAY_PP_SHOLDERBASH -- I know I spelled it wrong oh well I'm not fixing it
}

COM_AddCommand("sj_cancel_style", function(p, arg)
	if p.ppc
	and arg == "sage"
	and p.mo and p.mo.valid
		p.sagesjcancel = true
		CONS_Printf(p, "Super jump cancel style set to Sage")
	elseif p.ppc
	and arg == "currentday"
	and p.mo and p.mo.valid
		p.sagesjcancel = false
		CONS_Printf(p, "Super jump cancel style set to current day PT")
	elseif not p.mo and p.mo.valid
		print("You need to be in a level")
	elseif not p.ppc
	and p.mo and p.mo.valid
		CONS_Printf(p, "You need to be Peppino to use this command")
	elseif arg == ""
	and p.mo and p.mo.valid
		CONS_Printf(p, "Switches the super jump cancel to sage or current day srb2")
	end
end)

addHook("PlayerThink", function(p)
	if p.sagesjcancel == nil
		p.sagesjcancel = true
	end
end)

local function ring(p)
	local ring = P_SpawnMobj(p.mo.x,p.mo.y,p.mo.z,MT_THOK)
	ring.state = S_MACH4RING
	ring.fuse = 999
	ring.tics = 20
	ring.angle = p.drawangle+ANGLE_90
	ring.scale = p.mo.scale-FRACUNIT/2
	ring.destscale = p.mo.scale*2
	ring.colorized = true
	ring.color = SKINCOLOR_WHITE
	if (p.mo.eflags & MFE_VERTICALFLIP)
		ring.flags2 = $|MF2_OBJECTFLIP
		ring.eflags = $|MFE_VERTICALFLIP
	end
end

addHook("SpinSpecial", function(p)
	if p.mo.state == S_PLAY_PP_SUPERJUMP
	and not (p.pflags & PF_USEDOWN)
	and not p.sagesjcancel
		p.mo.state = S_PLAY_SHOULDERBASHSTART
		S_StartSound(p.mo, sfx_shbst)
	end
end)

addHook("SpinSpecial", function(p)
	if p.mo.state == S_PLAY_PP_SUPERJUMP
	and not (p.pflags & PF_USEDOWN)
	and p.sagesjcancel
		p.sjtimer = 0
		p.shouldmach3 = true
		ring(p)
	end
end)

local function funny(p)
	if p.mo.state ~= S_PLAY_RUN
	and p.mo.state ~= S_PLAY_PP_MACH3
		p.mo.state = S_PLAY_RUN
	end
end

addHook("ThinkFrame", do
	for p in players.iterate
		if p.shouldmach3 == nil
			p.shouldmach3 = false
		end
		if p.shouldmach3 == true
			P_InstaThrust(p.mo,p.mo.angle,42*FRACUNIT)
			p.machcounter = 10*FRACUNIT
			funny(p)
			p.mo.momz = 0
		end
		if p.shouldmach3
		and p.mo.state == S_PLAY_PP_MACH3
			p.shouldmach3 = false
		end
		if p.shouldmach3
		and not (p.cmd.buttons & BT_USE or p.playerstate == PST_DEAD)
			p.shouldmach3 = false
		end
	end
end)
		

addHook("ThinkFrame", do
	for p in players.iterate
		if p.mo.state == S_PLAY_SHOULDERBASHSTART
			p.mo.momz = 0
			p.mo.momx = 0
			p.mo.momy = 0
			p.shoulderwait = true
		end
		if p.shoulderwait
		and p.mo.state ~= S_PLAY_SHOULDERBASHSTART
			p.shoulderwait = false
			p.mo.momz = p.mo.momz
			p.mo.momx = p.mo.momx
			p.mo.momy = p.mo.momy
			p.sjtimer = 0
			p.shouldstillthrust = false
			P_SetObjectMomZ(p.mo, 5*FRACUNIT)
			P_InstaThrust(p.mo, p.mo.angle, 24*FRACUNIT)
			p.machcounter = 10*FRACUNIT
			ring(p)
		end
		if p.mo.state == S_PLAY_PP_SHOLDERBASH
		and P_IsObjectOnGround(p.mo)
			p.mo.state = S_PLAY_RUN
		end
	end
end)