-- 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.

local cv_extra = CV_RegisterVar({"rafa_trik", "On", CV_NETVAR, CV_OnOff})

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.

-- Jump ability off spring :> (This is not part from the original script)
addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid and player.mo.skin == "rafael"
		if player.mo.eflags & MFE_SPRUNG and not P_IsObjectOnGround(player.mo)
			player.pflags = $|PF_JUMPED&~PF_THOKKED
		end
	end
end)

-- Trystan's Patch (Also not part from the original script)
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)

//Mini Rings
addHook("PlayerThink", function(player)
  if (player.mo.skin ~= "rafael")
  or not (cv_extra.value)
    return
  end
	if not player.rafxtra
	return
  end
	local enemy = P_LookForEnemies(player)
	if not (enemy) return end
	if (enemy.valid)
	and (enemy.flags & MF_ENEMY)
		P_KillMobj(enemy)
		local explosion = P_SpawnMobj(enemy.x, enemy.y, enemy.z, MT_RING)
		explosion.scale = enemy.scale/3
		explosion.state = S_RING
		explosion.target = player.mo
		explosion.nodmg = true
   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)]]