Advertisement
Guest User

recycle

a guest
Nov 30th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. --=================================================
  2. -- Recycle Bin by Kadoba
  3. ---------------------------------------------------
  4. -- Turns an attached chest into a recycle bin.
  5. -- Recycle bins only delete stacks that have been
  6. -- inside of them for more than a specific length
  7. -- of time. By default this is one minute.
  8. --=================================================
  9.  
  10. ---------------------------------------------------
  11. -- Config
  12. ---------------------------------------------------
  13.  
  14. -- Delay in seconds between deleting items
  15. local delay = 5
  16.  
  17. -- The side of the computer that the recycle
  18. -- bin is on. Takes the first program argument
  19. -- or defaults to "right" if one isn't passed.
  20.  
  21. local side = ({...})[1] or "right"
  22.  
  23. -- If true then recycled items are logged
  24. local dolog = true
  25.  
  26. -- The log filename
  27. local logname = "recycle_log"
  28.  
  29. ---------------------------------------------------
  30. -- Setup -
  31. ---------------------------------------------------
  32.  
  33. local timeleft = delay    -- Delay time left
  34. local blink = false       -- Cursor blinking
  35. local deleted = 0         -- Total deleted stacks
  36. local list = {}           -- Buffered deleted list
  37. list.size = 8             -- Buffer size
  38. list.i = 0                -- List increment
  39.  
  40. -- Returns the buffer index for the list given
  41. -- the incremental index
  42. function list.increment_index(n)
  43.   return n % list.size
  44. end
  45.  
  46. -- Returns the buffer index for the list given
  47. -- the terminal display offset.
  48. function list.display_index(n)
  49.   return (n + list.i) % list.size
  50. end
  51.  
  52. -- Terminal display lines
  53. local lines = {}
  54. lines[1]  = "Recycle program has started."
  55. lines[2]  = "Attaching to %s on %s side."
  56. lines[3]  = ""
  57. lines[4]  = "Waiting 0..."
  58. lines[5]  = ""
  59. lines[6]  = "Most recent deleted items:"
  60. lines[7]  = "-------------------------------------"
  61. -- Buffered list is lines 8 through 15
  62. lines[16] = "-------------------------------------"
  63. lines[17] = "Deleted a total of 0 item stacks."
  64.  
  65. local log
  66.  
  67. if dolog then
  68.   local exists = fs.exists(logname)
  69.   log = fs.open(logname, exists and "a" or "w")
  70. end
  71.  
  72. ---------------------------------------------------
  73. -- Peripheral Attachment
  74. ---------------------------------------------------
  75.  
  76. local chest = peripheral.wrap(side)
  77.  
  78. if not chest then
  79.   error("Could not find chest on side: "..side)
  80. else
  81.   lines[2] = string.format(lines[2],
  82.     chest.getInventoryName(),
  83.     side
  84.   )
  85. end
  86.  
  87. local prev_stacks = chest.getAllStacks()
  88.  
  89. ---------------------------------------------------
  90. -- Loop Functions
  91. ---------------------------------------------------
  92.  
  93. -- Recycle stacks
  94. local function recycle()
  95.  
  96.   local stacks = chest.getAllStacks()
  97.   local prev_info, info, text
  98.   local prev_deleted = deleted
  99.  
  100.   if not stacks then
  101.     error("Chest appears to have been destroyed.")
  102.   end
  103.  
  104.   for slot, stack in pairs(prev_stacks) do
  105.     if stacks[slot] then
  106.       prev_info = stack.basic()
  107.       info = stacks[slot].basic()
  108.       if prev_info.id == info.id then
  109.         deleted = deleted + 1
  110.         text = string.format("%s x %d",
  111.                             info.display_name,
  112.                             info.qty)
  113.         list[ list.increment_index(list.i) ] = text
  114.         list.i = list.i + 1
  115.         if dolog then
  116.           log.writeLine(text)
  117.         end
  118.         chest.destroyStack(slot)
  119.         stacks[slot] = nil
  120.       end  
  121.     end
  122.   end
  123.  
  124.   if deleted > prev_deleted and dolog then
  125.     log.flush()
  126.   end
  127.   prev_stacks = stacks
  128.      
  129. end
  130.  
  131. -- Draw to terminal
  132. local function draw()
  133.  
  134.   local text = "Deleted a total of %d item stacks."
  135.   lines[17] = string.format(text, deleted)
  136.   text = "Waiting %d..."
  137.   lines[4] = string.format(text, timeleft)
  138.  
  139.   for i=0,list.size-1 do
  140.     lines[8+i] = list[ list.display_index(i) ] or ""
  141.   end
  142.  
  143.   term.clear()
  144.   for k,v in pairs(lines) do
  145.     term.setCursorPos(1,k)
  146.     term.write(lines[k])
  147.   end
  148.  
  149. end    
  150.    
  151. ---------------------------------------------------
  152. -- Main Program
  153. ---------------------------------------------------
  154.  
  155. while true do
  156.  
  157.   timeleft = timeleft - 1
  158.   if timeleft <= 0 then
  159.     recycle()
  160.     timeleft = delay
  161.   end
  162.  
  163.   draw()
  164.   sleep(1)
  165.  
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement