Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if turtle.getFuelLevel()==0 then
- turtle.refuel(20)
- end
- MoveCount = 0
- Kill = false
- PosOffset = {x=0,y=0,z=0}
- Forward = {x=1,y=0,z=0}
- function shift(direction)
- if direction=="right" then
- print("shifting direction right")
- if Forward.x==1 then
- Forward.x=0
- Forward.z=1
- elseif Forward.z==1 then
- Forward.x=-1
- Forward.z=0
- elseif Forward.x==-1 then
- Forward.x=0
- Forward.z=-1
- elseif Forward.z==-1 then
- Forward.z=0
- Forward.x=1
- end
- end
- if direction=="left" then
- print("shifting direction left")
- if Forward.x==1 then
- Forward.x=0
- Forward.z=-1
- elseif Forward.z==-1 then
- Forward.x=-1
- Forward.z=0
- elseif Forward.x==-1 then
- Forward.x=0
- Forward.z=1
- elseif Forward.z==1 then
- Forward.z=0
- Forward.x=1
- end
- end
- end
- function GetDirectionFromForward()
- if Forward.x==1 then
- return "forward"
- elseif Forward.x==-1 then
- return "backwards"
- elseif Forward.z==1 then
- return "right"
- elseif Forward.z==-1 then
- return "left"
- end
- end
- function base(num)
- if num>0 then
- return 1
- elseif num<0 then
- return -1
- else
- return 0
- end
- end
- function SetDirection(direction)
- if GetDirectionFromForward()~=direction:lower() then
- repeat
- turtle.turnRight()
- shift("right")
- until
- GetDirectionFromForward()==direction:lower()
- end
- end
- function ShiftOffsetForward()
- PosOffset.x=PosOffset.x+Forward.x
- PosOffset.y=PosOffset.y+Forward.y
- PosOffset.z=PosOffset.z+Forward.z
- end
- function isore(...)
- local tb = {...}
- if tb and #tb>0 then
- if pcall(function() return tb[2] end) and tb[2].name then
- return tb[2].name:find("ore")~=nil
- end
- end
- end
- function isoff()
- if PosOffset.x~=0 or PosOffset.y~=0 or PosOffset.z~=0 then
- return true
- end
- end
- function returnTunnel()
- print("returning to tunnel")
- if PosOffset.x~=0 then
- print('adjusting x')
- repeat
- print("adjusting x to "..(-base(PosOffset.x))..", current = "..Forward.x..","..Forward.z)
- shift("right")
- turtle.turnRight()
- until Forward.x==-base(PosOffset.x)
- repeat
- if turtle.detect() then turtle.dig() end
- turtle.forward()
- local offori = math.abs(PosOffset.x)
- ShiftOffsetForward()
- if math.abs(PosOffset.x) > offori then
- turtle.turnRight()
- turtle.turnRight()
- shift("right")
- shift("right")
- end
- until PosOffset.x==0
- end
- if PosOffset.z~=0 then
- print('adjusting z : '..Forward.z)
- repeat
- print("adjusting z to "..(-base(PosOffset.z))..", current = "..Forward.x..","..Forward.z)
- shift("right")
- turtle.turnRight()
- until Forward.z==-base(PosOffset.z)
- repeat
- if turtle.detect() then turtle.dig() end
- turtle.forward()
- local offori = math.abs(PosOffset.z)
- ShiftOffsetForward()
- if math.abs(PosOffset.z) > offori then
- turtle.turnRight()
- turtle.turnRight()
- shift("right")
- shift("right")
- end
- until PosOffset.z==0
- end
- if PosOffset.y~=0 then
- print('adjusting y')
- if PosOffset.y>0 then
- repeat
- if turtle.detectDown() then turtle.digDown() end
- turtle.down()
- PosOffset.y=PosOffset.y-1
- until PosOffset.y==0
- end
- if PosOffset.y<0 then
- repeat
- if turtle.detectUp() then turtle.digUp() end
- turtle.up()
- PosOffset.y=PosOffset.y+1
- until PosOffset.y==0
- end
- end
- SetDirection("Forward")
- check(true)
- end
- function check(turn)
- print("checking for ore")
- local cont = true
- local incheck = function()
- if cont and turtle.detectUp() then
- if isore(turtle.inspectUp()) then
- print("FoundUp")
- turtle.digUp()
- turtle.up()
- PosOffset.y = PosOffset.y+1
- check(true)
- end
- end
- if cont and turtle.detectDown() then
- if isore(turtle.inspectDown()) then
- print("FoundDown")
- turtle.digDown()
- turtle.down()
- PosOffset.y = PosOffset.y-1
- check(true)
- end
- end
- end
- incheck()
- if cont and turn then
- print("Turning")
- turtle.turnRight()
- shift("right")
- if cont and turtle.detect() then
- if isore(turtle.inspect()) then
- turtle.dig()
- turtle.forward()
- ShiftOffsetForward()
- cont = false
- check(true)
- end
- end
- if cont then
- turtle.turnLeft();turtle.turnLeft()
- shift("left");shift("left")
- if isore(turtle.inspect()) then
- turtle.dig()
- turtle.forward()
- ShiftOffsetForward()
- check(true)
- end
- end
- end
- if cont and PosOffset.x~=0 or PosOffset.y ~= 0 or PosOffset.z~=0 then
- returnTunnel()
- end
- SetDirection("forward")
- end
- function mine()
- table.foreach(Forward,print)
- if turtle.getFuelLevel()>0 then
- print("Continuing Tunnel")
- check()
- if turtle.detect() then
- turtle.dig()
- turtle.forward()
- MoveCount = MoveCount+1
- else
- repeat
- turtle.forward()
- MoveCount = MoveCount+1
- until
- (turtle.detect() or turtle.getFuelLevel()==0)
- mine()
- end
- else
- print("Out of fuel")
- end
- print(MoveCount)
- end
- for i = 1,100 do
- mine()
- end
- SetDirection("backwards")
- for i = 1,MoveCount do
- repeat if turtle.detect() then turtle.dig() end until not turtle.detect()
- turtle.forward()
- end
- print("Offset")
- table.foreach(PosOffset,print)
- print("Forward")
- table.foreach(Forward,print)
- print("Movements:"..MoveCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement