Advertisement
neo34rd

rotational.lua

Mar 24th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. os.loadAPI('vector')
  2.  
  3.  
  4.  
  5. function turnLeft(currentTransform)
  6. turtle.turnLeft()
  7. return updateTurnIndex(currentTransform, -1)
  8. end
  9.  
  10. function turnRight(currentTransform)
  11. turtle.turnRight()
  12. return updateTurnIndex(currentTransform, 1)
  13. end
  14.  
  15. function turnAround(currentTransform)
  16. for i = 1, 2 do
  17. currentTransform = turnLeft(currentTransform)
  18. end
  19. return currentTransform
  20. end
  21.  
  22. function updateTurnIndex(currentTransform, delta)
  23. local currentTurnIndex = currentTransform.rotation
  24. currentTurnIndex = currentTurnIndex + delta
  25. if currentTurnIndex > 2 then
  26. currentTurnIndex = -1
  27. elseif currentTurnIndex < -2 then
  28. currentTurnIndex = 1
  29. end
  30. currentTransform.rotation = currentTurnIndex
  31. currentTransform.forward = rotationIndexToForwardVec(currentTurnIndex)
  32. return currentTransform
  33. end
  34.  
  35. function rotationIndexToForwardVec(currentTurnIndex)
  36. local turnIndexToAxis = {}
  37. --- Forward
  38. turnIndexToAxis[0] = vector.make(0, 0, 1)
  39. --- Left
  40. turnIndexToAxis[-1] = vector.make(-1, 0, 0)
  41. --- Right
  42. turnIndexToAxis[1] = vector.make(1, 0, 0)
  43. --- Back
  44. turnIndexToAxis[-2] = vector.make(0, 0, -1)
  45. turnIndexToAxis[2] = vector.make(0, 0, -1)
  46.  
  47. return turnIndexToAxis[currentTurnIndex]
  48. end
  49.  
  50.  
  51. function forwardVecToRotationIndex(axis)
  52. local axisToRotationIndex = {}
  53. -- Forward
  54. axisToRotationIndex[vector.toString(vector.make(0,0,1))] = 0
  55. -- Left
  56. axisToRotationIndex[vector.toString(vector.make(-1,0,0))] = -1
  57. -- Right
  58. axisToRotationIndex[vector.toString(vector.make(1,0,0))] = 1
  59. -- Back
  60. axisToRotationIndex[vector.toString(vector.make(0,0,-1))] = 2
  61.  
  62. return axisToRotationIndex[vector.toString(axis)]
  63. end
  64.  
  65.  
  66. function faceAxis(transform, axis)
  67. --- axis must be unit length
  68. local len = math.abs(axis.x) + math.abs(axis.z)
  69. if len ~= 1 then
  70. print("axis is not well defined!")
  71. transform.ret = false
  72. return false
  73. end
  74.  
  75. if axis.y ~= 0 then
  76. print("Cannot handle face Y axis values, turtle can only turn left/right!")
  77. transform.ret = false
  78. return false
  79. end
  80.  
  81. -- To-From lookup table for rotation
  82. func = {}
  83. func[0] = nil
  84. func[-4] = nil
  85. func[4] = nil
  86. func[-1] = turnRight
  87. func[3] = turnRight
  88. func[-2] = turnAround
  89. func[2] = turnAround
  90. func[1] = turnLeft
  91. func[-3] = turnLeft
  92.  
  93.  
  94.  
  95. d = transform.rotation - forwardVecToRotationIndex(axis)
  96. turn = func[d]
  97. if turn ~= nil then
  98. turn(transform)
  99. end
  100.  
  101. transform.ret = true
  102. return true
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement