Advertisement
nocv

Untitled

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