happydude11209

Better Computer Craft Turtle Item Sorter

Apr 1st, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. slotsFinished = {}
  2. for fillIndex = 1, 16 do
  3.     slotsFinished[fillIndex] = false
  4. end
  5.    
  6. function compactInv()
  7.     for checkSlot = 1, 16 do
  8.         if turtle.getItemCount(checkSlot) > 0 then
  9.             turtle.select(checkSlot)
  10.             for testSlot = checkSlot, 16 do
  11.                 if turtle.compareTo(testSlot) then
  12.                     turtle.select(testSlot)
  13.                     turtle.transferTo(checkSlot, turtle.getItemCount(testSlot))
  14.                     turtle.select(checkSlot)
  15.                 end
  16.             end
  17.         end
  18.     end
  19. end
  20.  
  21. function cleanInv(startSlot, stopSlot, stepSize)
  22.     if stepSize > 0 then
  23.         stepSize = 1
  24.         endOfInv = 1
  25.     elseif stepSize < 0 then
  26.         stepSize = -1
  27.         endOfInv = 16
  28.     else
  29.         stepSize = -1
  30.         endOfInv = 16
  31.     end
  32.    
  33.     if startSlot > 16 then startSlot = 16 end
  34.     if startSlot < 1 then startSlot = 1 end
  35.     if stopSlot > 16 then stopSlot = 16 end
  36.     if stopSlot < 1 then stopSlot = 1 end
  37.    
  38.     for checkSlot = startSlot, stopSlot, stepSize do
  39.         while turtle.getItemCount(checkSlot) > 0 do
  40.             moveSlot = endOfInv
  41.             turtle.select(checkSlot)
  42.             while not turtle.transferTo(moveSlot, turtle.getItemCount(checkSlot)) do
  43.                 moveSlot = moveSlot + stepSize
  44.             end
  45.             if moveSlot == checkSlot then
  46.                 break
  47.             end
  48.         end
  49.     end
  50. end
  51.  
  52. function checkIfFull() --returns true if the inventory is full and needs to dropUp() the first slot to free a slot
  53.     if turtle.getItemCount(1) > 0 then
  54.         return true
  55.     else
  56.         return false
  57.     end
  58. end
  59.  
  60. function getNextUnsorted()
  61.     for checkIndex = 1, 16 do
  62.         if not slotsFinished[checkIndex] then
  63.             return checkIndex -- returns the first unsorted slot
  64.             -- break -- should be unnecessary, but there just in case
  65.         end
  66.     end
  67.     return 0
  68. end
  69.  
  70. function sortInv()
  71.     local droppedItem = false
  72.     if checkIfFull() then
  73.         turtle.select(1)
  74.         turtle.dropUp()
  75.         droppedItem = true
  76.     end
  77.     sortsMade = 0
  78.     while sortsMade <= 16 do
  79.         turtle.select(16)
  80.         openSlot = getNextUnsorted()
  81.         turtle.transferTo(openSlot, turtle.getItemCount(16))
  82.         slotsFinished[openSlot] = true
  83.         cleanInv(16, getNextUnsorted(), -1)
  84.         sortsMade = sortsMade + 1
  85.         for checkSlot = 16, openSlot, -1 do
  86.             if checkSlot == (getNextUnsorted()) then
  87.                 break
  88.             end
  89.             turtle.select(openSlot)
  90.             if turtle.compareTo(checkSlot) then
  91.                 turtle.select(checkSlot)
  92.                 while turtle.getItemCount(checkSlot) > 0 do
  93.                     moveSlot = openSlot + 1
  94.                     while not turtle.transferTo(moveSlot, turtle.getItemCount(checkSlot)) do
  95.                         moveSlot = moveSlot + 1
  96.                     end
  97.                 end
  98.                 slotsFinished[moveSlot] = true
  99.                 cleanInv(checkSlot, getNextUnsorted(), -1)
  100.                 sortsMade = sortsMade + 1
  101.                 checkSlot = 16
  102.             end
  103.         end
  104.     end
  105.     if droppedItem then
  106.         turtle.suckUp()
  107.     end
  108. end
  109.  
  110. compactInv()
  111. cleanInv(16, 1, -1)
  112. sortInv()
  113. cleanInv(1, 16, 1)
  114. turtle.select(1)
Advertisement
Add Comment
Please, Sign In to add comment