Advertisement
Guest User

dig.lua

a guest
Oct 5th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. local robot = component.proxy(component.list("robot"))
  2. local sides = require("sides")
  3.  
  4. local size = 16
  5.  
  6. local r = component.robot
  7. --local x, y, z, f = 0, 0, 0, 0
  8. local x, y, z, f = 0
  9. local dropping = false
  10. local delta = {[0] = function() x = x + 1 end, [1] = function() y = y + 1 end,
  11.                [2] = function() x = x - 1 end, [3] = function() y = y - 1 end}
  12.  
  13. local function turnRight()
  14.   robot.turnRight()
  15.   f = (f + 1) % 4
  16. end
  17.  
  18. local function turnLeft()
  19.   robot.turnLeft()
  20.   f = (f - 1) % 4
  21. end
  22.  
  23. local function turnTowards(side)
  24.   if f == side - 1 then
  25.     turnRight()
  26.   else
  27.     while f ~= side do
  28.       turnLeft()
  29.     end
  30.   end
  31. end
  32.  
  33. local checkedDrop
  34.  
  35. local function clearBlock(side, cannotRetry)
  36.   while r.suck(side) do
  37.     checkedDrop()
  38.   end
  39.   local result, reason = r.swing(side)
  40.   if result then
  41.     checkedDrop()
  42.   else
  43.     local _, what = r.detect(side)
  44.     if cannotRetry and what ~= "air" and what ~= "entity" then
  45.       return false
  46.     end
  47.   end
  48.   return true
  49. end
  50.  
  51. local function tryMove(side)
  52.   side = side or sides.forward
  53.   local tries = 10
  54.   while not r.move(side) do
  55.     tries = tries - 1
  56.     if not clearBlock(side, tries < 1) then
  57.       return false
  58.     end
  59.   end
  60.   if side == sides.down then
  61.     z = z + 1
  62.   elseif side == sides.up then
  63.     z = z - 1
  64.   else
  65.     delta[f]()
  66.   end
  67.   return true
  68. end
  69.  
  70. local function moveTo(tx, ty, tz, backwards)
  71.   local axes = {
  72.     function()
  73.       while z > tz do
  74.         tryMove(sides.up)
  75.       end
  76.       while z < tz do
  77.         tryMove(sides.down)
  78.       end
  79.     end,
  80.     function()
  81.       if y > ty then
  82.         turnTowards(3)
  83.         repeat tryMove() until y == ty
  84.       elseif y < ty then
  85.         turnTowards(1)
  86.         repeat tryMove() until y == ty
  87.       end
  88.     end,
  89.     function()
  90.       if x > tx then
  91.         turnTowards(2)
  92.         repeat tryMove() until x == tx
  93.       elseif x < tx then
  94.         turnTowards(0)
  95.         repeat tryMove() until x == tx
  96.       end
  97.     end
  98.   }
  99.   if backwards then
  100.     for axis = 3, 1, -1 do
  101.       axes[axis]()
  102.     end
  103.   else
  104.     for axis = 1, 3 do
  105.       axes[axis]()
  106.     end
  107.   end
  108. end
  109.  
  110. function checkedDrop(force)
  111.   local empty = 0
  112.   for slot = 1, 16 do
  113.     if robot.count(slot) == 0 then
  114.       empty = empty + 1
  115.     end
  116.   end
  117.   if not dropping and empty == 0 or force and empty < 16 then
  118.     local ox, oy, oz, of = x, y, z, f
  119.     dropping = true
  120.     moveTo(0, 0, 0)
  121.     turnTowards(2)
  122.  
  123.     for slot = 1, 16 do
  124.       if robot.count(slot) > 0 then
  125.         robot.select(slot)
  126.         local wait = 1
  127.         repeat
  128.           if not robot.drop() then
  129.             os.sleep(wait)
  130.             wait = math.min(10, wait + 1)
  131.           end
  132.         until robot.count(slot) == 0
  133.       end
  134.     end
  135.     robot.select(1)
  136.  
  137.     dropping = false
  138.     moveTo(ox, oy, oz, true)
  139.     turnTowards(of)
  140.   end
  141. end
  142.  
  143. local function step()
  144.   clearBlock(sides.down)
  145.   if not tryMove() then
  146.     return false
  147.   end
  148.   clearBlock(sides.up)
  149.   return true
  150. end
  151.  
  152. local function turn(i)
  153.   if i % 2 == 1 then
  154.     turnRight()
  155.   else
  156.     turnLeft()
  157.   end
  158. end
  159.  
  160. local function digLayer()
  161.   for i = 1, size do
  162.     for j = 1, size - 1 do
  163.       if not step() then
  164.         return false
  165.       end
  166.     end
  167.     if i < size then
  168.       turn(i)
  169.       if not step() then
  170.         return false
  171.       end
  172.       turn(i)
  173.     else
  174.       turnRight()
  175.       if size % 2 == 1 then
  176.         turnRight()
  177.       end
  178.       for i = 1, 3 do
  179.         if not tryMove(sides.down) then
  180.           return false
  181.         end
  182.       end
  183.     end
  184.   end
  185.   return true
  186. end
  187.  
  188. repeat until not digLayer()
  189. moveTo(0, 0, 0)
  190. turnTowards(0)
  191. checkedDrop(true)
  192.  
  193. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement