Advertisement
CaptainSpaceCat

TreeMiner

May 21st, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. x, y, z, f = 0, 0, 0, 0
  2. local breaks = 0
  3. local returning = false
  4. local template = {"log", "leaves"}
  5.  
  6. function checkFuel(flag)
  7.   if not flag then
  8.     local fuel = turtle.getFuelLevel()
  9.     local distance = math.abs(x) + math.abs(y) + math.abs(z)
  10.     if fuel-1 <= distance then
  11.       home()
  12.       error("Alert: Fuel level critical. Returning to home point...")
  13.     end
  14.   end
  15. end
  16.  
  17. function forward(num)
  18.   num = num or 1
  19.   for i = 1, num do
  20.     if turtle.forward() then
  21.       if f == 0 then
  22.         x = x + 1
  23.       elseif f == 1 then
  24.         z = z + 1
  25.       elseif f == 2 then
  26.         x = x - 1
  27.       elseif f == 3 then
  28.         z = z - 1
  29.       end
  30.       checkFuel(returning)
  31.     else
  32.       return false
  33.     end
  34.   end
  35.   return true
  36. end
  37.  
  38. function back(num)
  39.   num = num or 1
  40.   for i = 1, num do
  41.     if turtle.back() then
  42.       if f == 0 then
  43.         x = x - 1
  44.       elseif f == 1 then
  45.         z = z - 1
  46.       elseif f == 2 then
  47.         x = x + 1
  48.       elseif f == 3 then
  49.         z = z + 1
  50.       end
  51.       checkFuel(returning)
  52.     else
  53.       return false
  54.     end
  55.   end
  56.   return true
  57. end
  58.  
  59. function up(num)
  60.   num = num or 1
  61.   for i = 1, num do
  62.     if turtle.up() then
  63.       y = y + 1
  64.       checkFuel(returning)
  65.     else
  66.       return false
  67.     end
  68.   end
  69.   return true
  70. end
  71.  
  72. function down(num)
  73.   num = num or 1
  74.   for i = 1, num do
  75.     if turtle.down() then
  76.       y = y - 1
  77.       checkFuel(returning)
  78.     else
  79.       return false
  80.     end
  81.   end
  82.   return true
  83. end
  84.  
  85. function left(num)
  86.   num = num or 1
  87.   for i = 1, num do
  88.     turtle.turnLeft()
  89.     f = (f+3)%4
  90.   end
  91. end
  92.  
  93. function right(num)
  94.   num = num or 1
  95.   for i = 1, num do
  96.     turtle.turnRight()
  97.     f = (f+1)%4
  98.   end
  99. end
  100.  
  101. function home()
  102.   returning = true
  103.   while y ~= 0 do
  104.     if y < 0 then
  105.       turtle.digUp()
  106.       up()
  107.     elseif y > 0 then
  108.       turtle.digDown()
  109.       down()
  110.     end
  111.   end
  112.   if z > 0 then
  113.     while f ~= 3 do
  114.       left()
  115.     end
  116.   elseif z < 0 then
  117.     while f ~= 1 do
  118.       left()
  119.     end
  120.   end
  121.   while z ~= 0 do
  122.     forward()
  123.   end
  124.   if x > 0 then
  125.     while f ~= 2 do
  126.       left()
  127.     end
  128.   elseif x < 0 then
  129.     while f ~= 0 do
  130.       left()
  131.     end
  132.   end
  133.   while x ~= 0 do
  134.     forward()
  135.   end
  136. end
  137.  
  138. function log(data)
  139.   local h = fs.open("log", "w")
  140.   if type(data) == "string" then
  141.     h.writeLine(data)
  142.   elseif type(data) == "table" then
  143.     for i = 1, #data do
  144.       h.writeLine(data[i])
  145.     end
  146.   end
  147.   h.close()
  148. end
  149.  
  150. function surface()
  151.   while down() do
  152.   end
  153. end
  154.  
  155. function bottom()
  156.   turtle.digDown()
  157.   while down() do
  158.     turtle.digDown()
  159.   end
  160. end
  161.  
  162. function scan(tStr)
  163.   local s,d = turtle.inspect()
  164.   if s then
  165.     for i = 1, #tStr do
  166.       if string.find(d.name, tStr[i]) then
  167.         return true
  168.       end
  169.     end
  170.     return false
  171.   end
  172.   return false
  173. end
  174. function scanUp(tStr)
  175.   local s,d = turtle.inspectUp()
  176.   if s then
  177.     for i = 1, #tStr do
  178.       if string.find(d.name, tStr[i]) then
  179.         return true
  180.       end
  181.     end
  182.     return false
  183.   end
  184.   return false
  185. end
  186. function scanDown(tStr)
  187.   local s,d = turtle.inspectDown()
  188.   if s then
  189.     for i = 1, #tStr do
  190.       if string.find(d.name, tStr[i]) then
  191.         return true
  192.       end
  193.     end
  194.     return false
  195.   end
  196.   return false
  197. end
  198.  
  199. function mineVein(tStr)
  200.   breaks = breaks + 1
  201.   if breaks == 200 then
  202.     home()
  203.     error("reached block limit")
  204.   end
  205.   if scan(tStr) then
  206.     turtle.dig()
  207.     forward()
  208.     mineVein(tStr)
  209.     back()
  210.   end
  211.   left()
  212.   if scan(tStr) then
  213.     turtle.dig()
  214.     forward()
  215.     mineVein(tStr)
  216.     back()
  217.   end
  218.   right(2)
  219.   if scan(tStr) then
  220.     turtle.dig()
  221.     forward()
  222.     mineVein(tStr)
  223.     back()
  224.   end
  225.   left()
  226.   if scanUp(tStr) then
  227.     turtle.digUp()
  228.     up()
  229.     mineVein(tStr)
  230.     down()
  231.   end
  232.   if scanDown(tStr) then
  233.     turtle.digDown()
  234.     down()
  235.     mineVein(tStr)
  236.     up()
  237.   end
  238. end
  239.  
  240. function drillDown(num)
  241.   for i = 1, num do
  242.     turtle.digDown()
  243.     if not down() then
  244.       break
  245.     end
  246.   end
  247. end
  248.  
  249. function stripMine(length, strips)
  250.   for i = 1, strips do
  251.     for i = 1, length do
  252.       turtle.dig()
  253.       forward()
  254.       mineVein(template)
  255.     end
  256.     left((i%2)*2+1)
  257.     for i = 1, 3 do
  258.       turtle.dig()
  259.       forward()
  260.       mineVein(template)
  261.     end
  262.     left((i%2)*2+1)
  263.   end
  264. end
  265.  
  266. function coast(num)
  267.   for i = 1, num do
  268.     while not forward() do
  269.       turtle.digUp()
  270.       up()
  271.     end
  272.     surface()
  273.     mineVein(template)
  274.   end
  275. end
  276.  
  277.  
  278. ------------------------Actual Program------------------------
  279. mineVein(template)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement