Tjakka5

Untitled

Dec 12th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. local function sign(v)
  2. return v > 0 and 1 or (v == 0 and 0 or -1)
  3. end
  4.  
  5. do -- Robot state
  6. local turtleState = {
  7. position = {x = 85, y = 66, z = 165},
  8. orientation = {x = 0, z = -1},
  9. }
  10.  
  11. local _turtleForward = turtle.forward
  12. turtle.forward = function()
  13. turtleState.position.x = turtleState.position.x + turtleState.orientation.x
  14. turtleState.position.z = turtleState.position.z + turtleState.orientation.z
  15.  
  16. return _turtleForward()
  17. end
  18.  
  19. local _turtleBack = turtle.back
  20. turtle.back = function(...)
  21. turtleState.position.x = turtleState.position.x - turtleState.orientation.x
  22. turtleState.position.z = turtleState.position.z - turtleState.orientation.z
  23.  
  24. return _turtleBack(...)
  25. end
  26.  
  27. local _turtleUp = turtle.up
  28. turtle.up = function(...)
  29. turtleState.position.y = turtleState.position.y + 1
  30.  
  31. return _turtleUp(...)
  32. end
  33.  
  34. local _turtleDown = turtle.down
  35. turtle.down = function(...)
  36. turtleState.position.y = turtleState.position.y - 1
  37.  
  38. return _turtleDown(...)
  39. end
  40.  
  41. local _turtleTurnLeft = turtle.turnLeft
  42. turtle.turnLeft = function(...)
  43. local oldOrientationX = turtleState.orientation.x
  44. local oldOrientationZ = turtleState.orientation.z
  45.  
  46. turtleState.orientation.x = oldOrientationZ
  47. turtleState.orientation.z = -oldOrientationX
  48.  
  49. return _turtleTurnLeft(...)
  50. end
  51.  
  52. local _turtleTurnRight = turtle.turnRight
  53. turtle.turnRight = function(...)
  54. local oldOrientationX = turtleState.orientation.x
  55. local oldOrientationZ = turtleState.orientation.z
  56.  
  57. turtleState.orientation.x = -oldOrientationZ
  58. turtleState.orientation.z = oldOrientationX
  59.  
  60. return _turtleTurnRight(...)
  61. end
  62.  
  63. turtle.getPosition = function()
  64. return turtleState.position.x, turtleState.position.y, turtleState.position.z
  65. end
  66.  
  67. turtle.getOrientation = function()
  68. return turtleState.orientation.x, turtleState.orientation.z
  69. end
  70.  
  71. turtle.orientX = function(targetX)
  72. if (turtleState.orientation.x == targetX) then
  73. return
  74. end
  75.  
  76. if (turtleState.orientation.x == -targetX) then
  77. turtle.turnLeft()
  78. turtle.turnLeft()
  79.  
  80. return
  81. end
  82.  
  83. if (turtleState.orientation.z == targetX) then
  84. turtle.turnLeft()
  85.  
  86. return
  87. end
  88.  
  89. if (turtleState.orientation.z == -targetX) then
  90. turtle.turnRight()
  91.  
  92. return
  93. end
  94. end
  95.  
  96. turtle.orientZ = function(targetZ)
  97. if (turtleState.orientation.z == targetZ) then
  98. return
  99. end
  100.  
  101. if (turtleState.orientation.z == -targetZ) then
  102. turtle.turnLeft()
  103. turtle.turnLeft()
  104.  
  105. return
  106. end
  107.  
  108. if (turtleState.orientation.x == targetZ) then
  109. turtle.turnRight()
  110.  
  111. return
  112. end
  113.  
  114. if (turtleState.orientation.x == -targetZ) then
  115. turtle.turnLeft()
  116.  
  117. return
  118. end
  119. end
  120.  
  121. turtle.gotoX = function(targetX)
  122. local dx = targetX - turtleState.position.x
  123.  
  124. if (dx == 0) then
  125. return
  126. end
  127.  
  128. turtle.orientX(sign(dx))
  129.  
  130. for i = 1, math.abs(dx) do
  131. turtle.forward()
  132. end
  133. end
  134.  
  135. turtle.gotoZ = function(targetZ)
  136. local dz = targetZ - turtleState.position.z
  137.  
  138. if (dz == 0) then
  139. return
  140. end
  141.  
  142. turtle.orientZ(sign(dz))
  143.  
  144. for i = 1, math.abs(dz) do
  145. turtle.forward()
  146. end
  147. end
  148. end
  149.  
  150. local function tryChop()
  151. if (turtle.detect()) then
  152. turtle.dig()
  153. turtle.forward()
  154.  
  155. while (turtle.detectUp()) do
  156. turtle.digUp()
  157. turtle.up()
  158. end
  159.  
  160. while (not turtle.detectDown()) do
  161. turtle.down()
  162. end
  163.  
  164. turtle.digDown()
  165. end
  166. end
  167.  
  168. local function harvest()
  169. turtle.forward()
  170. turtle.up()
  171.  
  172. turtle.gotoZ(160)
  173.  
  174. turtle.gotoX(73)
  175.  
  176. for i = 0, 5 do
  177. turtle.gotoZ(156 - i * 4)
  178. end
  179. end
  180.  
  181. harvest()
  182.  
  183. local x, y, z = turtle.getPosition()
  184. print(x, y, z)
  185.  
  186. local ox, oz = turtle.getOrientation()
  187. print(ox, oz)
Advertisement
Add Comment
Please, Sign In to add comment