Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local turtleutils = { } do
- local currentDirection = 1
- function turtleutils.dirInit()
- trying = true
- tryingNum = 1
- while(trying)do
- startLocation = {gps.locate()}
- if(tryingNum == 1)then
- turtle.forward()
- endLocation = {gps.locate()}
- if(endLocation[1] > startLocation[1])then
- currentDirection = 2
- trying = false
- elseif(endLocation[1] < startLocation[1])then
- currentDirection = 4
- trying = false
- elseif(endLocation[3] > startLocation[3])then
- currentDirection = 3
- trying = false
- elseif(endLocation[3] < startLocation[3])then
- currentDirection = 1
- trying = false
- end
- elseif(tryingNum == 2)then
- turtle.back()
- endLocation = {gps.locate()}
- if(endLocation[1] > startLocation[1])then
- currentDirection = 4
- trying = false
- elseif(endLocation[1] < startLocation[1])then
- currentDirection = 2
- trying = false
- elseif(endLocation[3] > startLocation[3])then
- currentDirection = 1
- trying = false
- elseif(endLocation[3] < startLocation[3])then
- currentDirection = 3
- trying = false
- end
- end
- tryingNum = tryingNum + 1
- end
- end
- --gets and stores the direction of the turtle
- function direction(change, strict)
- if change == "left" then
- if currentDirection == 1 then
- currentDirection = 4
- else
- currentDirection = currentDirection - 1
- end
- return currentDirection
- elseif change == "right" then
- if currentDirection == 4 then
- currentDirection = 1
- else
- currentDirection = currentDirection + 1
- end
- return currentDirection
- else
- if strict then
- print("the function \"direction\" recieved and invalid direction while sctrict was set to true")
- else
- return currentDirection
- end
- end
- end
- function turtleutils.turn(idirection)
- if idirection == "left" then
- turtle.turnLeft()
- elseif idirection == "right" then
- turtle.turnRight()
- end
- return direction(idirection, true)
- end
- -- moves and mines at the same time to ensure there is no block where it is trying to move
- function turtleutils.moveMine(idirection)
- local waitingCount = 0
- if idirection == "up" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspectUp()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.digUp()
- else
- waitingCount = waitingCount + 1
- sleep(1)
- end
- turtle.up()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.dig()
- turtle.forward()
- return
- end
- end
- elseif idirection == "forward" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspect()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.dig()
- else
- waitingCount = waitingCount + 1
- sleep(1)
- end
- turtle.forward()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- sleep(3)
- return
- end
- end
- elseif idirection == "down" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspectDown()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.digDown()
- else
- waitingCount = waitingCount + 1
- end
- turtle.down()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.dig()
- turtle.forward()
- return
- end
- end
- else
- print("function \"moveMine\" recieved invalid direction")
- end
- end
- -- goes to a coordinate position
- function turtleutils.turtleGoto(x, y, z, doMoveMine, reverse, gettingValues)
- if gettingValues then
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- return distances
- end
- pathing = true
- if reverse then
- while pathing do
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- print(textutils.serialize(distances))
- if distances[3] > 0 then
- while direction() ~= 3 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[3] < 0 then
- while direction() ~= 1 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[1] > 0 then
- while direction() ~= 2 do
- turtleutils.turn("right")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[1] < 0 then
- while direction() ~= 4 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[2] > 0 then
- if(domoveMine)then
- moveMine("up")
- else
- turtle.up()
- end
- elseif distances[2] < 0 then
- if(domoveMine)then
- moveMine("down")
- else
- turtle.down()
- end
- else
- print("at location")
- pathing = false
- return
- end
- end
- else
- while pathing do
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- print(textutils.serialize(distances))
- if distances[2] > 0 then
- if(domoveMine)then
- moveMine("up")
- else
- turtle.up()
- end
- elseif distances[2] < 0 then
- if(domoveMine)then
- moveMine("down")
- else
- turtle.down()
- end
- elseif distances[1] > 0 then
- while direction() ~= 2 do
- turtleutils.turn("right")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[1] < 0 then
- while direction() ~= 4 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[3] > 0 then
- while direction() ~= 3 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- elseif distances[3] < 0 then
- while direction() ~= 1 do
- turtleutils.turn("left")
- end
- if(domoveMine)then
- moveMine("forward")
- else
- turtle.forward()
- end
- else
- print("at location")
- pathing = false
- return
- end
- end
- end
- end
- function turtleutils.getCoalCount(x, y, z)
- local distance = ((math.abs(x) + math.abs(y) + math.abs(z)) * 2)
- print(distance)
- return distance / 80 + 3
- end
- end return turtleutils
Add Comment
Please, Sign In to add comment