ToLazyToThink

ToLazyToThink's tree turtle

Apr 1st, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.10 KB | None | 0 0
  1.  -- This is meant for a 1x1 tree, with transposers or equivelent to pick up drops/sapplings,
  2.  --   a fuel chest on the right, a sappling chest underneath, an optional bone meal
  3.  --   chest behind and an optional "watcher" connected via rednet to pause logging
  4.  --
  5.  -- If not using bonemeal, set boneChestSlot to -1
  6.  -- If not using a watcher, set watcherID to -1
  7.  -- Chest can share the same slot as long as they are the same type to turtle.compare(),
  8.  --   enderchest compare() equal even if they have different colors
  9.  
  10. woodSlot = 1
  11. sapplingSlot = 2
  12. chestSlot = 3
  13. fuelChestSlot = 3
  14. boneChestSlot = 3
  15. startSlot = 4
  16. endSlot = 16
  17.  
  18. watcherID = 30
  19.  
  20. minFuel = 200
  21.  
  22. function plant()
  23.   if turtle.getItemCount(sapplingSlot) < 2 then
  24.     turtle.select(sapplingSlot)
  25.     turtle.suckDown()
  26.     if turtle.getItemCount(sapplingSlot) < 2 then
  27.       print("Out of Sapplings")
  28.       sleep(30)
  29.       return plant
  30.     end
  31.   end
  32.  
  33.   dropExtras()
  34.   turtle.select(sapplingSlot)
  35.   turtle.place()
  36.   print("waitForGrowth")
  37.   return waitForGrowth
  38. end
  39.  
  40. function waitForGrowth()
  41.   if not keepLogging() then
  42.     sleep(5)
  43.     return waitForGrowth
  44.   end
  45.  
  46.   if compare(woodSlot) then
  47.     checkFuel()
  48.     print("cutUp")
  49.     return cutUp
  50.   else
  51.     if boneIt() then
  52.       -- we'll get it next time
  53.       return waitForGrowth
  54.     end
  55.     sleep(15)
  56.     return waitForGrowth
  57.   end
  58. end
  59.  
  60. function cutUp()
  61.   turtle.select(sapplingSlot)
  62.   turtle.digUp()
  63.  
  64.   if turtle.detect() then
  65.     turtle.dig()
  66.   else
  67.     print("returnHome")
  68.     return returnHome
  69.   end
  70.  
  71.   moveUp()  
  72.  
  73.   return cutUp
  74. end
  75.  
  76. function returnHome()
  77.   turtle.dig() -- in case of side growth
  78.  
  79.   if compareDown(chestSlot) then
  80.     dropExtras()
  81.     checkFuel()
  82.     print("plant")
  83.     return plant
  84.   end
  85.  
  86.   turtle.dig() -- in case of side growth
  87.   moveDown()
  88.  
  89.   return returnHome
  90. end
  91.  
  92. function watchWood()
  93.   print("watching")
  94.   rednet.open("right")
  95.   while true do
  96.     local id, msg, dist = rednet.receive()
  97.     if msg == "wood-status" then
  98.       if redstone.getInput("back") then
  99.         rednet.send(id, "wood-stop", false)
  100.       else
  101.         rednet.send(id, "wood-go", false)
  102.       end
  103.     end
  104.   end
  105.  
  106.   return nill
  107. end
  108.  
  109. function boneIt()
  110.   if boneChestSlot > 0 and not compare(woodSlot) then
  111.     dropExtras()
  112.    
  113.     turtle.turnRight()
  114.     turtle.turnRight()
  115.    
  116.     turtle.select(endSlot)
  117.     turtle.suck()
  118.  
  119.     turtle.turnLeft()
  120.     turtle.turnLeft()
  121.  
  122.     for i = 1, 10, 1 do
  123.       if not compare(woodSlot) then
  124.         turtle.select(endSlot)
  125.         turtle.place()
  126.       end
  127.     end
  128.    
  129.     dropExtras()
  130.  
  131.     if not compare(woodSlot) then
  132.       print("Boneing failed")
  133.       return false
  134.     end    
  135.     return true
  136.   end
  137.  
  138.   return false
  139. end
  140.  
  141. function resetHomeDir()
  142.   while compare(fuelChestSlot) or (boneChestSlot > 0 and compare(boneChestSlot)) do
  143.     turtle.turnLeft()
  144.   end
  145. end
  146.  
  147. function dropExtras()
  148.   for i = startSlot, endSlot, 1 do
  149.     turtle.select(i)
  150.     turtle.drop()
  151.   end
  152. end
  153.  
  154. function compare(slot)
  155.   turtle.select(slot)
  156.   return turtle.compare()
  157. end
  158.  
  159. function compareDown(slot)
  160.   turtle.select(slot)
  161.   return turtle.compareDown()
  162. end
  163.  
  164. function checkFuel()
  165.   if turtle.getFuelLevel() < minFuel then
  166.     resetHomeDir()
  167.     turtle.turnRight()
  168.  
  169.     while turtle.getFuelLevel() < minFuel do
  170.       dropExtras()
  171.       turtle.select(16)
  172.       turtle.suck()
  173.       turtle.refuel()
  174.       if turtle.getFuelLevel() < minFuel then
  175.         print("Not enough Fuel")
  176.         sleep(30)
  177.       end
  178.     end
  179.  
  180.     print("refueled")
  181.   end
  182.  
  183.   resetHomeDir()
  184.  
  185. end
  186.  
  187. function moveUp()
  188.   while not turtle.up() do
  189.     print("can't move up")
  190.     turtle.attackUp()
  191.     turtle.digUp()
  192.     sleep(5)
  193.   end
  194. end
  195.  
  196. function moveDown()
  197.   while not turtle.down() do
  198.     print("can't move down")
  199.     turtle.digDown()
  200.     turtle.attackDown()
  201.     sleep(5)
  202.   end
  203. end
  204.  
  205. function keepLogging()
  206.   if watcherID < 0 then
  207.     return true
  208.   end
  209.  
  210.   rednet.open("right")
  211.   rednet.send(watcherID, "wood-status", true)
  212.   local id, msg, dist = rednet.receive(30)
  213.   if id == nill then
  214.     print("couldn't reach watcher")
  215.     return false
  216.   end
  217.  
  218.   return msg == "wood-go"
  219. end
  220.  
  221.  
  222. function determineState()
  223.   -- is this the watcher?
  224.   if watcherID == os.getComputerID() then
  225.     print("Determined state = watchWood")
  226.     return watchWood
  227.   end
  228.  
  229.   -- nope, we're a logger
  230.  
  231.   if compareDown(chestSlot) then
  232.     resetHomeDir()
  233.     dropExtras()
  234.     checkFuel()
  235.    
  236.     if turtle.detect() then
  237.       print("Determined state = waitForGrowth")
  238.       return waitForGrowth
  239.     end    
  240.   elseif turtle.detect() then
  241.     print("Determined state = cutUp")
  242.     return cutUp
  243.   end
  244.  
  245.   -- it's possible we removed the log in front of us
  246.   --  before the reset, it's also possible we were
  247.   --  returning home
  248.  
  249.   turtle.digUp()
  250.   moveUp()
  251.   if turtle.detect() then
  252.     print("Determined state = cutUp")
  253.     return cutUp
  254.   else
  255.     print("Determined state = returnHome")
  256.     return returnHome
  257.   end
  258. end
  259.  
  260. state = determineState()
  261. while true do
  262.   state = state()
  263. end
Advertisement
Add Comment
Please, Sign In to add comment