Advertisement
Ignius12

treeFarmer

Aug 12th, 2022 (edited)
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. host = 3
  2. status = "Inactive"
  3. pos = vector.new(gps.locate())
  4. local modem = peripheral.find("modem")
  5. rednet.open("left")
  6. L = 0
  7. R = 1
  8. pathX = {5,3,2,3,2,3,5,9}
  9. turns = {R,R,L,L,R,R,R}
  10. step = 1
  11. currX = 1
  12.  
  13.  
  14. function save_config()
  15.   sw = fs.open("config.txt", "w")  
  16.   sw.writeLine(step)
  17.   sw.writeLine(currX)
  18.   sw.close()
  19. end
  20.  
  21. function load_config()
  22.   sr = fs.open("config.txt", "r")
  23.   step = tonumber(sr.readLine())
  24.   currX  = tonumber(sr.readLine())
  25.   sr.close()
  26. end
  27.  
  28. function left(times)
  29.     for i=1,times do
  30.         turtle.turnLeft()
  31.     end
  32. end
  33.  
  34. function right(times)
  35.     for i=1,times do
  36.         turtle.turnRight()
  37.     end
  38. end
  39. function chopTree()
  40.     refuel(1)
  41.     status = "Chopping"
  42.     while turtle.detect() do
  43.         turtle.dig()
  44.         turtle.digUp()
  45.         turtle.up()
  46.         pos = vector.new(gps.locate())
  47.         refuel(1)
  48.     end
  49.     status = "Returning"
  50.     local has_block, data = turtle.inspectDown()
  51.     while not has_block or (data.name ~= "minecraft:andesite" and data.name ~= "computercraft:wired_modem_full") do
  52.         if(not turtle.down()) then
  53.             turtle.digDown()
  54.             turtle.down()
  55.         end
  56.         has_block, data = turtle.inspectDown()
  57.         pos = vector.new(gps.locate())
  58.     end
  59.         turtle.select(1)
  60.         turtle.place()
  61.         turtle.select(2)
  62.     status = "Roaming"
  63. end
  64.  
  65. function cleanInv()
  66.     local data = turtle.getItemDetail(1)
  67.     if(data ~= nil and data.name ~= "minecraft:oak_sapling") then
  68.         turtle.select(1)
  69.         turtle.drop()
  70.     end
  71.     for i=1, 16 do
  72.         local data = turtle.getItemDetail(i)
  73.         if(data ~= nil and data.name == "minecraft:oak_sapling") then
  74.             turtle.select(i)
  75.             turtle.transferTo(1)
  76.         end
  77.     end
  78.     turtle.suck()
  79.     turtle.select(1)
  80. end
  81.  
  82. function emptyInv()
  83.     local modem = peripheral.wrap("bottom")
  84.     local turtleName = modem.getNameLocal()
  85.     local chest = peripheral.find("minecraft:chest")
  86.     if(chest ~= nil) then    
  87.         for slot, item in pairs(chest.list()) do
  88.             if(item.name == "minecraft:oak_sapling") then
  89.                 chest.pushItems(turtleName, slot, 64, 1)
  90.             end
  91.         end
  92.     end
  93.     for idx=2,16 do
  94.         chest.pullItems(turtleName, idx)
  95.     end
  96. end
  97.  
  98. function refuel(lvl)
  99.         while(turtle.getFuelLevel() <= lvl) do
  100.             status = "Waiting for Fuel"
  101.             for i=2, 16 do
  102.                 turtle.select(i)
  103.                 turtle.refuel()
  104.             end
  105.             turtle.select(1)
  106.             sleep(2)
  107.        end
  108. end
  109.  
  110. function checkForTree()
  111.    local has_block, data = turtle.inspect()
  112.     if(has_block) then
  113.         if (data.name == "minecraft:oak_log") then
  114.             chopTree()
  115.             cleanInv()
  116.             refuel(1)
  117.         end
  118.     else
  119.         turtle.select(1)
  120.         turtle.place()
  121.     end
  122. end
  123. function farmTree()
  124.    while true do
  125.         while(currX <= pathX[step]) do
  126.             refuel(1)
  127.             left(1)
  128.             checkForTree()
  129.             right(2)
  130.             checkForTree()
  131.             left(1)
  132.            
  133.             while(not turtle.forward()) do
  134.                 turtle.dig()
  135.                 turtle.attack()
  136.             end
  137.            
  138.             currX = currX + 1
  139.         end
  140.         checkForTree()
  141.         currX = 1
  142.         if(turns[step] == R) then
  143.             left(1)
  144.             checkForTree()
  145.             right(2)
  146.         else
  147.             right(1)
  148.             checkForTree()
  149.             left(2)
  150.         end
  151.         step = step + 1
  152.         if(step > table.getn(pathX)) then
  153.             step = 1
  154.             emptyInv()
  155.         end
  156.    end
  157. end
  158. function rangeCheck()
  159.     while true do
  160.         if rednet.receive("failsafe") == null then
  161.             os.shutdown()
  162.         end
  163.         sleep(1)
  164.     end
  165. end
  166. function transmit()
  167.     while(true) do
  168.     local sender, message, protocol = rednet.receive("ping")
  169.         if(message ~= nil and message == "send") then
  170.             rednet.send(host, os.getComputerLabel() .. " " .. turtle.getFuelLevel() .. " " .. pos.x .. " " .. pos.y .." " .. pos.z .. " " .. status, "miners")
  171.             sleep(0.5)
  172.         end
  173.     end
  174. end
  175. parallel.waitForAny(farmTree, rangeCheck, transmit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement