Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. --[[
  2. A module providing:
  3. - Relative and absolute movement
  4. - Refuelling support
  5. -
  6. ]]--
  7.  
  8.  
  9. --[[
  10. dig: Digs a space before moving forward. NOTE: Dig is highly inefficient for moving backwards.
  11. tunnel: Ensures to dig space above and below for players.
  12. direct: UNIMPLEMENTED - Take straight line to location.
  13. ]]--
  14.  
  15. local self = {}
  16.  
  17. self.mode = {dig = true, tunnel = true, direct = false}
  18.  
  19. self.dirPreference = {"x","y","z"}
  20. pos = vector.new(0,0,0)
  21. -- heading = 0: +x
  22. -- heading = 1: +y
  23. heading = 0
  24.  
  25. function self.getHeading()
  26. return heading
  27. end
  28. function getPos()
  29. return pos
  30. end
  31.  
  32. function self.faceDir(dir)
  33. self.turn(dir - heading)
  34. end
  35.  
  36. function self.zeroHeading(heading)
  37. local oldHeading = heading
  38. heading = 0
  39. return oldHeading
  40. end
  41.  
  42. function self.turn(dir)
  43. dir = dir % 4
  44. if dir == 3 then
  45. turtle.turnRight()
  46. else
  47. for i = 1,dir do
  48. turtle.turnRight()
  49. end
  50. end
  51. heading = (heading + dir) % 4
  52. end
  53.  
  54. function forward()
  55. if self.mode.dig then
  56. turtle.dig()
  57. end
  58. if turtle.forward() then
  59. if self.mode.tunnel then
  60. turtle.digUp()
  61. turtle.digDown()
  62. end
  63. return true
  64. else
  65. return false
  66. end
  67. end
  68.  
  69. function up()
  70. if self.mode.dig then
  71. turtle.digUp()
  72. end
  73. return turtle.up()
  74. end
  75.  
  76. function down()
  77. if self.mode.dig then
  78. turtle.digDown()
  79. end
  80. return turtle.down()
  81. end
  82.  
  83. function self.moveForward(n)
  84. n = n or 1
  85. local dist = 0
  86. local oldHeading = heading
  87.  
  88. -- Face backwards and move forward.
  89. if n < 0 then
  90. self.turn(2)
  91. n = -n
  92. end
  93.  
  94. for i = 1,n do
  95. -- Takes care of direction and sign.
  96. if forward() then
  97. if heading % 2 == 0 then
  98. pos.x = pos.x + heading - 1 -- heading = 0 or 2
  99. else
  100. pos.y = pos.y + heading - 2 -- heading = 1 or 3
  101. end
  102. dist = dist + 1
  103. else
  104. return dist
  105. end
  106. end
  107. face(oldHeading)
  108. end
  109.  
  110. function self.moveVert(n)
  111. n = n or 1
  112. local dist = 0
  113. if n > 0 then
  114. for i = 1,n do
  115.  
  116. if up() then
  117. pos.z = pos.z + 1
  118. dist = dist + 1
  119. else
  120. return dist
  121. end
  122. end
  123. else
  124. for i = 1,-n do
  125. if down() then
  126. pos.z = pos.z - 1
  127. dist = dist - 1
  128. else
  129. return dist
  130. end
  131. end
  132. end
  133. end
  134.  
  135. function self.moveTo(vect)
  136. oldHeading = heading
  137. for dir = 1,#self.dirPreference do
  138. if self.dirPreference[dir] == "x" then
  139. self.faceDir(0)
  140. self.moveForward(vect.x)
  141. elseif self.dirPreference[dir] == "y" then
  142. self.faceDir(1)
  143. self.moveForward(vect.y)
  144. elseif self.dirPreference[dir] == "z" then
  145. self.moveVert(vect.z)
  146. else
  147. print("Error! self.dirPreference is corrupted")
  148. os.exit()
  149. end
  150. end
  151. self.faceDir(oldHeading)
  152. end
  153.  
  154.  
  155.  
  156. return self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement