freeslot("sfx_redo", "sfx_drop", "sfx_fock", "S_PLAY_JMP1", "S_PLAY_JMP2", "S_PLAY_DRPD")

states[S_PLAY_JMP1] = {SPR_PLAY, SPR2_SPNG|FF_ANIMATE, 12, nil, 0, 0, S_PLAY_JMP1}
states[S_PLAY_JMP2] = {SPR_PLAY, SPR2_ROLL|FF_ANIMATE, 1, nil, 0, 0, S_PLAY_JMP2}
states[S_PLAY_DRPD] = {SPR_PLAY, SPR2_FLT_|FF_ANIMATE, 1, nil, 0, 0, S_PLAY_DRPD}

//This is for the Jump To Thok
local second_jump_speed = 13 * FRACUNIT
local third_jump_speed = 46 * FRACUNIT
local jump_cooldown = 5 -- cooldown between jumps in tics

addHook("ThinkFrame", function()
for player in players.iterate do
	if player.mo and player.mo.valid and player.mo.skin == "miguel" then
-- initialize custom variables properly
	if player.jump_count == nil then player.jump_count = 0 end
	if player.second_jump == nil then player.second_jump = false end
	if player.third_jump == nil then player.third_jump = false end
	if player.jump_cooldown_timer == nil then player.jump_cooldown_timer = 0 end
	if player.last_spin_pressed == nil then player.last_spin_pressed = false end
	if player.has_jumped == nil then player.has_jumped = false end

-- reset jumps when landing on the ground
	if P_IsObjectOnGround(player.mo) then
	player.jump_count = 0
	player.second_jump = false
	player.third_jump = false
	player.jump_cooldown_timer = 0
	player.has_jumped = false
end

-- detect the initial jump
	if (player.cmd.buttons & BT_JUMP) ~= 0 and not player.has_jumped then
	player.has_jumped = true
end

-- decrement cooldown timer
	if player.jump_cooldown_timer > 0 then
	player.jump_cooldown_timer = player.jump_cooldown_timer - 1
end

-- perform second and third jumps only if player has jumped, is not touching the ground, and the cooldown has passed
	if player.has_jumped and not P_IsObjectOnGround(player.mo) and (player.cmd.buttons & BT_SPIN) ~= 0 and not player.last_spin_pressed and player.jump_cooldown_timer == 0 then
	if player.jump_count == 0 then
-- perform second jump
	player.mo.momz = second_jump_speed
	S_StartSound(player.mo, sfx_jump)
	player.mo.state = S_PLAY_JMP1
	player.jump_count = 1
	player.second_jump = true
	player.jump_cooldown_timer = jump_cooldown
	elseif player.jump_count == 1 and not player.third_jump then
-- perform third jump
	player.mo.momx = FixedMul(cos(player.mo.angle), third_jump_speed) -- set horizontal speed
	player.mo.momy = FixedMul(sin(player.mo.angle), third_jump_speed)
	S_StartSound(player.mo, sfx_fock)
	player.mo.state = S_PLAY_JMP2
	player.jump_count = 2
	player.third_jump = true
	player.jump_cooldown_timer = jump_cooldown
	end
end
-- Store the last spin button state
player.last_spin_pressed = (player.cmd.buttons & BT_SPIN) ~= 0
end
end
end)

//This is for the Hard Drop
-- specify the stall duration and downward speed
local stall_duration = 20 -- 1 second (35 tics)
local downward_speed = -30 * FRACUNIT
local PF_FLY = 0x4000
-- ghost spawn interval
local ghost_interval = 4 -- number of tics between ghost mobj spawns

addHook("ThinkFrame", function()
for player in players.iterate do
-- initialize custom variables
player.stalling = player.stalling or false
player.stalltimer = player.stalltimer or 0
player.hasStalled = player.hasStalled or false
player.spawn_ghost = player.spawn_ghost or false
player.ghost_timer = player.ghost_timer or 0

if player.mo and player.mo.valid and player.mo.skin == "miguel" then
local jumped = (player.pflags & PF_JUMPED) ~= 0  -- check if player has jumped
local onground = (player.pflags & PF_JUMPED) == 0  -- check if player is on the ground
local custom1_pressed = (player.cmd.buttons & BT_CUSTOM1) ~= 0

if custom1_pressed then
player.spawn_ghost = true
player.ghost_timer = ghost_interval
end

if custom1_pressed and jumped and not player.stalling and not player.hasStalled then
player.stalling = true
player.stalltimer = stall_duration
player.hasStalled = true

-- play the start sound effect for stalling
S_StartSound(player.mo, sfx_redo)
end

if player.stalling then
player.mo.momz = 0 -- stall vertical movement
player.pflags = $ | PF_FLY

player.stalltimer = player.stalltimer - 1

if player.stalltimer <= 0 then
player.stalling = false
player.mo.momz = downward_speed -- throw player downward
player.pflags = $ & ~PF_FLY -- resume normal physics

-- play the end sound effect when stalling ends
S_StartSound(player.mo, sfx_drop)
player.mo.state = S_PLAY_DRPD
end	
end

-- spawn ghost mobjs while custom 1 is pressed
if player.spawn_ghost then
player.ghost_timer = player.ghost_timer - 1
if player.ghost_timer <= 0 then
P_SpawnGhostMobj(player.mo)
player.ghost_timer = ghost_interval
end
end

if onground then
player.stalling = false -- reset the stalling flag when the player touches down again
player.stalltimer = 0
player.hasStalled = false -- allow another stall after touching the ground

player.spawn_ghost = false -- stop spawning ghost mobjs
player.ghost_timer = 0 -- reset the ghost timer
end
end
end
end)
