-- Homing Jump Thok ability, version 2.3
-- Created by Wolfy with lots of assistance from Sryder13
-- Original Riders coding by Chaos Zero 64

-- To switch the script's selected skin, edit the restriction in the AbilitySpecial hook below and scroll down to the MobjThinker hook near the bottom, since that has another restriction.

addHook("AbilitySpecial", function(player) -- This new hook takes care of literally everything!
	if (player.mo and player.mo.skin == "rafael") then -- Skin restriction
		player.charability = CA_HOMINGTHOK
		if (not (player.pflags & PF_THOKKED) or player.charability2 == CA2_MULTIABILITY) and not (player.homing) then -- If they can use their ability and haven't already.
			-- Actionspeed cap, commented out since it makes homing way too slow.
			--[[if (player.actionspd > player.normalspeed) then -- Speed cap. Remove if using custom actionspd.
				player.actionspd = player.normalspeed 
			end]]
			--player.actionspd = $1 / 3; -- (BALANCE) TD leftover. Makes the JumpThok execute slower, so the thrust is less instant.
			-- NOTE: Thokking will divide your current x and y momentum by three, this is a hardcoded behavior. If you want to get rid of it though, just multiply them by three again.
			player.pflags = $1 & ~PF_JUMPED
			P_DoJump(player, false)
		end
	end
end) -- The hook may look like it's missing things since it's much shorter than the default JumpThok script, but basing it off of CA_HOMINGTHOK allows it to be lots simpler.

-- SupaCustardBro's (Jim) Abilities (This is not part from the original script)
//Persona freeslot
freeslot( "sfx_rfsum1", "sfx_rfsum2", "sfx_rfsum3", "sfx_rfcrt1",
	"sfx_rfskl1","sfx_rfuse1","sfx_rfhrt1", "sfx_rfhrt2",
	"sfx_rfhtx1","sfx_rfdie1", "sfx_rfhl1", "sfx_rfhl2",
	"sfx_rfhl1", "sfx_rfhl2", "sfx_rfskl1", "sfx_rfskl2",
	"sfx_rfwin2", "sfx_rfwin3", "sfx_rfsum2", "sfx_rfcrt1",
	"sfx_rfaoa1", "sfx_rfaoa1", "sfx_rfmis2", "sfx_rfmis3",
	"sfx_rfmis1", "sfx_rfmis2", "sfx_rfmis3", "sfx_rfdge1",
	"sfx_rfwin1", "sfx_rfwin2", "sfx_rfwin3", "sfx_rfwin4",
	"sfx_rfsum2", "sfx_rflvl1", "sfx_rfuse1", "SPR2_GARD",
	"SPR2_HWLP", "SPR2_HOWL")

//Checking for transformation (to avoid dive/curve)
addHook("PlayerThink", function(player)
	if (player.mo and player.mo.valid and player.mo.skin == "rafael")
		if player.mo.state != S_PLAY_SUPER_TRANS1
		and player.mo.state != S_PLAY_SUPER_TRANS2
		and player.mo.state != S_PLAY_SUPER_TRANS3
		and player.mo.state != S_PLAY_SUPER_TRANS4
		and player.mo.state != S_PLAY_SUPER_TRANS5
		and player.mo.state != S_PLAY_SUPER_TRANS6
		and not player.exiting then
			player.rafatransfrom = false
		else
			player.rafatransfrom = true
		end
	end
end)

//haha remove me! 
freeslot("S_PLAY_RAFOUCH", "SPR2_PAPF")

states[S_PLAY_RAFOUCH] = {SPR_PLAY, SPR2_PAPF, 35, nil, 0, 0, S_PLAY_RAFOUCH}

addHook("ShouldDamage", function(t, i, s, d, dt)
	if dt == DMG_CRUSHED and t.player then
        P_KillMobj(t)
		t.state = S_PLAY_RAFOUCH
		t.player.refdedpos = t.z
		t.player.rafded = 2
		return false
	end
end)

addHook("PlayerSpawn", function(player)
	player.rafded = 0
end)


//Divethok
addHook("PlayerThink", function(player)
	if (player.mo and player.mo.valid and player.mo.skin == "rafael") and not player.rafatransfrom 
		if not (player.cmd.buttons & BT_SPIN) then
			player.jumptapready = true
			player.jumptapping = false
		elseif player.jumptapready then
			player.jumptapping = true
			player.jumptapready = false
		else
			player.jumptapping = false
		end
		
		if player.jumptapping then
			if P_IsObjectOnGround(player.mo) == false and player.smakuse == false then
				P_SetObjectMomZ(player.mo, (0 - 20*FRACUNIT), true) 
				S_StartSound(player.mo,sfx_zoom, player)
				player.smakuse = true
				player.mo.state = S_PLAY_FALL
				player.timon = true
				player.pflags = $ |PF_SPINNING
			end
		elseif P_IsObjectOnGround(player.mo) == true then
			player.smakuse = false
			player.timon = false
			player.timer = 0
		elseif player.timon == true then
			player.timer = $ + 1
			P_SpawnGhostMobj(player.mo)
			if player.timer >= 5 and player.timer <= 35 
			and (player.mo.momz <= 15*FRACUNIT or (P_MobjFlip(player.mo) == (0 - 1) and player.mo.momz >= (0 - 15*FRACUNIT))) then 
				P_SetObjectMomZ(player.mo, (4*FRACUNIT), true)  
				player.mo.state = S_PLAY_ROLL
				player.pflags = $ & ~PF_SPINNING
				S_StartSound(player.mo,sfx_spndsh, player)
			elseif player.timer >= 1*TICRATE - 1*TICRATE/2 and player.timer <= 2*TICRATE then
				player.mo.state = S_PLAY_SPRING
				player.pflags = $ & ~PF_SPINNING
				player.timon = false
				player.timer = 0
			end
			if P_IsObjectOnGround(player.mo) then
				P_RadiusAttack(player.mo, player.mo, 128*FRACUNIT)
			end
		end
	end
end)

//Skid Attack
addHook("PlayerThink", function(player)
    if (player.mo and player.mo.valid and player.mo.skin == "rafael")
        if player.mo.state == S_PLAY_SKID
            local sectorthing = P_PlayerTouchingSectorSpecial(player, 0, 0)
            if sectorthing.valid then
                for fof in sectorthing.ffloors()
                    if fof.valid and (fof.flags & FF_BUSTUP) and (fof.flags & FF_EXISTS)
                    and (abs(fof.topheight - player.mo.z) <= 40*player.mo.scale) then
                        EV_CrumbleChain(player.sectorthing, fof)
                    end
                end
            end
        end
    end
end)

addHook("PlayerCanDamage", function(player, mobj)
    if player.mo and player.mo.valid and player.mo.skin == "rafael"
    and player.mo.state == S_PLAY_SKID
    return true end
end)

//Jump ability off spring
addHook("PlayerThink", function(player)
    if (player.mo and player.mo.valid and player.mo.skin == "rafael")
        if player.mo.state == S_PLAY_SPRING
        and (player.mo.eflags & MFE_SPRUNG) then
            player.rafacurlready = true
        end
    end
end)

addHook("PlayerThink", function(player)
    if (player.mo and player.mo.valid and player.mo.skin == "rafael")
        if P_IsObjectOnGround(player.mo) then
            player.rafacurlready = false
        elseif player.rafacurlready
        and (player.cmd.buttons & BT_SPIN) then
            player.mo.state = S_PLAY_ROLL
            player.pflags = $ |PF_JUMPED
        end
    end
end)

-- Trystan's Patch (Also not part from the original script)
local cv_extra = CV_RegisterVar({"rafa_trik", "On", CV_NETVAR, CV_OnOff})

addHook("ThinkFrame", do
	for player in players.iterate
	if (player.mo.skin ~= "rafael")
	or not (cv_extra.value)
		return
	end
	if player.mo and player.mo.skin == "rafael"
		and player.mo.state == S_PLAY_SPRING
			and (player.pflags & PF_JUMPED)
			and (player.pflags & PF_JUMPDOWN)
			and not (player.mo.eflags & MFE_SPRUNG)
			P_InstaThrust(player.mo, player.mo.angle, 30*FRACUNIT)
			P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
			player.mo.angle = R_PointToAngle2(0, 0, player.mo.momx, player.mo.momy)
			player.mo.state = S_PLAY_JUMP
			elseif player.mo.state == S_PLAY_SPRING
			and (player.pflags & PF_JUMPED)
			and not (player.pflags & PF_JUMPDOWN)
			end
			if player.mo and player.mo.skin == "rafael"
			and (player.mo.eflags & MFE_SPRUNG)
			and player.mo.state == S_PLAY_SPRING
			and (player.pflags & PF_JUMPED)
			player.pflags = $1|PF_JUMPED
			player.mo.state = S_PLAY_ROLL
		end
	end
end)

-- Riders' fall frame code, uncomment the MobjThinker to use it.
--[[
addHook("MobjThinker", function(mobj)
	local player = mobj.player
	if (player.mo and player.mo.skin == "rafael") then
		if (player.mo.momz < 0)
		and (player.pflags & PF_THOKKED)
		and not (player.mo.state >= S_PLAY_FALL1 and player.mo.state <= S_PLAY_FALL2) 
		and not (player.homing) then
			player.pflags = $1 & ~PF_JUMPED
			player.mo.state = S_PLAY_FALL1
		end
	end
end, MT_PLAYER)]]