Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local xPos, zPos, yPos = 0, 0, 0
- local movementArray = {}
- local facing = 1
- --constructing movementArray with the proper values for x, y increment based on facing
- --array looks like |a11 a12 a13 a14|
- -- |a21 a22 a23 a24|
- for facingInc=1,4 do
- movementArray[facingInc] = {}
- for xzInc=1,2 do
- movementArray[facingInc][xzInc] = 0
- end
- end
- --filling in proper values in movementArray
- --row 1 is the z movement, row 2 is the x movement
- movementArray[1][1] = 1
- movementArray[1][2] = 0
- movementArray[1][3] = -1
- movementArray[1][4] = 0
- movementArray[2][1] = 0
- movementArray[2][2] = 1
- movementArray[2][3] = 0
- movementArray[2][4] = -1
- --move turtle forward with tracking
- function tForward()
- --print("moving forward")
- --print("curPos:"..xPos..yPos..zPos)
- if turtle.forward() then
- zPos = zPos + movementArray[1][facing]
- xPos = xPos + movementArray[2][facing]
- --print("moved")
- --print("newPos:"..xPos..yPos..zPos)
- return true
- else
- --print("could not move")
- return false
- end
- end
- --move turtle back with tracking
- function tBack()
- --print("moving back")
- --print("curPos:"..xPos..yPos..zPos)
- if turtle.back() then
- zPos = zPos - movementArray[1][facing]
- xPos = xPos - movementArray[2][facing]
- --print("moved")
- --print("newPos:"..xPos..yPos..zPos)
- return true
- else
- --print("could not move")
- return false
- end
- end
- --turn turtle right with tracking
- function tTurnRight()
- --print("turning Right")
- --print("curFac:"..facing)
- if turtle.turnRight() then
- if facing < 4 then
- facing = facing + 1
- elseif facing == 4 then
- facing = 1
- end
- --print("turned")
- --print("newFac:"..facing)
- return true
- else
- return false
- end
- end
- --turn turtle left with tracking
- function tTurnLeft()
- --print("turning Left")
- --print("curFac:"..facing)
- if turtle.turnLeft() then
- if facing > 1 then
- facing = facing - 1
- elseif facing == 1 then
- facing = 4
- end
- --print("turned")
- --print("newFac:"..facing)
- return true
- else
- return false
- end
- end
- --move turtle up with tracking
- function tUp()
- --print("moving up")
- --print("curElev:"..yPos)
- if turtle.up() then
- yPos = yPos + 1
- --print("moved up")
- --print("newElev:"..yPos)
- return true
- else
- --print("could not move up")
- return false
- end
- end
- --move turtle down with tracking
- function tDown()
- --print("moving down")
- --print("curElev:"..yPos)
- if turtle.down() then
- yPos = yPos - 1
- --print("moved down")
- --print("newElev:"..yPos)
- return true
- else
- --print("could not move down")
- return false
- end
- end
- --faces a specified direction
- function face(targetFacing)
- --print("turning to face new direction")
- --print("curFac:"..facing)
- while facing ~= targetFacing do
- tTurnRight()
- end
- --print("newFac:"..facing)
- end
- --move turtle back to position using tracking data
- function moveToPos(targetRelPos)
- --print("moving to target position")
- --print("target pos:" .. targX .. targY .. targZ)
- local isOnTarget = false
- local stuckness = {}
- for i=1,3 do stuckness[i] = 0 end
- while not isOnTarget do
- --attemps to move towards origin, first in y, then z, then x.
- -- it it gets stuck, then the stuckness val is ++ and it
- -- will attempt to move away from 0 in one dimension to
- -- unstick itself
- --It does this after each axis normal movement to prevent
- -- immediate backtracking
- --normal attempts start with y axis
- if yPos < targetRelPos[2] then
- --print("I need to go up")
- if not tUp() then
- stuckness[2] = stuckness[2] + 1
- --print("stuckness:"..stuckness[2])
- end
- elseif yPos > targetRelPos[2] then
- --print("I need to go down")
- if not tDown() then
- stuckness[2] = stuckness[2] + 1
- --print("stuckness:"..stuckness[2])
- end
- end
- --if stuck for 3 cycles it will move opposite to normal to
- -- try to escape
- if stuckness[2] >= 3 then
- --print("Im stuck in the y axis")
- if yPos < targetRelPos[2] then
- if tDown() then
- --print("Ive moved down")
- stuckness[2] = 0
- end
- else
- if tUp() then
- --print("Ive moved up")
- stuckness [2] = 0
- end
- end
- end
- --then z axis
- if zPos > targetRelPos[3] then
- --print("I need to go south")
- face(3)
- if not tForward() then
- stuckness[3] = stuckness[3] + 1
- --print("stuckness:"..stuckness[3])
- end
- elseif zPos < targetRelPos[3] then
- --print("I need to go north")
- face(1)
- if not tForward() then
- stuckness[3] = stuckness[3] + 1
- --print("stuckness:"..stuckness[3])
- end
- end
- --if stuck for 3 cycles it will move opposite to normal to
- -- try to escape
- if stuckness[3] >= 3 then
- --print("Im stuck in the z axis")
- if tBack() then
- --print("Ive moved back")
- stuckness[3] = 0
- end
- end
- --then x axis
- if xPos > targetRelPos[1] then
- --print("I need to go west")
- face(4)
- if not tForward() then
- stuckness[1] = stuckness[1] + 1
- --print("stuckness:"..stuckness[1])
- end
- elseif xPos < targetRelPos[1] then
- --print("I need to go east")
- face(2)
- if not tForward() then
- stuckness[1] = stuckness[1] + 1
- --print("stuckness:"..stuckness[1])
- end
- end
- --if stuck for 3 cycles it will move opposite to normal to
- -- try to escape
- if stuckness[1] >= 3 then
- --print("Im stuck in the x axis")
- if tBack() then
- --print("Ive moved back")
- stuckness[1] = 0
- end
- end
- if (xPos == targetRelPos[1] and yPos == targetRelPos[2] and zPos == targetRelPos[3]) then
- isOnTarget = true
- end
- end
- end
- --returns to the original start location
- function goHome()
- moveToPos({0,0,0})
- end
- --returns current coordinates
- function getPos()
- return({xPos, yPos, zPos})
- end
- --returns current facing
- function getFacing()
- return(facing)
- end
Add Comment
Please, Sign In to add comment