Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function sign(v)
- return v > 0 and 1 or (v == 0 and 0 or -1)
- end
- do -- Robot state
- local turtleState = {
- position = {x = 85, y = 66, z = 165},
- orientation = {x = 0, z = -1},
- }
- local _turtleForward = turtle.forward
- turtle.forward = function()
- turtleState.position.x = turtleState.position.x + turtleState.orientation.x
- turtleState.position.z = turtleState.position.z + turtleState.orientation.z
- return _turtleForward()
- end
- local _turtleBack = turtle.back
- turtle.back = function(...)
- turtleState.position.x = turtleState.position.x - turtleState.orientation.x
- turtleState.position.z = turtleState.position.z - turtleState.orientation.z
- return _turtleBack(...)
- end
- local _turtleUp = turtle.up
- turtle.up = function(...)
- turtleState.position.y = turtleState.position.y + 1
- return _turtleUp(...)
- end
- local _turtleDown = turtle.down
- turtle.down = function(...)
- turtleState.position.y = turtleState.position.y - 1
- return _turtleDown(...)
- end
- local _turtleTurnLeft = turtle.turnLeft
- turtle.turnLeft = function(...)
- local oldOrientationX = turtleState.orientation.x
- local oldOrientationZ = turtleState.orientation.z
- turtleState.orientation.x = oldOrientationZ
- turtleState.orientation.z = -oldOrientationX
- return _turtleTurnLeft(...)
- end
- local _turtleTurnRight = turtle.turnRight
- turtle.turnRight = function(...)
- local oldOrientationX = turtleState.orientation.x
- local oldOrientationZ = turtleState.orientation.z
- turtleState.orientation.x = -oldOrientationZ
- turtleState.orientation.z = oldOrientationX
- return _turtleTurnRight(...)
- end
- turtle.getPosition = function()
- return turtleState.position.x, turtleState.position.y, turtleState.position.z
- end
- turtle.getOrientation = function()
- return turtleState.orientation.x, turtleState.orientation.z
- end
- turtle.orientX = function(targetX)
- if (turtleState.orientation.x == targetX) then
- return
- end
- if (turtleState.orientation.x == -targetX) then
- turtle.turnLeft()
- turtle.turnLeft()
- return
- end
- if (turtleState.orientation.z == targetX) then
- turtle.turnLeft()
- return
- end
- if (turtleState.orientation.z == -targetX) then
- turtle.turnRight()
- return
- end
- end
- turtle.orientZ = function(targetZ)
- if (turtleState.orientation.z == targetZ) then
- return
- end
- if (turtleState.orientation.z == -targetZ) then
- turtle.turnLeft()
- turtle.turnLeft()
- return
- end
- if (turtleState.orientation.x == targetZ) then
- turtle.turnRight()
- return
- end
- if (turtleState.orientation.x == -targetZ) then
- turtle.turnLeft()
- return
- end
- end
- turtle.gotoX = function(targetX)
- local dx = targetX - turtleState.position.x
- if (dx == 0) then
- return
- end
- turtle.orientX(sign(dx))
- for i = 1, math.abs(dx) do
- turtle.forward()
- end
- end
- turtle.gotoZ = function(targetZ)
- local dz = targetZ - turtleState.position.z
- if (dz == 0) then
- return
- end
- turtle.orientZ(sign(dz))
- for i = 1, math.abs(dz) do
- turtle.forward()
- end
- end
- end
- local function harvest()
- for i = 1, 7 do
- turtle.forward()
- end
- turtle.gotoX(73)
- turtle.gotoZ(156)
- end
- harvest()
- local x, y, z = turtle.getPosition()
- print(x, y, z)
- local ox, oz = turtle.getOrientation()
- print(ox, oz)
Advertisement
Add Comment
Please, Sign In to add comment