addHook("PlayerThink",function(p)
	if p.spinbuf == nil
		p.spinbuf = 0
		p.dropped = false
		p.cust1buf = 0
	end
	if p.cmd.buttons & BT_SPIN
		p.spinbuf = $+1
	else
		p.spinbuf = 0
	end
	
	if p.cmd.buttons & BT_CUSTOM1
		p.cust1buf = $+1
	else
		p.cust1buf = 0
	end
	
	if P_IsObjectOnGround(p.mo) or p.mo.eflags & MFE_SPRUNG
	or P_PlayerInPain(p)
	or p.powers[pw_carry] >= 1
		p.dropped = false
	end
	if p.dropped == true and p.mo.skin == "sonic"
		P_SpawnGhostMobj(p.mo)
	end
	
	if p.dashmode == 108
		if not P_IsObjectOnGround(p.mo)
			p.dashmode = 108
		end
	end
	
	if p.mo.skin == "sonic"
		if P_IsObjectOnGround(p.mo)
			if p.cust1buf == 1
				S_StartSound(p.mo,sfx_cdfm01)
				p.mo.angle = $+ANGLE_180
				P_InstaThrust(p.mo,p.mo.angle,p.speed)
			end
		end
	end
end)

addHook("JumpSpinSpecial",function(p)
	if not p.mo and p.mo.valid and p.mo.skin == "sonic"
	or p.exiting
	or P_PlayerInPain(p)
	or p.powers[pw_shield] >= 5
	or p.powers[pw_shield] == 2
	or p.powers[pw_shield] == 3
		return
	end
	
	if p.mo.skin == "sonic"
		if p.pflags & PF_JUMPED
			if p.spinbuf == 1 and not p.dropped
				P_SetObjectMomZ(p.mo,-40*FRACUNIT,false)
				S_StartSound(p.mo,sfx_kc65)
				S_StartSound(p.mo,sfx_zoom)
				p.dropped = true
			end
		end
	end
end)

addHook("AbilitySpecial", function (p)
	
	local thokcharge
	local orientation = 1
	
	if (p.mo.eflags & MFE_VERTICALFLIP)
		orientation = -1
	else
		orientation = 1
	end
	
	if thokcharge == nil
		p.thokcharge = true
	end
	
	if P_IsObjectOnGround(p.mo)
		p.thokcharge = true
	end
	
	if not p.mo
	or p.mo.skin ~= "sonic"
	or (p.pflags & PF_JUMPDOWN)
	or not p.thokcharge
	or (p.pflags & PF_THOKKED)
	or (p.pflags & PF_SHIELDABILITY)
	or p.gotflag > 0
		return
	end
	
	if not p.thokcharge or P_IsObjectOnGround(p.mo)
	end
	
	if p.pflags & PF_THOKKED and p.cmd.buttons & BT_JUMP
	end
	
	if p.thokcharge 
		and not P_IsObjectOnGround(p.mo) then
			if p.speed > p.normalspeed/2 then
				local mo = p.mo
				local angle = p.mo.angle
		
				P_InstaThrust(mo, angle, p.speed+(20*FRACUNIT))
				mo.momz = FixedMul(10*FRACUNIT*orientation,mo.scale)
				S_StartSound(mo, sfx_thok)
				p.pflags = $ | PF_THOKKED
				p.thokcharge = false
				P_SpawnGhostMobj(mo)
				//print "you did a supersonic thok"
			else
				local mo = p.mo
				local angle = p.mo.angle
				P_InstaThrust(mo, angle, p.speed+(10 * FRACUNIT))
				mo.momz = FixedMul(5*FRACUNIT*orientation,mo.scale)
				S_StartSound(mo, sfx_thok)
				p.pflags = $ | PF_THOKKED
				p.thokcharge = false
				//print "haha you did a pity thok"
			end
		end
	end
)

