annotate libMyLilPony/libMyLilPony_mountFunctions.lua @ 14:b1e344c17ab5

Changing to BSD license for more flexibility in licensing for derived addons and using as a library.
author syzler
date Sun, 10 Apr 2011 07:12:30 +0000
parents d96c15f7477b
children 21764271e02f
rev   line source
syzler@1 1 -- libMyLilPony
syzler@14 2 -- Copyright (c) 2011, Syzler
syzler@14 3 -- All rights reserved.
syzler@1 4 --
syzler@14 5 -- Redistribution and use in source and binary forms, with or without
syzler@14 6 -- modification, are permitted provided that the following conditions
syzler@14 7 -- are met:
syzler@1 8 --
syzler@14 9 -- * Redistributions of source code must retain the above copyright
syzler@14 10 -- notice, this list of conditions and the following disclaimer.
syzler@14 11 -- * Redistributions in binary form must reproduce the above copyright
syzler@14 12 -- notice, this list of conditions and the following disclaimer in
syzler@14 13 -- the documentation and/or other materials provided with the
syzler@14 14 -- distribution.
syzler@14 15 -- * Neither the name of the MyLilPony Project nor the names of its
syzler@14 16 -- contributors may be used to endorse or promote products derived
syzler@14 17 -- from this software without specific prior written permission.
syzler@1 18 --
syzler@14 19 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
syzler@14 20 -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
syzler@14 21 -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
syzler@14 22 -- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
syzler@14 23 -- COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
syzler@14 24 -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
syzler@14 25 -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
syzler@14 26 -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
syzler@14 27 -- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
syzler@14 28 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
syzler@14 29 -- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
syzler@14 30 -- POSSIBILITY OF SUCH DAMAGE.
syzler@1 31
syzler@1 32 -- API functions for calling mounts
syzler@1 33
syzler@5 34 --- Summons a random mount.
syzler@5 35 -- Does nothing if the only available mount is already summoned.
syzler@6 36 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 37 function MyLilPony.CallMount()
syzler@5 38 local filter = function (i)
syzler@1 39 local _, _, _, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@5 40 return not summoned;
syzler@1 41 end
syzler@5 42 return MyLilPony.CallMountByFilter(filter);
syzler@1 43 end
syzler@1 44
syzler@6 45 --- Summons a random flying mount.
syzler@6 46 -- Does nothing if the only available mount is already summoned.
syzler@6 47 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 48 function MyLilPony.CallFlyingMount()
syzler@1 49 local filter = function (i)
syzler@1 50 local _, _, id, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@1 51 return not summoned and MyLilPony.IsFlyingMount(id);
syzler@1 52 end
syzler@1 53 return MyLilPony.CallMountByFilter(filter);
syzler@1 54 end
syzler@1 55
syzler@6 56 --- Summons a random ground mount.
syzler@6 57 -- Does nothing if the only available mount is already summoned.
syzler@6 58 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 59 function MyLilPony.CallGroundMount()
syzler@1 60 local filter = function (i)
syzler@1 61 local _, _, id, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@1 62 return not summoned and MyLilPony.IsGroundMount(id);
syzler@1 63 end
syzler@1 64 return MyLilPony.CallMountByFilter(filter);
syzler@1 65 end
syzler@1 66
syzler@6 67 --- Summons a random aquatic mount.
syzler@6 68 -- Does nothing if the only available mount is already summoned.
syzler@6 69 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 70 function MyLilPony.CallAquaticMount()
syzler@1 71 local filter = function (i)
syzler@1 72 local _, _, id, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@1 73 return not summoned and MyLilPony.IsAquaticMount(id);
syzler@1 74 end
syzler@1 75 return MyLilPony.CallMountByFilter(filter);
syzler@1 76 end
syzler@1 77
syzler@6 78 --- Summons a mount by slot number in mount spellbook.
syzler@6 79 -- Does nothing if the specified mount is already summoned.
syzler@6 80 -- @param slotNumber The slot number of the desired mount in the mount spellbook.
syzler@6 81 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 82 function MyLilPony.CallMountBySlot(slotNumber)
syzler@1 83 local countMounts = GetNumCompanions("MOUNT");
syzler@1 84 if slotNumber > 0 and slotNumber < countMounts+1 then
syzler@1 85 local _, _, _, _, summoned = GetCompanionInfo("MOUNT", slotNumber);
syzler@1 86 MyLilPony.CallCompanion("MOUNT", slotNumber, not summoned);
syzler@1 87 return true;
syzler@1 88 end
syzler@1 89 return false;
syzler@1 90 end
syzler@1 91
syzler@6 92 --- Summons a mount by exact name match.
syzler@6 93 -- Does nothing if the specified mount is already summoned.
syzler@6 94 -- @param mountName The exact name (including correct upper/lower case) of the desired mount.
syzler@6 95 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 96 function MyLilPony.CallMountByName(mountName)
syzler@1 97 local result = false;
syzler@1 98 local countMounts = GetNumCompanions("MOUNT");
syzler@1 99 for i = 1, countMounts do
syzler@6 100 local _, name, _, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 101 if name == mountName then
syzler@1 102 MyLilPony.CallCompanion("MOUNT", i, not summoned);
syzler@1 103 result = true;
syzler@1 104 do break end
syzler@6 105 end
syzler@6 106 end
syzler@1 107 return result;
syzler@1 108 end
syzler@1 109
syzler@6 110 --- Summons a mount by name pattern match.
syzler@6 111 -- If the pattern matches multiple mounts, a random one is chosen.
syzler@6 112 -- Does nothing if the specified mount is already summoned.
syzler@6 113 -- @param mountNamePattern A string pattern against which mount names are matched (does not require correct upper/lower case).
syzler@6 114 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 115 function MyLilPony.CallMountByPattern(mountNamePattern)
syzler@1 116 local filter = function (i)
syzler@1 117 local _, name, _, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@1 118 return not summoned and MyLilPony.StringMatchIgnoreCase(name, mountNamePattern);
syzler@1 119 end
syzler@1 120 return MyLilPony.CallMountByFilter(filter);
syzler@1 121 end
syzler@1 122
syzler@6 123 --- Summons a mount by creature or spell ID.
syzler@6 124 -- Does nothing if the specified mount is already summoned.
syzler@6 125 -- @param id The creature or spell ID of the desired mount.
syzler@6 126 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 127 function MyLilPony.CallMountById(id)
syzler@1 128 local result = false;
syzler@1 129 local countMounts = GetNumCompanions("MOUNT");
syzler@1 130 for i = 1, countMounts do
syzler@6 131 local creatureId, _, spellId, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 132 if id == creatureId or id == spellId then
syzler@1 133 MyLilPony.CallCompanion("MOUNT", i, not summoned);
syzler@1 134 result = true;
syzler@1 135 do break end
syzler@6 136 end
syzler@6 137 end
syzler@1 138 return result;
syzler@1 139 end
syzler@1 140
syzler@6 141 --- Summons a mount by spell ID.
syzler@6 142 -- Does nothing if the specified mount is already summoned.
syzler@6 143 -- @param spellId The spell ID of the desired mount.
syzler@6 144 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 145 function MyLilPony.CallMountBySpell(spellId)
syzler@1 146 local result = false;
syzler@1 147 local countMounts = GetNumCompanions("MOUNT");
syzler@1 148 for i = 1, countMounts do
syzler@6 149 local _, _, id, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 150 if id == spellId then
syzler@1 151 MyLilPony.CallCompanion("MOUNT", i, not summoned);
syzler@1 152 result = true;
syzler@1 153 do break end
syzler@6 154 end
syzler@6 155 end
syzler@1 156 return result;
syzler@1 157 end
syzler@1 158
syzler@6 159 --- Summons a mount by creature ID.
syzler@6 160 -- Does nothing if the specified mount is already summoned.
syzler@6 161 -- @param creatureId The creature ID of the desired mount.
syzler@6 162 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@1 163 function MyLilPony.CallMountByCreature(creatureId)
syzler@1 164 local result = false;
syzler@1 165 local countMounts = GetNumCompanions("MOUNT");
syzler@1 166 for i = 1, countMounts do
syzler@6 167 local id, _, _, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 168 if id == creatureId then
syzler@1 169 MyLilPony.CallCompanion("MOUNT", i, not summoned);
syzler@1 170 result = true;
syzler@1 171 do break end
syzler@6 172 end
syzler@6 173 end
syzler@1 174 return result;
syzler@1 175 end
syzler@1 176
syzler@6 177 --- Summons a mount by attempting to match the mount of the specified unit.
syzler@6 178 -- The function checks the buffs of the specified unit against the mount spellbook for matches.
syzler@6 179 -- As a result, the function will only likely work against player characters.
syzler@6 180 -- Does nothing if the specified unit's mount is already summoned.
syzler@6 181 -- @param unit The unit frame name (e.g. "target", "player", "focus") of the unit whose mount is desired.
syzler@6 182 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@7 183 -- @usage MyLilPony.CallMountByMatch("target");
syzler@1 184 function MyLilPony.CallMountByMatch(unit)
syzler@1 185 local result = false;
syzler@1 186 local unitBuffs = MyLilPony.GetUnitBuffs(unit);
syzler@1 187 local countMounts = GetNumCompanions("MOUNT");
syzler@1 188 for i = 1, countMounts do
syzler@6 189 local _, _, id, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 190 if unitBuffs[id] ~= nil then
syzler@1 191 MyLilPony.CallCompanion("MOUNT", i, not summoned);
syzler@1 192 result = true;
syzler@1 193 do break end
syzler@6 194 end
syzler@6 195 end
syzler@1 196 return result;
syzler@1 197 end
syzler@1 198
syzler@6 199 --- Summons a random mount after filtering the mount spellbook.
syzler@6 200 -- Does nothing if the specified mount is already summoned.
syzler@6 201 -- @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.
syzler@6 202 -- @return A Boolean value indicating whether or not a mount was successfully summoned.
syzler@6 203 -- @usage local filter = function (i)
syzler@6 204 -- local _, name, _, _, summoned = GetCompanionInfo("MOUNT", i);
syzler@6 205 -- return not summoned and string.match(name, pattern);
syzler@6 206 -- end
syzler@7 207 -- MyLilPony.CallMountByFilter(filter);
syzler@1 208 function MyLilPony.CallMountByFilter(filter)
syzler@1 209 local countMounts = GetNumCompanions("MOUNT");
syzler@1 210 if countMounts > 0 then
syzler@1 211 local results = {};
syzler@1 212 local countResults = 0;
syzler@1 213
syzler@1 214 for i = 1, countMounts do
syzler@1 215 if filter(i) then
syzler@1 216 countResults = countResults + 1;
syzler@1 217 results[countResults] = i;
syzler@1 218 end
syzler@1 219 end
syzler@1 220
syzler@1 221 if countResults > 0 then
syzler@1 222 local i = random(1, countResults);
syzler@1 223 local _, _, _, _, summoned = GetCompanionInfo("MOUNT", results[i]);
syzler@1 224 MyLilPony.CallCompanion("MOUNT", results[i], not summoned);
syzler@1 225 return true;
syzler@1 226 end
syzler@1 227 end
syzler@1 228 return false;
syzler@1 229 end
syzler@1 230
syzler@6 231 --- Lists available mounts by name.
syzler@6 232 -- @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.
syzler@1 233 function MyLilPony.ListMounts()
syzler@1 234 local results = {};
syzler@1 235 local countMounts = GetNumCompanions("MOUNT");
syzler@1 236 for i = 1, countMounts do
syzler@1 237 local _, name, _, _, _ = GetCompanionInfo("MOUNT", i);
syzler@1 238 results[i] = name;
syzler@1 239 end
syzler@1 240 return results;
syzler@1 241 end
syzler@1 242
syzler@6 243 --- Lists available ground mounts by name.
syzler@6 244 -- @return A list of ground mount names.
syzler@1 245 function MyLilPony.ListGroundMounts()
syzler@1 246 local results = {};
syzler@1 247 local countMounts = GetNumCompanions("MOUNT");
syzler@1 248 local x = 1;
syzler@1 249 for i = 1, countMounts do
syzler@1 250 local _, _, id, _, _ = GetCompanionInfo("MOUNT", i);
syzler@1 251 if MyLilPony.IsGroundMount(id) then
syzler@1 252 results[x] = name;
syzler@1 253 x = x + 1;
syzler@1 254 end
syzler@1 255 end
syzler@1 256 return results;
syzler@1 257 end
syzler@1 258
syzler@6 259 --- Lists available flying mounts by name.
syzler@6 260 -- @return A list of flying mount names.
syzler@1 261 function MyLilPony.ListFlyingMounts()
syzler@1 262 local results = {};
syzler@1 263 local countMounts = GetNumCompanions("MOUNT");
syzler@1 264 local x = 1;
syzler@1 265 for i = 1, countMounts do
syzler@1 266 local _, _, id, _, _ = GetCompanionInfo("MOUNT", i);
syzler@1 267 if MyLilPony.IsFlyingMount(id) then
syzler@1 268 results[x] = name;
syzler@1 269 x = x + 1;
syzler@1 270 end
syzler@1 271 end
syzler@1 272 return results;
syzler@1 273 end
syzler@1 274
syzler@6 275 --- Lists available aquatic mounts by name.
syzler@6 276 -- @return A list of aquatic mount names.
syzler@1 277 function MyLilPony.ListAquaticMounts()
syzler@1 278 local results = {};
syzler@1 279 local countMounts = GetNumCompanions("MOUNT");
syzler@1 280 local x = 1;
syzler@1 281 for i = 1, countMounts do
syzler@1 282 local _, _, id, _, _ = GetCompanionInfo("MOUNT", i);
syzler@1 283 if MyLilPony.IsAquaticMount(id) then
syzler@1 284 results[x] = name;
syzler@1 285 x = x + 1;
syzler@1 286 end
syzler@1 287 end
syzler@1 288 return results;
syzler@1 289 end
syzler@1 290
syzler@6 291 --- Lists available mounts names whose name matches a given pattern.
syzler@6 292 -- @param mountNamePattern A string pattern against which mount names are matched (does not require correct upper/lower case).
syzler@6 293 -- @return A list of matching mount names.
syzler@1 294 function MyLilPony.ListMountsByPattern(mountNamePattern)
syzler@1 295 local results = {};
syzler@1 296 local countMounts = GetNumCompanions("MOUNT");
syzler@1 297 local x = 1;
syzler@1 298 for i = 1, countMounts do
syzler@1 299 local _, name, _, _, _ = GetCompanionInfo("MOUNT", i);
syzler@1 300 if MyLilPony.StringMatchIgnoreCase(name, mountNamePattern) then
syzler@1 301 results[x] = name;
syzler@1 302 x = x + 1;
syzler@1 303 end
syzler@1 304 end
syzler@1 305 return results;
syzler@1 306 end
syzler@1 307