view libMyLilPony/libMyLilPony_mountFunctions.lua @ 7:d96c15f7477b

Updated documentation for API.
author syzler
date Tue, 05 Apr 2011 02:05:57 +0000
parents 21d6611a1307
children b1e344c17ab5
line wrap: on
line source
-- libMyLilPony
-- Copyright (c) 2011 Syzler
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

-- 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 = GetCompanionInfo("MOUNT", 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 = GetCompanionInfo("MOUNT", 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 = GetCompanionInfo("MOUNT", 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 = GetCompanionInfo("MOUNT", 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 = GetNumCompanions("MOUNT");
    if slotNumber > 0 and slotNumber < countMounts+1 then
        local _, _, _, _, summoned = GetCompanionInfo("MOUNT", slotNumber);
        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;
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local _, name, _, _, summoned = GetCompanionInfo("MOUNT", 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 = GetCompanionInfo("MOUNT", 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;
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local creatureId, _, spellId, _, summoned = GetCompanionInfo("MOUNT", 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;
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local _, _, id, _, summoned = GetCompanionInfo("MOUNT", 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;
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local id, _, _, _, summoned = GetCompanionInfo("MOUNT", 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 unit frame name (e.g. "target", "player", "focus") 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);
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local _, _, id, _, summoned = GetCompanionInfo("MOUNT", 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, _, _, summoned = GetCompanionInfo("MOUNT", i);
--     return not summoned and string.match(name, pattern);
-- end
-- MyLilPony.CallMountByFilter(filter);
function MyLilPony.CallMountByFilter(filter)
    local countMounts = GetNumCompanions("MOUNT");
    if countMounts > 0 then
        local results = {};
        local countResults = 0;
        
        for i = 1, countMounts do
            if filter(i) then
                countResults = countResults + 1;
                results[countResults] = i;
            end
        end
    
        if countResults > 0 then
            local i = random(1, countResults);
            local _, _, _, _, summoned = GetCompanionInfo("MOUNT", results[i]);
            MyLilPony.CallCompanion("MOUNT", results[i], not summoned);
            return true;
        end
    end
    return false;
end

--- Lists available mounts by name.
-- @return A list of mount names. Since this function returns all known mounts, the index of each entry in the list is also the mount's slot number.
function MyLilPony.ListMounts()
    local results = {};
    local countMounts = GetNumCompanions("MOUNT");
    for i = 1, countMounts do
        local _, name, _, _, _ = GetCompanionInfo("MOUNT", i);
        results[i] = name;
    end
    return results;
end

--- Lists available ground mounts by name.
-- @return A list of ground mount names.
function MyLilPony.ListGroundMounts()
    local results = {};
    local countMounts = GetNumCompanions("MOUNT");
    local x = 1;
    for i = 1, countMounts do
        local _, _, id, _, _ = GetCompanionInfo("MOUNT", 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 countMounts = GetNumCompanions("MOUNT");
    local x = 1;
    for i = 1, countMounts do
        local _, _, id, _, _ = GetCompanionInfo("MOUNT", 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 countMounts = GetNumCompanions("MOUNT");
    local x = 1;
    for i = 1, countMounts do
        local _, _, id, _, _ = GetCompanionInfo("MOUNT", 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 countMounts = GetNumCompanions("MOUNT");
    local x = 1;
    for i = 1, countMounts do
        local _, name, _, _, _ = GetCompanionInfo("MOUNT", i);
        if MyLilPony.StringMatchIgnoreCase(name, mountNamePattern) then
            results[x] = name;
            x = x + 1;
        end
    end
    return results;
end