Advertisement
Houshalter

Modified Excavation Script

Mar 2nd, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. function digforward(n)
  2. for i = 1, n do
  3. repeat
  4. if turtle.detect() then turtle.dig() end
  5. until turtle.forward()
  6. if direction == 1 then gx = gx + 1 end
  7. if direction == 2 then gy = gy + 1 end
  8. if direction == 3 then gx = gx - 1 end
  9. if direction == 4 then gy = gy - 1 end
  10. end
  11. end
  12.  
  13. function movedown(n)
  14. for i = 1, n do
  15. repeat
  16. if turtle.detectDown() then turtle.digDown() end
  17. until turtle.down()
  18. gz = gz + 1
  19. end
  20. end
  21.  
  22. function moveup(n)
  23. for i = 1, n do
  24. repeat
  25. if turtle.detectUp() then turtle.digUp() end
  26. until turtle.up()
  27. gz = gz - 1
  28. end
  29. end
  30.  
  31. function turnright(n)
  32. for i = 1, n do
  33. turtle.turnRight()
  34. direction = direction + 1
  35. if direction > 4 then direction = 1 end
  36. end
  37. end
  38.  
  39. function dumpinventory()
  40. for i = 2, 16 do
  41. turtle.select(i)
  42. turtle.drop()
  43. end
  44. turtle.select(1)
  45. end
  46.  
  47. function dumpores()
  48. local rx, ry, rz, rdirection = gx, gy, gz, direction
  49. gohome()
  50. face(3)
  51. dumpinventory()
  52. gotoz(rz)
  53. gotoy(ry)
  54. gotox(rx)
  55. face(rdirection)
  56. end
  57.  
  58. function waitforfuel()
  59. local rx, ry, rz, rdirection = gx, gy, gz, direction
  60. gohome()
  61. face(3)
  62. dumpinventory()
  63. print("Turtle needs more fuel. Refill and press ENTER to continue.")
  64. io.read()
  65. refuel()
  66. gotoz(rz)
  67. gotoy(ry)
  68. gotox(rx)
  69. face(rdirection)
  70. end
  71.  
  72. function digoutblock(isup, isdown)
  73. if isup and turtle.detectUp() and (not turtle.compareUp())
  74. then turtle.digUp() end
  75. if isdown and turtle.detectDown() and (not turtle.compareDown())
  76. then turtle.digDown() end
  77. if turtle.getItemCount(16) > 0 then dumpores() end
  78. if turtle.getFuelLevel() < 100 then waitforfuel() end
  79. end
  80.  
  81. function turn(isright)
  82. if isright then
  83. turtle.turnRight()
  84. direction = direction + 1
  85. if direction > 4 then direction = 1 end
  86. else
  87. turtle.turnLeft()
  88. direction = direction - 1
  89. if direction < 1 then direction = 4 end
  90. end
  91. end
  92.  
  93. function digoutrow(x, isup, isdown)
  94. digoutblock(isup, isdown)
  95. if x > 1 then digforward(1) digoutrow((x - 1), isup, isdown) end
  96. end
  97.  
  98. function digoutlayer(x, y, isup, isdown, isright)
  99. digoutrow(x, isup, isdown)
  100. if y > 1 then
  101. turn(isright)
  102. digforward(1)
  103. turn(isright)
  104. digoutlayer(x, (y - 1), isup, isdown, (not isright))
  105. else turnright(2) end
  106. end
  107.  
  108. function iseven(n)
  109. if math.fmod(n, 2) == 0 then return true
  110. else return false end
  111. end
  112.  
  113. function getright(y, isright)
  114. if iseven(y) then return not isright
  115. else return isright end
  116. end
  117.  
  118. function face(n)
  119. if direction == n then return end
  120. turnright(1)
  121. face(n)
  122. end
  123.  
  124. function gotox(x)
  125. if gx > x then face(3)
  126. elseif gx < x then face(1)
  127. elseif gx == x then return end
  128. digforward(1)
  129. gotox(x)
  130. end
  131.  
  132. function gotoy(y)
  133. if gy > y then face(4)
  134. elseif gy < y then face(2)
  135. elseif gy == y then return end
  136. digforward(1)
  137. gotoy(y)
  138. end
  139.  
  140. function gotoz(z)
  141. if gz > z then moveup(1) gotoz(z) end
  142. if gz < z then movedown(1) gotoz(z) end
  143. end
  144.  
  145. function gohome()
  146. gotox(0)
  147. gotoy(0)
  148. gotoz(0)
  149. face(1)
  150. end
  151.  
  152. function excavate(x, y, z, isright)
  153.  
  154. print("Excacating "..x.." "..y.." "..z)
  155. if z >= 3 then
  156. movedown(1)
  157. digoutlayer(x, y, true, true, isright)
  158. end
  159.  
  160. if z > 3 then
  161. movedown(2)
  162. excavate(x, y, (z - 3), getright(y, isright))
  163. end
  164.  
  165. if z == 2 then digoutlayer(x, y, false, true, isright) end
  166. if z == 1 then digoutlayer(x, y, false, false, isright) end
  167. if z < 4 then gohome() end
  168. end
  169.  
  170. function refuel()
  171. for i = 1, 16 do
  172. turtle.select(i)
  173. turtle.refuel()
  174. end
  175. turtle.select(1)
  176. end
  177.  
  178.  
  179.  
  180. refuel()
  181.  
  182. turtle.select(1)
  183. gx = 0
  184. gy = 0
  185. gz = 0
  186. direction = 1
  187. excavate(30, 3, 50, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement