Advertisement
Guest User

shuffledQueues.lua

a guest
Jul 19th, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. -- here's a shuffled queues library ig?
  2. -- written by rixithechao on 1/4/2024
  3. -- v0.1.0
  4.  
  5.  
  6.  
  7. -- QUEUE CLASS
  8. local Queue = {}
  9.  
  10. Queue = {
  11.  
  12.     -- METHODS
  13.     -- Returns true if there's no items left in the queue
  14.     isEmpty = function(self)
  15.         return (#self.remaining == 0)
  16.     end,
  17.  
  18.     -- Refills the queue and shuffles it
  19.     --    runEvent:         bool (default false)    if true and an onRefill event is defined for the cue, run that function
  20.     refill = function(self, runEvent)
  21.         self.remaining = table.ishuffle(table.iclone(self.itemSet))
  22.         if  self.onRefill ~= nil  and  runEvent == true  then
  23.             self:onRefill()
  24.         end
  25.     end,
  26.  
  27.     -- Gets the next item from the queue and removes it
  28.     --    avoidRepeats:     bool (default false)    attempt to avoid selecting a repeated item
  29.     --    maxAttempts:      int (default 10)        the max number of attempts to try picking a unique item
  30.     popNext = function(self, avoidRepeats, maxAttempts)
  31.         local pick = nil
  32.         local i = 0
  33.         maxAttempts = maxAttempts  or  10
  34.  
  35.         while  (pick == nil  or  (pick == self.previous  and  avoidRepeats  and  i < maxAttempts))  do
  36.            
  37.             -- Reset the queue
  38.             if  self:isEmpty()  then
  39.                 self:refill(true)
  40.             end
  41.  
  42.             -- Pick the attack
  43.             pick = self.remaining[1]
  44.  
  45.             -- Increment the attempts count
  46.             i = i+1
  47.  
  48.             -- If it's a repeat and there's attempts left
  49.             if  i < maxAttempts  then
  50.                 if  pick == self.previous  and  avoidRepeats  then
  51.                     if  #self.remaining > 1  then
  52.                         self.remaining[#self.remaining+1] = pick
  53.                     end
  54.                     table.remove(self.remaining, 1)
  55.                 end
  56.    
  57.             -- Otherwise, give up and go with the repeat
  58.             else
  59.                 --Misc.dialog("QUEUE FAILED AVOIDING A REPEAT AFTER "..tostring(maxAttempts).." ATTEMPTS.\nThis warning only appears when playtesting from the editor.", "\n\nFINAL PICK", pick, "\nREMAINING:", self.remaining, "\nITEM SET:", self.itemSet)
  60.             end
  61.         end
  62.  
  63.         if  not self:isEmpty()  then
  64.             table.remove(self.remaining, 1)
  65.         end
  66.  
  67.         self.previous = pick
  68.         return pick
  69.     end,
  70.  
  71.     -- Adds an item to the end of the queue
  72.     push = function(self, item)
  73.         self.remaining[#self.remaining+1] = item
  74.     end,
  75.  
  76.     -- Adds an item to the specified position in the queue (wrapper for lua's table.insert)
  77.     insert = function(self, ...)
  78.         table.insert(self.remaining, ...)
  79.     end,
  80.  
  81.     -- Refills the queue if it's empty, then gets the next item without removing it
  82.     getNext = function(self, shouldRefill)
  83.         if  self:isEmpty()  then
  84.             self:refill(true)
  85.         end
  86.         return self.remaining[1]
  87.     end,
  88.  
  89.     -- Refills the queue if it's empty, then gets the item at the end of the queue without removing it
  90.     getEnd = function(self)
  91.         if  self:isEmpty()  then
  92.             self:refill(true)
  93.         end
  94.         return self.remaining[#self.remaining]
  95.     end,
  96. }
  97. Queue.pop = Queue.popNext
  98.  
  99. local _classreadonly = {isEmpty=true, refill=true, popNext=true, pop=true, push=true, insert=true, getNext=true, getEnd=true}
  100.  
  101.  
  102.  
  103. -- QUEUE CLASS METATABLE
  104. local QueueMT = {
  105.     __index = function(obj,key)
  106.  
  107.         -- If it exists in the base structure, use that.
  108.         if  Queue[key] ~= nil  then
  109.             return Queue[key]
  110.  
  111.         -- Otherwise, get the member function/property.
  112.         else
  113.             return rawget(obj,key)
  114.         end
  115.     end,
  116.  
  117.     __newindex = function(obj,key,val)
  118.  
  119.         if  Queue[key] ~= nil  then
  120.             if  _classreadonly[key] ~= nil  then
  121.                 error(key.." is a read-only property of the Queue class")
  122.             else
  123.                 Queue[key] = val
  124.             end
  125.  
  126.         elseif  _classreadonly[key] ~= nil  then
  127.             error(key.." is a read-only property of the Queue object")
  128.         else
  129.             rawset(obj, key, val)
  130.         end
  131.     end
  132. }
  133.  
  134.  
  135. -- THE LIBRARY TABLE ITSELF
  136. local shuffledqueues = {
  137.  
  138.     -- Create a new queue and return the object reference
  139.     new = function(args)
  140.         local tbl = {
  141.             itemSet = args.items,
  142.             remaining = (args.remaining or {}),
  143.             previous = nil,
  144.             onRefill = args.onRefill
  145.         }
  146.         setmetatable(tbl, QueueMT)
  147.  
  148.         return tbl
  149.     end
  150. }
  151.  
  152.  
  153. return shuffledqueues
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement