ToLazyToThink

isolator automation

Aug 24th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. -- Turtle to sort the serums from an isolator recycling
  2. --   the ones that we already have enough of
  3. -- Requires: Open Peripherals
  4. -- By ToLazyToThink
  5. --
  6. -- http://forum.feed-the-beast.com/threads/automated-isolator.29888/
  7.  
  8. -- NOTES:
  9. --  FUEL: If you have more than one serum chest (including the "keep"
  10. --    chest) the turtle will use fuel to move up and check the extra
  11. --    chest.  This program doesn't check fuel levels, so make sure
  12. --    you give it plenty of fuel before you start it.
  13. --
  14. --  open peripherals uses compass directions, so it matters where you
  15. --    build things on the compass, not just in relation to the turtle.
  16. --
  17. -- If you are using something other than the direwolf20 1.5 v1.1.2 pack
  18. --   you may need to change the serumID to match the item id of filled
  19. --   serums in your pack.
  20. --
  21.  
  22. -- Turtles used 1 based slot indexes, but openp uses 0 based
  23. local keepSlot = 0
  24. local dumpSlot = 1
  25.  
  26. -- direction of the various blocks relative to the turtle
  27. local chestSide = "left"
  28. local dumpSide = "bottom"
  29. local isoSide = "right"
  30.  
  31. -- turtle is <compass dir> of the <isoloator|serumchest|dumpchest>
  32. local turtleIsoDir = "east"
  33. local turtleChestDir= "west"
  34. local turtleDumpDir = "up"
  35.  
  36. -- Item ID of a filled serum (all serums except the empty use
  37. --  the same ID)
  38. local serumId = 8803
  39.  
  40. -- The number of serum holding chest, this includes the bottom
  41. --   "keep" chest, that, at least in my setup, is just an
  42. --   AE import chest
  43. local numChest = 4
  44.  
  45. -- Maximum number of copies of the same serum to keep, this will
  46. --  not affect already stored serums, just new ones from the
  47. --  isolator
  48. local maxKeepSerums = 2
  49.  
  50. local knownSerums = nil
  51.  
  52. -- Check the isolator for new serums returns a table
  53. --   with the serum name as the key, and an array of
  54. --   slots as the value
  55. function checkIsolator()
  56.   local iso = peripheral.wrap(isoSide)
  57.  
  58.   local ret = {}
  59.   local n = iso.getSizeInventory()
  60.   for i = 0,(n-1) do
  61.     local itemInfo = iso.getStackInSlot(i)
  62.     if itemInfo ~= nil and itemInfo['id'] == serumId then
  63.       local slots = ret[itemInfo["name"]]
  64.       if slots == nil then
  65.         slots = {}
  66.         ret[itemInfo["name"]] = slots
  67.       end
  68.       slots[#slots + 1] = i
  69.     end
  70.   end
  71.  
  72.   return ret
  73. end
  74.  
  75. function addSerums(serums)
  76.   local chest = peripheral.wrap(chestSide)
  77.  
  78.   local n = chest.getSizeInventory()
  79.   for i = 0,(n-1) do
  80.     local itemInfo = chest.getStackInSlot(i)
  81.     if itemInfo ~= nil and itemInfo['id'] == serumId then
  82.       local count = serums[itemInfo["name"]]
  83.       if count == nil then
  84.         count = 0
  85.       end
  86.       serums[itemInfo["name"]] = count + itemInfo["qty"]
  87.     end
  88.   end
  89. end
  90.  
  91. function findKnownSerums()
  92.   local ret = {}
  93.   addSerums(ret)
  94.   if numChest > 1 then
  95.     for i = 2,numChest do
  96.       turtle.up()
  97.       addSerums(ret)
  98.     end
  99.   end
  100.   while turtle.down() do
  101.     -- no op
  102.   end
  103.   return ret
  104. end
  105.  
  106. function isSerumNeeded(name)
  107.   if knownSerums == nil then
  108.     knownSerums = findKnownSerums()
  109.   end
  110.  
  111.   local count = knownSerums[name]
  112.   if count == nil or count < maxKeepSerums then
  113.     return true
  114.   else
  115.     return false
  116.   end
  117. end
  118.  
  119. function keepSerum(name, slot)
  120.   local iso = peripheral.wrap(isoSide)
  121.   if iso.pushIntoSlot(turtleIsoDir, slot, 1, keepSlot) > 0 then
  122.     local chest = peripheral.wrap(chestSide)
  123.     if chest.pull(turtleChestDir, keepSlot, 1) > 0 then
  124.       local count = knownSerums[name]    
  125.       if count == nil then
  126.         count = 0
  127.       end
  128.       knownSerums[name] = count + 1
  129.       print("added " .. name)
  130.     else
  131.       print("failed to pull to keep chest: " .. name)
  132.     end
  133.   else
  134.     print("keep failed to push: " .. name)
  135.   end
  136. end
  137.  
  138. function dumpSerum(name, slot)
  139.   local iso = peripheral.wrap(isoSide)
  140.   if iso.pushIntoSlot(turtleIsoDir, slot, 1, dumpSlot) > 0 then
  141.     local chest = peripheral.wrap(dumpSide)
  142.     if chest.pull(turtleDumpDir, dumpSlot, 1) > 0 then
  143.       -- print("dumped " .. name)
  144.     else
  145.       print("failed to pull to dump chest: " .. name)
  146.     end
  147.   else
  148.     print("dump failed to push: " .. name)
  149.   end
  150. end
  151.  
  152. function reset()
  153.   while not turtle.detectDown() do
  154.     turtle.down()
  155.   end
  156.  
  157.   -- NOTE: turtles start slots at 1, but openp starts at 0
  158.   if turtle.getItemCount(keepSlot + 1) > 0 then
  159.     local chest = peripheral.wrap(chestSide)
  160.     while chest.pull(turtleChestDir, keepSlot, 1) < 1 do
  161.       print("failed to pull stale keep item")
  162.       sleep(5)
  163.     end
  164.   end
  165.  
  166.   -- NOTE: turtles start slots at 1, but openp starts at 0
  167.   if turtle.getItemCount(dumpSlot + 1) > 0 then
  168.     local chest = peripheral.wrap(dumpSide)
  169.     while chest.pull(turtleDumpDir, dumpSlot, 1) < 1 do
  170.       print("failed to pull stale dump item")
  171.       sleep(5)
  172.     end
  173.   end
  174. end
  175.  
  176. function main()
  177.   reset()
  178.   while true do
  179.     local newSerums = checkIsolator()
  180.     for name, slots in pairs(newSerums) do
  181.       if isSerumNeeded(name) then
  182.         for i, slot in ipairs(slots) do
  183.           keepSerum(name, slot)
  184.         end
  185.       else
  186.         for i, slot in ipairs(slots) do
  187.           dumpSerum(name, slot)
  188.         end
  189.       end
  190.     end
  191.     sleep(1)
  192.   end
  193. end
  194.  
  195. main()
Add Comment
Please, Sign In to add comment