nocv

Untitled

Oct 15th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. local curX = 0
  2. local curY = 0
  3. local curZ = 0
  4. local curO = 1
  5.  
  6. orientations = {
  7. NORTH = 1,
  8. EAST = 2,
  9. SOUTH = 3,
  10. WEST = 4
  11. }
  12.  
  13. directions = {
  14. FORWARD = 1,
  15. UP = 2,
  16. DOWN = 3
  17. }
  18.  
  19. function init(x, y, z, o)
  20. curX = x
  21. curY = y
  22. curZ = z
  23. curO = o
  24. end
  25.  
  26. function getPosition()
  27. local res = {}
  28. res["x"] = curX
  29. res["y"] = curY
  30. res["z"] = curZ
  31. res["o"] = curO
  32. return res
  33. end
  34.  
  35. function move(dir, canDig, iter)
  36. if(canDig == nil) then canDig = false end
  37. if(iter == nil) then iter = 0 end
  38.  
  39. if(iter >= 20) then
  40. print("20 times trying to move but failed...")
  41. return false
  42. end
  43.  
  44. local moveResult = false
  45.  
  46. if(dir == directions.FORWARD) then
  47. --print("Moving forward.")
  48. if(turtle.detect()) then
  49. moveResult = turtle.dig()
  50. end
  51. moveResult = turtle.forward()
  52. end
  53.  
  54. if(dir == directions.UP) then
  55. --print("Moving up.")
  56. if(turtle.detectUp()) then
  57. moveResult = turtle.digUp()
  58. end
  59. moveResult = turtle.up()
  60. end
  61.  
  62. if(dir == directions.DOWN) then
  63. --print("Moving down.")
  64. if(turtle.detectDown()) then
  65. moveResult = turtle.digDown()
  66. end
  67. moveResult = turtle.down()
  68. end
  69.  
  70. if(moveResult) then
  71. handleMove(dir)
  72. else
  73. print("Having trouble moving, maybe gravel?")
  74. iter = iter + 1
  75. move(dir, canDig, iter)
  76. end
  77. end
  78.  
  79. function moveTo(dest)
  80. local moveAmt = 0
  81. if(curY < dest.y) then
  82. moveAmt = dest.y - curY
  83. print("Moving up " .. moveAmt .. " blocks")
  84. for i=1,moveAmt,1 do move(directions.UP, true) end
  85. end
  86.  
  87. if(curY > dest.y) then
  88. moveAmt = curY - dest.y
  89. print("Moving down " .. moveAmt .. " blocks")
  90. for i=1,moveAmt,1 do move(directions.DOWN, true) end
  91. end
  92.  
  93. if(curZ < dest.z) then
  94. setOrientation(orientations.SOUTH)
  95. moveAmt = dest.z - curZ
  96. print("Moving south " .. moveAmt .. " blocks")
  97. for i=1,moveAmt,1 do move(directions.FORWARD, true) end
  98. end
  99.  
  100. if(curZ > dest.z) then
  101. setOrientation(orientations.NORTH)
  102. moveAmt = curZ - dest.z
  103. print("Moving north " .. moveAmt .. " blocks")
  104. for i=1,moveAmt,1 do move(directions.FORWARD, true) end
  105. end
  106.  
  107. if(curX < dest.x) then
  108. setOrientation(orientations.EAST)
  109. moveAmt = dest.x - curX
  110. print("Moving east " .. moveAmt .. " blocks")
  111. for i=1,moveAmt,1 do move(directions.FORWARD, true) end
  112. end
  113.  
  114. if(curX > dest.x) then
  115. setOrientation(orientations.WEST)
  116. moveAmt = curX - dest.x
  117. print("Moving west " .. moveAmt .. " blocks")
  118. for i=1,moveAmt,1 do move(directions.FORWARD, true) end
  119. end
  120. end
  121.  
  122. function handleMove(dir)
  123. if(dir == directions.UP) then
  124. curY = curY + 1
  125. end
  126.  
  127. if(dir == directions.DOWN) then
  128. curY = curY - 1
  129. end
  130.  
  131. if(dir == directions.FORWARD) then
  132. if(curO == orientations.NORTH) then
  133. curZ = curZ - 1
  134. elseif(curO == orientations.SOUTH) then
  135. curZ = curZ + 1
  136. elseif(curO == orientations.EAST) then
  137. curX = curX + 1
  138. else
  139. curX = curX - 1
  140. end
  141. end
  142. end
  143.  
  144. function turnLeft()
  145. if(turtle.turnLeft()) then
  146. curO = curO - 1
  147. if(curO < 1) then curO = 4 end
  148. end
  149. end
  150.  
  151. function turnRight()
  152. if(turtle.turnRight()) then
  153. curO = curO + 1
  154. if(curO > 4) then curO = 1 end
  155. end
  156. end
  157.  
  158. function setOrientation(o)
  159. while(curO ~= o) do
  160. turnLeft()
  161. end
  162. end
  163.  
  164. function excavateUp(l, w, h)
  165. local isForward = true
  166. local startPos = getPosition()
  167.  
  168. for ch=1,h,1 do
  169. for cw=1,w,1 do
  170. for cl=1,l-1,1 do
  171. move(directions.FORWARD, true)
  172. end
  173.  
  174. if(cw < w) then
  175. if(isForward) then
  176. turnRight()
  177. move(directions.FORWARD, true)
  178. turnRight()
  179.  
  180. isForward = false
  181. else
  182. turnLeft()
  183. move(directions.FORWARD, true)
  184. turnLeft()
  185.  
  186. isForward = true
  187. end
  188. end
  189. end
  190.  
  191. if(ch < h) then
  192. move(directions.UP, true)
  193. turnLeft()
  194. turnLeft()
  195. end
  196. end
  197.  
  198. for rl=1,h-1,1 do
  199. move(directions.DOWN, true)
  200. end
  201.  
  202. setOrientation(startPos.o)
  203.  
  204. if(curX ~= startPos.x) then
  205. turnLeft()
  206. for rsw=1,w-1,1 do move(directions.FORWARD, true) end
  207. turnLeft()
  208. for rsl=1,l-1,1 do move(directions.FORWARD, true) end
  209. setOrientation(startPos.o)
  210. end
  211. end
Add Comment
Please, Sign In to add comment