-- Reserve sprite2 slots
freeslot(
    "S_PLAY_SHADOWTRICK1", "S_PLAY_SHADOWTRICK2",
    "S_PLAY_SHADOWTRICK3", "S_PLAY_SHADOWTRICK4",
    "S_PLAY_SHADOWTRICK5", "S_PLAY_SHADOWTRICK6",
    "S_PLAY_SHADOWTRICK7", "S_PLAY_SHADOWTRICK8",
	"sfx_tricka", "sfx_trickb", "sfx_trickc", "SPR2_ARCK", "SPR2_BRCK", "SPR2_CRCK", "SPR2_DRCK"
)



-- Randomized spring-stunt SFX (cycles between tricka/trickb/trickc)
local function SurgeTrickSound(mo)
	local r = P_RandomKey(3) -- 0..2
	if r == 0
		S_StartSound(mo, sfx_tricka)
	elseif r == 1
		S_StartSound(mo, sfx_trickb)
	else
		S_StartSound(mo, sfx_trickc)
	end
end

-- === State Definitions ===
-- We reference SPR_PLAY as the base, and use SPR2_POSE
-- to indicate “pose” states, chaining into “hold” loops.

states[S_PLAY_SHADOWTRICK1] = { SPR_PLAY, SPR2_ARCK, 10, nil, 0, 0, S_PLAY_SHADOWTRICK2 }
states[S_PLAY_SHADOWTRICK2] = {SPR_PLAY, FF_ANIMATE|SPR2_FALL, -1, nil, 2, 2, S_PLAY_SHADOWTRICK2}

states[S_PLAY_SHADOWTRICK3] = {SPR_PLAY, SPR2_BRCK, 10, nil, 0, 0, S_PLAY_SHADOWTRICK4}
states[S_PLAY_SHADOWTRICK4] = {SPR_PLAY, FF_ANIMATE|SPR2_FALL, -1, nil, 2, 2, S_PLAY_SHADOWTRICK4}

states[S_PLAY_SHADOWTRICK5] = { SPR_PLAY, SPR2_CRCK, 10, nil, 0, 0, S_PLAY_SHADOWTRICK6 }
states[S_PLAY_SHADOWTRICK6] = {SPR_PLAY, FF_ANIMATE|SPR2_FALL, -1, nil, 2, 2, S_PLAY_SHADOWTRICK6}

states[S_PLAY_SHADOWTRICK7] = { SPR_PLAY, SPR2_DRCK, 10, nil, 0, 0, S_PLAY_SHADOWTRICK8 }
states[S_PLAY_SHADOWTRICK8] = {SPR_PLAY, FF_ANIMATE|SPR2_FALL, -1, nil, 2, 2, S_PLAY_SHADOWTRICK8}


-- === Hook: Trigger First Trick on Spring + Button ===
addHook("PlayerThink", function(player)
    if player.mo.skin == "ultsonic"
    and (player.cmd.buttons & BT_JUMP) ~= 0
    and player.shadowspringed
    and not player.shadowspinpress
    and (player.mo.state == S_PLAY_SPRING
         or player.mo.state == S_PLAY_FALL
         or player.mo.state == S_PLAY_SHADOWTRICK8)
    then
        player.mo.state = S_PLAY_SHADOWTRICK1
        SurgeTrickSound(player.mo)
    end
end)

-- === Hooks: Chain to Next Trick on Toss ===
local function makeChainHook(fromState, toState)
    addHook("PlayerThink", function(player)
        if player.mo.skin == "ultsonic"
        and (player.cmd.buttons & BT_JUMP) ~= 0
        and not player.shadowspinpress
        and player.mo.state == fromState
        then
            player.mo.state = toState
            SurgeTrickSound(player.mo)
        end
    end)
end

makeChainHook(S_PLAY_SHADOWTRICK2, S_PLAY_SHADOWTRICK3)
makeChainHook(S_PLAY_SHADOWTRICK4, S_PLAY_SHADOWTRICK5)
makeChainHook(S_PLAY_SHADOWTRICK6, S_PLAY_SHADOWTRICK7)

-- === Hooks: Track “just sprung” and reset flag on landing/pain/death ===
addHook("PlayerThink", function(player)
    if player.mo.skin == "ultsonic"
    and player.powers[pw_justsprung]
    then
        player.shadowspringed = true
    end
end)

addHook("PlayerThink", function(player)
    if player.mo.skin == "ultsonic"
    and (P_IsObjectOnGround(player.mo)
         or player.playerstate == PST_DEAD
         or P_PlayerInPain(player))
    then
        player.shadowspringed = false
    end
end)

addHook("PlayerThink", function(player)
    if player.mo.skin ~= "ultsonic" then
        player.shadowspringed = false
    end
end)

-- === Hook: Exit Trick on Landing (into Stand or Roll) ===
addHook("PlayerThink", function(player)
    if player.mo.skin == "ultsonic"
    and player.mo.state >= S_PLAY_SHADOWTRICK1
    and player.mo.state <= S_PLAY_SHADOWTRICK8
    and P_IsObjectOnGround(player.mo)
    then
        if not (player.pflags & PF_SPINNING) then
            player.mo.state = S_PLAY_STND
        else
            player.mo.state = S_PLAY_ROLL
        end
    end
end)

-- === Hook: Track if Toss Button Is Held ===
addHook("PlayerThink", function(player)
    if player.mo.skin == "ultsonic"
    and (player.cmd.buttons & BT_JUMP) ~= 0
    then
        player.shadowspinpress = true
    else
        player.shadowspinpress = false
    end
end)