Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local robot = require("robot")
  4. local shell = require("shell")
  5. local sides = require("sides")
  6.  
  7. if not component.isAvailable("robot") then
  8. io.stderr:write("can only run on robots")
  9. return
  10. end
  11.  
  12. local args, options = shell.parse(...)
  13. if #args < 1 then
  14. io.write("Usage: dig [-s] \n")
  15. io.write(" -s: shutdown when done.")
  16. return
  17. end
  18.  
  19. local size = tonumber(args[1])
  20. local maxDeph = tonumber(args[2])
  21. local startDeph = tonumber(args[3])
  22. if not size then
  23. io.stderr:write("invalid size")
  24. return
  25. end
  26.  
  27. if not maxDeph then
  28. io.stderr:write("invalid max deph")
  29. return
  30. end
  31.  
  32. if not startDeph then
  33. io.stderr:write("invalid start deph")
  34. return
  35. end
  36.  
  37. local r = component.robot
  38. local x, y, z, f = 0, 0, 0, 0
  39. local dropping = false -- avoid recursing into drop()
  40. local delta = {[0] = function() x = x + 1 end, [1] = function() y = y + 1 end,
  41. [2] = function() x = x - 1 end, [3] = function() y = y - 1 end}
  42.  
  43. local function turnRight()
  44. robot.turnRight()
  45. f = (f + 1) % 4
  46. end
  47.  
  48. local function turnLeft()
  49. robot.turnLeft()
  50. f = (f - 1) % 4
  51. end
  52.  
  53. local function turnTowards(side)
  54. if f == side - 1 then
  55. turnRight()
  56. else
  57. while f ~= side do
  58. turnLeft()
  59. end
  60. end
  61. end
  62.  
  63. local checkedDrop -- forward declaration
  64.  
  65. local function clearBlock(side, cannotRetry)
  66. while r.suck(side) do
  67. checkedDrop()
  68. end
  69. local result, reason = r.swing(side)
  70. if result then
  71. checkedDrop()
  72. else
  73. local _, what = r.detect(side)
  74. if cannotRetry and what ~= "air" and what ~= "entity" then
  75. return false
  76. end
  77. end
  78. return true
  79. end
  80.  
  81. local function tryMove(side)
  82. side = side or sides.forward
  83. local tries = 10
  84. while not r.move(side) do
  85. tries = tries - 1
  86. if not clearBlock(side, tries < 1) then
  87. return false
  88. end
  89. end
  90. if side == sides.down then
  91. z = z + 1
  92. elseif side == sides.up then
  93. z = z - 1
  94. else
  95. delta[f]()
  96. end
  97. return true
  98. end
  99.  
  100. local function moveTo(tx, ty, tz, backwards)
  101. local axes = {
  102. function()
  103. while z > tz do
  104. tryMove(sides.up)
  105. end
  106. while z < tz do
  107. tryMove(sides.down)
  108. end
  109. end,
  110. function()
  111. if y > ty then
  112. turnTowards(3)
  113. repeat tryMove() until y == ty
  114. elseif y < ty then
  115. turnTowards(1)
  116. repeat tryMove() until y == ty
  117. end
  118. end,
  119. function()
  120. if x > tx then
  121. turnTowards(2)
  122. repeat tryMove() until x == tx
  123. elseif x < tx then
  124. turnTowards(0)
  125. repeat tryMove() until x == tx
  126. end
  127. end
  128. }
  129. if backwards then
  130. for axis = 3, 1, -1 do
  131. axes[axis]()
  132. end
  133. else
  134. for axis = 1, 3 do
  135. axes[axis]()
  136. end
  137. end
  138. end
  139.  
  140. function checkedDrop(force)
  141. local empty = 0
  142. for slot = 1, 16 do
  143. if robot.count(slot) == 0 then
  144. empty = empty + 1
  145. end
  146. end
  147. if not dropping and empty == 0 or force and empty < 16 then
  148. local ox, oy, oz, of = x, y, z, f
  149. dropping = true
  150. moveTo(0, 0, 0)
  151. turnTowards(2)
  152.  
  153. for slot = 1, 16 do
  154. if robot.count(slot) > 0 then
  155. robot.select(slot)
  156. local wait = 1
  157. repeat
  158. if not robot.drop() then
  159. os.sleep(wait)
  160. wait = math.min(10, wait + 1)
  161. end
  162. until robot.count(slot) == 0
  163. end
  164. end
  165. robot.select(1)
  166.  
  167. dropping = false
  168. moveTo(ox, oy, oz, true)
  169. turnTowards(of)
  170. end
  171. end
  172.  
  173. local function step()
  174. clearBlock(sides.down)
  175. if not tryMove() then
  176. return false
  177. end
  178. clearBlock(sides.up)
  179. return true
  180. end
  181.  
  182. local function turn(i)
  183. if i % 2 == 1 then
  184. turnRight()
  185. else
  186. turnLeft()
  187. end
  188. end
  189.  
  190. local function digLayer()
  191. --[[ We move in zig-zag lines, clearing three layers at a time. This means we
  192. have to differentiate at the end of the last line between even and odd
  193. sizes on which way to face for the next layer:
  194. For either size we rotate once to the right. For even sizes this will
  195. cause the next layer to be dug out rotated by ninety degrees. For odd
  196. ones the return path is symmetrical, meaning we just turn around.
  197.  
  198. Examples for two layers:
  199.  
  200. s--x--x e--x--x s--x--x--x x--x x--x
  201. | | | | | | |
  202. x--x--x -> x--x--x x--x--x--x x x x x
  203. | | | -> | | | |
  204. x--x--e x--x--s x--x--x--x x x x x
  205. | | | | |
  206. e--x--x--x s x--x e
  207.  
  208. Legend: s = start, x = a position, e = end, - = a move
  209. ]]
  210. for i = 1, size do
  211. for j = 1, size - 1 do
  212. if not step() then
  213. return false
  214. end
  215. end
  216. if i < size then
  217. -- End of a normal line, move the "cap".
  218. turn(i)
  219. if not step() then
  220. return false
  221. end
  222. turn(i)
  223. else
  224. turnRight()
  225. if size % 2 == 1 then
  226. turnRight()
  227. end
  228. for i = 1, 3 do
  229. if not tryMove(sides.down) or z >= maxDeph then
  230. return false
  231. end
  232. end
  233. end
  234. end
  235. return true
  236. end
  237.  
  238. io.stderr:write(tostring(z) .. " / " .. tostring(startDeph))
  239.  
  240. while z > startDeph do
  241. tryMove(sides.down)
  242. end
  243.  
  244. repeat until not digLayer()
  245. moveTo(0, 0, 0, true)
  246. turnTowards(0)
  247. checkedDrop(true)
  248.  
  249. if options.s then
  250. computer.shutdown()
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement