Octagonal_Square

quarry

Mar 23rd, 2021 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.08 KB | None | 0 0
  1. os.loadAPI("inv")
  2. os.loadAPI("t")
  3.  
  4. local x = 0
  5. local y = 0
  6. local z = 0
  7. local lastx = 0
  8. local lasty = 0
  9. local lastz = 0
  10. local first = true
  11. local max = 32
  12. local deep = 64
  13. local facingfw = true
  14. local avoidBlocks = {}
  15. local COAL = "coal"
  16. local CHAR = "char"
  17.  
  18. local OK = 0
  19. local ERROR = 1
  20. local LAYERCOMPLETE = 2
  21. local OUTOFFUEL = 3
  22. local FULLINV = 4
  23. local BLOCKEDMOV = 5
  24. local USRINTERRUPT = 6
  25.  
  26. local CHARCOALONLY = false
  27. local USEMODEM = false
  28.  
  29.  
  30. -- Arguments
  31. local tArgs = {...}
  32. for i=1,#tArgs do
  33.     local arg = tArgs[i]
  34.     if string.find(arg, "-") == 1 then
  35.         for c=2,string.len(arg) do
  36.             local ch = string.sub(arg,c,c)
  37.             if ch == 'c' then
  38.                 CHARCOALONLY = true
  39.             elseif ch == 'm' then
  40.                 USEMODEM = true
  41.             else
  42.                 write("Invalid flag '")
  43.                 write(ch)
  44.                 print("'")
  45.             end
  46.         end
  47.     end
  48. end
  49.  
  50.  
  51. function out(s)
  52.  
  53.     s2 = s .. " @ [" .. x .. ", " .. y .. ", " .. z .. "]"
  54.            
  55.     print(s2)
  56.     if USEMODEM then
  57.         rednet.broadcast(s2, "miningTurtle")
  58.     end  
  59. end
  60.  
  61. function dropInChest()
  62.     turtle.turnLeft()
  63.    
  64.     local success, data = turtle.inspect()
  65.     out(data.name)
  66.     if success then
  67.       if string.find(data.name,"chest") then   
  68.             out("Dropping items in chest")
  69.            
  70.             for i=1, 16 do
  71.                 turtle.select(i)
  72.                
  73.                 data = turtle.getItemDetail()
  74.                
  75.                 if data ~= nil and
  76.         (data.name == "minecraft:coal" or data.name == "minecraft:charcoal") == false and
  77.                     (data.damage == nil or data.name ~= "minecraft:coal" or data.name ~= "minecraft:charcoal") then
  78.         turtle.drop()
  79.     end
  80.             end
  81.   end
  82.     end
  83.    
  84.     turtle.turnRight()
  85.    
  86. end
  87.  
  88. function goDown()
  89.     while true do
  90.         if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  91.             if not refuel() then
  92.                 return OUTOFFUEL
  93.             end
  94.         end
  95.    
  96.         if not turtle.down() then
  97.             turtle.up()
  98.             z = z+1
  99.             return
  100.         end
  101.         z = z-1
  102.     end
  103. end
  104.  
  105. function fuelNeededToGoBack()
  106.     return -z + x + y + 2
  107. end
  108.  
  109. function refuel()
  110.     for i=1, 16 do
  111.         -- Only run on Charcoal
  112.         turtle.select(i)
  113.        
  114.         item = turtle.getItemDetail()
  115.         if item and string.find(item.name,"coal") and turtle.refuel(1) then
  116.             return true
  117.         end
  118.     end
  119.    
  120.     return false
  121. end
  122.  
  123. function moveH()
  124.     if inv.isInventoryFull() then
  125.         out("Dropping thrash")
  126.         inv.dropThrash()
  127.        
  128.         if inv.isInventoryFull() then
  129.             out ("Stacking items")
  130.             inv.stackItems()
  131.         end
  132.        
  133.         if inv.isInventoryFull() then
  134.             out("Full inventory!")
  135.             return FULLINV  
  136.         end
  137.     end
  138.    
  139.     if turtle.getFuelLevel() <= fuelNeededToGoBack() then
  140.         if not refuel() then
  141.             out("Out of fuel!")
  142.             return OUTOFFUEL
  143.         end
  144.     end
  145.    
  146.     local sd, bd = turtle.inspectDown()
  147.    
  148.     if sd then
  149.    
  150.         if has_value(avoidBlocks, bd.name) then
  151.        
  152.             out("Hit bad block, remove manually, then restart")
  153.             return BLOCKEDMOV
  154.            
  155.         end
  156.     end
  157.  
  158.  
  159.     local su, bu = turtle.inspectUp()
  160.    
  161.     if su then
  162.    
  163.         if has_value(avoidBlocks, bu.name) then
  164.        
  165.             out("Hit bad block, remove manually, then restart")
  166.             return BLOCKEDMOV
  167.            
  168.         end
  169.     end
  170.  
  171.     local sf, bf = turtle.inspect()
  172.    
  173.     if sf then
  174.    
  175.         if has_value(avoidBlocks, bf.name) then
  176.        
  177.             out("Hit bad block, remove manually, then restart")
  178.             return BLOCKEDMOV
  179.            
  180.         end
  181.     end
  182.    
  183.     if facingfw and y<max-1 then
  184.     -- Going one way
  185.         local dugFw = t.dig()
  186.         if dugFw == false then
  187.             out("Hit bedrock, can't keep going")
  188.             return BLOCKEDMOV
  189.         end
  190.         t.digUp()
  191.         t.digDown()
  192.    
  193.         if t.fw() == false then
  194.             return BLOCKEDMOV
  195.         end
  196.        
  197.         y = y+1
  198.    
  199.     elseif not facingfw and y>0 then
  200.     -- Going the other way
  201.         t.dig()
  202.         t.digUp()
  203.         t.digDown()
  204.        
  205.         if t.fw() == false then
  206.             return BLOCKEDMOV
  207.         end
  208.        
  209.         y = y-1
  210.        
  211.     else
  212.         if x+1 >= max then
  213.             t.digUp()
  214.             t.digDown()
  215.             return LAYERCOMPLETE -- Done with this Y level
  216.         end
  217.        
  218.         -- If not done, turn around
  219.         if facingfw then
  220.             turtle.turnRight()
  221.         else
  222.             turtle.turnLeft()
  223.         end
  224.        
  225.         t.dig()
  226.         t.digUp()
  227.         t.digDown()
  228.        
  229.         if t.fw() == false then
  230.             return BLOCKEDMOV
  231.         end
  232.        
  233.         x = x+1
  234.        
  235.         if facingfw then
  236.             turtle.turnRight()
  237.         else
  238.             turtle.turnLeft()
  239.         end
  240.        
  241.         facingfw = not facingfw
  242.     end
  243.    
  244.     return OK
  245. end
  246.  
  247. function digLayer()
  248.    
  249.     local errorcode = OK
  250.  
  251.     while errorcode == OK do
  252.         if USEMODEM then
  253.             local msg = rednet.receive(1)
  254.             if msg ~= nil and string.find(msg, "return") ~= nil then
  255.                 return USRINTERRUPT
  256.             end
  257.         end
  258.         errorcode = moveH()
  259.     end
  260.    
  261.     if errorcode == LAYERCOMPLETE then
  262.         return OK
  263.     end
  264.    
  265.     return errorcode  
  266. end
  267.  
  268. function goToOrigin(up)
  269.    
  270.     if facingfw then
  271.        
  272.         turtle.turnLeft()
  273.        
  274.         t.fw(x)
  275.        
  276.         turtle.turnLeft()
  277.        
  278.         t.fw(y)
  279.        
  280.         turtle.turnRight()
  281.         turtle.turnRight()
  282.        
  283.     else
  284.        
  285.         turtle.turnRight()
  286.        
  287.         t.fw(x)
  288.        
  289.         turtle.turnLeft()
  290.        
  291.         t.fw(y)
  292.        
  293.         turtle.turnRight()
  294.         turtle.turnRight()
  295.        
  296.     end
  297.    
  298.     x = 0
  299.     y = 0
  300.     facingfw = true
  301.  
  302.     if up then
  303.         goUp()
  304.     end
  305.    
  306. end
  307.  
  308. function has_value(tab, val)
  309.     for index, value in ipairs(tab) do
  310.         if value == val then
  311.             return true
  312.         end
  313.     end
  314.  
  315.     return false
  316. end
  317.  
  318. function goUp()
  319.  
  320.     while z < 0 do
  321.        
  322.         t.up()
  323.        
  324.         z = z+1
  325.        
  326.     end
  327.    
  328. end
  329.  
  330. function returnToLast()
  331.  
  332.     t.down(lastz)
  333.        
  334.     t.fw(lasty)
  335.    
  336.     turtle.turnRight()
  337.  
  338.     t.fw(lastx)
  339.  
  340.     if lastx % 2 == 1 then
  341.         turtle.turnRight()
  342.     else
  343.         turtle.turnLeft()
  344.     end
  345.    
  346.     x = lastx
  347.     y = lasty
  348.     z = -lastz
  349.     facingfw = true
  350.    
  351. end
  352.  
  353. function mainloop()
  354.  
  355.     while true do
  356.  
  357.         local errorcode = digLayer()
  358.    
  359.         if errorcode ~= OK then
  360.             lastx = x
  361.             lasty = y
  362.             lastz = -z
  363.             goToOrigin(true)
  364.             return errorcode
  365.         end
  366.        
  367.         goToOrigin(false)
  368.        
  369.         for i=1, 3 do
  370.             t.digDown()
  371.             success = t.down()
  372.        
  373.             if not success then
  374.                 lastx = x
  375.                 lasty = y
  376.                 lastz = -z
  377.                 goToOrigin(true)
  378.                 return BLOCKEDMOV
  379.             end
  380.  
  381.             z = z-1
  382.             out("Z: " .. z)
  383.  
  384.         end
  385.     end
  386. end
  387.  
  388. if USEMODEM then
  389.     rednet.open("right")
  390. end
  391.  
  392. out("\n\n\n-- WELCOME TO THE MINING TURTLE --\n\n")
  393.  
  394. while true do
  395.     if first then
  396.         goDown()
  397.         first = false
  398.     else
  399.         returnToLast()
  400.     end
  401.  
  402.     local errorcode = mainloop()
  403.     dropInChest()
  404.    
  405.     if errorcode ~= FULLINV then
  406.         break
  407.     end
  408. end
  409.  
  410. if USEMODEM then
  411.     rednet.close("right")
  412. end
Advertisement
Add Comment
Please, Sign In to add comment