Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mocking the APIs for editor support
- -- Remove before running in minecraft
- --[[
- local turtle = {
- select = function(_) end,
- getFuelLevel = function()
- return 1000
- end,
- refuel = function(_) end,
- dropDown = function() end,
- getItemCount = function(_)
- return 0
- end,
- forward = function()
- return true
- end,
- up = function()
- return true
- end,
- down = function()
- return true
- end,
- dig = function() end,
- digUp = function() end,
- digDown = function() end,
- back = function() end,
- }
- local redstone = {
- setOutput = function(_, _) end,
- getInput = function(_)
- return false
- end,
- }
- local os = {
- sleep = function(_) end,
- }
- local gps = {
- locate = function(_)
- return 0, 0, 0
- end,
- }
- local fs = {
- exists = function(_)
- return false
- end,
- open = function(_, _)
- return {
- writeLine = function(_) end,
- readLine = function()
- return "0"
- end,
- close = function() end,
- }
- end,
- }
- local rednet = {
- open = function(_) end,
- close = function(_) end,
- }
- --]]
- -- Sapling goes to slot 1
- -- Wood goes to slot 2
- -- Fuel goes to slot 3
- -- Trees should be on the right side of the turtle
- local HOME_FILE_NAME = "tree_choppa_home"
- local BLOCKS_BEFORE_TREES = 3
- local TREE_BLOCKS = 27
- local FUEL_MINIMUM = 1000
- local function pulse(side)
- redstone.setOutput(side, true)
- os.sleep(0.05)
- redstone.setOutput(side, false)
- os.sleep(0.05)
- end
- local function refuel()
- turtle.select(3)
- while turtle.getFuelLevel() < FUEL_MINIMUM do
- print("Refueling...")
- if not turtle.refuel(0) then
- pulse("top")
- print("Waiting for fuel...")
- while not turtle.refuel(0) do
- os.sleep(10)
- pulse("top")
- end
- end
- turtle.refuel()
- print("Refueled!")
- end
- turtle.select(1)
- end
- local function restock()
- print("Restocking saplings...")
- turtle.dropDown()
- pulse("back")
- if turtle.getItemCount(1) == 0 then
- print("Waiting for saplings...")
- while turtle.getItemCount(1) == 0 do
- os.sleep(10)
- pulse("back")
- end
- end
- print("Restocked!")
- end
- local function dropAll()
- print("Dropping all collected items...")
- for i = 3, 16 do
- if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- turtle.select(1)
- print("Dropped all collected items!")
- end
- local function forward()
- if not turtle.forward() then
- print("Can't go forward, please clear the way manually") --Should happen rarely if ever
- while not turtle.forward() do
- os.sleep(10)
- end
- print("The way is cleared, continuing...")
- end
- end
- local function back()
- if not turtle.back() then
- print("Can't go back, please clear the way manually") --Should happen rarely if ever
- while not turtle.back() do
- os.sleep(10)
- end
- print("The way is cleared, continuing...")
- end
- end
- local function down()
- while not turtle.down() do
- turtle.digDown()
- end
- end
- local function up()
- while not turtle.up() do
- turtle.digUp()
- end
- end
- local function comeDown(dy)
- print("Coming down for " .. dy .. " blocks")
- for _ = dy, 1, -1 do
- down()
- end
- print("Came down!")
- end
- local function chopUp()
- print("Chopping up the tree...")
- local dy = 0
- while turtle.detect() do
- turtle.dig()
- up()
- dy = dy + 1
- end
- print("Chopped up the tree of height " .. dy)
- return dy
- end
- local function locate()
- print("Getting the current location via GPS...")
- rednet.open("right")
- local x, y, z = gps.locate(1)
- if x == nil then
- print("Waiting for GPS signal, probably lost signal due to a storm")
- while x == nil do
- os.sleep(10)
- x, y, z = gps.locate(1)
- end
- end
- rednet.close("right")
- print("Got the current location via GPS!")
- return x, y, z
- end
- local function comeBack(xz, xz0)
- print("Coming back to the home point...")
- for _ = 1, math.abs(xz0 - xz) do
- back()
- end
- print("At the home point!")
- end
- local function checkAndChop()
- turtle.turnRight()
- turtle.select(2)
- if turtle.compare() then
- local dy = chopUp()
- comeDown(dy)
- dropAll()
- end
- turtle.select(1)
- turtle.place()
- turtle.turnLeft()
- end
- local function init()
- local x, y, z = locate()
- turtle.select(1)
- local x0, y0, z0, s0 = nil, nil, nil, nil
- print("Checking if the home point is saved...")
- if not fs.exists(HOME_FILE_NAME) then
- print("Home point is not saved, I am at the very start, creating a file for the home point...")
- x0 = x
- y0 = y
- z0 = z
- print("Home point is at x=" .. x0 .. ", y=" .. y0 .. ", z=" .. z0)
- restock()
- refuel()
- print("Moving to determine the orientation...")
- forward()
- local x1, _, z1 = locate()
- back()
- if x1 > x0 then
- s0 = "x+"
- elseif x1 < x0 then
- s0 = "x-"
- elseif z1 > z0 then
- s0 = "z+"
- else
- s0 = "z-"
- end
- print("Orientation is " .. s0)
- print("Writing the home point and orientation to the file...")
- local f = fs.open(HOME_FILE_NAME, "w")
- f.writeLine(tostring(x0))
- f.writeLine(tostring(y0))
- f.writeLine(tostring(z0))
- f.writeLine(s0)
- f.close()
- print("A home point and an orientation are saved!")
- return
- end
- print("Not at the very start, loading the home point from the file...")
- local f = fs.open(HOME_FILE_NAME, "r")
- x0 = tonumber(f.readLine())
- y0 = tonumber(f.readLine())
- z0 = tonumber(f.readLine())
- s0 = f.readLine()
- f.close()
- print("Home point is at x=" .. x0 .. ", y=" .. y0 .. ", z=" .. z0)
- print("Orientation is " .. s0)
- print("Checking if turtle is in the air...")
- if y > y0 then
- print("Turtle is in the air")
- turtle.dig()
- up()
- local dy = chopUp()
- comeDown(y + 1 + dy - y0)
- turtle.place()
- turtle.turnLeft()
- dropAll()
- if (s0 == "x+") or (s0 == "x-") then
- comeBack(x, x0)
- else
- comeBack(z, z0)
- end
- return
- end
- print("Turtle is on the ground")
- print("Checking if turtle is not in line with the home point...")
- if ((s0 == "x+") or (s0 == "x-")) and (z ~= z0) then
- print("Turtle is not in line with the home point, correcting")
- for _ = 1, math.abs(z - z0) do
- forward()
- end
- turtle.place()
- turtle.turnLeft()
- dropAll()
- comeBack(x, x0)
- return
- end
- if ((s0 == "z+") or (s0 == "z-")) and (x ~= x0) then
- print("Turtle is not in line with the home point, correcting")
- for _ = 1, math.abs(x - x0) do
- forward()
- end
- turtle.place()
- turtle.turnLeft()
- dropAll()
- comeBack(z, z0)
- return
- end
- print("Moving the turtle to tell orientation...")
- if not turtle.back() then
- print("Can't move back, must be at the start") -- Can be false positive, but I doubt this situation will ever happen
- return
- end
- local x1, _, z1 = locate()
- forward()
- if (s0 == "x+") or (s0 == "x-") then
- if z1 ~= z0 then
- turtle.place()
- turtle.turnLeft()
- end
- dropAll()
- comeBack(x, x0)
- return
- else
- if x1 ~= x0 then
- turtle.place()
- turtle.turnLeft()
- end
- dropAll()
- comeBack(z, z0)
- return
- end
- end
- print("Initializing the turtle...")
- init()
- restock()
- refuel()
- print("Initialized!")
- local loopCount = 0
- -- Main loop
- while true do
- loopCount = loopCount + 1
- print("Starting loop " .. loopCount)
- for _ = 1, BLOCKS_BEFORE_TREES do
- forward()
- end
- for _ = 1, TREE_BLOCKS do
- forward()
- checkAndChop()
- end
- for _ = 1, TREE_BLOCKS - 1 do
- back()
- checkAndChop()
- end
- for _ = 1, BLOCKS_BEFORE_TREES + 1 do
- back()
- end
- restock()
- refuel()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement