Advertisement
WhiteFire_Sondergaar

Bean Counter

May 16th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. -- Bean Counter
  2. -- How much stuff are we getting?
  3. -- By Fenthis
  4.  
  5. -- Peripheral Sides
  6. local pdirs = { "top", "bottom", "front", "back", "left", "right" }
  7. -- Map out the other side from the inventory we find
  8. local oposit = { [0] = 1, 0, 3, 2, 5, 4 }
  9. -- Direction names
  10. local sdirs = { [0] = "top", "bottom", "north", "south", "west", "east" }
  11. -- Sleep between cycles
  12. local delay = 1
  13. -- Status channel
  14. local status_channel = 21231
  15.  
  16.  
  17. local sorter
  18. local modem
  19. local i
  20.  
  21. for i = 1, #pdirs do
  22.   sorter = peripheral.wrap(pdirs[i])
  23.   if p then
  24.     if p.sort then
  25.       sorter = p
  26.     end
  27.     if p.transmit then
  28.       modem = p
  29.     end
  30.   end
  31. end
  32.  
  33. if not sorter then
  34.   error("Can not find the interactive sorter!")
  35. end
  36.  
  37. if not modem then
  38.   print("WARNING: No modem found, unable to broadcast updates.")
  39. end
  40.  
  41. local inventories = {}
  42. for i = 0, 5 do
  43.   if sorter.list(i) then
  44.     table.insert(inventories, { ["direction"] = i, ["lastlist"] = {} })
  45.     print("Inventory to the "..sdirs[i])
  46.   end
  47. end
  48.  
  49. if #inventories == 0 then
  50.   error("Cound not find any attached inventories.")
  51. end
  52.  
  53. -- Only update when actually moving stuff
  54. local seen = {}
  55.  
  56. -- New inventory we are working with
  57. local new
  58.  
  59. print("Running! Hold CTRL-T to quit.")
  60.  
  61. while true do
  62.   local dirty = false
  63.   for i = 1, #inventories do
  64.     new = sorter.list(inventories[i].direction)
  65.     for item, count in pairs(new) do
  66.       if new[item] > 64 or new[item] == inventories[i].lastlist[item] then
  67.         sorter.extract(
  68.           inventories[i].direction,
  69.           item,
  70.           oposit[inventories[i].direction],
  71.           new[item]
  72.           )
  73.         print("Extracting: ", item, " count ", new[item],
  74.             " fromt ",  inventories[i].direction,
  75.             " to ", oposit[inventories[i].direction])
  76.         if not seen[item] then
  77.           seen[item] = 0
  78.         end
  79.         seen[item] = seen[item] + new[item]
  80.         new[item] = nil
  81.         dirty = true
  82.       end
  83.     end
  84.     inventories[i].lastlist = new
  85.   end
  86.  
  87.   if dirty then
  88.     modem.transmit(status_channel, 0, textutils.serialize(seen))
  89.   end
  90.  
  91.   sleep(delay)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement