Guest User

[SuperMacro][Auto-Drink][Auto-Standup] ConjurerHelper 1.0c

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