-- The idea is to stop the walk2sprint animation if the player stops sprinting during the animation

-- Use my transition animations script as 'truth' for currently playing animation
-- Likely to break if multiple scripts are using actor_on_hud_animation_play, but should be good enough for transition animations specifically
local current_animation = ""
local stop_animation = false

-- Slightly cleaner string equivalence check which should ignore variants like _empty or _jammed
function isAnimationEqualTo(anim_name)
    if current_animation:match(anim_name) then
        return true
    end
    return false
end

aol_anim_transitions_anm_transitions = aol_anim_transitions.anm_transitions
function aol_anim_transitions.anm_transitions(anm_table, item)
    local prev_name = anm_table.anm_name
    aol_anim_transitions_anm_transitions(anm_table, item)
    -- Restore original animation (probably idle) if previous animation was cancelled
    -- This should prevent sprint2walk from playing if its counterpart walk2sprint was cancelled
    if stop_animation then
        stop_animation = false
        if (isAnimationEqualTo("anm_walk2sprint") or isAnimationEqualTo("anm_sprint2walk")) then
            anm_table.anm_name = prev_name
        end
    end
    current_animation = anm_table.anm_name
end

-- Detect if the player is not sprinting during the walk2sprint animation
-- Firebreath: added the elseif in because sometimes the player stops sprint animation 
--             when interrupted by small obstacles like stairs
function actor_on_movement_changed(num)
    if stop_animation then return end
    if (not IsMoveState("mcSprint") and isAnimationEqualTo("anm_walk2sprint")) then
        stop_animation = true
        game.stop_hud_motion()
    elseif (IsMoveState("mcSprint") and isAnimationEqualTo("anm_sprint2walk")) then
        stop_animation = true
        game.stop_hud_motion()
    end
end

function on_game_start()
	RegisterScriptCallback("actor_on_movement_changed",actor_on_movement_changed)
end