SHARE
TWEET

Untitled

a guest May 19th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[Balance chasing functions for Stygian.
  2.  
  3. --This function will take as its arguments
  4. --Stygian_chaser(codeString, options)
  5.  
  6. --options are passed as a table and parsed as follows (use boolean objects true or false. All will default to true)
  7.  
  8. --needsEq:     Do you have to have equilibrium in order perform the action?
  9. --needsBal:    Do you have to have balance in order to perform the action?
  10. --consumesEq:  Will it strip you of equilibrium to perform the action?
  11. --consumesBal: Will it strip you of balance to perform the action?
  12.  
  13. --What this means is it will default to all true. You need both to do it, and lose both when you do. Which doesn't happen often,
  14. --but a side effect of this is that to create a trigger which requires both but only consumes balance, you could call ]]--
  15.  
  16. --Stygian_chaser([[stygian_cleanse()]], {consumesEq = false})
  17.  
  18. --[[Minus the -- of course. The codeString argument should be used like the code to execute portion of tempTimer.
  19. Now, this function should be called as part of an event. I'd put it in two events, actually. onBalanceRegained, and onEquilibriumRegained or similar. Have these events check if you're chasing eq/bal currently, and if so do nothing (in case you get a prompt in between doing something and sending what you're doing) otherwise, it'll run the Stygian_doChase() function to actually try chasing eq/bal.
  20. The execution order is thus:
  21. First, if you have both balance and eq, it will execute all of the things which require both, but consume nothing. Then those which require eq, but consume nothing, then those which require bal, but consume nothing. Then it will check if you have anything which requires both and consumes both, and do that. Then its execution varies based on configuration. If you have it configured to check eq first, it will check for those things which require both but consume only eq, and execute that. If there is nothing which requires both but consumes only eq, it'll check for those which require both and consume only balance. It will then check for this which require only Eq and consume it, then those which require only balance and consume it.
  22. If you don't have both balance and equilibrium, it will only execute for the one you do have.
  23. ]]
  24. --initialize the tables of actions if they do not currently exist
  25. Stygian = Stygian or {}
  26. Stygian.config = Stygian.config or {}
  27. if Stygian.config.ChaseEqFirst == nil then Stygian.config.ChaseEqFirst = true end
  28. Stygian.NeedsEqAndBal = Stygian.NeedsEqAndBal or {}
  29. Stygian.NeedsEqAndBal.ConsumesEqAndBal = Stygian.NeedsEqAndBal.ConsumesEqAndBal or {}
  30. Stygian.NeedsEqAndBal.ConsumesEq = Stygian.NeedsEqAndBal.ConsumesEq or {}
  31. Stygian.NeedsEqAndBal.ConsumesBal = Stygian.NeedsEqAndBal.ConsumesBal or {}
  32. Stygian.NeedsEqAndBal.ConsumesNeither = Stygian.NeedsEqAndBal.ConsumesNeither or {}
  33. Stygian.NeedsEq = Stygian.NeedsEq or {}
  34. Stygian.NeedsEq.ConsumesEq = Stygian.NeedsEq.ConsumesEq or {}
  35. Stygian.NeedsEq.ConsumesNothing = Stygian.NeedsEq.ConsumesNothing or {}
  36. Stygian.NeedsBal = Stygian.NeedsBal or {}
  37. Stygian.NeedsBal.ConsumesBal = Stygian.NeedsBal.ConsumesBal or {}
  38. Stygian.NeedsBal.ConsumesNothing = Stygian.NeedsBal.ConsumesNothing or {}
  39. Stygian.chasingEq = Stygian.chasingEq or false
  40. Stygian.chasingBal = Stygian.chasingBal or false
  41. Stygian.currentlyChasingEq = Stygian.currentlyChasingEq or ""
  42. Stygian.currentlyChasingBal = Stygian.currentlyChasingBal or ""
  43. Stygian.balances = Stygian.balances or {}
  44. Stygian.balances.balance = Stygian.balances.balance or 1
  45. Stygian.balances.equilibrium = Stygian.balances.equilibrium or 1
  46.  
  47. function Stygian_chaserReset()
  48.   Stygian.NeedsEqAndBal = {}
  49.   Stygian.NeedsEqandBal.ConsumesEqAndBal = {}
  50.   Stygian.NeedsEqAndBal.ConsumesEq = {}
  51.   Stygian.NeedsEqAndBal.ConsumesBal = {}
  52.   Stygian.NeedsEqAndBal.ConsumesNeither = {}
  53.   Stygian.NeedsEq = Stygian.NeedsEq or {}
  54.   Stygian.NeedsEq.ConsumesEq = {}
  55.   Stygian.NeedsEq.ConsumesNothing = {}
  56.   Stygian.NeedsBal = {}
  57.   Stygian.NeedsBal.ConsumesBal = {}
  58.   Stygian.NeedsBal.ConsumesNothing = {}
  59.   Stygian.chasingEq = false
  60.   Stygian.chasingBal = false
  61.   Stygian.currentlyChasingEq = ""
  62.   Stygian.currentlyChasingBal = ""
  63. end
  64.  
  65. function Stygian_chaser(code, optiontable)
  66.     if not code then --If they don't give us code to send, print some usage information
  67.     cecho(align("You should at least tell us what to do.", {cap = "--=={{", capColor = "<green>", inside = false, textColor = "<purple>"}))
  68.     cecho("<purple>Usage is:\n  Stygian_chaser([[lua code to execute]], {table of options})\n\nMinimum usage is Stygian_chaser([[send(\"command\")]]) which will send \"command\" to the mud, minus the quotes, when you have EQ and Balance, and expect it to consume both")
  69.   end
  70.   local options
  71.   if type(code) ~= "string" then code = tostring(code) end --ensure the code they're sending is a string
  72.  
  73. --if they send a table as the second argument, copy it into our options. If they send anything else, or nothing at all, make options an empty table
  74.   if type(optiontable) ~= "table" then options = {} else options = optiontable end
  75.  
  76. --initialize all options to true, but only if the user hasn't specified false. (this way if they specify something other than false, like "banana", it'll default to true. This keeps my code from breaking later, and what the options are is pretty clearly laid out.
  77.   if options.needsEq ~= false then options.needsEq = true end
  78.   if options.needsBal ~= false then options.needsBal = true end
  79.   if options.consumesEq ~= false then options.consumesEq = options.needsEq end --We can't consume EQ if we don't need EQ
  80.   if options.consumesBal ~= false then options.consumesBal = options.needsBal end --Same for balance... if we're not requiring balance, we cannot consume
  81.  
  82.  
  83. --a bunch of messy if/elseif logic to build the tables we'll be using (ok, not that messy, but ugly)
  84.   if options.needsEq and options.needsBal then
  85.     if options.consumesEq and options.consumesBal then
  86.       table.insert(Stygian.NeedsEqAndBal.ConsumesEqAndBal, code)
  87.     elseif options.consumesEq and not options.consumesBal then
  88.       table.insert(Stygian.NeedsEqAndBal.ConsumesEq, code)
  89.     elseif not options.consumesEq and options.consumesBal then
  90.       table.insert(Stygian.NeedsEqAndBal.ConsumesBal, code)
  91.     elseif not options.consumesEq and not options.consumesBal then
  92.       table.insert(Stygian.NeedsEqAndBal.ConsumesNeither, code)
  93.     end
  94.   elseif options.needsEq and not options.needsBal then
  95.     if options.consumesEq then
  96.       table.insert(Stygian.NeedsEq.ConsumesEq, code)
  97.     else
  98.       table.insert(Stygian.NeedsEq.ConsumesNothing, code)
  99.     end
  100.   elseif not options.needsEq and options.needsBal then
  101.     if options.consumesBal then
  102.       table.insert(Stygian.NeedsBal.ConsumesBal, code)
  103.     else
  104.       table.insert(Stygian.NeedsBal.ConsumesNothing, code)
  105.     end
  106.   end
  107. end
  108.  
  109.  
  110. --a function to do maintenance stuff for chasing EQ
  111. function Stygian_chasingEq()
  112.    Stygian.chasingEq = true
  113.   tempTimer(0.5, [[Stygian.chasingEq = false;Stygian.currentlyChasingEq = ""
  114. end
  115.  
  116.  
  117. --a function to do maintenance stuff for chasing BAL
  118. function Stygian_chasingBal()
  119.   Stygian.chasingBal = true
  120.   tempTimer(0.5, [[Stygian.chasingBal = false ; Stygian.currentlyChasingBal = ""]])
  121. end
  122.  
  123.  
  124. --the function to do the chasing! Should be fired by either the prompt, or by events for regaining Eq and Bal.
  125. function Stygian_doChase()
  126. --  if Stygian.writhing > 0 then return end
  127.   if Stygian.balances.balance == 1 and Stygian.balances.equilibrium == 1 then  --we have both EQ and balance
  128.         if #Stygian.NeedsEqAndBal.ConsumesNeither > 0 then --so do the things which need both but consume nothing
  129.           for i=1,#Stygian.NeedsEqAndBal.ConsumesNeither do
  130.         assert(loadstring(table.remove(Stygian.NeedsEqAndBal.ConsumesNeither,1))) ()
  131.       end
  132.     end
  133.     if #Stygian.NeedsEq.ConsumesNothing > 0 then -- and the things which require only eq but consume nothing
  134.           for i=1,#Stygian.NeedsEq.ConsumesNothing do
  135.         assert(loadstring(table.remove(Stygian.NeedsEq.ConsumesNothing,1))) ()
  136.       end
  137.     end
  138.     if #Stygian.NeedsBal.ConsumesNothing > 0 then -- and the things which require balance but consume nothing
  139.           for i=1, #Stygian.NeedsBal.ConsumesNothing do
  140.         assert(loadstring(table.remove(Stygian.NeedsBal.ConsumesNothing,1))) ()
  141.       end
  142.     end
  143.     if #Stygian.NeedsEqAndBal.ConsumesEqAndBal > 0 and not Stygian.chasingEq and not Stygian.chasingBal then -- then if we have anything which requires both and consumes both, do that.
  144.             Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEqAndBal.ConsumesEqAndBal,1)
  145.       Stygian.currentlyChasingBal = _G[Stygian[currentlyChasingEq]]
  146.       Stygian_chasingBal()
  147.       Stygian_chasingEq()
  148.       assert(loadstring(Stygian.currentlyChasingEq)) ()
  149.       return
  150.     end
  151.     if Stygian.config.ChaseEqFirst == true then --then, we check if we're configured to chase equilibrium before balance. If so
  152.             if #Stygian.NeedsEqAndBal.ConsumesEq > 0 and not Stygian.chasingEq and not Stygian.chasingBal then -- Check if we have something which requires Eq and Bal, but consumes only Eq. Use failsafe to be sure we're not in the process of trying to use up one or both
  153.                 Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEqAndBal.ConsumesEq,1)
  154.         Stygian_chasingEq()
  155.         assert(loadstring(Stygian.currentlyChasingEq)) ()
  156.       end
  157.       if #Stygian.NeedsEqAndBal.ConsumesBal > 0 and not Stygian.chasingEq and not Stygian.chasingBal then --then check if we have something which needs both eq and bal, but consumes only Bal. Same failsafe, so if the previous if fires off an action, this one won't eval true, even if we haven't lost EQ yet.
  158.                 Stygian.currentlyChasingBal = table.remove(Stygian.NeedsEqAndBal.ConsumeBal,1)
  159.         Stygian_chasingBal()
  160.         assert(loadstring(Stygian.currentlyChasingBal)) ()
  161.       end
  162.       if #Stygian.NeedsEq.ConsumesEq > 0 and not Stygian.chasingEq then
  163.                Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEq.ConsumesEq,1)
  164.         Stygian_chasingEq()
  165.         assert(loadstring(Stygian.currentlyChasingEq)) ()
  166.       end
  167.       if #Stygian.NeedsBal.ConsumesBal > 0 and not Stygian.chasingBal then
  168.                Stygian.currentlyChasingBal = table.remove(Stygian.NeedsBal.ConsumesBal,1)
  169.         Stygian_chasingBal()
  170.         assert(loadstring(Stygian.currentlyChasingBal)) ()
  171.       end
  172.     else
  173.       if #Stygian.NeedsEqAndBal.ConsumesBal > 0 and not Stygian.chasingBal then
  174.                 Stygian.currentlyChasingBal = table.remove(Stygian.NeedsEqAndBal.ConsumesBal,1)
  175.         Stygian_chasingBal()
  176.         assert(loadstring(Stygian.currentlyChasingBal)) ()
  177.       end
  178.       if #Stygian.NeedsEqAndBal.ConsumesEq > 0 and not Stygian.chasingEq then
  179.                 Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEqAndBal.ConsumesEq,1)
  180.         Stygian_chasingEq()
  181.         assert(loadstring(Stygian.currentlyChasingEq)) ()
  182.       end
  183.       if #Stygian.NeedsBal.ConsumesBal > 0 and not Stygian.chasingBal then
  184.         Stygian.currentlyChasingBal = table.remove(Stygian.NeedsBal.ConsumesBal,1)
  185.                 Stygian_chasingBal()
  186.         assert(loadstring(Stygian.currentlyChasingBal)) ()
  187.       end
  188.       if #Stygian.NeedsEq.ConsumesEq > 0 and not Stygian.chasingEq then
  189.                 Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEq.ConsumesEq,1)
  190.         Stygian_chasingEq()
  191.         assert(loadstring(Stygian.currentlyChasingEq)) ()
  192.       end
  193.     end
  194.     return
  195.   elseif Stygian.balances.balance < 1 and Stygian.balances.equilibrium == 1 then
  196.        if #Stygian.NeedsEq.ConsumesNothing > 0 then
  197.             for i=1,#Stygian.NeedsEq.ConsumesNothing do
  198.         assert(loadstring(table.remove(Stygian.NeedsEq.ConsumesNothing,1))) ()
  199.       end
  200.     end
  201.     if #Stygian.NeedsEq.ConsumesEq > 0 and not Stygian.chasingEq then
  202.             Stygian.currentlyChasingEq = table.remove(Stygian.NeedsEq.ConsumesEq,1)
  203.       Stygian_chasingEq()
  204.       assert(loadstring(Stygian.currentlyChasingEq)) ()
  205.     end
  206.     return
  207.   elseif Stygian.balances.balance == 1 and Stygian.balances.equilibrium ~= 1 then
  208.     if #Stygian.NeedsBal.ConsumesNothing > 0 then
  209.             for i=1,#Stygian.NeedsBal.ConsumesNothing do
  210.         assert(loadstring(table.remove(Stygian.needsEq.ConsumesNothing,1))) ()
  211.       end
  212.     end
  213.     if #Stygian.NeedsBal.ConsumesBal > 0 and not Stygian.chasingBal then
  214.       Stygian.currentlyChasingBal = table.remove(Stygian.NeedsBal.ConsumesBal,1)
  215.       Stygian_chasingBal()
  216.       assert(loadstring(Stygian.currentlyChasingBal)) ()
  217.     end
  218.     return
  219.   else
  220.     return
  221.   end      
  222. end
RAW Paste Data
Top