BOF007

Fir Wood Tree Farm (Troy Cheek)

Jul 21st, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. -- Tree Farm Tending Turtle
  2. -- Cuts down and replants fir trees
  3. -- Variables
  4. turtle.select(1)
  5. local tArgs = { ... }
  6. local togo = tonumber(tArgs[1])
  7. togo = togo or 1
  8. local sapling = 0
  9. term.clear()
  10. term.setCursorPos(1,1)
  11. print(os.getComputerLabel() .. " at your service!")
  12. print()
  13. print("Please place blocks as follows:")
  14. print("Slot 1 - Fuel (coal, lava, wood)")
  15. print("Slot 2 - Saplings (Fir is best)")
  16. print()
  17. print("Place turtle over chest at edge of farm.")
  18. print()
  19. print("Press [ENTER] when ready.")
  20. read(input)
  21.  
  22. -- Starting Out
  23. if turtle.getItemCount(1) < 1 then
  24.   print("No fuel! Abort!")
  25.   os.quit()
  26. end
  27. sapling = 0
  28. if turtle.getItemCount(2) > 0 then
  29.   sapling = 1
  30. end
  31.  
  32. -- Functions
  33. local function tfuel(amount)
  34.  if turtle.getFuelLevel() < 16 then
  35.   turtle.select(1)
  36.   turtle.refuel(amount)
  37.  end
  38. end
  39.  
  40. local function tforward(steps) -- turtle takes x steps forward
  41.   if steps==nil or steps==0 then steps=1 end
  42.   tfuel(1)
  43.   for i=1,steps do
  44.     while not turtle.forward() do
  45.       turtle.dig()
  46.       turtle.attack()
  47.     end
  48.   end
  49. end
  50.  
  51. local function turnaround()
  52.   turtle.turnRight()
  53.   turtle.turnRight()
  54. end
  55.  
  56. local function discard() -- drop off extra stuff into water
  57.   if turtle.getItemCount(3)<1 then
  58.     return
  59.   end
  60.   while not turtle.down() do
  61.     turtle.digDown()
  62.   end
  63.   for i = 3,16 do
  64.     if turtle.getItemCount(i)>0 then
  65.       turtle.select(i)
  66.       turtle.dropDown()
  67.     end
  68.   end
  69.   while not turtle.up() do
  70.     turtle.digUp()
  71.   end
  72.   turtle.select(1)  
  73. end
  74.  
  75. local function climb()
  76.   if turtle.detect() then
  77.     turtle.dig()
  78.     tforward()
  79.     local cnt=0
  80.     repeat
  81.       cnt = cnt + 1
  82.       turtle.dig()
  83.       sleep(0.5)
  84.       turtle.digUp()
  85.       tfuel(1)
  86.       turtle.up()
  87.     until not turtle.detect()
  88.     turtle.turnRight()
  89.     tforward()
  90.     turtle.turnLeft()
  91.     for i = 1,cnt do
  92.       turtle.digDown()
  93.       sleep(0.5)
  94.       tfuel(1)
  95.       turtle.down()
  96.       turtle.dig()
  97.     end
  98.     turtle.digDown()
  99.     turtle.select(2)
  100.     turtle.placeDown()
  101.     tforward()
  102.     turtle.digDown()
  103.     turtle.select(2)
  104.     turtle.placeDown()
  105.     turtle.turnLeft()
  106.     tforward()
  107.     turtle.digDown()
  108.     turtle.select(2)
  109.     turtle.placeDown()
  110.     turtle.turnLeft()
  111.     tforward()
  112.     turtle.digDown()
  113.     turtle.select(2)
  114.     turtle.placeDown()
  115.     turnaround()
  116.     tforward(2)
  117.   else
  118.     tforward(3)
  119.   end
  120.   discard()
  121. end
  122.  
  123. -- Main Loop
  124. tforward(2)  -- move into first position
  125. for r = 1,4 do
  126.   for i = 1,4 do
  127.     climb() -- climb and cut tree
  128.     if i<4 then
  129.       tforward(2)
  130.     end
  131.   end
  132.   if r<4 then
  133.     if r%2>0 then
  134.       turtle.turnRight()
  135.     else
  136.       turtle.turnLeft()
  137.     end
  138.     tforward(4+2*(r%2))
  139.     if r%2>0 then
  140.       turtle.turnRight()
  141.     else
  142.       turtle.turnLeft()
  143.     end
  144.   end
  145. end
  146. turtle.turnRight()
  147. tforward(16)  -- move back to base
  148. turtle.turnLeft()
  149. tforward(2)
  150. turnaround()
  151. turtle.select(2)  -- reload saplings
  152. turtle.suckDown()
  153. turtle.select(3)
  154. turtle.dropDown()
  155. turtle.select(1)
Add Comment
Please, Sign In to add comment