LuaSlinger»Smart Bandage

Smart Bandage

Bandage (complex), or The Only Bandage Macro You'll Ever Need

Description: This finds and uses the smallest bandage possible to heal your target (default: self) to the desired level. The default is 80%.
How to use: Bind to key.

Note: after refining this code within LuaSlinger, I moved it out to a standalone Addon called SmartBandage, which has numerous enhancements over this script. However, this is a good example of some complex lua coding within LuaSlinger.

Note 2: SmartBandage is now SmartMedic, handling potions, food, and water in addition to bandages.

-- Use the smallest bandage possible.  Find it anywhere in the player's 
-- inventory.  Make sure the target receives healing, even if it's not optimal.
-- The algorithm is as follows:
-- Our goal is to heal the target to at least 80 percent of his/her maximum 
-- (adjustable below).  Identify the smallest type of bandage that can meet the 
-- healing need.  Try to find it in the player's inventory.  If not found, try 
-- the next better bandage and so on up the line.  If we didn't find a bandage, 
-- then go back and try to find the bandage just smaller than the desired bandage.  
-- If not found, try the next smaller bandage and so on.
--
-- NOTE: This requires the FindContainerItemByName() and p() functions found 
-- elsewhere on this wiki.
--

TARGET_HEALTH_PCT = 80

-- these MUST be listed in ascending order of potency
-- All known bandages are included as of 11/05/2005
bandages = {
  {"Linen Bandage", 66},
  {"Heavy Linen Bandage", 114},
  {"Wool Bandage", 161},
  {"Heavy Wool Bandage", 301},
  {"Silk Bandage", 400},
  {"Heavy Silk Bandage", 640},
  {"Mageweave Bandage", 800},
  {"Heavy Mageweave Bandage", 1104},
  {"Runecloth Bandage", 1360},
  {"Heavy Runecloth Bandage", 2000}
}

if (not UnitIsFriend("player","target")) then
    TargetUnit("player")
end

if (not UnitInParty("target")) then
    return
end

need = (UnitHealthMax("target") * TARGET_HEALTH_PCT/100) - UnitHealth("target")

if (need <= 0) then
    p("## Bandage: not enough need")
    return
end

desiredBandageIdx = table.getn(bandages)
for idx,b in bandages do
     if (b[2] >= need) then
          desiredBandageIdx = idx
          break
      end
end
b = bandages[desiredBandageIdx]
p("## Bandage: "..UnitName("target").." needs "..need.." HP, I'll try to use a "..b[1].."("..b[2]..")")

bag, slot = nil, nil
for i = desiredBandageIdx, table.getn(bandages) do
    bag, slot = FindContainerItemByName(bandages[i][1])
    if (bag ~= nil) then
        p("## Bandage: Using "..bandages[i][1].." ("..bandages[i][2]..") on "..UnitName("target"))
        UseContainerItem(bag, slot)
        return
    end
end

for i = desiredBandageIdx - 1, 1, -1 do
    bag, slot = FindContainerItemByName(bandages[i][1])
    if (bag ~= nil) then
        p("## Bandage: Using "..bandages[i][1].." ("..bandages[i][2]..") on "..UnitName("target"))
        UseContainerItem(bag, slot)
        return
    end
end

p("## Bandage: Sorry, couldn't find a bandage")