Advertisement
nocv

Untitled

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