addHook("PlayerThink", function(p)
	if p.pflags & PF_THOKKED and p.speed > p.normalspeed/2 and p.dashmode > 3*TICRATE
		if p.mo.skin == "sonic"
			P_SpawnGhostMobj(p.mo)
		end
	end
	if p.dashmode > 3 * TICRATE and p.mo.skin == "sonic"
//		p.normalspeed = 63
//		p.acceleration = 45
		local ghost = P_SpawnGhostMobj(p.mo)
		ghost.fuse = 4
		ghost.scale = 1*FRACUNIT
		ghost.destscale = 0
		//local trail = P_SpawnMobjFromMobj(p.mo, 0, 0, p.mo.scale, MT_THOK)
		//trail.fuse = 10
		//trail.scale = 1*FRACUNIT
		//trail.destscale = 1*FRACUNIT/4
		//trail.colorized = true
		//trail.color = p.mo.color
	end
	if not P_IsObjectOnGround(p.mo) and p.dashmode > 3*TICRATE
		p.dashmode = (7/2)*TICRATE
	end
end)


addHook("PlayerCanDamage", function(p)
	if p.mo.skin == "sonic"
		if p.pflags & PF_THOKKED or p.dropped
			return true
		end
	end
end
)

addHook("SpinSpecial", function(p)
	local angle = p.mo.angle
	local mo = p.mo
	
	if p.dashmode < 3*TICRATE
		if P_IsObjectOnGround(p.mo)
		and p.pflags & PF_SPINNING 
		and p.mo.state == S_PLAY_ROLL 
		and not (p.pflags & (PF_USEDOWN|PF_STARTDASH)) then
			p.mo.state = S_PLAY_RUN
			p.pflags = ($ & ~PF_SPINNING) | PF_USEDOWN
			P_Thrust(mo, angle, 12 * FRACUNIT)
			S_StartSound(mo,sfx_zoom)
//			print "wow you uncurl boosted"
			return true
		end
	elseif p.dashmode > 3*TICRATE
		if P_IsObjectOnGround(p.mo)
		and p.pflags & PF_SPINNING 
		and p.mo.state == S_PLAY_ROLL 
		and not (p.pflags & (PF_USEDOWN|PF_STARTDASH)) then
			p.mo.state = S_PLAY_RUN
			p.pflags = ($ & ~PF_SPINNING) | PF_USEDOWN
//			print "wowie you didn't uncurl boost"
			return true
		end
	end
end)

local function Break_shit (plr, thing)
	-- copied from p_map.c PIT_CheckThing
	if (thing.type == MT_SPIKE
	or thing.type == MT_WALLSPIKE)
	then
		if (thing.flags & MF_SOLID) and ((thing.type == MT_SPIKE) or (thing.type == MT_WALLSPIKE))
		then
			S_StartSound(plr, thing.info.deathsound)
		end
		for iter in thing.subsector.sector.thinglist()
		do
			-- this is a fucked formatting
			if (
				iter.type == thing.type and
				iter.health > 0 and
				( iter.flags & MF_SOLID ) and
				(iter == thing or
				P_AproxDistance(P_AproxDistance(
				thing.x - iter.x, thing.y - iter.y),
				thing.z - iter.z) < 56*thing.scale))
			then
				P_KillMobj(iter, plr, tmthing, 0)
			end
		end
		return 2
		elseif (( thing.flags & MF_MONITOR ))
		then
		if (P_DamageMobj(thing, tmthing, tmthing, 1, 0))
		then
			return 2
		end
	end
end

addHook ("MobjMoveCollide", function (plr, thing)
	if plr.player ~= nil and plr.player.dashmode >= 3*TICRATE
		if (thing.type == MT_SPIKE
		or thing.type == MT_WALLSPIKE)
			if (plr.z + plr.height < thing.z) 
			or (plr.z > thing.z + thing.height)
				return false
			else
				return Break_shit(plr, thing)
			end
		end
	end
end)

