kLeeNex24

fill

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