geek_in_crusade

mining program

Apr 11th, 2021 (edited)
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. dist_param = 256
  2. step = 0
  3.  
  4.  
  5. --Movements custom functions
  6.  
  7. function dig()
  8.     while turtle.detect() do
  9.         turtle.dig()
  10.     end
  11. end
  12.  
  13.  
  14. --utility functions
  15.  
  16. function tryRefuel()
  17.     turtle.select(1)
  18.     fcount = turtle.getItemCount()
  19.     if fcount > 1 then
  20.         turtle.refuel(fcount-1)
  21.     end
  22. end
  23.  
  24.  
  25. function homing_or_resume()
  26.     turtle.turnLeft()
  27.     turtle.turnLeft()
  28.     for i=1,step do
  29.         turtle.forward()
  30.     end
  31. end
  32.  
  33. function unload()
  34.     for i=4,16 do
  35.         turtle.select(i)
  36.         turtle.drop()
  37.     end
  38. end
  39.  
  40.  
  41. function diag()
  42.     if turtle.getFuelLevel() < 1000 then
  43.         tryRefuel()
  44.     end
  45.     if turtle.getFuelLevel() < dist_param+20 or turtle.getItemCount(2) < 2 then
  46.         homing_or_resume()
  47.         turtle.select(1)
  48.         turtle.suckUp(turtle.getItemSpace())
  49.         turtle.select(2)
  50.         turtle.suckUp(turtle.getItemSpace())
  51.         tryRefuel()
  52.         unload()
  53.         homing_or_resume()
  54.     end
  55.     if turtle.getItemCount(16) > 0 then
  56.         homing_or_resume()
  57.         unload()
  58.         homing_or_resume()
  59.     end
  60. end
  61.  
  62.  
  63. function follow()
  64.  
  65.     for i=1,4 do
  66.         turtle.turnRight()
  67.         _,id = turtle.inspect()
  68.         if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  69.             dig()
  70.             turtle.forward()
  71.             follow()
  72.             turtle.back()
  73.         end
  74.     end
  75.  
  76.     _,id = turtle.inspectUp()
  77.     if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  78.         turtle.digUp()
  79.         turtle.up()
  80.         follow()
  81.         turtle.down()
  82.     end
  83.  
  84.     _,id = turtle.inspectDown()
  85.     if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  86.         turtle.digDown()
  87.         turtle.down()
  88.         follow()
  89.         turtle.up()
  90.     end
  91. end
  92.  
  93. --Scan functions
  94. function scan()
  95.     for j=1,4 do
  96.         _,id = turtle.inspect()
  97.         if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  98.             dig()
  99.             turtle.forward()
  100.             follow()
  101.             turtle.back()
  102.         end
  103.         turtle.turnRight()
  104.     end
  105. end
  106.  
  107. function scanDown()
  108.     _,id = turtle.inspectDown()
  109.     if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  110.         turtle.digDown()
  111.         turtle.down()
  112.         follow()
  113.         turtle.up()
  114.     end
  115. end
  116.  
  117. function scanUp()
  118.     _,id = turtle.inspectUp()
  119.     if id ~= "No block to inspect" and string.find(id["name"],"ore") ~= nil then
  120.         turtle.digUp()
  121.         turtle.up()
  122.         follow()
  123.         turtle.down()
  124.     end
  125. end
  126.  
  127.  
  128. --main function
  129.  
  130. function mining(dist)
  131.     for i=1,dist do
  132.         step = step+1
  133.  
  134.         if turtle.detect() then
  135.             dig()
  136.             turtle.forward()
  137.             scanDown()
  138.             scan()
  139.             turtle.digUp()
  140.             turtle.up()
  141.             scan()
  142.             scanUp()
  143.             turtle.down()
  144.         else
  145.             turtle.forward()
  146.             turtle.digUp()
  147.         end
  148.  
  149.         if not turtle.detectDown() then
  150.             turtle.select(3)
  151.             turtle.placeDown()
  152.         end
  153.         if i%8 == 0 then
  154.             turtle.select(2)
  155.             turtle.back()
  156.             turtle.placeUp()
  157.             turtle.forward()
  158.         end
  159.         diag()
  160.     end
  161. end
  162.  
  163. mining(dist_param)
Add Comment
Please, Sign In to add comment