neo34rd

transformation.lua

Mar 24th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. os.loadAPI("rotational")
  2.  
  3.  
  4. function newTransform()
  5. local transform = {}
  6. transform['position'] = vector.makeZero()
  7. transform['forward'] = vector.make(0, 0, 1)
  8. transform['rotation'] = 0
  9. transform['ret'] = true --For error checking
  10. return transform
  11. end
  12.  
  13.  
  14. function clamp(value, low, high)
  15. return math.max(math.min(value, high), low)
  16. end
  17.  
  18.  
  19. function gotoPosition(transform, target)
  20.  
  21. --print("Moving to position: ", vector.toString(target))
  22.  
  23. local p = transform.position
  24. local d = vector.sub(target, p)
  25.  
  26. --print("movement vec: ", vector.toString(d))
  27.  
  28. x_axis = vector.make(clamp(d.x, -1, 1), 0, 0)
  29. z_axis = vector.make(0, 0, clamp(d.z, -1, 1))
  30. y_axis = vector.make(0, clamp(d.y, -1, 1), 0)
  31.  
  32. print("facing x-axis")
  33. rotational.faceAxis(transform, x_axis)
  34. print("moving in x-axis")
  35. for i = 1, math.abs(d.x) do
  36. forward(transform)
  37. if transform.ret == false then
  38. print("Error moving forward!")
  39. return false
  40. end
  41. end
  42.  
  43. print("facing z-axis")
  44. rotational.faceAxis(transform, z_axis)
  45. print("moving in z-axis")
  46. for i = 1, math.abs(d.z) do
  47. forward(transform)
  48. if transform.ret == false then
  49. print("Error moving forward!")
  50. return false
  51. end
  52. end
  53.  
  54. motion = nil
  55. print("moving in y-axis")
  56. if d.y > 0 then --Move up
  57. motion = up
  58. elseif d.y < 0 then --Move down
  59. motion = down
  60. end
  61.  
  62. if motion ~= nil then
  63. for i = 1, math.abs(d.y) do
  64. motion(transform)
  65. if transform.ret == false then
  66. print("Error moving vertically!")
  67. return false
  68. end
  69. end
  70. end
  71.  
  72. transform.ret = true
  73. return true
  74. end
  75.  
  76.  
  77. function forward(currentTransform)
  78. local p = currentTransform.position
  79. local f = currentTransform.forward
  80.  
  81. --- Try to move forward
  82. local b = turtle.forward()
  83. currentTransform.ret = b
  84.  
  85. --- Error
  86. if not b then
  87. return b
  88. end
  89.  
  90. --- Update position
  91. p = vector.add(p, f)
  92. currentTransform.position = p
  93. return b
  94. end
  95.  
  96.  
  97. function up(currentTransform)
  98. local p = currentTransform.position
  99.  
  100. --- Try to move upwards
  101. local b = turtle.up()
  102. currentTransform.ret = b
  103. if not b then
  104. return b
  105. end
  106.  
  107. --- Update position
  108. p = vector.add(p, vector.make(0, 1, 0))
  109. currentTransform.position = p
  110. return b
  111. end
  112.  
  113.  
  114. function down(currentTransform)
  115. local p = currentTransform.position
  116.  
  117. --- Try to move downwards
  118. local b = turtle.down()
  119. currentTransform.ret = b
  120. if not b then
  121. return b
  122. end
  123.  
  124. --- Update position
  125. p = vector.add(p, vector.make(0, -1, 0))
  126. currentTransform.position = p
  127. return b
  128. end
  129.  
  130.  
  131. function back(currentTransform)
  132. local p = currentTransform.position
  133. local f = currentTransform.forward
  134.  
  135. --- Try to move backwards
  136. local b = turtle.back()
  137. currentTransform.ret = b
  138. if not b then
  139. return b
  140. end
  141.  
  142. --- Update position
  143. p = vector.sub(p, f)
  144. currentTransform.position = p
  145. return b
  146. end
  147.  
  148.  
  149. function turnRight(currentTransform)
  150. return rotational.turnRight(currentTransform)
  151. end
  152.  
  153.  
  154. function turnLeft(currentTransform)
  155. return rotational.turnLeft(currentTransform)
  156. end
  157.  
  158.  
  159. function turnAround(currentTransform)
  160. return rotational.turnAround(currentTransform)
  161. end
  162.  
  163. function face(transform, axis)
  164. rotational.faceAxis(transform, axis)
  165. end
Add Comment
Please, Sign In to add comment