Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local curX = 0
- local curY = 0
- local curZ = 0
- local curO = 1
- orientations = {
- NORTH = 1,
- EAST = 2,
- SOUTH = 3,
- WEST = 4
- }
- directions = {
- FORWARD = 1,
- UP = 2,
- DOWN = 3
- }
- function init(x, y, z, o)
- curX = x
- curY = y
- curZ = z
- curO = o
- end
- function getPosition()
- local res = {}
- res["x"] = curX
- res["y"] = curY
- res["z"] = curZ
- res["o"] = curO
- return res
- end
- function move(dir, canDig, iter)
- if(canDig == nil) then canDig = false end
- if(iter == nil) then iter = 0 end
- if(iter >= 20) then
- print("20 times trying to move but failed...")
- return false
- end
- local moveResult = false
- if(dir == directions.FORWARD) then
- --print("Moving forward.")
- if(turtle.detect()) then
- moveResult = turtle.dig()
- end
- moveResult = turtle.forward()
- end
- if(dir == directions.UP) then
- --print("Moving up.")
- if(turtle.detectUp()) then
- moveResult = turtle.digUp()
- end
- moveResult = turtle.up()
- end
- if(dir == directions.DOWN) then
- --print("Moving down.")
- if(turtle.detectDown()) then
- moveResult = turtle.digDown()
- end
- moveResult = turtle.down()
- end
- if(moveResult) then
- handleMove(dir)
- else
- print("Having trouble moving, maybe gravel?")
- iter = iter + 1
- move(dir, canDig, iter)
- end
- end
- function moveTo(dest)
- local moveAmt = 0
- if(curY < dest.y) then
- moveAmt = dest.y - curY
- print("Moving up " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.UP, true) end
- end
- if(curY > dest.y) then
- moveAmt = curY - dest.y
- print("Moving down " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.DOWN, true) end
- end
- if(curZ < dest.z) then
- setOrientation(orientations.SOUTH)
- moveAmt = dest.z - curZ
- print("Moving south " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.FORWARD, true) end
- end
- if(curZ > dest.z) then
- setOrientation(orientations.NORTH)
- moveAmt = curZ - dest.z
- print("Moving north " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.FORWARD, true) end
- end
- if(curX < dest.x) then
- setOrientation(orientations.EAST)
- moveAmt = dest.x - curX
- print("Moving east " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.FORWARD, true) end
- end
- if(curX > dest.x) then
- setOrientation(orientations.WEST)
- moveAmt = curX - dest.x
- print("Moving west " .. moveAmt .. " blocks")
- for i=1,moveAmt,1 do move(directions.FORWARD, true) end
- end
- end
- function handleMove(dir)
- if(dir == directions.UP) then
- curY = curY + 1
- end
- if(dir == directions.DOWN) then
- curY = curY - 1
- end
- if(dir == directions.FORWARD) then
- if(curO == orientations.NORTH) then
- curZ = curZ - 1
- elseif(curO == orientations.SOUTH) then
- curZ = curZ + 1
- elseif(curO == orientations.EAST) then
- curX = curX + 1
- else
- curX = curX - 1
- end
- end
- end
- function turnLeft()
- if(turtle.turnLeft()) then
- curO = curO - 1
- if(curO < 1) then curO = 4 end
- end
- end
- function turnRight()
- if(turtle.turnRight()) then
- curO = curO + 1
- if(curO > 4) then curO = 1 end
- end
- end
- function setOrientation(o)
- while(curO ~= o) do
- turnLeft()
- end
- end
- function excavateUp(l, w, h)
- local isForward = true
- local startPos = getPosition()
- for ch=1,h,1 do
- for cw=1,w,1 do
- for cl=1,l-1,1 do
- move(directions.FORWARD, true)
- end
- if(cw < w) then
- if(isForward) then
- turnRight()
- move(directions.FORWARD, true)
- turnRight()
- isForward = false
- else
- turnLeft()
- move(directions.FORWARD, true)
- turnLeft()
- isForward = true
- end
- end
- end
- if(ch < h) then
- move(directions.UP, true)
- turnLeft()
- turnLeft()
- end
- end
- for rl=1,h-1,1 do
- move(directions.DOWN, true)
- end
- setOrientation(startPos.o)
- if(curX ~= startPos.x) then
- turnLeft()
- for rsw=1,w-1,1 do move(directions.FORWARD, true) end
- turnLeft()
- for rsl=1,l-1,1 do move(directions.FORWARD, true) end
- setOrientation(startPos.o)
- end
- end
Add Comment
Please, Sign In to add comment