Zoidbergie

Untitled

Oct 31st, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. -- Helper function for looping over a range
  2. function range(limit)
  3. local function seq(state, x)
  4. if (x >= limit) then
  5. return nil
  6. else
  7. return x + 1
  8. end
  9. end
  10. return seq, nil, 0
  11. end
  12.  
  13. -- Cover an area
  14. function coverArea(w)
  15. local X,Y = w.size.x,w.size.y
  16.  
  17. -- Prepare for first row
  18. w.forward()
  19. w.right()
  20.  
  21. -- Loop over each row
  22. for y in range(Y) do
  23. -- Loop over each column
  24. for x in range(X) do
  25. -- Do the callback for each point
  26. w.callback()
  27.  
  28. -- Unless we have completed the row
  29. if x ~= X then
  30. -- Move forward
  31. w.forward()
  32. end
  33. end
  34.  
  35. -- Unless we have completed all rows
  36. if y ~= Y then
  37. -- Move to next row
  38. if y%2 == 1 then
  39. w.left()
  40. w.forward()
  41. w.left()
  42. else
  43. w.right()
  44. w.forward()
  45. w.right()
  46. end
  47. end
  48. end
  49.  
  50. -- Move back to starting point
  51. if Y%2 == 1 then
  52. w.left()
  53. w.left()
  54. for x in range(X-1) do w.forward() end
  55. end
  56. w.left()
  57. for y in range(Y) do w.forward() end
  58. w.right()
  59. w.right()
  60. end
  61.  
  62. -- Persistent forward function
  63. function forward()
  64. while not turtle.forward() do
  65. sleep(0.5)
  66. turtle.dig()
  67. end
  68. end
  69.  
  70. -- Level wherever the turtle is
  71. function level()
  72. -- How far down we have gone
  73. local depth = 0
  74.  
  75. -- Make sure we have an empty slot for picking up junk
  76. -- This is an attempt to not mess up the convention by
  77. -- the blocks we dig that are picked up. Not 100%
  78. -- reliable still...
  79. turtle.select(9)
  80. turtle.drop()
  81.  
  82. -- Move down to first block we can find
  83. while not turtle.detectDown() and turtle.down() do
  84. depth = depth + 1
  85. end
  86.  
  87. -- Remove top block
  88. turtle.digDown()
  89. turtle.down()
  90. depth = depth + 1
  91.  
  92. -- Dig down to first non-dirt block we can find
  93. turtle.select(1)
  94. while (turtle.compareDown() and turtle.digDown() and turtle.down()) or turtle.down() do
  95. depth = depth + 1
  96. end
  97.  
  98. -- Start filling
  99. while depth > 0 do
  100. -- Until we move up 1 successfully
  101. while not turtle.up() do
  102. -- Try dig upwards and wait a bit
  103. turtle.digUp()
  104. sleep(0.5)
  105. end
  106. depth = depth - 1
  107.  
  108. -- As long as we have something to place
  109. if selectSource(depth) > 0 then
  110. -- Place it below us
  111. turtle.placeDown()
  112. end
  113. end
  114. end
  115.  
  116. -- Returns the count of an appropriate slot
  117. function selectSource(depth)
  118. -- If we're at the top
  119. if depth==1 then
  120. -- Select slot 1 and return its count
  121. turtle.select(1)
  122. return turtle.getItemCount(1)
  123. -- Otherwise
  124. else
  125. -- Move through the rest of the slots
  126. for i=2,9 do
  127. turtle.select(i)
  128. local count = turtle.getItemCount(i)
  129. -- Until we find one with something in it
  130. if count > 0 then
  131. return count
  132. end
  133. end
  134. end
  135. return 0
  136. end
  137.  
  138. -- Program starter
  139. local arg = {...}
  140. if #arg == 2 then
  141. coverArea({
  142. size = {
  143. x = tonumber(arg[1]),
  144. y = tonumber(arg[2]),
  145. },
  146. forward = forward,
  147. left = turtle.turnLeft,
  148. right = turtle.turnRight,
  149. callback = level,
  150. })
  151. else
  152. print("Usage: level x y")
  153. end
Add Comment
Please, Sign In to add comment