view libMyLilPony/libMyLilPony_mountFunctions.lua @ 50:22011265a16f 1.2.0-b1

Updated and fixed for draenor xpack (patch 6.0) API changes
author syzler
date Sat, 07 Mar 2015 02:52:57 -0500
parents 2ae5f67e313a
children 64e8f8e5fa41
line wrap: on
line source
-- Copyright (c) 2011, 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@
--
-- API functions for calling mounts
------------------------------------------------------------------------

--- Summons a random mount.
-- Does nothing if the only available mount is already summoned.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMount()
    local filter = function (i)
        local _, _, _, summoned = C_MountJournal.GetMountInfo(i);
        return not summoned;
    end
    return MyLilPony.CallMountByFilter(filter);
end

--- Summons a random flying mount.
-- Does nothing if the only available mount is already summoned.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallFlyingMount()
    local filter = function (i)
        local _, id, _, summoned = C_MountJournal.GetMountInfo(i);
        return not summoned and MyLilPony.IsFlyingMount(id);
    end
    return MyLilPony.CallMountByFilter(filter);
end

--- Summons a random ground mount.
-- Does nothing if the only available mount is already summoned.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallGroundMount()
    local filter = function (i)
        local _, id, _, summoned = C_MountJournal.GetMountInfo(i);
        return not summoned and MyLilPony.IsGroundMount(id);
    end
    return MyLilPony.CallMountByFilter(filter);
end

--- Summons a random aquatic mount.
-- Does nothing if the only available mount is already summoned.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallAquaticMount()
    local filter = function (i)
        local _, id, _, summoned = C_MountJournal.GetMountInfo(i);
        return not summoned and MyLilPony.IsAquaticMount(id);
    end
    return MyLilPony.CallMountByFilter(filter);
end

--- Summons a mount by slot number in mount spellbook.
-- Does nothing if the specified mount is already summoned.
-- @param slotNumber The slot number of the desired mount in the mount spellbook.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountBySlot(slotNumber)
    local countMounts = C_MountJournal.GetNumMounts();
    if slotNumber > 0 and slotNumber < countMounts+1 then
        local _, _, _, summoned = C_MountJournal.GetMountInfo(i);
        MyLilPony.CallCompanion("MOUNT", slotNumber, not summoned);
        return true;
    end
    return false;
end

--- Summons a mount by exact name match.
-- Does nothing if the specified mount is already summoned.
-- @param mountName The exact name (including correct upper/lower case) of the desired mount.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountByName(mountName)
    local result = false;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name, _, _, summoned = C_MountJournal.GetMountInfo(i);
        if name == mountName then
            MyLilPony.CallCompanion("MOUNT", i, not summoned);
            result = true;
            do break end
        end
    end
    return result;
end

--- Summons a mount by name pattern match.
-- If the pattern matches multiple mounts, a random one is chosen.
-- Does nothing if the specified mount is already summoned.
-- @param mountNamePattern A string pattern against which mount names are matched (does not require correct upper/lower case).
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountByPattern(mountNamePattern)
    local filter = function (i)
        local name, _, _, summoned = C_MountJournal.GetMountInfo(i);
        return not summoned and MyLilPony.StringMatchIgnoreCase(name, mountNamePattern);
    end
    return MyLilPony.CallMountByFilter(filter);
end

--- Summons a mount by creature or spell ID.
-- Does nothing if the specified mount is already summoned.
-- @param id The creature or spell ID of the desired mount.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountById(id)
    local result = false;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local _, spellId, _, summoned = C_MountJournal.GetMountInfo(i);
        local creatureId = C_MountJournal.GetMountInfoExtra(i);
        if id == creatureId or id == spellId then
            MyLilPony.CallCompanion("MOUNT", i, not summoned);
            result = true;
            do break end
        end
    end
    return result;
end

--- Summons a mount by spell ID.
-- Does nothing if the specified mount is already summoned.
-- @param spellId The spell ID of the desired mount.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountBySpell(spellId)
    local result = false;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local _, id, _, summoned = C_MountJournal.GetMountInfo(i);
        if id == spellId then
            MyLilPony.CallCompanion("MOUNT", i, not summoned);
            result = true;
            do break end
        end
    end
    return result;
