Advertisement
soee

tsort

Sep 19th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --pastebin:VCNreRFK
  2. --file:tsort
  3.  
  4. local t = turtle
  5. local offset = 0
  6. local sleepTime = 10
  7.  
  8. local function dropAt(pos)
  9.     for i = 1, pos + offset do
  10.         t.forward()
  11.     end
  12.     t.select(16)
  13.     local dropped = t.dropDown()
  14.     local curCount = t.getItemCount(pos)
  15.     if curCount > 1 then
  16.         t.select(pos)
  17.         t.dropDown(curCount - 1)
  18.     end
  19.     for i = 1, pos + offset do
  20.         t.back()
  21.     end
  22.     return dropped
  23. end
  24.  
  25. local function pickItem()
  26.     t.select(16)
  27.     local current = t.getItemCount(16)
  28.     if current > 0 then
  29.         return true
  30.     end
  31.     for i = 1, 15 do
  32.         local slotCount = t.getItemCount(i)
  33.         if slotCount > 1 then
  34.             t.select(i)
  35.             t.transferTo(16, slotCount-1)
  36.             return true
  37.         end
  38.     end
  39.     return t.suckDown()
  40. end
  41.  
  42. local function sortItem()
  43.     print("Sorting item")
  44.     local matchFound = false
  45.     local dropped = false
  46.     for i = 1, 15 do
  47.         if (t.compareTo(i)) then
  48.             matchFound = true
  49.             dropped = dropAt(i)
  50.             break
  51.         end
  52.     end
  53.     if (not matchFound) then
  54.         t.dropUp()
  55.     end
  56.     if (matchFound and not dropped) then
  57.         t.dropUp()
  58.     end
  59. end
  60.  
  61. local function detectItems()
  62.     print("Detecting items...")
  63.     t.turnRight()
  64.     t.forward()
  65.     t.turnLeft()
  66.     t.forward()
  67.     t.down()
  68.     t.down()
  69.     t.turnLeft()
  70.     t.forward()
  71.     t.turnRight()
  72.     for i = 1, 15 do
  73.         t.select(i)
  74.         if t.getItemCount(i) == 0 then
  75.             t.suckUp()
  76.         end
  77.         if (i < 15) then
  78.             t.forward()
  79.         end
  80.     end
  81.     for i = 1, 14 do
  82.         t.back()
  83.     end
  84.     t.turnRight()
  85.     t.forward()
  86.     t.up()
  87.     t.up()
  88.     t.back()
  89.     t.turnLeft()
  90.     t.back()
  91.     print("Item detection complete.")
  92. end
  93.  
  94. function main()
  95.     detectItems()
  96.     while (true) do
  97.         local hasItem = true
  98.         while (hasItem) do
  99.             local hasItem = pickItem()
  100.             if (hasItem) then
  101.                 sortItem()
  102.             end
  103.         end
  104.         sleep(sleepTime)
  105.     end
  106. end
  107.  
  108.  
  109. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement