Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("basics")
- dofile("ui")
- local direction = 0
- local px = 0
- local py = 0
- local pz = 0
- local pathCounter = 1
- local path = {}
- local function addToPath(action)
- path[pathCounter] = action
- pathCounter = pathCounter + 1
- end
- local function left()
- turtle.turnLeft()
- if direction == 0 then
- direction = 3
- else
- direction = direction - 1
- end
- addToPath("r")
- end
- local function right()
- turtle.turnRight()
- if direction == 3 then
- direction = 0
- else
- direction = direction + 1
- end
- addToPath("l")
- end
- local function turnto(desdir)
- if direction == desdir then
- return
- end
- if desdir > direction then
- if direction == 0 and desdir == 3 then
- left()
- else
- while direction ~= desdir do
- right()
- end
- end
- elseif desdir < direction then
- if direction == 3 and desdir == 0 then
- right()
- else
- while direction ~= desdir do
- left()
- end
- end
- end
- end
- local function dig()
- local returnvalue = false
- while turtle.detect() do
- if turtle.dig() then
- returnvalue = true
- end
- end
- return returnvalue
- end
- local function digUp()
- local returnvalue = false
- while turtle.detectUp() do
- if turtle.digUp() then
- returnvalue = true
- end
- end
- return returnvalue
- end
- local function digDown()
- if turtle.digDown() then
- return true
- end
- return false
- end
- local function Fforward()
- if not turtle.forward() then
- print("something is obstructing the way")
- sleep(1)
- ui.clear()
- Fforward()
- end
- end
- local function forward()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.forward() then
- ui.clear()
- sleep(1)
- Fforward()
- end
- if direction == 0 then
- px = px + 1
- elseif direction == 1 then
- pz = pz + 1
- elseif direction == 2 then
- px = px - 1
- elseif direction == 3 then
- pz = pz - 1
- else
- print("invalid direction = ")
- end
- addToPath("b")
- end
- local function Fback()
- if not turtle.back() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fback()
- end
- end
- local function back()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.back() then
- ui.clear()
- sleep(1)
- Fback()
- end
- if direction == 0 then
- px = px - 1
- elseif direction == 1 then
- pz = pz - 1
- elseif direction == 2 then
- px = px + 1
- elseif direction == 3 then
- pz = pz + 1
- else
- print("invalid direction = ")
- end
- addToPath("f")
- end
- local function Fup()
- if not turtle.up() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fup()
- end
- end
- local function up()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.up() then
- ui.clear()
- sleep(1)
- Fup()
- end
- py = py + 1
- addToPath("d")
- end
- local function Fdown()
- if not turtle.down() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fdown()
- end
- end
- local function down()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.down() then
- ui.clear()
- sleep(1)
- Fdown()
- end
- py = py - 1
- addToPath("u")
- end
- local function returnX()
- if px < 0 then
- turnto(0)
- else
- turnto(2)
- end
- while px ~= 0 do
- forward()
- end
- end
- local function returnY()
- while py ~= 0 do
- if py < 0 then
- up()
- end
- if py > 0 then
- down()
- end
- end
- end
- local function returnZ()
- if pz < 0 then
- turnto(1)
- else
- turnto(3)
- end
- while pz ~= 0 do
- forward()
- end
- end
- local function setStart()
- px = 0
- py = 0
- pz = 0
- direction = 0
- path = {}
- pathCounter = 1
- end
- local function normalReturn()
- if px ~= 0 then
- returnX()
- end
- if py ~= 0 then
- returnY()
- end
- if pz ~= 0 then
- returnZ()
- end
- turnto(0)
- setStart()
- end
- local function sameWayReturn()
- for I = (pathCounter -1), 1, -1 do
- if path[I] == "l" then
- left()
- elseif path[I] == "r" then
- right()
- elseif path[I] == "b" then
- back()
- elseif path[I] == "f" then
- forward()
- elseif path[I] == "d" then
- down()
- elseif path[I] == "u" then
- up()
- else
- print("Error returning the same way")
- ui.pressenter()
- end
- pathCounter = pathCounter - 1
- path[I] = nil
- end
- setStart()
- end
- local function setStart()
- px = 0
- py = 0
- pz = 0
- direction = 0
- path = {}
- pathCounter = 1
- end
- local xNegative = false
- local yNegative = false
- local zNegative = false
- local function digUpOrDown()
- if yNegative == true then
- digDown()
- else
- turtle.digUp()
- end
- end
- local function xBeforeZ(x, z, digging)
- local leftorright = false
- if zNegative == true then
- leftorright = true
- end
- if xNegative == true then
- if zNegative == true then
- leftorright = false
- else
- leftorright = true
- end
- end
- local function UseFunction() end
- if digging == true then
- UseFunction = dig
- end
- if xNegative == true then
- left()
- left()
- end
- for I = 1, z do
- for C = 1, (x-1) do
- UseFunction()
- forward()
- end
- if leftorright == false and I ~= z then
- right()
- UseFunction()
- forward()
- right()
- leftorright = true
- elseif leftorright == true and I ~= z then
- left()
- UseFunction()
- forward()
- left()
- leftorright = false
- else
- return
- end
- end
- end
- local function zBeforeX(x, z, digging)
- local leftorright = true
- if xNegative == true then
- leftorright = false
- end
- if zNegative == true then
- if xNegative == true then
- leftorright = true
- else
- leftorright = false
- end
- end
- local function UseFunction() end
- if digging == true then
- UseFunction = dig
- end
- if zNegative == true then
- left()
- else
- right()
- end
- for I = 1, x do
- for C = 1, (z-1) do
- UseFunction()
- forward()
- end
- if leftorright == false and I ~= x then
- right()
- UseFunction()
- forward()
- right()
- leftorright = true
- elseif leftorright == true and I ~= x then
- left()
- UseFunction()
- forward()
- left()
- leftorright = false
- else
- return
- end
- end
- end
- local function setNegatives(x, y, z)
- if x < 0 then
- x = math.abs(x)
- xNegative = true
- end
- if y < 0 then
- y = math.abs(y)
- yNegative = true
- end
- if z < 0 then
- z = math.abs(z)
- zNegative = true
- end
- return x, y, z
- end
- local function resetNegatives()
- xNegative = false
- yNegative = false
- zNegative = false
- end
- local function field(x, z, digging)
- if digging == nil then
- digging = false
- end
- if x < z then
- zBeforeX(x, z, digging)
- else
- xBeforeZ(x, z, digging)
- end
- returnX()
- returnZ()
- turnto(0)
- end
- local function block(x, y, z, digging)
- setStart()
- if x == nil or y == nil or z == nil or x == 0 or y == 0 or z == 0 then
- ui.clear()
- ui.printing("checkBlock(xAxis, yAxis, zAxis)", colors.red)
- print("Waring: x and z axis are relative to turtles position and 0 is invalid")
- ui.pressenter()
- return
- end
- x, y, z = setNegatives(x, y, z)
- if digging == nil then
- digging = false
- end
- local function UseFunction() end
- if digging == true then
- UseFunction = digUpOrDown
- end
- for I = 1, y do
- field(x, z, digging)
- if I ~= y then
- UseFunction()
- if yNegative == true then
- down()
- else
- up()
- end
- end
- end
- resetNegatives()
- end
- local function excavate(x, y, z)
- block(x, y, z, true)
- end
- local function relativeTo(x,y,z)
- if x == nil or y == nil or z == nil then
- ui.printing("Enter x, y, z as parameters", colors.red)
- ui.pressenter()
- return false
- end
- if tonumber(x) == nil or tonumber(y) == nil or tonumber(z) == nil then
- ui.printing("One of the parameters you entered isnt a number", colors.red)
- ui.pressenter()
- return false
- end
- local FxNegative = false
- if x < 0 then
- x = math.abs(x)
- FxNegative = true
- left()
- left()
- end
- for I = 1, x do
- forward()
- end
- if y < 0 then
- y = math.abs(y)
- for I = 1, y do
- down()
- end
- else
- for I = 1, y do
- up()
- end
- end
- if z == 0 then return true end
- if z < 0 then
- if FxNegative == true then
- z = math.abs(z)
- right()
- else
- z = math.abs(z)
- left()
- end
- else
- if FxNegative == true then
- left()
- else
- right()
- end
- end
- for I = 1, z do
- forward()
- end
- end
- local function globalTo(x,y,z)
- end
- returning = {sameWay = sameWayReturn, setStart = setStart, normalWay = normalReturn, Z = returnZ, Y = returnY, X = returnX}
- move = { turnto = turnto, up = up, down = down, left = left, right = right, forward = forward, back = back, relativeTo = relativeTo }
- check = { block = block }
- digging = { excavate = excavate, up = digUp, forward = dig, down = digDown }
RAW Paste Data