end

--- Summons a mount by creature ID.
-- Does nothing if the specified mount is already summoned.
-- @param creatureId The creature ID of the desired mount.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
function MyLilPony.CallMountByCreature(creatureId)
    local result = false;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local _, _, _, summoned = C_MountJournal.GetMountInfo(i);
        local id = C_MountJournal.GetMountInfoExtra(i);
        if id == creatureId then
            MyLilPony.CallCompanion("MOUNT", i, not summoned);
            result = true;
            do break end
        end
    end
    return result;
end

--- Summons a mount by attempting to match the mount of the specified unit.
-- The function checks the buffs of the specified unit against the mount spellbook for matches.
-- As a result, the function will only likely work against player characters.
-- Does nothing if the specified unit's mount is already summoned.
-- @param unit The UnitId (e.g. "target", "player", "focus", "party1", etc.) of the unit whose mount is desired.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
-- @usage MyLilPony.CallMountByMatch("target");
function MyLilPony.CallMountByMatch(unit)
    local result = false;
    local unitBuffs = MyLilPony.GetUnitBuffs(unit);
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local _, id, _, summoned = C_MountJournal.GetMountInfo(i);
        if unitBuffs[id] ~= nil then
            MyLilPony.CallCompanion("MOUNT", i, not summoned);
            result = true;
            do break end
        end
    end
    return result;
end

--- Summons a random mount after filtering the mount spellbook.
-- Does nothing if the specified mount is already summoned.
-- @param filter A filter callback function which takes a single mount slot number and returns a Boolean value indicating whether or not the the indicated mount should be included.
-- @return A Boolean value indicating whether or not a mount was successfully summoned.
-- @usage local filter = function (i)
--     local name = C_MountJournal.GetMountInfo(i);
--     return string.match(name, pattern);
-- end
-- MyLilPony.CallMountByFilter(filter);
function MyLilPony.CallMountByFilter(filter)
    local mounts = {};
    local x = 0;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        if filter(i) then
            x = x + 1;
            mounts[x] = i;
        end
    end
    
    if x > 0 then
        local i = mounts[random(1, x)];
        local _, _, _, summoned =  C_MountJournal.GetMountInfo(i);
        MyLilPony.CallCompanion("MOUNT", i, not summoned);
        return true;
    end
    return false;
end

--- Lists available mounts by name.
-- @return A list of mount names.
function MyLilPony.ListMounts()
    local results = {};
    local x = 1;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name = C_MountJournal.GetMountInfo(i);
        results[i] = name;
        x = x + 1;
    end
    return results;
end

--- Lists available ground mounts by name.
-- @return A list of ground mount names.
function MyLilPony.ListGroundMounts()
    local results = {};
    local x = 1;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name, id = C_MountJournal.GetMountInfo(i);
        if MyLilPony.IsGroundMount(id) then
            results[x] = name;
            x = x + 1;
        end
    end
    return results;
end

--- Lists available flying mounts by name.
-- @return A list of flying mount names.
function MyLilPony.ListFlyingMounts()
    local results = {};
    local x = 1;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name, id = C_MountJournal.GetMountInfo(i);
        if MyLilPony.IsFlyingMount(id) then
            results[x] = name;
            x = x + 1;
        end
    end
    return results;
end

--- Lists available aquatic mounts by name.
-- @return A list of aquatic mount names.
function MyLilPony.ListAquaticMounts()
    local results = {};
    local x = 1;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name, id = C_MountJournal.GetMountInfo(i);
        if MyLilPony.IsAquaticMount(id) then
            results[x] = name;
            x = x + 1;
        end
    end
    return results;
end

--- Lists available mounts names whose name matches a given pattern.
-- @param mountNamePattern A string pattern against which mount names are matched (does not require correct upper/lower case).
-- @return A list of matching mount names.
function MyLilPony.ListMountsByPattern(mountNamePattern)
    local results = {};
    local x = 1;
    for i in MyLilPony.EnumerateKnownMountSlotIDs() do
        local name = C_MountJournal.GetMountInfo(i);
        if MyLilPony.StringMatchIgnoreCase(name, mountNamePattern) then
            results[x] = name;
            x = x + 1;
        end
    end
    return results;
end