Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Extended turtle functions
- by BigSHinyToys
- ]]--
- if not turtle then
- error("Not turtle")
- return
- end
- if turtle.restore then
- error("API currently active already")
- return
- end
- local oldUp = turtle.up
- local oldDown = turtle.down
- local oldBack = turtle.back
- local oldForward = turtle.forward
- local oldTurnLeft = turtle.turnLeft
- local oldTurnRight = turtle.turnRight
- local pos = {x = 0,y = 0,z = 0,f = 1}
- local tFace= {"north","east","south","west"}
- local tInc = {{"y",1},{"x",1},{"y",-1},{"x",-1}}
- function turtle.restore()
- turtle.up = oldUp
- turtle.down = oldDown
- turtle.back = oldBack
- turtle.forward = oldForward
- turtle.turnLeft = oldTurnLeft
- turtle.turnRight = oldTurnRight
- turtle.restore = nil
- turtle.getInternalGPS = nil
- turtle.setInternalGPS = nil
- turtle.face = nil
- turtle.getFace = nil
- turtle.goto = nil
- end
- function turtle.getInternalGPS()
- return pos.x , pos.y , pos.z , pos.f
- end
- function turtle.setInternalGPS(...)
- local tIn = {...}
- for i = 1,4 do
- if tIn[i] and type(tIn[i]) == "number" then
- else
- return false
- end
- end
- if tIn[4] >= 1 and tIn[4] <= 4 then
- pos.x = tIn[1]
- pos.y = tIn[2]
- pos.z = tIn[3]
- pos.f = tIn[4]
- return true
- else
- return false
- end
- end
- function turtle.up()
- if oldUp() then
- pos.z = pos.z + 1
- return true
- else
- return false
- end
- end
- function turtle.down()
- if oldDown() then
- pos.z = pos.z - 1
- return true
- else
- return false
- end
- end
- function turtle.turnLeft()
- pos.f = pos.f - 1
- if pos.f < 1 then
- pos.f = 4
- end
- return oldTurnLeft()
- end
- function turtle.turnRight()
- pos.f = pos.f + 1
- if pos.f > 4 then
- pos.f = 1
- end
- return oldTurnRight()
- end
- function turtle.back()
- if oldBack() then
- local face = tInc[pos.f][1]
- pos[face] = pos[face] - tInc[pos.f][2]
- return true
- else
- return false
- end
- end
- function turtle.forward()
- if oldForward() then
- local face = tInc[pos.f][1]
- pos[face] = pos[face] + tInc[pos.f][2]
- return true
- else
- return false
- end
- end
- function turtle.face(direction)
- local function test(nFace)
- if nFace < 1 then
- nFace = 4
- end
- if nFace > 4 then
- nFace = 1
- end
- return nFace
- end
- local face = direction
- if direction < 1 or direction > 4 then
- return false
- end
- if face == pos.f then
- return true
- elseif face == test(pos.f+1) then
- return turtle.turnRight()
- elseif face == test(pos.f-1) then
- return turtle.turnLeft()
- else
- turtle.turnRight()
- turtle.turnRight()
- return true
- end
- end
- function turtle.getFace()
- return pos.f
- end
- function turtle.goto(x,y,z,f)
- if x and x ~= pos.x then
- if pos.x < x then
- turtle.face(2)
- else
- turtle.face(4)
- end
- for i = 1,math.abs(pos.x - x) do
- if not turtle.forward() then
- return false
- end
- end
- end
- if y and y ~= pos.y then
- if pos.y < y then
- turtle.face(1)
- else
- turtle.face(3)
- end
- for i = 1,math.abs(pos.y - y) do
- if not turtle.forward() then
- return false
- end
- end
- end
- if z and z ~= pos.z then
- if pos.z < z then
- for i = 1,math.abs(pos.z - z) do
- turtle.up()
- end
- else
- for i = 1,math.abs(pos.z - z) do
- turtle.down()
- end
- end
- end
- if f and f >= 1 and f <= 4 then
- turtle.face(f)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement