slay_mithos

Lumber V0.2

Nov 17th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | None | 0 0
  1. local x=0
  2. local y=0
  3. local dir=0
  4. local rows=4
  5. local columns=5
  6. local stop = false
  7.  
  8. local function forward()
  9.     local moved = true
  10.     while not turtle.forward() do
  11.         if not turtle.dig() and
  12.         not turtle.attack() then
  13.             moved = false
  14.             print("can't move")
  15.             break
  16.         end
  17.     end
  18.     if moved then
  19.         if dir == 0 then
  20.             x=x+1
  21.         elseif dir == 2 then
  22.             x=x-1
  23.         elseif dir == 1 then
  24.             y=y+1
  25.         elseif dir == 3 then
  26.             y=y-1
  27.         else
  28.             print("Unknown dir")
  29.             return false
  30.         end
  31.         return true
  32.     end
  33.     return false
  34. end
  35.  
  36. local function turnRight()
  37.     turtle.turnRight()
  38.     dir = (dir+1)%4
  39. end
  40.  
  41. local function turnLeft()
  42.     turtle.turnLeft()
  43.     dir = (dir-1)%4
  44. end
  45.  
  46. local function cutTree()
  47.     local height = 0
  48.     turtle.dig()
  49.     forward()
  50.     turtle.digDown()
  51.     turtle.select(3)
  52.     while turtle.compareUp() do
  53.         turtle.digUp()
  54.         turtle.up()
  55.         height = height +1
  56.     end
  57.     for i=1, height do
  58.         turtle.down()
  59.     end
  60. end
  61.  
  62. local function plantTree()
  63.     turtle.suckDown()
  64.     if turtle.getItemCount(2)>1 and not turtle.detectDown() then
  65.         turtle.select(2)
  66.         return turtle.placeDown()
  67.     end
  68.     return false
  69. end
  70.  
  71. local function detectTree()
  72.     turtle.select(3)
  73.     if turtle.compare() then
  74.         return true
  75.     else
  76.         return false
  77.     end
  78. end
  79.  
  80. local function move()
  81.     local mod = y%6
  82.     if y==(rows-1)*3 and x==0 then
  83.         stop = true
  84.         print("Stoppinzg")
  85.         return
  86.     elseif (x== columns*3 and mod ==0 and dir == 0) or
  87.         (x== columns*3 and mod == 3 and dir == 1)then
  88.         turnRight()
  89.     elseif (x==0 and mod==3 and dir == 2) or
  90.         (x==0 and mod == 0 and dir == 1)then
  91.         turnLeft()
  92.     else
  93.         if not forward() then
  94.             stop = true
  95.             return
  96.         end
  97.     end
  98.    
  99. end
  100.  
  101. local function manageInventory()
  102.     local charcoal, saplings
  103.     local time=0
  104.     local charcoalOk = false
  105.     local saplingsOk = false
  106.    
  107.     for i=4, 16 do
  108.         turtle.select(i)
  109.         turtle.dropDown()
  110.     end
  111.    
  112.     turtle.down()
  113.    
  114.     while not (charcoalOk and saplingsOk) do
  115.         turtle.select(1)
  116.         turtle.refuel(turtle.getItemCount(1)-1)
  117.         charcoal = (200-turtle.getFuelLevel())/96
  118.         saplings = (rows*columns)-turtle.getItemCount(2)
  119.         if charcoal > 0 then
  120.             print("Waiting for "..charcoal.." charcoal")
  121.             redstone.setOutput("left", true)
  122.             time = charcoal
  123.         else
  124.             time=0
  125.             redstone.setOutput("left", false)
  126.             charcoalOk = true
  127.         end
  128.         if saplings > 0 then
  129.             print("Waiting for "..saplings.." saplings")
  130.             redstone.setOutput("right", true)
  131.             if time > saplings or time == 0 then
  132.                 time = saplings
  133.             end
  134.         else
  135.             redstone.setOutput("right", false)
  136.             saplingsOk = true
  137.         end
  138.        
  139.         os.sleep(time*2.5)
  140.     end
  141.    
  142.     redstone.setOutput("left", false)
  143.     redstone.setOutput("right", false)
  144.     if(time>0) then
  145.         os.sleep(10)
  146.     end
  147.     turtle.up()
  148. end
  149.  
  150. local function returnHome()
  151.     turnRight()
  152.     for i=1,y do
  153.         while not turtle.forward() do
  154.             turtle.attack()
  155.         end
  156.     end
  157.     x=0
  158.     y=0
  159.     dir=0
  160.     turnRight()
  161. end
  162.  
  163. local function mainLoop()
  164.     while true do
  165.         manageInventory()
  166.         while not stop do
  167.             move()
  168.             if detectTree() then
  169.                 cutTree()
  170.             end
  171.             plantTree()
  172.         end
  173.         returnHome()
  174.         print("finished")
  175.         os.sleep(120)
  176.     end
  177. end
  178.  
  179. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment