Xonoa

tMV

Jan 16th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. local xPos, zPos, yPos = 0, 0, 0
  2. local movementArray = {}
  3. local facing = 1
  4.  
  5. --constructing movementArray with the proper values for x, y increment based on facing
  6. --array looks like |a11 a12 a13 a14|
  7. -- |a21 a22 a23 a24|
  8. for facingInc=1,4 do
  9. movementArray[facingInc] = {}
  10. for xzInc=1,2 do
  11. movementArray[facingInc][xzInc] = 0
  12. end
  13. end
  14.  
  15. --filling in proper values in movementArray
  16. --row 1 is the z movement, row 2 is the x movement
  17. movementArray[1][1] = 1
  18. movementArray[1][2] = 0
  19. movementArray[1][3] = -1
  20. movementArray[1][4] = 0
  21.  
  22. movementArray[2][1] = 0
  23. movementArray[2][2] = 1
  24. movementArray[2][3] = 0
  25. movementArray[2][4] = -1
  26.  
  27. --move turtle forward with tracking
  28. function tForward()
  29. --print("moving forward")
  30. --print("curPos:"..xPos..yPos..zPos)
  31. if turtle.forward() then
  32. zPos = zPos + movementArray[1][facing]
  33. xPos = xPos + movementArray[2][facing]
  34. --print("moved")
  35. --print("newPos:"..xPos..yPos..zPos)
  36. return true
  37. else
  38. --print("could not move")
  39. return false
  40. end
  41. end
  42.  
  43. --move turtle back with tracking
  44. function tBack()
  45. --print("moving back")
  46. --print("curPos:"..xPos..yPos..zPos)
  47. if turtle.back() then
  48. zPos = zPos - movementArray[1][facing]
  49. xPos = xPos - movementArray[2][facing]
  50. --print("moved")
  51. --print("newPos:"..xPos..yPos..zPos)
  52. return true
  53. else
  54. --print("could not move")
  55. return false
  56. end
  57. end
  58.  
  59. --turn turtle right with tracking
  60. function tTurnRight()
  61. --print("turning Right")
  62. --print("curFac:"..facing)
  63. if turtle.turnRight() then
  64. if facing < 4 then
  65. facing = facing + 1
  66. elseif facing == 4 then
  67. facing = 1
  68. end
  69. --print("turned")
  70. --print("newFac:"..facing)
  71. return true
  72. else
  73. return false
  74. end
  75. end
  76.  
  77. --turn turtle left with tracking
  78. function tTurnLeft()
  79. --print("turning Left")
  80. --print("curFac:"..facing)
  81. if turtle.turnLeft() then
  82. if facing > 1 then
  83. facing = facing - 1
  84. elseif facing == 1 then
  85. facing = 4
  86. end
  87. --print("turned")
  88. --print("newFac:"..facing)
  89. return true
  90. else
  91. return false
  92. end
  93. end
  94.  
  95. --move turtle up with tracking
  96. function tUp()
  97. --print("moving up")
  98. --print("curElev:"..yPos)
  99. if turtle.up() then
  100. yPos = yPos + 1
  101. --print("moved up")
  102. --print("newElev:"..yPos)
  103. return true
  104. else
  105. --print("could not move up")
  106. return false
  107. end
  108. end
  109.  
  110. --move turtle down with tracking
  111. function tDown()
  112. --print("moving down")
  113. --print("curElev:"..yPos)
  114. if turtle.down() then
  115. yPos = yPos - 1
  116. --print("moved down")
  117. --print("newElev:"..yPos)
  118. return true
  119. else
  120. --print("could not move down")
  121. return false
  122. end
  123. end
  124.  
  125. --faces a specified direction
  126. function face(targetFacing)
  127. --print("turning to face new direction")
  128. --print("curFac:"..facing)
  129. while facing ~= targetFacing do
  130. tTurnRight()
  131. end
  132. --print("newFac:"..facing)
  133. end
  134.  
  135. --move turtle back to position using tracking data
  136. function moveToPos(targetRelPos)
  137. --print("moving to target position")
  138. --print("target pos:" .. targX .. targY .. targZ)
  139. local isOnTarget = false
  140. local stuckness = {}
  141. for i=1,3 do stuckness[i] = 0 end
  142.  
  143. while not isOnTarget do
  144. --attemps to move towards origin, first in y, then z, then x.
  145. -- it it gets stuck, then the stuckness val is ++ and it
  146. -- will attempt to move away from 0 in one dimension to
  147. -- unstick itself
  148. --It does this after each axis normal movement to prevent
  149. -- immediate backtracking
  150.  
  151. --normal attempts start with y axis
  152. if yPos < targetRelPos[2] then
  153. --print("I need to go up")
  154. if not tUp() then
  155. stuckness[2] = stuckness[2] + 1
  156. --print("stuckness:"..stuckness[2])
  157. end
  158. elseif yPos > targetRelPos[2] then
  159. --print("I need to go down")
  160. if not tDown() then
  161. stuckness[2] = stuckness[2] + 1
  162. --print("stuckness:"..stuckness[2])
  163. end
  164. end
  165.  
  166. --if stuck for 3 cycles it will move opposite to normal to
  167. -- try to escape
  168. if stuckness[2] >= 3 then
  169. --print("Im stuck in the y axis")
  170. if yPos < targetRelPos[2] then
  171. if tDown() then
  172. --print("Ive moved down")
  173. stuckness[2] = 0
  174. end
  175. else
  176. if tUp() then
  177. --print("Ive moved up")
  178. stuckness [2] = 0
  179. end
  180. end
  181. end
  182.  
  183.  
  184. --then z axis
  185. if zPos > targetRelPos[3] then
  186. --print("I need to go south")
  187. face(3)
  188. if not tForward() then
  189. stuckness[3] = stuckness[3] + 1
  190. --print("stuckness:"..stuckness[3])
  191. end
  192. elseif zPos < targetRelPos[3] then
  193. --print("I need to go north")
  194. face(1)
  195. if not tForward() then
  196. stuckness[3] = stuckness[3] + 1
  197. --print("stuckness:"..stuckness[3])
  198. end
  199. end
  200.  
  201. --if stuck for 3 cycles it will move opposite to normal to
  202. -- try to escape
  203. if stuckness[3] >= 3 then
  204. --print("Im stuck in the z axis")
  205. if tBack() then
  206. --print("Ive moved back")
  207. stuckness[3] = 0
  208. end
  209. end
  210.  
  211.  
  212. --then x axis
  213. if xPos > targetRelPos[1] then
  214. --print("I need to go west")
  215. face(4)
  216. if not tForward() then
  217. stuckness[1] = stuckness[1] + 1
  218. --print("stuckness:"..stuckness[1])
  219. end
  220. elseif xPos < targetRelPos[1] then
  221. --print("I need to go east")
  222. face(2)
  223. if not tForward() then
  224. stuckness[1] = stuckness[1] + 1
  225. --print("stuckness:"..stuckness[1])
  226. end
  227. end
  228.  
  229. --if stuck for 3 cycles it will move opposite to normal to
  230. -- try to escape
  231. if stuckness[1] >= 3 then
  232. --print("Im stuck in the x axis")
  233. if tBack() then
  234. --print("Ive moved back")
  235. stuckness[1] = 0
  236. end
  237. end
  238.  
  239.  
  240. if (xPos == targetRelPos[1] and yPos == targetRelPos[2] and zPos == targetRelPos[3]) then
  241. isOnTarget = true
  242. end
  243. end
  244. end
  245.  
  246. --returns to the original start location
  247. function goHome()
  248. moveToPos({0,0,0})
  249. end
  250.  
  251. --returns current coordinates
  252. function getPos()
  253. return({xPos, yPos, zPos})
  254. end
  255.  
  256. --returns current facing
  257. function getFacing()
  258. return(facing)
  259. end
Add Comment
Please, Sign In to add comment