Advertisement
kssr3951

sifting(for chunkDig series)

Oct 24th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- slot1 : chest x3
  2. -- slot2 : cobblestone x1
  3. -- slot3 : dirt x1
  4. -- slot4 : gravel x1
  5. -- ----------------------------
  6. -- config
  7. -- ----------------------------
  8. local CHEST_SLOT = 1
  9. local EXCLUSION_SLOTS = {2,3,4}
  10. local OTHER_SLOTS = {5,6,7,8,9,10,11,12,13,14,15,16}
  11.  
  12. -- ----------------------------
  13. -- application
  14. -- ----------------------------
  15. local function extractItems(itemChestPlaced)
  16.   local garbageList = {}
  17.   local itemList = {}
  18.   for _, i in ipairs(OTHER_SLOTS) do
  19.     if 0 < turtle.getItemCount(i) then
  20.       turtle.select(i)
  21.       local isItem = true
  22.       for _, j in ipairs(EXCLUSION_SLOTS) do
  23.         if turtle.compareTo(j) then
  24.           isItem = false
  25.           table.insert(garbageList, i)
  26.           break
  27.         end
  28.       end
  29.       if isItem then
  30.         if not itemChestPlaced then
  31.           turtle.select(CHEST_SLOT)
  32.           while not turtle.placeUp() do
  33.             turtle.digUp()  
  34.           end
  35.           itemChestPlaced = true
  36.           turtle.select(i)
  37.         end
  38.         turtle.dropUp()
  39.         table.insert(itemList, i)
  40.       end
  41.     end
  42.   end
  43.   return garbageList, itemList, itemChestPlaced
  44. end
  45.  
  46. local function dump(garbageList, garbageChestPlaced)
  47.   turtle.turnLeft()
  48.   turtle.turnLeft()
  49.   if not garbageChestPlaced then
  50.     turtle.select(CHEST_SLOT)
  51.     turtle.place()
  52.   end
  53.   for _, i in ipairs(garbageList) do
  54.     turtle.select(i)
  55.     turtle.drop()
  56.   end
  57.   for _, i in ipairs(EXCLUSION_SLOTS) do
  58.     turtle.select(i)
  59.     turtle.drop(turtle.getItemCount(i) - 1)
  60.   end
  61.   turtle.turnRight()
  62.   turtle.turnRight()
  63.   return true
  64. end
  65.  
  66. local function sifting()
  67.   local gcPlaced = false
  68.   local icPlaced = false
  69.   local garbageList
  70.   local itemList
  71.   while true do
  72.     local empty = true
  73.     while turtle.suck() do empty = false end
  74.     garbageList, itemList, icPlaced = extractItems(icPlaced)
  75.     if 0 == #itemList then
  76.       gcPlaced = dump(garbageList, gcPlaced)
  77.     end
  78.     if empty then
  79.       if turtle.getItemCount(CHEST_SLOT) < 64 then
  80.         turtle.select(CHEST_SLOT)
  81.         turtle.dig()
  82.       end
  83.       break
  84.     end
  85.   end
  86. end
  87.  
  88. local function dumpItems()
  89.   turtle.turnLeft()
  90.   turtle.turnLeft()
  91.   for _, i in ipairs(OTHER_SLOTS) do
  92.     if 0 < turtle.getItemCount(i) then
  93.       turtle.select(i)
  94.       while not turtle.drop() do
  95.         print("Cannot drop items. sleep(5)")
  96.         os.sleep(5)
  97.       end
  98.     end
  99.   end
  100.   local chestCnt = turtle.getItemCount(CHEST_SLOT)
  101.   if 3 < chestCnt then
  102.     turtle.select(CHEST_SLOT)
  103.     while not turtle.drop(chestCnt - 3) do
  104.       print("Cannot drop chests. sleep(5)")
  105.       os.sleep(5)
  106.     end
  107.   end
  108.   turtle.turnRight()
  109.   turtle.turnRight()
  110. end
  111.  
  112. local function transportItems()
  113.   while turtle.suckUp() do empty = false end
  114. end
  115.  
  116. local function hasEmptySlot()
  117.   for i = 1, 16 do
  118.     if 0 == turtle.getItemCount(i) then
  119.       return true
  120.     end
  121.   end
  122.   return false
  123. end
  124. -- ----------------------------
  125. -- main
  126. -- ----------------------------
  127. while true do
  128.   local noResult = true
  129.   for i = 1, 100 do
  130.     turtle.up()
  131.     if turtle.detect() then
  132.       if turtle.suck() then
  133.         sifting()
  134.         noResult = false
  135.       else
  136.         turtle.select(CHEST_SLOT)
  137.         if turtle.compare() then
  138.           if turtle.getItemCount(CHEST_SLOT) < 64 then
  139.             turtle.dig()
  140.           else
  141.             noResult = false
  142.           end
  143.         end
  144.       end
  145.     end
  146.     if turtle.detectUp() then
  147.       if turtle.suckUp() then
  148.         transportItems()
  149.         noResult = false
  150.       else
  151.         turtle.select(CHEST_SLOT)
  152.         if turtle.compareUp() then
  153.           if hasEmptySlot() and not turtle.suckUp() then
  154.             turtle.digUp()
  155.           end
  156.           noResult = false
  157.         else
  158.           break
  159.         end
  160.       end
  161.     end
  162.   end
  163.   while turtle.down() do end
  164.   dumpItems()
  165.   if noResult then
  166.     break
  167.   end
  168. end
  169. print("sifting was completed!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement