Forte40

Sorting Turtle

Aug 19th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. local sortSlots = {}
  2. local input = turtle.suckDown
  3. local output = turtle.dropUp
  4. local wait = 10
  5.  
  6. local facing = 0
  7. function orient(dir)
  8.   if facing == dir then
  9.     return
  10.   elseif math.abs(facing - dir) == 2 then
  11.     turtle.turnLeft()
  12.     turtle.turnLeft()
  13.     facing = dir
  14.   elseif facing - dir == 1 or facing - dir == -3 then
  15.     turtle.turnLeft()
  16.     facing = dir
  17.   else
  18.     turtle.turnRight()
  19.     facing = dir
  20.   end
  21. end
  22.  
  23. for i = 1, 16 do
  24.   if turtle.getItemCount(i) > 0 then
  25.     sortSlots[i] = true
  26.   end
  27. end
  28. while true do
  29.   -- get items
  30.   turtle.select(1)
  31.   while input() do
  32.   end
  33.   local full = {}
  34.   for slot = 1, 16 do
  35.     if sortSlots[slot] and turtle.getItemCount(slot) > 1
  36.         or not sortSlots[slot] and turtle.getItemCount(slot) > 0 then
  37.       turtle.select(slot)
  38.       local similarSlots = {}
  39.       for sortSlot in pairs(sortSlots) do
  40.         if turtle.compareTo(sortSlot) then
  41.           table.insert(similarSlots, sortSlot)
  42.         end
  43.       end
  44.       if #similarSlots > 0 then
  45.         for i, sortSlot in ipairs(similarSlots) do
  46.           local chest = math.floor((sortSlot-1)/4)
  47.           if not full[chest] then
  48.             orient(chest)
  49.             local amount = turtle.getItemCount(slot)
  50.             if sortSlots[slot] then
  51.               amount = amount - 1
  52.             end
  53.  
  54.             -- temp fix for duplication bug
  55.             while amount > 64 do
  56.               if not turtle.drop(64) then
  57.                 full[chest] = true
  58.                 break
  59.               end
  60.               amount = amount - 64
  61.             end
  62.             if not full[chest] then
  63.             -- end temp fix
  64.  
  65.               if turtle.drop(amount) then
  66.                 break
  67.               else
  68.                 full[chest] = true
  69.               end
  70.  
  71.             -- temp fix
  72.             end
  73.             -- end temp fix
  74.  
  75.           end
  76.         end
  77.       else
  78.         output()
  79.       end
  80.     end
  81.   end
  82.   orient(0)
  83.   sleep(wait)
  84. end
Advertisement
Add Comment
Please, Sign In to add comment