--EVERY FRAME TRIGGER
function()
    if not WeakAurasSaved.HerbSniffer then WeakAurasSaved.HerbSniffer = {} end
    local function range (val, min, max, max2)
        val = 1 - (((max - val) / (max - min)) * max2)
        return val
    end
    local id = aura_env.GetClosestHerb()
    if id > -1 then
        if not aura_env.herbs then return false end
        local hX, hY = aura_env.herbs[id].X, aura_env.herbs[id].Y
        local pX = GetPlayerMapPosition("player") or 0
        local pY = select(2, GetPlayerMapPosition("player")) or 0
        pX, pY = pX * 100, pY * 100
        local X = pX - hX
        local Y = pY - hY
        local playerFace = GetPlayerFacing() or 0
        local playerA = math.floor(playerFace * 100)
        playerA = range(playerA, 0, 630, 360) - 1
        playerA = -playerA
        playerA = playerA - 90
        if playerA < 0 then playerA = playerA + 360 end
        aura_env.hyp = math.sqrt((math.abs(X ^ 2)) + (math.abs(Y ^ 2))) * 100
        WeakAurasSaved.HerbSniffer.hyp = aura_env.hyp
        aura_env.angle = math.deg(math.atan2(Y, X))
        aura_env.angle = aura_env.angle - playerA
        aura_env.angle = aura_env.angle - 180
        if aura_env.hyp < aura_env.wipeDistance then --When reach herb activate it's cooldown (herbcd[cd] = coords)
            aura_env.herbs[id].CD = math.floor(GetTime() + aura_env.cooldown)
            WeakAurasSaved.HerbSniffer.CD = WeakAurasSaved.HerbSniffer.CD + 1
            WeakAurasSaved.HerbSniffer.NoCD = WeakAurasSaved.HerbSniffer.NoCD - 1
            aura_env.minID = {
                ["X"] = 0,
                ["Y"] = 0,
                ["D"] = 10000,
                ["ID"] = -1
            }
        end
        for k, v in ipairs(aura_env.herbs) do --if cooldown is up then insert back into proper array
            local cd = aura_env.herbs[k].CD
            if cd > 0 then
                if GetTime() > cd then
                    aura_env.herbs[k].CD = 0
                    WeakAurasSaved.HerbSniffer.NoCD = WeakAurasSaved.HerbSniffer.NoCD + 1
                    WeakAurasSaved.HerbSniffer.CD = WeakAurasSaved.HerbSniffer.CD - 1
                end
            end
        end
    else
        return false
    end
    return true
end

--RESET_COUNTER
function()
    WeakAurasSaved.HerbSniffer.CD = 0
    WeakAurasSaved.HerbSniffer.NoCD = #aura_env.herbs
end

--ANIMATION
function()
    return - aura_env.angle
end

--INIT
if WeakAuras.IsOptionsOpen() then WeakAurasSaved.HerbSniffer.CD = 0; WeakAurasSaved.HerbSniffer.NoCD = #aura_env.herbs end
