//Script to restore 1.09.4 glide behavior.
//Translated from C to Lua by Ikkarou Tatsuru
//Tweaked and 2.2-ified by TrickyTex
local function ChucklesJumpThinker(p)
	if p.mo.skin != "uglyknux" return end
	
	if p.mo.skin == "uglyknux" and (p.pflags & PF_GLIDING)
		local leeway

		if (p.mo.momz == (-2*FRACUNIT)/TICRATE)
			p.mo.momz = (-2*FRACUNIT)/TICRATE
		elseif (p.mo.momz < (-2*FRACUNIT)/TICRATE)
			p.mo.momz = $ + (3*(FRACUNIT/4))/TICRATE
		end

		-- Strafing while gliding.
		leeway = FixedAngle(p.cmd.sidemove * (FRACUNIT/2))

		if p.mo.skin == "uglyknux" and ((p.mo.eflags & MFE_UNDERWATER))
			P_InstaThrust(p.mo, p.mo.angle - leeway, ((p.actionspd*(p.powers[pw_super] and 40 or 20)) + p.glidetime * 750)/TICRATE)
		elseif p.mo.skin == "uglyknux"
			P_InstaThrust(p.mo, p.mo.angle - leeway, ((p.actionspd*(p.powers[pw_super] and 80 or 40)) + p.glidetime * 1500)/TICRATE)
		end
			
		p.glidetime = $ + 1

		if p.mo.skin == "uglyknux" and not (p.pflags & PF_JUMPDOWN) -- If not holding the jump button
			P_ResetPlayer(p) -- down, stop gliding.
			
			if p.mo.skin == "uglyknux" and ((p.charability2 == CA2_MULTIABILITY)
			or (p.powers[pw_super]
			and All7Emeralds(p.powers[pw_emeralds])
			and p.mo.skin != "uglyknux" and p.charability == CA_GLIDEANDCLIMB))
				p.pflags = $ | PF_JUMPED
				p.mo.state = S_PLAY_ATK1
			else
				p.mo.momx = $ >> 1
				p.mo.momy = $ >> 1
				p.mo.state = S_PLAY_FALL1
			end
		end
	elseif p.mo.skin == "uglyknux" and p.climbing -- 'Deceleration' for climbing on walls.
		if (p.mo.momz > 0)
			p.mo.momz = $ - FRACUNIT/(TICRATE*2)
		elseif (p.mo.momz < 0)
			p.mo.momz = $ + FRACUNIT/(TICRATE*2)
		end
	end
end

addHook("JumpSpecial", ChucklesJumpThinker)

addHook("PlayerThink", function(player)
    if (player.mo and player.mo.valid and player.mo.skin ~= "uglyknux") then
        return
    end

    if (player.climbing > 1) then
        player.pflags = (player.pflags | PF_FORCESTRAFE)
    elseif (player.climbing) then
        player.mo.angle = player.mo.angle
    elseif (player.climbing == 0)
    and (player.pflags & PF_FORCESTRAFE) then
        player.pflags = (player.pflags & ~PF_FORCESTRAFE)
    end
end)

//What if i write a whole story here?
//I don't care if no one looks at this, I'll have a reason to cancel this mod.
//'OMG FILESIZE TOO BEEG CANNOT CONTINUE MOD SORRY'
//Yeah this will probably happen someday.
//Let's start my big story!!
//Once upon a time, a little boi called Johnny Wallbank (get it? get it? pffhaha im so funny)
//Walks with his friend A.J. Freda (pfft!) in the forest.
//And what did they see?
//A PINETREE SONIC!!!!!!! enjoy your mod that's as big as your mom (pffffft!!!!)
//I might copy-paste this in the lua scripts.. this will be fun