Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --NOTE: currently only works for Oak, Birch, or any other kind of tree that only generates with simple pillars as trunks
- --NOTE: currently only plants trees in a grid with 3 blocks in between, beginning at position 1,1
- print("Beginning Lumberjack v0.1")
- --Setup parameters
- direction = 0 --Current Direction
- location = {0, 0} --Current Location
- fuelLoc = nil --Location to be in to reful
- fuelDir = nil --Direction to face to refuel
- outLoc = nil --Location to be in to output
- outDir = nil --Direction to face to output
- boxW = 0 --Width of the bounding box
- boxH = 0 --Height of the bounding box
- saplingSlot = 1 --Slot in which to store saplings
- fuelSlot = 2 --Slot in which to store fuel
- treeGridW = 0 --Number of trees across X
- treeGridH = 0 --Number of trees across Y
- --Vector functions
- function vectorCopy(v)
- local u = {}
- for i = 1, #v do
- u[i] = v[i]
- end
- return u
- end
- function vectorEq(u, v)
- for i = 1, #u do
- if u[i] ~= v[i] then return false end
- end
- return true
- end
- function vectorAdd(u, v)
- local w = {}
- for i = 1, #u do
- w[i] = u[i] + v[i]
- end
- end
- function vectorSub(u, v)
- local w = {}
- for i = 1, #u do
- w[i] = u[i] - v[i]
- end
- end
- --Utility functions
- smart = {
- --Same as turtle.forward, but will wait for entities in the way to move, and will register movement in location variable.
- forward = function()
- if turtle.detect() then return false end
- repeat until turtle.forward()
- if direction == 0 then location[1] = location[1] + 1
- elseif direction == 1 then location[2] = location[2] + 1
- elseif direction == 2 then location[1] = location[1] - 1
- elseif direction == 3 then location[2] = location[2] - 1 end
- --print(location[1], " ", location[2])
- return true
- end,
- back = function()
- repeat until turtle.back()
- if direction == 0 then location[1] = location[1] - 1
- elseif direction == 1 then location[2] = location[2] - 1
- elseif direction == 2 then location[1] = location[1] + 1
- elseif direction == 3 then location[2] = location[2] + 1 end
- return true
- end,
- --Same as turtle.turnLeft, but registers rotation in direction variable
- turnLeft = function()
- turtle.turnLeft()
- direction = (direction+1) % 4
- --print(direction)
- end,
- --Same as turtle.turnRight, but registers rotation in direction variable
- turnRight = function()
- turtle.turnRight()
- direction = (direction+3) % 4
- --print(direction)
- end,
- }
- --Action functions
- function chopTree()
- local _, data = turtle.inspect()
- if data.name == "minecraft:sapling" then return end
- turtle.dig()
- smart.forward()
- while turtle.digUp() do
- turtle.up()
- end
- repeat until not turtle.down()
- smart.back()
- turtle.select(saplingSlot)
- turtle.place()
- end
- --Assumes turtle starts in a corner facing along the left edge
- function gather() --gathering process thought loop
- local phase = true
- turtle.select(1)
- repeat
- while (phase and location[2] < boxH-1) or (location[2] > 0 and not phase) do
- turtle.suck()
- smart.forward()
- end
- if phase then smart.turnRight() else smart.turnLeft() end
- smart.forward()
- smart.forward()
- if phase then smart.turnRight() else smart.turnLeft() end
- phase = not phase
- until location[1] >= boxW-1
- end
- function harvest() --harvesting process thought loop
- pathTo({2,1})
- turnTo(2)
- turtle.select(1)
- for i = 1, treeGridW do
- for j = 1, treeGridH do
- chopTree()
- if j < treeGridH then
- smart.turnRight()
- for i = 1, 4 do smart.forward() end
- smart.turnLeft()
- end
- end
- if i < treeGridW then
- if i % 2 == 1 then
- smart.turnRight()
- smart.turnRight()
- smart.forward()
- smart.forward()
- else
- smart.turnRight()
- smart.forward()
- smart.turnLeft()
- for i = 1, 6 do smart.forward() end
- smart.turnLeft()
- smart.forward()
- smart.turnLeft()
- end
- end
- end
- end
- function output()
- pathTo(outLoc)
- turnTo(outDir)
- for i = 1, 16 do
- if i ~= saplingSlot then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- function refuel()
- pathTo(fuelLoc)
- turnTo(fuelDir)
- turtle.select(fuelSlot)
- turtle.suck()
- turtle.refuel(turtle.getItemCount())
- turtle.select(fuelSlot)
- turtle.drop()
- end
- function turnTo(target)
- while direction ~= target do smart.turnLeft() end
- end
- --Simple pathing uses presumed locations of trees to go where it wants. Cannot reliably path onto tree locations
- function pathTo(target)
- if vectorEq(location, target) then return end
- --Ensure turtle starts off the tree grid
- if location[1] % 4 == 1 then --Turtle has a tree straight North/South
- if location[1] < target[1] then turnTo(0) else turnTo(2) end
- smart.forward()
- elseif location[2] % 4 == 1 then --Turtle has a tree straight East/West
- if location[2] < target[2] then turnTo(1) else turnTo(3) end
- smart.forward()
- end
- --Walk Either Vertical->Horizontal or Horizontal->Vertical depending on where target is on the tree grid.
- if target[1] % 4 == 1 then --Target has a tree straight North/South
- if location[2] < target[2] then turnTo(1) else turnTo(3) end
- for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
- if location[1] < target[1] then turnTo(0) else turnTo(2) end
- for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
- elseif target[2] % 4 == 1 then --Target has a tree straight East/West
- if location[1] < target[1] then turnTo(0) else turnTo(2) end
- for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
- if location[2] < target[2] then turnTo(1) else turnTo(3) end
- for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
- else
- if location[1] < target[1] then turnTo(0) else turnTo(2) end
- for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
- if location[2] < target[2] then turnTo(1) else turnTo(3) end
- for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
- end
- end
- --Startup procedure by which the turtle finds the area to which it is assigned.
- function findBounds() --finds boundaries at startup,
- while not turtle.detect() do turtle.turnLeft() end --rotate to face "east" from the "southwest" corner of the enclosure
- while turtle.detect() do turtle.turnLeft() end
- repeat
- --Check for chests
- if fuelLoc == nil or outLoc == nil then
- turtle.select(2)
- smart.turnRight() --Check edge
- local test, data = turtle.inspect()
- if test and data.name == "minecraft:chest" then
- if turtle.suck() then fuelLoc = vectorCopy(location); fuelDir = direction else outLoc = vectorCopy(location); outDir = direction end
- end
- smart.turnLeft() --turn back forward
- end
- if direction == 0 then boxW = boxW + 1 elseif direction == 1 then boxH = boxH + 1 end
- if not smart.forward() then smart.turnLeft() end
- until vectorEq(location, {0,0})
- treeGridW = math.ceil((boxW - 2) / 4)
- treeGridH = math.ceil((boxH - 2) / 4)
- end
- --Begin execution
- print("Please ensure the turtle has a comfortable amount of fuel to complete the setup process")
- read()
- print("Please ensure the turtle is in the corner of a flat, clear, rectangular area bound at the corners by blocks.")
- read()
- print("Please ensure the rectangular area has a fuel chest somewhere on its edge with fuel inside.")
- read()
- print("Please ensure the rectangular area has an empty output chest somewhere on its edge, and has saplings in slot 1")
- read()
- print("Please stand outside the rectangular area. Once the turtle has finished detecting boundaries, you may remove them.")
- findBounds()
- print(boxW .. " by " .. boxH)
- if boxW < 3 or boxH < 3 then error("Error, area must be at least 3x3") end
- if fuelLoc == nil then
- error("Error, no fuel chest found, please ensure your intended fuel chest contains some fuel")
- else print("Fuel at " .. fuelLoc[1] .. "," .. fuelLoc[2] .. " facing " .. fuelDir) end
- if outLoc == nil then
- error("Error, no output chest found, please ensure your intended output chest is empty")
- else print("Output at " .. outLoc[1] .. "," .. outLoc[2] .. " facing " .. outDir) end
- if treeGridH * treeGridW > 16 then
- error("Error, space given is for " .. treeGridH * treeGridW .. " trees, 16 is the max number allowed!")
- else print("Enough space for " .. treeGridH * treeGridW .. " trees.") end
- while true do --basic thought loop
- harvest()
- sleep()
- pathTo({0, 0})
- turnTo(1)
- gather()
- sleep(60)
- pathTo({0, 0})
- turnTo(1)
- gather()
- output()
- refuel()
- sleep(60)
- end
Add Comment
Please, Sign In to add comment