view libMyLilPony/libMyLilPony_miscFunctions.lua @ 64:a8813eee6bb8 tip

Added tag 1.2.3 for changeset db0578847bb5
author syzler
date Tue, 27 Dec 2016 16:42:57 -0500
parents 81145a681a59
children
line wrap: on
line source
-- Copyright (c) 2015, Syzler
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions
-- are met:
--
--  * Redistributions of source code must retain the above copyright
--    notice, this list of conditions and the following disclaimer.
--  * Redistributions in binary form must reproduce the above copyright
--    notice, this list of conditions and the following disclaimer in
--    the documentation and/or other materials provided with the
--    distribution.
--  * Neither the name of the MyLilPony Project nor the names of its
--    contributors may be used to endorse or promote products derived
--    from this software without specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.

------------------------------------------------------------------------
-- Project: libMyLilPony
-- Project Version: @project-version@
-- Last Author: @file-author@
-- Last Updated: @file-date-iso@
--
-- Misc helper functions used in the library
------------------------------------------------------------------------

--- Gets a hashtable of buffs on the specified unit.
-- @param unit The unit frame name (e.g. "target", "player", "focus") of the unit whose buffs are to be retrieved.
-- @returns A hashtable of buff spell IDs keyed on the ID.
function MyLilPony.GetUnitBuffs(unit)
    local buffs = {};
    for i = 1, 40 do
        local _, _, _, _, _, _, _, _, _, _, id = UnitAura(unit, i, "HELPFUL");
        if id ~= nil then
            buffs[id] = id;
        end
    end
    return buffs;
end

--- Performs case-sensitive string pattern matching.
-- @param subject The string on which the pattern matching is performed.
-- @param pattern The pattern to be matched.
-- @returns The match result captures, the entire string if there are no captures, or nil if the subject is not a match.
function MyLilPony.StringMatch(subject, pattern)
    if subject == nil and pattern == nil then return true end
    if subject == nil or pattern == nil then return false end
    return string.match(subject, pattern);
end

--- Performs case-insensitive string pattern matching.
-- @param subject The string on which the pattern matching is performed.
-- @param pattern The pattern to be matched.
-- @returns The match result captures, the entire string if there are no captures, or nil if the subject is not a match.
function MyLilPony.StringMatchIgnoreCase(subject, pattern)
    if subject == nil and pattern == nil then return true end
    if subject == nil or pattern == nil then return false end
    local lSub = string.lower(subject);
    local lPat = string.lower(pattern);
    return string.match(lSub, lPat);
end

--- Calls a mount if a specified condition checks out.
-- @param slotID The slot ID of the mount to be called.
-- @param condition An optional Boolean condition.
-- @returns True if a mount was summoned, or False otherwise.
function MyLilPony.CallMount(slotID, condition)
    if condition == nil or condition then
        C_MountJournal.SummonByID(slotID);
        return true;
    end
    return false;
end

--- OBSOLETE. Calls a mount if a specified condition checks out.
-- @param unused The type of companion to be called (e.g. "MOUNT").
-- @param slotID The slot ID of the mount to be called.
-- @param condition An optional Boolean condition.
function MyLilPony.CallCompanion(unused, slotID, condition)
    MyLilPony.CallMount(slotID, condition);
end

--- Iterates over all known and non-hidden (i.e. not dead or opposite faction) mount slot IDs.
-- @return A list of valid mount slot IDs.
function MyLilPony.EnumerateKnownMountSlotIDs()
    local countMounts = C_MountJournal.GetNumMounts();
    local x = 1;
    return function()
        for i = x, countMounts do
            local _, _, _, _, _, _, _, _, _, hidden, known = C_MountJournal.GetMountInfoByID(i);
            if known and not hidden then
                x = i + 1;
                return i;
            end
        end
    end
end

--- Gets a value indicating whether or not the current character is able to fly at the current location.
-- This function checks whether or not the current location is a flyable area, and then additionally checks for knowledge of the proper flying skill (e.g. Cold Weather Flying for Northrend).
-- @returns A Boolean value indicating whether or not the current character is able to fly at the current location.
function MyLilPony.CanFlyHere()
    if IsFlyableArea() then
        SetMapToCurrentZone();
        local continent = GetCurrentMapContinent();
        if continent == 4 then
            -- Northrend: requires Cold Weather Flying
            return IsSpellKnown(54197);
        elseif (continent == 1 or continent == 2) then
            -- Old World: requires Flight Master's License
            return IsSpellKnown(90267);
        end
        return true;
    end
    return false;
end

function MyLilPony.Print(msg)
    DEFAULT_CHAT_FRAME:AddMessage("|cff8ed6f0"..msg);
end

function MyLilPony.Log(msg)
    if MYLILPONY_DEBUG_LOGGING then
        DEFAULT_CHAT_FRAME:AddMessage("|cff8ed6f0"..format("MyLilPony: %s", msg));
    end
end