Advertisement
dlord

/miner/movement

Dec 18th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. directions={
  2. NORTH={0, 1},
  3. EAST={1, 0},
  4. SOUTH={0, -1},
  5. WEST={-1, 0},
  6. ORDER={"NORTH", "EAST", "SOUTH", "WEST"},
  7. indexOf=function(self, direction)
  8. for i, value in ipairs(self.ORDER) do
  9. if value==direction then
  10. return i
  11. end
  12. end
  13. end
  14. }
  15.  
  16. directionX, directionY=unpack(directions.NORTH)
  17. currentDirection=1 --NORTH=1, EAST=2, SOUTH=3, WEST=4
  18. currentX, currentY, currentZ=0, 0, 0
  19.  
  20. function getCurrentCoordinates()
  21. return currentX, currentY, currentZ
  22. end
  23.  
  24. function getCurrentDirection()
  25. return directions.ORDER[currentDirection]
  26. end
  27.  
  28. function Stack()
  29. local stack={
  30. push=function(self, ...)
  31. for i, value in ipairs{...} do
  32. self[#self+1]=value
  33. end
  34. end,
  35.  
  36. pop=function(self, count)
  37. local count = count or 1
  38. if count > #self then
  39. error("Stack underflow!")
  40. end
  41.  
  42. local values={}
  43. for i=count, 1, -1 do
  44. values[#values+1]=table.remove(self)
  45. end
  46.  
  47. return unpack(values)
  48. end
  49. }
  50.  
  51. return setmetatable({}, {__index = stack})
  52. end
  53.  
  54. function turnRight()
  55. turtle.turnRight()
  56. currentDirection=currentDirection+1
  57.  
  58. if currentDirection > 4 then
  59. currentDirection=1
  60. end
  61.  
  62. directionX, directionY=unpack(directions[directions.ORDER[currentDirection]])
  63.  
  64. return turnLeft
  65. end
  66.  
  67. function turnLeft()
  68. turtle.turnLeft()
  69. currentDirection=currentDirection-1
  70.  
  71. if currentDirection < 1 then
  72. currentDirection=4
  73. end
  74.  
  75. directionX, directionY=unpack(directions[directions.ORDER[currentDirection]])
  76.  
  77. return turnRight
  78. end
  79.  
  80. function forward()
  81. local success=turtle.forward()
  82.  
  83. if success==false then
  84. return false
  85. end
  86.  
  87. currentX=currentX+directionX
  88. currentY=currentY+directionY
  89.  
  90. return true
  91. end
  92.  
  93. function up()
  94. local success=turtle.up()
  95.  
  96. if success==false then
  97. return false
  98. end
  99.  
  100. currentZ=currentZ+1
  101.  
  102. return true
  103. end
  104.  
  105. function down()
  106. local success=turtle.down()
  107.  
  108. if success==false then
  109. return false
  110. end
  111.  
  112. currentZ=currentZ-1
  113.  
  114. return true
  115. end
  116.  
  117. turnAround=function()
  118. turnRight()
  119. turnRight()
  120. end
  121.  
  122. function face(direction)
  123. local destinationDirection=directions:indexOf(direction)
  124. local offset=destinationDirection-currentDirection
  125.  
  126. local turnFunction=turnRight
  127. if (offset < 1 and offset~=-3) or (offset > 1 and offset==3) then
  128. turnFunction=turnLeft
  129. end
  130.  
  131. while currentDirection~=destinationDirection do
  132. turnFunction()
  133. end
  134. end
  135.  
  136. function move(destinationX, destinationY, destinationZ)
  137. local dx=destinationX-currentX
  138. local dy=destinationY-currentY
  139. local dz=destinationZ-currentZ
  140.  
  141. face("NORTH")
  142.  
  143. local xDirection="WEST"
  144. if dx>0 then
  145. xDirection="EAST"
  146. end
  147.  
  148. local yDirection="SOUTH"
  149. if dy>0 then
  150. yDirection="NORTH"
  151. end
  152.  
  153. local zFunction=down
  154. if dz>0 then
  155. zFunction=up
  156. end
  157.  
  158. while currentZ~=destinationZ do
  159. zFunction()
  160. end
  161.  
  162. if dx~=0 then
  163. face(xDirection)
  164. while currentX~=destinationX do
  165. forward()
  166. end
  167. end
  168.  
  169. if dy~=0 then
  170. face(yDirection)
  171. while currentY~=destinationY do
  172. forward()
  173. end
  174. end
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement