Blazuno

deforestation

Mar 15th, 2022 (edited)
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.95 KB | None | 0 0
  1. local deforestation = {trees = {
  2. "minecraft:oak_log",
  3. "minecraft:birch_log",
  4. "minecraft:acacia_log",
  5. "minecraft:dark_oak_log",
  6. "minecraft:jungle_log",
  7. "minecraft:spruce_log"}
  8. }
  9.  
  10.  
  11.  
  12. ----------movement library
  13. movement = {}
  14. movement.curXPos = 0
  15. movement.curYPos = 0
  16. movement.curZPos = 0
  17. movement.orientation = "north"
  18.  
  19.  
  20. movement.forward = function()
  21.     local k = turtle.forward()
  22.     if k == true then
  23.         if movement.orientation == "north" then
  24.             movement.curXPos = movement.curXPos + 1
  25.         elseif movement.orientation == "east" then
  26.             movement.curYPos = movement.curYPos + 1
  27.         elseif movement.orientation == "south" then
  28.             movement.curXPos = movement.curXPos - 1
  29.         elseif  movement.orientation == "west" then
  30.             movement.curYPos = movement.curYPos - 1
  31.         end
  32.     end
  33.     return k
  34. end
  35.  
  36. movement.up = function()
  37.     local k = turtle.up()
  38.     if k == true then
  39.         movement.curZPos = movement.curZPos + 1
  40.     end
  41.     return k
  42. end
  43.  
  44. movement.down = function()
  45.     local k = turtle.down()
  46.     if k == true then
  47.         movement.curZPos = movement.curZPos - 1
  48.     end
  49.     return k
  50. end
  51.  
  52. movement.turnLeft = function()
  53.     local k = turtle.turnLeft()
  54.     if movement.orientation == "north" then
  55.         movement.orientation = "west"
  56.     elseif movement.orientation == "west" then
  57.         movement.orientation = "south"
  58.     elseif movement.orientation == "south" then
  59.         movement.orientation = "east"
  60.     elseif movement.orientation == "east" then
  61.         movement.orientation = "north"
  62.     end
  63.     return k
  64. end
  65.  
  66. movement.turnRight = function()
  67.     local k = turtle.turnRight()
  68.     if movement.orientation == "north" then
  69.         movement.orientation = "east"
  70.     elseif movement.orientation == "east" then
  71.         movement.orientation = "south"
  72.     elseif movement.orientation == "south" then
  73.         movement.orientation = "west"
  74.     elseif movement.orientation == "west" then
  75.         movement.orientation = "north"
  76.     end
  77.     return k
  78. end
  79. ---------------these functions are useful for specific situations (NOTE: you should probably use movement.faceDirection() instead of turtle.turn as it updates orientation which is relevant for the other functions)
  80.  
  81.  
  82. movement.goHome = function()
  83.     local savedPos = {}
  84.     savedPos.x = movement.curXPos
  85.     savedPos.y = movement.curYPos
  86.     savedPos.z = movement.curZPos
  87.     print("Facing direction: "..movement.orientation)
  88.     if movement.curYPos > 0 then
  89.         movement.faceDirection("west")
  90.         for i=1,savedPos.y do
  91.             movement.forward()
  92.         end
  93.     elseif movement.curYPos < 0 then
  94.         movement.faceDirection("east")
  95.         for i=1,math.abs(savedPos.y) do
  96.             movement.forward()
  97.         end
  98.     end
  99.     if movement.curXPos > 0 then
  100.         movement.faceDirection("south")
  101.         for i=1,savedPos.x do
  102.             movement.forward()
  103.         end
  104.     elseif movement.curXPos < 0 then
  105.         movement.faceDirection("north")
  106.         for i=1,math.abs(savedPos.x) do
  107.             movement.forward()
  108.         end
  109.     end
  110. end
  111.  
  112. movement.faceDirection = function(orientation)
  113.     if orientation == "north" then
  114.         if movement.orientation == "south" then
  115.             for i=1,2 do
  116.                 turtle.turnLeft()
  117.             end
  118.         elseif movement.orientation == "east" then
  119.             turtle.turnLeft()
  120.         elseif movement.orientation == "west" then
  121.             turtle.turnRight()
  122.         end
  123.         movement.orientation = "north"
  124.     elseif orientation == "west" then
  125.         if movement.orientation == "south" then
  126.             turtle.turnRight()
  127.         elseif movement.orientation =="east" then
  128.             for i=1,2 do
  129.                 turtle.turnLeft()
  130.             end
  131.         elseif movement.orientation == "north" then
  132.             turtle.turnLeft()
  133.         end
  134.         movement.orientation = "west"
  135.     elseif orientation == "south" then
  136.         if movement.orientation == "east" then
  137.             turtle.turnRight()
  138.         elseif movement.orientation == "north"  then
  139.             for i=1,2 do
  140.                 turtle.turnLeft()
  141.             end
  142.         elseif movement.orientation == "west" then
  143.             turtle.turnLeft()
  144.         end
  145.         movement.orientation = "south"
  146.     elseif orientation == "east" then
  147.         if movement.orientation == "north" then
  148.             turtle.turnRight()
  149.         elseif movement.orientation == "west" then
  150.             for i=1,2 do
  151.                 turtle.turnLeft()
  152.             end
  153.         elseif movement.orientation == "south" then
  154.             turtle.turnLeft()
  155.         end
  156.         movement.orientation = "east"
  157.     end
  158. end
  159.  
  160.  
  161. movement.goToCoords = function(toX, toY, toZ, orientation)
  162.     local savedPos = {}
  163.     local changeInPos = {}
  164.     savedPos.toX = toX
  165.     savedPos.toY = toY
  166.     savedPos.toZ = toZ
  167.     savedPos.orient = orientation
  168.     local placeholder = {}
  169.     placeholder.x = math.abs(savedPos.toX - movement.curXPos)
  170.     placeholder.y = math.abs(savedPos.toY - movement.curYPos)
  171.     placeholder.z = math.abs(savedPos.toZ - movement.curZPos)
  172.     if savedPos.x > movement.curXPos then
  173.         movement.faceDirection("north")
  174.         for i=1,placeholder.x do
  175.             movement.forward()
  176.         end
  177.     elseif savedPos.x < movement.curXPos then
  178.         movement.faceDirection("south")
  179.         for i=1,placeholder.x do
  180.             movement.forward()
  181.         end
  182.     end
  183.     if savedPos.y > movement.curYPos then
  184.         movement.faceDirection("east")
  185.         for i=1, placeholder.y do
  186.             movement.forward()
  187.         end
  188.     elseif savedPos.y < movement.curYPos then
  189.         movement.faceDirection("west")
  190.         for i=1,placeholder.y do
  191.             movement.forward()
  192.         end
  193.     end
  194.     if savedPos.z > curZPos then
  195.         for i=1,placeholder.z do
  196.             turtle.digUp()
  197.             movement.up()
  198.         end
  199.     elseif savedPos.z < curZPos then
  200.         for i=1,placeholder.z do
  201.             movement.down()
  202.         end
  203.     end
  204.     movement.faceDirection(savedPos.orient)
  205. end
  206.  
  207. -----------------
  208. -----------------
  209. ----------------deforestation movement functions dedicated to climbing over obstacles
  210.  
  211. deforestation.parseArgs = function()
  212.     print("Do you want the turtle to refuel using wood farmed? (y/n)")
  213.     deforestation.selfSufficient = read()
  214. end
  215.  
  216.  
  217. deforestation.mineTree = function()
  218.     local tablee = {}
  219.     local goDown = false
  220.     repeat
  221.         local mineTree = false
  222.         for i,v in pairs(deforestation.trees) do
  223.             x, tablee = turtle.inspect()
  224.             if tablee.name == v then
  225.                 mineTree = true
  226.             end
  227.         end
  228.         if mineTree == true  then
  229.             turtle.dig()
  230.             turtle.digUp()
  231.             movement.up()
  232.             goDown = true
  233.         end
  234.     until mineTree == false
  235.     if goDown == true then
  236.         if turtle.detectDown() == false then
  237.             repeat
  238.                 movement.down()
  239.             until turtle.detectDown() == true
  240.         end
  241.     end
  242. end
  243.  
  244. deforestation.forward = function()
  245.     if turtle.detectDown() == false then
  246.         repeat
  247.             local k = movement.down()
  248.             if k == false then
  249.                 turtle.digDown()
  250.             end
  251.         until turtle.detectDown() == true
  252.     end
  253.     deforestation.mineTree()
  254.     if movement.forward() == false then
  255.         deforestation.mineTree()
  256.         while movement.forward() == false do
  257.             deforestation.mineTree()
  258.             if movement.up() == false then
  259.                 repeat
  260.                     turtle.digUp()
  261.                 until movement.up() == true
  262.             end
  263.         end
  264.     end
  265.     check = true
  266. end
  267.  
  268.  
  269.  
  270. deforestation.goHome = function()
  271.     local savedPos = {}
  272.     savedPos.x = movement.curXPos
  273.     savedPos.y = movement.curYPos
  274.     savedPos.z = movement.curZPos
  275.     print("Facing direction: "..movement.orientation)
  276.     if movement.curYPos > 0 then
  277.         movement.faceDirection("west")
  278.         for i=1,savedPos.y do
  279.             deforestation.forward()
  280.         end
  281.     elseif movement.curYPos < 0 then
  282.         movement.faceDirection("east")
  283.         for i=1,math.abs(savedPos.y) do
  284.             deforestation.forward()
  285.         end
  286.     end
  287.     if movement.curXPos > 0 then
  288.         movement.faceDirection("south")
  289.         for i=1,savedPos.x do
  290.             deforestation.forward()
  291.         end
  292.     elseif movement.curXPos < 0 then
  293.         movement.faceDirection("north")
  294.         for i=1,math.abs(savedPos.x) do
  295.             deforestation.forward()
  296.         end
  297.     end
  298. end
  299.  
  300. deforestation.goToCoords = function(x, y, z, orientation)
  301.     local savedPos = {}
  302.     savedPos.x = x
  303.     savedPos.y = y
  304.     savedPos.z = z
  305.     savedPos.orient = orientation
  306.     if savedPos.x > 0 then
  307.         for i=1,savedPos.x do
  308.             deforestation.forward()
  309.         end
  310.     elseif savedPos.x < 0 then
  311.         for i=1,2 do
  312.             turtle.turnLeft()
  313.         end
  314.         for i=1,math.abs(savedPosX) do
  315.             deforestation.forward()
  316.         end
  317.     end
  318.     if savedPos.y > 0 then
  319.         movement.faceDirection("east")
  320.         for i=1, savedPos.y do
  321.             deforestation.forward()
  322.         end
  323.     elseif savedPos.y < 0 then
  324.         movement.faceDirection("west")
  325.         for i=1,savedPos.y do
  326.             deforestation.forward()
  327.         end
  328.     end
  329.     if savedPos.z > 0 then
  330.         for i=1,savedPos.z do
  331.             turtle.digUp()
  332.             movement.up()
  333.         end
  334.     elseif savedPos.z < 0 then
  335.         for i=1,savedPos.z do
  336.             movement.negZ()
  337.         end
  338.     end
  339.     movement.faceDirection(savedPos.orient)
  340. end
  341.  
  342. deforestation.refuel = function()
  343.     local tablee = {}
  344.     local savedPos = {}
  345.     savedPos.x = movement.curXPos
  346.     savedPos.y = movement.curYPos
  347.     savedPos.z = movement.curZPos
  348.     savedPos.orient = movement.orientation
  349.     local curSlot = 1
  350.     if deforestation.selfSufficient == "y" then
  351.         if turtle.getFuelLevel() <= savedPos.x + savedPos.y + 20 then
  352.             if turtle.getItemCount(16) ~= 0 then
  353.                 turtle.select(16)
  354.                 turtle.refuel(16)
  355.             elseif turtle.getItemCount(16) == 0 then
  356.                 for i=1,15 do
  357.                     turtle.select(curSlot)
  358.                     for i,v in pairs(deforestation.trees) do
  359.                         local tablee = turtle.getItemDetail()
  360.                         if tablee ~= nil and tablee.name == v then
  361.                             turtle.transferTo(16)
  362.                             turtle.select(16)
  363.                             turtle.refuel(16)
  364.                             turtle.select(1)
  365.                         end
  366.                     end
  367.                     curSlot = curSlot + 1
  368.                     if curSlot == 15 then
  369.                         turtle.select(1)
  370.                     end
  371.                 end
  372.             end
  373.         end
  374.     elseif turtle.getFuelLevel() <= savedPos.x + savedPos.y + 20 and turtle.getItemCount(16) ~= 0 then
  375.         turtle.select(16)
  376.         turtle.refuel()
  377.     elseif turtle.getFuelLevel() <= savedPos.x + savedPos.y + 20 and turtle.getItemCount(16) == 0 then
  378.         savedPos.x = movement.curXPos
  379.         savedPos.y = movement.curYPos
  380.         savedPos.z = movement.curZPos
  381.         deforestation.goHome()
  382.         movement.faceDirection("north")
  383.         turtle.select(16)
  384.         if turtle.suckDown(32) == false then
  385.             print("Out of fuel, please add fuel to chest or 16th slot")
  386.             repeat
  387.                 sleep(1)
  388.             until turtle.refuel() == true or turtle.suck(32) == true
  389.         end
  390.         turtle.refuel()
  391.         deforestation.goToCoords(savedPos.x, savedPos.y, savedPos.z, savedPos.orient)
  392.     end
  393. end
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. deforestation.deforest = function()
  403.     local amount = 1
  404.     while true do
  405.         deforestation.refuel()
  406.         deforestation.mineTree()
  407.         for i=1, amount do
  408.             term.clear()
  409.             term.setCursorPos(1, 1)
  410.             print([[X Position: ]]..movement.curXPos)
  411.             term.setCursorPos(1, 2)
  412.             print([[Y Position: ]]..movement.curYPos)
  413.             term.setCursorPos(1, 3)
  414.             print("Orientation: "..movement.orientation)
  415.             term.setCursorPos(1,4)
  416.             print("Amount moving: "..amount)
  417.             deforestation.forward()
  418.             term.setCursorPos(1,5)
  419.             print("Movement check: "..tostring(check))
  420.         end
  421.         movement.faceDirection("east")
  422.         deforestation.refuel()
  423.         deforestation.mineTree()
  424.         for i=1, amount do
  425.             term.clear()
  426.             term.setCursorPos(1, 1)
  427.             print([[X Position: ]]..movement.curXPos)
  428.             term.setCursorPos(1, 2)
  429.             print([[Y Position: ]]..movement.curYPos)
  430.             term.setCursorPos(1, 3)
  431.             print("Orientation: "..movement.orientation)
  432.             term.setCursorPos(1,4)
  433.             print("Amount moving: "..amount)
  434.             deforestation.forward()
  435.             term.setCursorPos(1,5)
  436.             print("Movement check: "..tostring(check))
  437.         end
  438.         movement.faceDirection("south")
  439.         amount = amount + 1
  440.         deforestation.refuel()
  441.         deforestation.mineTree()
  442.         for i=1, amount do
  443.             term.clear()
  444.             term.setCursorPos(1, 1)
  445.             print([[X Position: ]]..movement.curXPos)
  446.             term.setCursorPos(1, 2)
  447.             print([[Y Position: ]]..movement.curYPos)
  448.             term.setCursorPos(1, 3)
  449.             print("Orientation: "..movement.orientation)
  450.             term.setCursorPos(1,4)
  451.             print("Amount moving: "..amount)
  452.             deforestation.forward()
  453.             term.setCursorPos(1,5)
  454.             print("Movement check: "..tostring(check))
  455.         end
  456.         movement.faceDirection("west")
  457.         deforestation.refuel()
  458.         deforestation.mineTree()
  459.         for i=1, amount do
  460.             term.clear()
  461.             term.setCursorPos(1, 1)
  462.             print([[X Position: ]]..movement.curXPos)
  463.             term.setCursorPos(1, 2)
  464.             print([[Y Position: ]]..movement.curYPos)
  465.             term.setCursorPos(1, 3)
  466.             print("Orientation: "..movement.orientation)
  467.             term.setCursorPos(1,4)
  468.             print("Amount moving: "..amount)
  469.             deforestation.forward()
  470.             term.setCursorPos(1,5)
  471.             print("Movement check: "..tostring(check))
  472.         end
  473.         movement.faceDirection("north")
  474.         amount = amount + 1
  475.     end
  476. end
  477.  
  478. deforestation.parseArgs()
  479. deforestation.deforest()
  480.  
Add Comment
Please, Sign In to add comment