craniumkid22

Feller Turtle

Nov 25th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. local function getFuel()
  2.   if turtle.getFuelLevel() < 16 then
  3.     while true do
  4.       if turtle.getItemCount(16) > 0 then
  5.         break
  6.       else
  7.         turtle.select(16)
  8.         print("Fuel needed in slot 16")
  9.         local events = {os.pullEvent()}
  10.         if events[1] == "key" and events[2] == 28 then
  11.           turtle.refuel()
  12.           getFuel()
  13.         end
  14.       end
  15.     end
  16.   end
  17. end
  18.  
  19. local function forward(dist)
  20.   if not dist then dist = 1 end
  21.   for i = 1, dist do
  22.     getFuel()
  23.     if not turtle.forward() then
  24.       turtle.dig()
  25.       turtle.forward()
  26.     end
  27.   end
  28. end
  29.  
  30. local function back(dist)
  31.   if not dist then dist = 1 end
  32.   for i = 1, dist do
  33.     getFuel()
  34.     if not turtle.back() then
  35.       turtle.turnRight()
  36.       turtle.turnRight()
  37.       turtle.dig()
  38.       turtle.turnRight()
  39.       turtle.turnRight()
  40.     end
  41.   end
  42. end
  43.  
  44. function down(dist)
  45.   if not dist then dist = 1 end
  46.   for i = 1, dist do
  47.     getFuel()
  48.     if not turtle.down() then
  49.       turtle.digDown()
  50.       turtle.down()
  51.     end
  52.   end
  53. end
  54.  
  55. function up(dist)
  56.   getFuel()
  57.   if not dist then dist = 1 end
  58.   for i = 1,dist do
  59.     if not turtle.up() then
  60.       turtle.digUp()
  61.       turtle.up()
  62.     end
  63.   end
  64. end
  65.  
  66. local function plant()
  67.   if turtle.getItemCount(1) >= 1 then
  68.     turtle.select(1)
  69.     turtle.forward()
  70.     turtle.placeDown()
  71.     turtle.back()
  72.   else
  73.     while true do
  74.       print("Out of Saplings")
  75.       print("Please place desired saplings in slot 1")
  76.       local events = {os.pullEvent()}
  77.       if events[1] == "key" and events[2] == 28 then
  78.         if turtle.getItemCount(1) > 0 then
  79.           turtle.select(1)
  80.           turtle.forward()
  81.           turtle.placeDown()
  82.           turtle.back()
  83.           break
  84.         end
  85.       end
  86.     end
  87.   end
  88. end
  89.  
  90. local function fell()
  91.   local height = 0
  92.   forward()
  93.   turtle.digDown()
  94.   while turtle.detectUp() do
  95.     up()
  96.     for i = 1,4 do
  97.       local branch = 0
  98.       local bBranch = 0
  99.       while turtle.detect() do
  100.         forward()
  101.         turtle.turnLeft()
  102.         while turtle.detect() do
  103.           forward()
  104.           bBranch = bBranch + 1
  105.         end
  106.         if bBranch ~= 0 then
  107.           repeat
  108.             back()
  109.             bBranch = bBranch - 1
  110.           until bBranch == 0
  111.         end
  112.         turtle.turnRight()
  113.         branch = branch + 1
  114.       end
  115.       if branch ~= 0 then
  116.         repeat
  117.           back()
  118.           branch = branch - 1
  119.         until branch == 0
  120.       end
  121.       turtle.turnLeft()
  122.     end
  123.     height = height + 1
  124.   end
  125.   repeat
  126.     down()
  127.     height = height - 1
  128.   until height == 0
  129.   back()
  130. end
  131.  
  132. local trees = 0
  133. while true do
  134.   print("Fuel level: "..turtle.getFuelLevel())
  135.   print("Planting")
  136.   plant()
  137.   print("Using redstone signal to grow sapling")
  138.   turtle.turnRight()
  139.   forward()
  140.   down()
  141.   rs.setOutput("back", true)
  142.   sleep(1)
  143.   rs.setOutput("back", false)
  144.   up()
  145.   back()
  146.   turtle.turnLeft()
  147.   print("Cutting down tree")
  148.   fell()
  149.   trees = trees + 1
  150.   print("Cut down "..trees.." trees")
  151. end
Advertisement
Add Comment
Please, Sign In to add comment