Advertisement
Jeerachee

Tree Farm

Jul 25th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. sel = 1;
  2. turtle.select(sel);
  3. select = function(n)
  4.     sel = n;
  5.     return turtle.select(n);
  6. end
  7. getSelected = function()
  8.     return sel;
  9. end
  10.  
  11. saplingSlot = 1
  12. logSlot = 2
  13. chestSlot = 3
  14.  
  15. function moveMethod(m)
  16.     local r = function()
  17.         local moved = m()
  18.         if not moved then
  19.             cantMove(m)
  20.         end
  21.         return moved
  22.     end
  23.     return r
  24. end
  25. function moveThenSuckMethod(m)
  26.     local r = function()
  27.         local moved = m()
  28.         if not moved then
  29.             cantMove(m)
  30.         end
  31.         turtle.suckDown()
  32.         return moved
  33.     end
  34.     return r
  35. end
  36.  
  37. up = moveMethod(turtle.up)
  38. down = moveMethod(turtle.down)
  39. forward = moveThenSuckMethod(turtle.forward)
  40. back = moveMethod(turtle.back)
  41.  
  42. function clearChestSlot()
  43.     if turtle.getItemCount(chestSlot) > 0 then
  44.         select(chestSlot)
  45.         for i=1,16 do
  46.             if turtle.getItemCount(i) == 0 then
  47.                 if i ~= saplingSlot and i ~= logSlot and i ~= chestSlot then
  48.                     turtle.transferTo(i)
  49.                 end
  50.             else
  51.                 if i~= chestSlot and turtle.compareTo(i) and turtle.getItemSpace(i) > 0 then
  52.                     turtle.transferTo(i)
  53.                     clearChestSlot()
  54.                 end
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60. cantMove = function(move)
  61.     if turtle.getFuelLevel() > 0 then
  62.         select(logSlot)
  63.         if turtle.compare() then
  64.             turtle.dig()
  65.             forward()
  66.             turtle.digDown()
  67.             if turtle.getItemCount(saplingSlot) > 0 then
  68.                 select(saplingSlot)
  69.                 turtle.placeDown()
  70.                 select(logSlot)
  71.             end
  72.             local n = 0
  73.             local didCheck = false -- encountered a bug that stops the turtle short sometimes
  74.             local keepChopping = turtle.compareUp()
  75.             while keepChopping do
  76.                 turtle.digUp()
  77.                 up()
  78.                 select(saplingSlot)
  79.                 for i=1,4 do
  80.                     turtle.dig()
  81.                     turtle.turnRight()
  82.                 end
  83.                 n = n + 1
  84.                 select(logSlot)
  85.                 if turtle.compareUp() then
  86.                     keepChopping = true
  87.                     didCheck = false
  88.                 else
  89.                     if didCheck then
  90.                         keepChopping = false
  91.                     else
  92.                         didCheck = true
  93.                     end
  94.                     select(saplingSlot)
  95.                 end
  96.             end
  97.             while n > 0 do
  98.                 down()
  99.                 n = n - 1
  100.             end
  101.         else
  102.             while not move() do
  103.                 sleep(1)
  104.             end
  105.         end
  106.     else
  107.         local dig, place, drop, suck = nil
  108.         select(logSlot)
  109.         if not turtle.detectUp() then
  110.             dig = turtle.digUp
  111.             place = turtle.placeUp
  112.             drop = turtle.dropUp
  113.             suck = turtle.suckUp
  114.         else
  115.             dig = turtle.dig
  116.             place = turtle.place
  117.             drop = turtle.drop
  118.             suck = turtle.suck
  119.         end
  120.         dig()
  121.         select(chestSlot)
  122.         place()
  123.         for i=1,16 do
  124.             select(i)
  125.             if i~=logSlot then
  126.                 drop()
  127.             end
  128.         end
  129.         if turtle.getItemCount(logSlot) > 3 then
  130.             turtle.craft(3)
  131.             turtle.refuel()
  132.         end
  133.         select(1)
  134.         while suck() do end
  135.         clearChestSlot()
  136.         select(chestSlot)
  137.         dig()
  138.         select(saplingSlot)
  139.         suck()
  140.         move()
  141.     end
  142. end
  143.  
  144. actions = {u=up,d=down,f=forward,b=back,tl=turtle.turnLeft,tr=turtle.turnRight}
  145. function doInstructions(arr)
  146.     for i,v in ipairs(arr) do
  147.         if type(v) == 'function' then
  148.             v()
  149.         else
  150.             local p = v:find(":")
  151.             local a = {}
  152.             if p == nil then
  153.                 a = {v, 1}
  154.             else
  155.                 a = {v:sub(1, p-1), tonumber(v:sub(p+1))}
  156.             end
  157.             if actions[a[1]] ~= nil then
  158.                 for vari=1,a[2],1 do
  159.                     actions[a[1]]()
  160.                 end
  161.             end
  162.         end
  163.     end
  164. end
  165.  
  166. function max(j, k)
  167.     if j > k then
  168.         return j
  169.     else
  170.         return k
  171.     end
  172. end
  173.  
  174. function dropOffItems()
  175.     for i=1,16 do
  176.         select(i)
  177.         if i == logSlot then
  178.             turtle.dropDown(max(turtle.getItemCount(logSlot) - 12, 0))
  179.         elseif i ~= saplingSlot and i ~= chestSlot then
  180.             turtle.dropDown()
  181.         end
  182.     end
  183. end
  184.  
  185. inst = {'u', 'f', 'tr', 'f:15', 'tr', 'f:3', 'tr', 'f:12', 'tl', 'f:3', 'tl', 'f:12', 'tr', 'f:3', 'tr', 'f:12', 'tl', 'f:3', 'tl', 'f:12', 'tr', 'f:3', 'tr', 'f:15', 'tr', 'f:14', 'd', dropOffItems}
  186. while true do
  187.     select(logSlot)
  188.     --[[while not turtle.compare() do
  189.         sleep(5)
  190.     end]]--
  191.     doInstructions(inst)
  192.     sleep(5)
  193.     --doInstructions(inst)
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement