name = "Shiv"
id = hero_id.shiv

function is_enabled()
    if not config.get_bool("enabled") then return false end
    if config.get_bool("always_on") then return true end
    local key = config.get_int("activation_key")
    return key > 0 and input.is_key_held(key)
end

function on_tick()
    local lp = local_player()
    if not lp then return end

    local ult = lp:get_ability(slot.ability4)
    if not ult or not ult.learned then return end

    if not lp:is_ability_ready(slot.ability4) then return end

    if not wait_for_cast_delay(slot.ability4, 3000) then return end
    if not wait_for_cooling_down(slot.ability4, 3000) then return end

    -- determine execute threshold based on ability level
    local threshold = (ult.points >= 7) and 0.25 or 0.20

    -- find execute target
    local best = find_closest_by_predicate(function(e)
        if not e:is_targetable() or not e:is_on_screen() then return false end
        if e:has_modifier_flag(modifier_flag.attached) or e:has_modifier_flag(modifier_flag.burrow) then return false end
        local hp_pct = e:get_health() / e:get_max_health()
        return hp_pct > 0 and hp_pct < threshold
    end, 10.0)

    if not best then return end

    snap_to_target(best, { bone = bone.spine_1 })
    sleep(100)
    press_ability(slot.ability4)
    sleep(100)
    left_click()

    sleep(500)
end

settings = {
    { key = "enabled",        type = "bool",    default = false,       label = "Enabled" },
    { key = "always_on",      type = "bool",    default = false,       label = "Always On" },
    { key = "activation_key", type = "keybind", default = VK.XBUTTON2, label = "Activation Key", depends_on = "always_on", depends_value = 0 },
}
