Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cutting starts by facing middle of the tree
- -- Turtle facing rotations
- -- 0
- -- 1 3
- -- 2
- x = 1
- y = 1
- z = 0
- rot = 0
- function cut()
- local fuelDelay = 260
- -- rot 0:+Z, 1:+X, 2:-Z
- local ix = 1
- local oldix = ix
- local sx, sz
- fuel(10000, 10000)
- empty()
- -- Map the tree area
- if not turtle.detect() then
- print("Error, no tree in front")
- return
- else
- -- Find Z depth of tree
- repeat
- turtle.dig()
- if turtle.forward() then
- z = z+1
- end
- until not turtle.detect()
- sz = z
- print("Size "..sz.." x "..sz)
- if sz > 1 then
- -- Go back to middle Z
- for i = 1, sz/2 do
- if turtle.back() then
- z = z-1
- end
- end
- end
- turtle.turnRight()
- -- rot = 3
- -- Find X minimum
- while turtle.detect() do
- turtle.dig()
- turtle.forward()
- end
- x = 1
- if sz > 2 then
- -- Go back to middle X
- for i = 1, (sz-1)/2 do
- if turtle.back() then
- x = x+1
- end
- end
- end
- -- Dig to treetop (facing right)
- repeat
- turtle.digUp()
- turtle.up()
- y = y+1
- until (not turtle.detectUp()) and (y > 2)
- print("Height "..y)
- -- rot = 3
- -- Go to min X
- while x > 1 do
- turtle.dig()
- if turtle.forward() then
- x = x-1
- end
- end
- turtle.turnRight()
- -- rot = 2
- -- Go to min Z
- while z > 1 do
- turtle.dig()
- if turtle.forward() then
- z = z-1
- end
- end
- turtle.turnRight()
- turtle.turnRight()
- rot = 0
- end
- sx = sz
- turtle.digDown()
- turtle.down()
- y = y-1
- -- Start cutting loop
- sdelay = 0
- while true do
- if sdelay == 0 then sleep(1) end
- sdelay = (sdelay+1) % 10
- oldix = ix
- if rot == 0 then -- Going forward
- if z < sz then
- if dig() then
- z = z+1
- end
- else -- Reached edge
- if ix > 0 then
- turtle.turnLeft()
- rot = 1 -- Left
- else
- turtle.turnRight()
- rot = 3 -- Right
- end
- if ((x >= sx) and (ix == 1)) or
- ((x <= 1) and (ix == -1)) then
- -- Reached horizontal plane corner
- if y <= 1 then
- -- Digging finished
- break
- else
- -- Move down
- for i=1,3 do
- if y > 1 then
- if turtle.detectDown() then
- if turtle.digDown() then
- empty()
- else
- break
- end
- end
- y = y-1
- turtle.down()
- end
- end
- ix = -ix
- end
- elseif sx>1 then
- -- dig 1 to side to prepare for next column
- if dig() then
- x = x+ix
- end
- end
- if oldix > 0 then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- rot = 2 -- backwards
- end
- elseif rot == 2 then -- Going backwards
- if z > 1 then
- if dig() then
- z = z-1
- end
- else -- Reached edge
- if ix > 0 then
- turtle.turnRight()
- rot = 1
- else
- turtle.turnLeft()
- rot = 3
- end
- if ((x >= sx) and (ix == 1)) or
- ((x <= 1) and (ix == -1)) then
- -- Reached horizontal plane corner
- if y <= 1 then
- -- Digging finished
- break
- else
- -- Move down
- for i=1,3 do
- if y > 1 then
- if turtle.detectDown() then
- if turtle.digDown() then
- empty()
- else
- break
- end
- end
- y = y-1
- turtle.down()
- end
- end
- ix = -ix
- end
- elseif sx>1 then
- -- dig 1 to side to prepare for next column
- if dig() then
- x = x+ix
- end
- end
- if oldix > 0 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- rot = 0 -- forwards
- end
- end
- fuelDelay = fuelDelay-1
- if fuelDelay < 0 then
- fuelDelay = 260
- if turtle.getFuelLevel() < 600 then
- fuel(10000, 10000)
- end
- end
- end
- -- Move back to starting position
- local moves = 40
- repeat
- moves = moves-1
- until moveStep(1+sx/2, 1, 0) or (moves < 0)
- -- Finally empty items to chest
- empty()
- print("Cutting trip finished!")
- end
- function dig()
- local ret
- if turtle.detectUp then
- turtle.digUp()
- end
- if (y > 1) and turtle.detectDown then
- turtle.digDown()
- end
- if turtle.detect() then
- turtle.dig()
- end
- ret = turtle.forward()
- if turtle.detectUp then
- turtle.digUp()
- end
- empty()
- return ret
- end
- function doEmpty()
- print("Emptying inventory...")
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- turtle.select(16) -- Select ender chest for placing
- if not turtle.placeUp() then
- print("Error placing ender chest")
- return
- end
- for i=1,14 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(16)
- turtle.digUp()
- sleep(1)
- print("Emptying finished")
- end
- function empty()
- if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
- doEmpty()
- end
- end
- function fuel(minfuel, maxfuel)
- empty() -- Make sure slot 14 is empty
- write("Checking fuel...")
- if turtle.getFuelLevel() > minfuel then
- print("Fuel tank already full")
- return
- end
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- local chest = false
- turtle.select(14)
- while turtle.getFuelLevel() < maxfuel do
- turtle.refuel()
- if not chest then
- turtle.select(15)
- if not turtle.placeUp() then
- print("Error placing ender chest")
- break
- end
- chest = true
- turtle.select(14)
- end
- turtle.suckUp()
- print("Fuel "..turtle.getFuelLevel())
- end
- if chest then
- turtle.select(15)
- turtle.digUp()
- end
- print("Fuel tank filled")
- end
- function turnTo(newRot)
- function capRot(r)
- if r < -0.5 then
- r = r+4
- elseif r > 3.5 then
- r = r-4
- end
- return r
- end
- if rot ~= newRot then
- if capRot(rot-1) == newRot then
- turtle.turnRight()
- elseif capRot(rot+1) == newRot then
- turtle.turnLeft()
- else
- turtle.turnRight()
- turtle.turnRight()
- end
- rot = newRot
- end
- end
- function moveStep(toX, toY, toZ)
- toX = math.floor(toX)
- toY = math.floor(toY)
- toZ = math.floor(toZ)
- local dx = math.abs(toX-x)
- local dz = math.abs(toZ-z)
- if (dx > dz+0.5) and (dx > 0.5) then
- if toX > x then
- turnTo(1)
- dx = 1
- else
- turnTo(3)
- dx = -1
- end
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- x = x+dx
- end
- elseif dz > 0.5 then
- if toZ < z then
- turnTo(2)
- dz = -1
- else
- turnTo(0)
- dz = 1
- end
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- z = z+dz
- end
- else
- -- In this script positive Y-axis goes upwards
- if toY > y+0.5 then
- if turtle.detectUp() then
- turtle.digUp()
- end
- if turtle.up() then
- y = y+1
- end
- elseif toY < y-0.5 then
- if turtle.detectDown() then
- turtle.digDown()
- end
- if turtle.down() then
- y = y-1
- end
- end
- end
- return (x == toX) and (y == toY) and (z == toZ)
- end
- cut()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement