Advertisement
Guest User

[SuperMacro][Auto-Drink][Auto-Standup] ConjurerHelper 0.01f

a guest
Feb 10th, 2019
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. local __version__ = "0.01f"
  2. -------------------------------------------------------------------------------
  3. local function HasBuff(texture_name)
  4.   for i=1, 32 do
  5.     local texture = UnitBuff("player", i)
  6.     if texture and string.find(texture, texture_name) then
  7.       return true
  8.     end
  9.   end
  10.   return false
  11. end
  12. local function ItemCount(search_name)
  13.   local search_count = 0
  14.   for bag_idx = 0, NUM_BAG_SLOTS do
  15.     for slot_idx = 0, GetContainerNumSlots(bag_idx) do
  16.       local item_link = GetContainerItemLink(bag_idx, slot_idx)
  17.       if item_link and string.find(item_link, search_name) then
  18.         local item_texture, item_slot_count = GetContainerItemInfo(bag_idx, slot_idx)
  19.         search_count = search_count + item_slot_count
  20.       end
  21.     end
  22.   end
  23.   return search_count
  24. end
  25. local function UseItem(item_name)
  26.   local function GetFirstItem(search_name)
  27.     for bag_idx = 0, NUM_BAG_SLOTS do
  28.       for slot_idx = 0, GetContainerNumSlots(bag_idx) do
  29.         local item_link = GetContainerItemLink(bag_idx, slot_idx)
  30.         if item_link and string.find(item_link, search_name) then
  31.           return bag_idx, slot_idx
  32.         end
  33.       end
  34.     end
  35.     return nil, nil
  36.   end
  37.  
  38.   local bag_idx, slot_idx = GetFirstItem(item_name)
  39.   if bag_idx == nil then
  40.     return false
  41.   end
  42.    
  43.   UseContainerItem(bag_idx, slot_idx)
  44.   return true
  45. end
  46. -------------------------------------------------------------------------------
  47. local function setup(settings, defaults)
  48.   if not ConjurerFrame then
  49.     ConjurerFrame = CreateFrame("Frame", "Conjurer")
  50.   end
  51.  
  52.   ConjurerFrame:SetScript("OnEvent", OnError)
  53.   ConjurerFrame:RegisterEvent("UI_ERROR_MESSAGE")
  54.  
  55.   defaults = defaults or {}
  56.   defaults.water_rank        = defaults.water_rank        or 7
  57.   defaults.water_amount      = defaults.water_amount      or 10*20
  58.   defaults.food_rank         = defaults.food_rank         or 6
  59.   defaults.food_amount       = defaults.food_amount       or 10*20
  60.   defaults.regeneration_rank = defaults.regeneration_rank or 7
  61.   defaults.outofmana_margin  = defaults.outofmana_margin  or 20
  62.  
  63.   settings = settings or {}
  64.   return {
  65.     water_rank        = settings.water_rank        or defaults.water_rank;
  66.     water_amount      = settings.water_amount      or defaults.water_amount;
  67.     food_rank         = settings.food_rank         or defaults.food_rank;
  68.     food_amount       = settings.food_amount       or defaults.food_amount;
  69.     regeneration_rank = settings.regeneration_rank or defaults.regeneration_rank;
  70.     outofmana_margin  = settings.outofmana_margin  or defaults.outofmana_margin;  
  71.   }
  72. end
  73. local function reset()
  74.   if not ConjurerFrame then return end
  75.  
  76.   ConjurerFrame:SetScript("OnEvent", nil)
  77.   ConjurerFrame:UnregisterEvent("UI_ERROR_MESSAGE")
  78. end
  79. -------------------------------------------------------------------------------
  80. local ShowErrors = 0
  81. local function disableErrorWarnings()
  82.   ShowErrors = GetCVar("ShowErrors")
  83.   SetCVar("ShowErrors", 0)
  84.   UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE")
  85. end
  86. local function restoreErrorWarnings()
  87.   SetCVar("ShowErrors", ShowErrors)
  88.   UIErrorsFrame:RegisterEvent("UI_ERROR_MESSAGE")
  89. end
  90. -------------------------------------------------------------------------------
  91. local errors = {}
  92. local NEED_TO_BE_STANDING = "You must be standing to do that";
  93. local NOT_ENOUGH_MANA = "Not enough mana"
  94.  
  95. function HasError(const_error)
  96.   for n, item in errors do
  97.     if item == const_error then
  98.       return n
  99.     end
  100.   end
  101.   return -1
  102. end
  103.  
  104. function OnError()
  105.   if arg1 == NEED_TO_BE_STANDING then
  106.     if HasError(NEED_TO_BE_STANDING) < 0 then
  107.       table.insert(errors, NEED_TO_BE_STANDING)
  108.     end
  109.   end
  110. end
  111.  
  112. function ResolveError(const_error)
  113.   n = HasError(const_error)
  114.   if n >= 0 then table.remove(errors, n) end
  115. end
  116. -------------------------------------------------------------------------------
  117. local conjuringspell = {
  118.   [1] = { food = "Muffin";        water = "Water";            cost = 60; };
  119.   [2] = { food = "Bread";         water = "Fresh Water";      cost = 105; };
  120.   [3] = { food = "Rye";           water = "Purified Water";   cost = 180; };
  121.   [4] = { food = "Pumpernickel";  water = "Spring Water";     cost = 285; };
  122.   [5] = { food = "Sourdough";     water = "Mineral Water";    cost = 420; };
  123.   [6] = { food = "Sweet Roll";    water = "Sparkling Water";  cost = 585; };
  124.   [7] = { food = "Cinnamon Roll"; water = "Crystal Water";    cost = 705; };
  125.   lookup = function(self, context, rank)
  126.     return {
  127.       spellname = 'Conjure '..context..'(Rank ' .. rank .. ')';
  128.       itemname = 'Conjured ' .. self[rank][strlower(context)];
  129.       spellcost = self[rank].cost;
  130.     }
  131.   end;
  132. }
  133. -------------------------------------------------------------------------------
  134. local function LowStockOrdered(conjureables)
  135.   for n, conjureable in conjureables do
  136.     if ItemCount(conjureable.itemname) < conjureable.amount then
  137.       return conjureable
  138.     end
  139.   end
  140.   return nil
  141. end
  142. --= Actual Macro Function to be called =---------------------------------------
  143. function Conjure_Click(settings)
  144.   reset()
  145.   local settings = setup(settings, {
  146.     water_rank        = 7;
  147.     water_amount      = 10*20;
  148.     food_rank         = 6;
  149.     food_amount       = 10*20;
  150.     regeneration_rank = 7;
  151.     outofmana_margin  = 20;
  152.   })
  153.  
  154.   local food = conjuringspell:lookup("Food", settings.food_rank)
  155.         food.amount = settings.food_amount
  156.   local water = conjuringspell:lookup("Water", settings.water_rank)
  157.         water.amount = settings.water_amount
  158.   local drink = conjuringspell:lookup("Water", settings.regeneration_rank)
  159.         drink.outofmana_margin = settings.outofmana_margin
  160.  
  161.   local conjureable = LowStockOrdered({water, food})
  162.   if not conjureable then return true end
  163.  
  164.   disableErrorWarnings()
  165.   if HasError(NEED_TO_BE_STANDING) >= 0 then
  166.     DoEmote("Stand")
  167.     ResolveError(NEED_TO_BE_STANDING)
  168.   elseif not HasBuff('Drink') then
  169.     if UnitMana('player') <= conjureable.spellcost+drink.outofmana_margin then
  170.       UseItem(drink.itemname)
  171.     else
  172.       CastSpellByName(conjureable.spellname)
  173.     end
  174.   end
  175.   restoreErrorWarnings()
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement