Advertisement
montana_1

Recursive Vein Mining

Oct 27th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. -- Function to check if turtle has sufficient fuel --
  2.  
  3. function sufficientFuel(vector1,vector2,fuel,remainder)
  4.     local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z)
  5.     if(fuel <= distance + remainder) then
  6.         return false
  7.     else
  8.         return true
  9.     end
  10. end
  11.  
  12.  
  13. -- Function to refuel turtle appropriately --
  14.  
  15. function consumeFuel(maxFuel)
  16.     local refuel = false
  17.     for i = 1, 16 do
  18.         turtle.select(i)
  19.         local x = turtle.getItemDetail(i)
  20.         if(x ~= nil) then
  21.             local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  22.             print(istr,"    t")
  23.             if(istr == "planks" or istr == "stick" or istr == "log") then
  24.                 turtle.refuel()
  25.                 refuel = true
  26.             end
  27.             if(turtle.getFuelLevel() < maxFuel and istr == "coal") then
  28.                 turtle.refuel(1)
  29.                 refuel = true
  30.             end
  31.         end
  32.     end
  33.     turtle.select(1)
  34.     print(turtle.getFuelLevel())
  35.     return refuel
  36. end
  37.  
  38.  
  39. -- Function to check for inv space --
  40.  
  41. function invSpace()
  42.     for i = 1, 16 do
  43.         turtle.select(i)
  44.         local x = turtle.getItemDetail(i)
  45.         if(x == nil or turtle.compare()) then
  46.             turtle.select(1)
  47.             return true
  48.         end
  49.     end
  50.     turtle.select(1)
  51.     return false
  52. end
  53.  
  54.  
  55. -- Function to sort inventory --
  56.  
  57. function sortInv()
  58.     for i = 1, 16 do -- loop through the slots
  59.         turtle.select(i)
  60.         if(turtle.getItemDetail(i) ~= nil) then
  61.             for c = i, 16 do
  62.                 if(turtle.getItemDetail(c) ~= nil) then
  63.                     if(turtle.compareTo(c)) then
  64.                         turtle.select(c)
  65.                         turtle.transferTo(i)
  66.                         turtle.select(i)
  67.                     end
  68.                 end
  69.             end
  70.         end
  71.     end
  72.     turtle.select(1)
  73. end
  74.  
  75.  
  76. function mineVein(start,moves,back)
  77.     local current = vector.new(gps.locate())
  78.     if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
  79.         if(not consumeFuel(400)) then
  80.             return false
  81.         end
  82.     end
  83.     if(not invSpace()) then
  84.         sortInv()
  85.         if(not invSpace()) then
  86.             return false
  87.         end
  88.     end
  89.    
  90.    
  91.     local success,data = turtle.inspect()
  92.     if(success) then
  93.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  94.         print(istr)
  95.         if(istr == "iron_ore") then
  96.             turtle.dig()
  97.             turtle.forward()
  98.             mineVein(start,moves+1,false)
  99.             turtle.back()
  100.         end
  101.     end
  102.     if(moves < 1) then
  103.         return true
  104.     end
  105.     turtle.turnLeft()
  106.     local success,data = turtle.inspect()
  107.     if(success) then
  108.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  109.         print(istr)
  110.         if(istr == "iron_ore") then
  111.             turtle.dig()
  112.             turtle.forward()
  113.             mineVein(start,moves+1,false)
  114.             turtle.back()
  115.         end
  116.     end
  117.     if(back) then
  118.         turtle.turnLeft()
  119.         local success,data = turtle.inspect()
  120.         if(success) then
  121.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  122.             print(istr)
  123.             if(istr == "iron_ore") then
  124.                 turtle.dig()
  125.                 turtle.forward()
  126.                 mineVein(start,moves+1,false)
  127.                 turtle.back()
  128.             end
  129.         end
  130.         turtle.turnLeft()
  131.         local success,data = turtle.inspect()
  132.         if(success) then
  133.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  134.             print(istr)
  135.             if(istr == "iron_ore") then
  136.                 turtle.dig()
  137.                 turtle.forward()
  138.                 mineVein(start,moves+1,false)
  139.                 turtle.back()
  140.             end
  141.         end
  142.         turtle.turnLeft()
  143.         local success,data = turtle.inspectUp()
  144.         if(success) then
  145.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  146.             print(istr)
  147.             if(istr == "iron_ore") then
  148.                 turtle.digUp()
  149.                 turtle.up()
  150.                 mineVein(start,moves+1,true)
  151.                 turtle.down()
  152.             end
  153.         end
  154.         --
  155.         local success,data = turtle.inspectDown()
  156.         if(success) then
  157.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  158.             print(istr)
  159.             if(istr == "iron_ore") then
  160.                 turtle.digDown()
  161.                 turtle.down()
  162.                 mineVein(start,moves+1,true)
  163.                 turtle.up()
  164.             end
  165.         end
  166.         --
  167.     else
  168.         turtle.turnRight()
  169.         turtle.turnRight()
  170.         local success,data = turtle.inspect()
  171.         if(success) then
  172.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  173.             print(istr)
  174.             if(istr == "iron_ore") then
  175.                 turtle.dig()
  176.                 turtle.forward()
  177.                 mineVein(start,moves+1,false)
  178.                 turtle.back()
  179.             end
  180.         end
  181.         turtle.turnLeft()
  182.         local success,data = turtle.inspectUp()
  183.         if(success) then
  184.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  185.             print(istr)
  186.             if(istr == "iron_ore") then
  187.                 turtle.digUp()
  188.                 turtle.up()
  189.                 mineVein(start,moves+1,true)
  190.                 turtle.down()
  191.             end
  192.         end
  193.         --
  194.         local success,data = turtle.inspectDown()
  195.         if(success) then
  196.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  197.             print(istr)
  198.             if(istr == "iron_ore") then
  199.                 turtle.digDown()
  200.                 turtle.down()
  201.                 mineVein(start,moves+1,true)
  202.                 turtle.up()
  203.             end
  204.         end
  205.         --
  206.     end
  207.     return true
  208. end
  209.  
  210. gpsData = fs.open("GPS_DATA","r")
  211. local start = vector.new(gps.locate())
  212.  
  213. mineVein(start,0,false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement