punchin

Turtle Inventory Sorting

Apr 2nd, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. local s = 0
  2. local l = 16
  3.  
  4. local function stack()
  5.     print("Stacking Items")
  6.     for i=1,15 do
  7.         s = turtle.getItemSpace(i)
  8.         for j=i+1,l do
  9.             turtle.select(j)
  10.             if turtle.compareTo(i) then
  11.                 turtle.transferTo(i,s)
  12.                 if turtle.getItemSpace(i) == 0 then
  13.                     break
  14.                 end
  15.             end
  16.         end
  17.     end
  18. end
  19.  
  20. local function compact()
  21.     print("Compressing Open Space")
  22.     for i=1,l do
  23.         if turtle.getItemCount(i) == 0 then
  24.             for j=i+1,l do
  25.                 if turtle.getItemCount(j) > 0 then
  26.                     turtle.select(j)
  27.                     turtle.transferTo(i)
  28.                     break
  29.                 end
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. local function vSpace()
  36.     if s == 1 then
  37.         print("Verified space")
  38.         return
  39.     else
  40.         print("Not enough space. Please clear at least 1 slot and press enter")
  41.         read()
  42.         space()
  43.     end
  44. end
  45.  
  46. local function space()
  47.     print("Checking for available space")
  48.  
  49.     s = 0
  50.  
  51.     for i=1,16 do
  52.         if turtle.getItemCount(i) == 0 then
  53.             s = 1
  54.             l = i-1
  55.             break
  56.         end
  57.     end
  58.     vSpace()
  59. end
  60.  
  61. local function sort()
  62.     print("Commensing sorting")
  63.     for i=1,l-1 do
  64.         for j=i+2,l+1 do
  65.             turtle.select(j)
  66.             if turtle.compareTo(i) then
  67.                 turtle.select(i+1)
  68.                 turtle.transferTo(l+1,64)
  69.                 turtle.select(j)
  70.                 turtle.transferTo(i+1,64)
  71.                 turtle.select(l+1)
  72.                 turtle.transferTo(j,64)
  73.                 break
  74.             end
  75.         end
  76.     end
  77.     l=l+1
  78.     compact()
  79.     stack()
  80.     turtle.select(1)
  81. end
  82.  
  83. stack()
  84. compact()
  85. space()
  86. sort()
Advertisement
Add Comment
Please, Sign In to add comment