Advertisement
carlosjuero

Minecraft Turtle Treefermer v1.0

Jul 14th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. tArgs = {...}
  2.  
  3. local saplingSlot = 15
  4. local fuelSlot = 16
  5. local isRedwood = false
  6. local defaultRefuelAmount = 5
  7.  
  8. function goUp(distance)
  9.   if distance == nil then distance = 1 end
  10.   for i = 1, distance do
  11.     turtle.up()
  12.   end
  13. end
  14.  
  15. function goDown(distance)
  16.   if distance == nil then distance = 1 end
  17.   for i = 1, distance do
  18.     turtle.down()
  19.   end
  20. end
  21.  
  22. function goForward(distance)
  23.   if distance == nil then distance = 1 end
  24.   for i = 1, distance do
  25.     turtle.forward()
  26.   end
  27. end
  28.  
  29. function goBack(distance)
  30.   if distance == nil then distance = 1 end
  31.   for i = 1, distance do
  32.     turtle.back()
  33.   end
  34. end
  35.  
  36. function digUp(distance)
  37.   if distance == nil then distance = 1 end
  38.   for i = 1, distance do
  39.     while turtle.detectUp() == true do
  40.       turtle.digUp()
  41.       sleep(0.5)
  42.     end
  43.     if distance > 1 then
  44.       goUp()
  45.     end
  46.   end
  47. end
  48.  
  49. function dig(distance)
  50.   if distance == nil then distance = 1 end
  51.   for i = 1, distance do
  52.     while turtle.detect() == true do
  53.       turtle.dig()
  54.       sleep(0.5)
  55.     end
  56.   end
  57. end
  58.  
  59. function digDown(distance)
  60.   if distance == nil then distance = 1 end
  61.   for i = 1, distance do
  62.     turtle.digDown()
  63.     sleep(0.5)
  64.     if distance > 1 then
  65.       goDown()
  66.     end
  67.   end
  68. end
  69.  
  70. function getFuel()
  71.   return turtle.getFuelLevel()
  72. end
  73.  
  74. function getFuelCount()
  75.   return turtle.getItemCount(fuelSlot)
  76. end
  77.  
  78. function refuel(amount)
  79.   if amount == nil then amount = defaultRefuelAmount end
  80.   turtle.select(fuelSlot)
  81.   turtle.refuel(amount)
  82.   turtle.select(1)
  83. end
  84.  
  85. function dropLoad()
  86.   print("Dropping off load of wood")
  87.   shell.run("turn", "around")
  88.   for i = 1, 14 do
  89.     turtle.select(i)
  90.     turtle.drop()
  91.   end
  92.   turtle.select(1)
  93.   shell.run("turn", "around")
  94. end
  95.  
  96. function returnToGround()
  97.   print("returning to ground level")
  98.   while turtle.detectDown() == false do
  99.     turtle.down()
  100.   end
  101. end
  102.  
  103. function waitForGrowth()
  104.   if turtle.detect() == false then
  105.     --print("TICK")
  106.     return false
  107.   else
  108.     print("Tree has grown!")
  109.     return true
  110.   end
  111. end
  112.  
  113. function chopTree(redwood)
  114.   print("Chopping down tree")
  115.   if redwood == true then
  116.     dig()
  117.     goForward()
  118.     while turtle.detectUp() == true do
  119.       shell.run("turn", "right")
  120.       dig()
  121.       shell.run("turn", "left")
  122.       dig()
  123.       goForward()
  124.       shell.run("turn", "right")
  125.       dig()
  126.       shell.run("turn", "left")
  127.       goBack()
  128.       digUp()
  129.       goUp()
  130.     end
  131.     if turtle.detect() == true then
  132.       shell.run("turn", "right")
  133.       dig()
  134.       shell.run("turn", "left")
  135.       dig()
  136.       goForward()
  137.       shell.run("turn", "right")
  138.       dig()
  139.       shell.run("turn", "left")
  140.       goBack()
  141.     end
  142.     returnToGround()
  143.     goBack()
  144.     dropLoad()
  145.   else
  146.     dig()
  147.     goForward()
  148.     while turtle.detectUp() == true do
  149.       turtle.digUp()
  150.       goUp()
  151.     end
  152.     returnToGround()
  153.     goBack()
  154.     dropLoad()
  155.   end
  156. end
  157.  
  158. function coreRoutine()
  159.   while true do
  160.     while waitForGrowth() == false do
  161.       sleep(60)
  162.     end
  163.     if waitForGrowth() == true then
  164.       if fuelStockCritical() == true then
  165.         goToRefuelChest()
  166.       end
  167.       if getFuel() < 50 then
  168.         refuel()
  169.       end
  170.       goDown()
  171.       chopTree(isRedwood)
  172.       plantSaplings(isRedwood)
  173.       goUp()
  174.     end
  175.   end
  176. end
  177.  
  178. function plantSaplings(redwood)
  179.   print("planting saplings")
  180.   if redwood == true then
  181.     if turtle.getItemCount(saplingSlot) == 0 then
  182.       print("Out of Saplings!")
  183.       return false
  184.     elseif turtle.getItemCount(saplingSlot) < 4 then
  185.       print("Insufficient Saplings")
  186.       return false
  187.     else
  188.       turtle.select(saplingSlot)
  189.       turtle.up()
  190.       turtle.forward()
  191.       turtle.placeDown()
  192.       turtle.forward()
  193.       turtle.placeDown()
  194.       turtle.turnRight()
  195.       turtle.forward()
  196.       turtle.placeDown()
  197.       turtle.turnRight()
  198.       turtle.forward()
  199.       turtle.placeDown()
  200.       turtle.forward()
  201.       turtle.turnRight()
  202.       turtle.forward()
  203.       turtle.turnRight()
  204.       turtle.down()
  205.       turtle.select(1)
  206.     end
  207.   else
  208.     if turtle.getItemCount(saplingSlot) == 0 then
  209.       print("Out of Saplings!")
  210.     else
  211.       turtle.select(saplingSlot)
  212.       turtle.up()
  213.       turtle.forward()
  214.       turtleplaceDown()
  215.       turtle.back()
  216.       turtle.down()
  217.       turtle.select(1)
  218.     end
  219.   end
  220. end
  221.  
  222. function fuelStockCritical()
  223.   if getFuelCount() <= 5 then
  224.     return true
  225.   else
  226.     return false
  227.   end
  228. end
  229.  
  230. function goToRefuelChest()
  231.    if turtle.detectDown() == true then
  232.      goUp()
  233.    else
  234.      returnToGround()
  235.      goUp()
  236.    end
  237.    shell.run("turn", "around")
  238.    goForward(3)
  239.    turtle.select(fuelSlot)
  240.    turtle.suckDown()
  241.    turtle.select(1)
  242.    shell.run("turn", "around")
  243.    goForward(3)
  244.    returnToGround()
  245. end
  246.  
  247. if tArgs[1] == nil then
  248.    if fuelStockCritical() == true then
  249.      print("Fuel stock low, going to grab more fuel")
  250.      goToRefuelChest()
  251.    end
  252.  
  253.    if getFuel() < 50 then
  254.      refuel()
  255.    end
  256.    
  257.    isRedwood = false
  258.    if turtle.detect() == false then
  259.      plantSaplings()
  260.    else
  261.      turtle.up()
  262.      if turtle.detect() == true then
  263.        turtle.down()
  264.        chopTree()
  265.        plantSaplings()
  266.      end
  267.    end
  268. elseif tArgs[1] == "redwood" then
  269.    if fuelStockCritical() == true then
  270.      print("Fuel stock low, going to grab more fuel")
  271.      goToRefuelChest()
  272.    end
  273.  
  274.    if getFuel() < 50 then
  275.      refuel()
  276.    end
  277.   isRedwood = true
  278.   if turtle.detect() == false then
  279.     plantSaplings(true)
  280.   else
  281.     turtle.up()
  282.     if turtle.detect() == true then
  283.       turtle.down()
  284.       chopTree(true)
  285.       plantSaplings(true)
  286.     end
  287.   end
  288. end
  289.  
  290. print("Starting main routine")
  291. if turtle.detectDown() == true then
  292.   goUp()
  293. end
  294. coreRoutine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement