Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- trees
- -- Harvests trees laid out in rows
- -- Trees must be 1x1 trunks with no branches
- -- If supplied, saplings will be replanted
- -- Program assumes turtle is pre-fueled
- -- Turtle returns to its starting point when mission is completed or fuel runs low
- -- Written by HarvDad, April 2014
- args = {...}
- nArgs = #args
- version = "trees: Rev 1.2"
- mission = "Harvest trees laid out in rows"
- usage = "Usage: trees <rows>"
- x = 0
- y = 0
- z = 0
- face = 0
- missionMessage = "Mission complete."
- abort = false
- logSlot = 1
- saplingSlot = 16
- markerSlot = 15
- local currentFuelLevel = turtle.getFuelLevel()
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(mission)
- print("Place sample log in slot ", logSlot)
- print("Place sapling supply in slot ", saplingSlot)
- print("Place marker block in slot ", markerSlot)
- print(usage)
- return
- end
- if nArgs ~= 1 then
- print(usage)
- return
- end
- rows = tonumber(args[1])
- if rows == nil then
- print("\"", args[1], "\" is not a valid row count.")
- return
- end
- if rows < 1 then
- print("Number of rows must be positive number.")
- end
- areaCovered = 1
- currentSlot = 2
- materialSlot = 1
- maxSlot = 16
- nextTurn = "left"
- function setFace(f)
- if f == 0 then
- if face == 0 then return end
- if face == 1 then right() return end
- if face == 2 then right() right() return end
- if face == 3 then left() return end
- end
- if f == 1 then
- if face == 0 then left() return end
- if face == 1 then return end
- if face == 2 then right() return end
- if face == 3 then right() right() return end
- end
- if f == 2 then
- if face == 0 then left() left() return end
- if face == 1 then left() return end
- if face == 2 then return end
- if face == 3 then right() return end
- end
- if f == 3 then
- if face == 0 then right() return end
- if face == 1 then left() left() return end
- if face == 2 then left() return end
- if face == 3 then return end
- end
- end
- function forward()
- for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
- if turtle.forward() then
- break
- end
- turtle.attack()
- sleep(2)
- end
- areaCovered = areaCovered + 1
- if face == 0 then z = z+1 return end
- if face == 1 then x = x-1 return end
- if face == 2 then z = z-1 return end
- if face == 3 then x = x+1 return end
- end
- function left()
- if face == 0 then face = 1 turtle.turnLeft() return end
- if face == 1 then face = 2 turtle.turnLeft() return end
- if face == 2 then face = 3 turtle.turnLeft() return end
- if face == 3 then face = 0 turtle.turnLeft() return end
- print("function left\(\): Bad face value: ", face)
- end
- function right()
- if face == 0 then face = 3 turtle.turnRight() return end
- if face == 1 then face = 0 turtle.turnRight() return end
- if face == 2 then face = 1 turtle.turnRight() return end
- if face == 3 then face = 2 turtle.turnRight() return end
- print("function right\(\): Bad face value: ", face)
- end
- function up()
- for i=1,10 do
- if not turtle.up() then
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- sleep(2)
- end
- else
- break
- end
- end
- y = y+1
- end
- function down()
- for i=1,10 do
- if not turtle.down() then
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- sleep(2)
- end
- else
- break
- end
- end
- y = y-1
- end
- function focusSaplings()
- local i
- saplingCount = turtle.getItemCount(saplingSlot)
- if saplingCount < 2 then
- for i=2,15 do
- turtle.select(i)
- if turtle.compareTo(saplingSlot) then
- turtle.transferTo(saplingSlot)
- end
- end
- end
- turtle.select(saplingSlot)
- end
- function nextTree()
- success = false
- while not success do
- if not turtle.forward() then
- turtle.select(logSlot)
- if turtle.compare() then
- success = true
- else
- turtle.select(saplingSlot)
- if turtle.compare() then
- success = true
- else
- if turtle.detect() then
- missionMessage = "Encountered unexpected block."
- sleep(5)
- abort = true
- return success
- else
- for i=1,10 do
- success = turtle.forward()
- if not success then
- turtle.attack()
- sleep(2)
- else
- if z >= lastZ or x <= lastX then
- return
- end
- updateLocation()
- end
- end
- missionMessage = "Could not get past unknown entity"
- abort = true
- return success
- end
- end
- end
- else
- if z >= lastZ or x <= lastX then
- return success
- end
- updateLocation()
- end
- -- print("nextTree: x = ", x, ", z = ", z)
- end
- return success
- end
- function updateLocation()
- if face == 0 then z = z+1 return end
- if face == 1 then x = x-1 return end
- if face == 2 then z = z-1 return end
- if face == 3 then x = x+1 return end
- end
- function harvestSapling()
- turtle.dig()
- forward()
- up()
- turtle.select(saplingSlot)
- turtle.placeDown()
- turtle.select(logSlot)
- end
- function harvestTree()
- turtle.dig()
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- up()
- end
- while y > 1 do
- down()
- end
- turtle.select(saplingSlot)
- turtle.placeDown()
- turtle.select(logSlot)
- end
- function chopTree()
- turtle.select(logSlot)
- if turtle.compare() then
- turtle.dig()
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while not turtle.detectDown() do
- turtle.suckDown()
- turtle.down()
- end
- else
- turtle.dig()
- forward()
- end
- forward()
- totalCut = totalCut + 1
- end
- function replantTree()
- focusSaplings()
- if turtle.getItemCount(saplingSlot) > 0 then
- left()
- left()
- turtle.select(saplingSlot)
- turtle.place()
- left()
- left()
- end
- end
- function _home()
- setFace(east)
- forward()
- if z < 0 then
- setFace(north)
- while z < 0 do
- forward()
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- forward()
- end
- end
- end
- if x < 0 then
- setFace(west)
- while x < 0 do
- forward()
- end
- else
- if x > 0 then
- setFace(east)
- while x > 0 do
- forward()
- end
- end
- end
- setFace(north)
- nextTurn = "left"
- end
- function nextRow()
- if nextTurn == "left" then
- left()
- for i=1,interval do
- forward()
- end
- left()
- nextTurn = "right"
- else
- right()
- for i=1,interval do
- forward()
- end
- right()
- nextTurn = "left"
- end
- end
- function facingTree()
- turtle.select(logSlot)
- return turtle.compare()
- end
- function facingSapling() -- WARNING!!! turtles cannot dependably compare to saplings (BUG)
- turtle.select(saplingSlot)
- return turtle.compare()
- end
- function facingMarker()
- turtle.select(markerSlot)
- return turtle.compare()
- end
- spanSet = false
- function harvestForward()
- if y > 0 then
- turtle.dig()
- forward()
- down()
- else
- if facingTree() then
- harvestTree()
- else
- if facingMarker() and x == 0 then
- spanZ = z
- spanSet = true
- else
- if turtle.detect() then -- sadly, we must assume it's a sapling
- harvestSapling()
- else
- forward()
- end
- end
- end
- end
- end
- function home()
- if z == 0 then
- left()
- while x < 0 do
- forward()
- end
- else
- right()
- while x < 0 do
- forward()
- end
- forward()
- right()
- while z > 0 do
- forward()
- end
- right()
- forward()
- end
- setFace(north)
- end
- -- Main program
- totalCut = 0
- rowCount = 0
- columnCount = 0
- firstZ = 0
- lastZ = 0
- lastX = 0
- interval = 5
- setFace(north)
- nextTurn = "left"
- --[[
- for i=1,30 do
- if facingTree() then
- break
- end
- forward()
- end
- --]]
- firstZ = z
- lastZ = firstZ + ((rows-1) * interval)
- spanZ = lastZ - firstZ
- spanX = -12
- spanZ = 100
- lastJ = 0
- for i = 1,rows do
- for j = 1,spanZ do
- -- forward()
- harvestForward()
- if x == 0 and spanSet then
- break
- end
- end
- if i < rows then
- nextRow()
- end
- end
- if y > 0 then
- turtle.dig()
- forward()
- down()
- end
- print("Heading home...")
- home()
- if abort then
- print("Mission aborted")
- end
- print(missionMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement