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
- --Same as turtle.forward, but will wait for entities in the way to move, and will register movement in location variable.
- function smartForward()
- 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
- return true
- end
- function smartBack()
- 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
- function aggressiveForward()
- function smartForward()
- if turtle.detect() and not turtle.dig() 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
- return true
- end
- --Same as turtle.turnLeft, but registers rotation in direction variable
- function smartTurnLeft()
- turtle.turnLeft()
- direction = (direction+1) % 4
- end
- --Same as turtle.turnRight, but registers rotation in direction variable
- function smartTurnRight()
- turtle.turnRight()
- direction = (direction+3) % 4
- end
- --Action functions
- --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)
- smartTurnRight() --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
- smartTurnLeft() --Turn back forward
- end
- if direction == 0 then boxW = boxW + 1 elseif direction == 1 then boxH = boxH + 1 end
- if not smartForward() then smartTurnLeft() end
- until vectorEq(location, {0,0})
- treeGridW = math.ceil((boxW - 2) / 4)
- treeGridH = math.ceil((boxH - 2) / 4)
- end
- function chopTree()
- turtle.dig()
- smartForward()
- while turtle.digUp() do
- turtle.up()
- end
- repeat until not turtle.down()
- smartBack()
- turtle.select(saplingSlot)
- turtle.place()
- end
- --Assumes turtle starts in a corner facing along the left edge
- function gather() --gathering process thought loop
- while true do
- repeat turtle.suck() until not smartForward() end
- smartTurnRight()
- if not smartForward() then return end
- if not smartForward() then return end
- smartTurnRight()
- end
- end
- function harvest() --harvesting process thought loop
- pathTo({2,1})
- turnTo(2)
- for i = 1, treeGridW do
- for j = 1, treeGridH do
- chopTree()
- if j < treeGridW then
- smartTurnLeft()
- smartForward()
- smartForward()
- smartForward()
- smartForward()
- smartTurnRight()
- end
- end
- if i < treeGridW then
- if i % 2 == 1 then
- smartTurnLeft()
- smartTurnLeft()
- smartForward()
- smartForward()
- else
- smartTurnRight()
- smartForward()
- smartTurnLeft()
- smartForward()
- smartForward()
- smartForward()
- smartForward()
- smartForward()
- smartForward()
- smartTurnLeft()
- smartForward()
- smartLeft()
- end
- end
- end
- end
- function output()
- pathTo(outLoc)
- turnTo(outDir)
- for i = 1, 16 do
- if i ~= saplingSlot then
- 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)
- repeat smartTurnLeft() until direction == target
- end
- --Simple pathing uses presumed locations of trees to go where it wants. Cannot reliably path onto tree locations
- function pathTo(target)
- --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 turtTo(0) else turnTo(2) end
- smartForward()
- elseif location[2] % 4 == 1 then --Turtle has a tree straight East/West
- if location[2] < target[2] then turtTo(1) else turnTo(3) end
- smartForward()
- 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[1] < target[1] then turtTo(0) else turnTo(2) end
- for i = 1, math.abs(target[1] - location[1]) do smartForward() end
- for i = 1, math.abs(target[2] - location[2]) do smartForward() end
- elseif target[2] % 4 == 1 then --Target has a tree straight East/West
- if location[2] < target[2] then turtTo(1) else turnTo(3) end
- for i = 1, math.abs(target[2] - location[2]) do smartForward() end
- for i = 1, math.abs(target[1] - location[1]) do smartForward() end
- else
- for i = 1, math.abs(target[1] - location[1]) do smartForward() end
- for i = 1, math.abs(target[2] - location[2]) do smartForward() end
- end
- 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.")
- pathTo({2, 0})
- 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 > 64 then
- error("Error, space given is for " .. treeGridH * treeGridW .. " trees, 64 is the max number allowed!")
- else print("Enough space for " .. treeGridH * treeGridW .. " trees.") end
- while true do --basic thought loop
- harvest()
- pathTo({boxW-1, boxH-1})
- turnTo(3)
- gather()
- output()
- refuel()
- sleep(30)
- end
Advertisement
Add Comment
Please, Sign In to add comment