Advertisement
hdgamer14

Miner 0.2.0

Aug 18th, 2022 (edited)
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. local x, y, x_dist, y_dist, x_return, y_return, direction_return, max_slots, work, torchs, debug, auto_refuel, direction
  2.  
  3. debug = false
  4. auto_refuel = false
  5.  
  6.  
  7. function mine()
  8.   turtle.digUp()
  9.   turtle.dig()
  10.   turtle.digDown()
  11. end
  12.  
  13. function mine1()
  14.   turtle.digUp()
  15.   turtle.dig()
  16. end
  17.  
  18. function unload(slot)
  19.   print("Unloading items...")
  20.   for n = 1, slot do
  21.     turtle.select(n)
  22.     turtle.drop()
  23.   end
  24.   turtle.select(1)
  25. end
  26.  
  27. function go_forward()
  28.   if turtle.forward() == false then
  29.     repeat
  30.       turtle.attack()
  31.       mine1()
  32.       sleep(0.3)
  33.     until turtle.forward() == true
  34.   end
  35. end
  36.  
  37. function turn(dir)
  38.   if (dir == "left") then
  39.     direction = direction - 1
  40.     if (direction == -1) then
  41.       direction = 3
  42.     end
  43.     turtle.turnLeft()
  44.   elseif (dir == "right") then
  45.     direction = direction + 1
  46.     if (direction == 4) then
  47.       direction = 0
  48.     end
  49.     turtle.turnRight()
  50.   end
  51. end
  52.  
  53. function go_back()
  54.   turn("right")
  55.   turn("right")
  56.   for i = 0, y_return-1 do
  57.     go_forward()
  58.   end
  59.   if (x_return > 0) then
  60.     turn("right")
  61.   else
  62.     turn("left")
  63.   end
  64.   for i = 0, math.abs(x_return) - 1 do
  65.     go_forward()
  66.   end
  67.   while direction ~= direction_return do
  68.     turn("left")
  69.   end
  70.   turtle.suck()
  71.   turtle.suckDown()
  72. end
  73.  
  74. function go_home()
  75.   x_return = x
  76.   y_return = y
  77.   direction_return = direction
  78.   if (x > 0) then
  79.     if (direction == 0) then
  80.       turn("left")
  81.     else
  82.       turn("right")
  83.     end
  84.   else
  85.     if (direction == 0) then
  86.       turn("right")
  87.     else
  88.       turn("left")
  89.     end
  90.   end
  91.   for i = 0, math.abs(x)-1 do
  92.     go_forward()
  93.   end
  94.   if (direction == 3) then
  95.     turn("left")
  96.   elseif (direction == 1) then
  97.     turn("right")
  98.   end
  99.   for i = 0, y-1 do
  100.     go_forward()
  101.   end
  102. end
  103.  
  104. function fuel_me(x_local, y_local)
  105.   print ("Waiting for fuel . . .")
  106.   turtle.select(1)
  107.   repeat
  108.     turtle.refuel(64)
  109.     sleep(1)
  110.   until turtle.getFuelLevel() > (y_local * 2) + (math.abs(x_local) * 2) + 10
  111. end
  112.  
  113. function check_fuel(x_local, y_local)
  114.   if turtle.getFuelLevel() < (y_local * 2) + (math.abs(x_local) * 2) + 10 then
  115.     print("Out of fuel!")
  116.     refueld = false
  117.     if auto_refuel == true then    
  118.       for f = 1, max_slots do
  119.         turtle.select(f)
  120.         if turtle.refuel(2) then
  121.           turtle.select(1)
  122.           return false
  123.         end
  124.       end
  125.     end
  126.     return true
  127.   else
  128.     return false
  129.   end
  130. end
  131.  
  132. function cycle()
  133.   if (check_fuel(x,y)) then
  134.     if debug then print("need fuel") end
  135.     go_home()
  136.     unload(max_slots)
  137.     fuel_me(x,y)
  138.     go_back()
  139.   end
  140.   if ( turtle.getItemCount(max_slots) > 0)  then
  141.     if debug then print("inv full") end
  142.     go_home()
  143.     unload(max_slots)
  144.     if (check_fuel(x_return,y_return)) then
  145.       fuel_me(x_return,y_return)
  146.     end
  147.     go_back()
  148.   end
  149.   if debug then print("y: "..y.. " x: " .. x .. " dir: " .. direction) end
  150.   mine()
  151.   if (torchs and (y % 7 == 0) and (x % 7 == 0)) then
  152.     turtle.select(16)
  153.     turtle.placeDown()
  154.     turtle.select(1)
  155.   end
  156.   go_forward()
  157.   if (direction == 0) then
  158.     y = y + 1
  159.     if (y == y_dist) then
  160.       if debug == true then print("End of col") end
  161.       if (x == x_dist) then
  162.         if debug == true then print("done") end
  163.         mine()
  164.         go_home()
  165.         unload(16)
  166.         work = false
  167.       else
  168.         if (x_dist > 0) then
  169.           turn("right")
  170.           mine()
  171.           go_forward()
  172.           turn("right")
  173.           x = x + 1
  174.         else
  175.           turn("left")
  176.           mine()
  177.           go_forward()
  178.           turn("left")
  179.           x = x - 1
  180.         end
  181.       end
  182.     end
  183.   elseif (direction == 2) then
  184.     y = y - 1
  185.     if (y == 0) then
  186.       if debug == true then print("End of col") end
  187.       if (x == x_dist) then
  188.         if debug == true then print("done") end
  189.         mine()
  190.         go_home()
  191.         unload(16)
  192.         work = false
  193.       else
  194.         if (x_dist > 0) then
  195.           turn("left")
  196.           mine()
  197.           go_forward()
  198.           turn("left")
  199.           x = x + 1
  200.         else
  201.           turn("right")
  202.           mine()
  203.           go_forward()
  204.           turn("right")
  205.           x = x - 1
  206.         end
  207.       end
  208.     end
  209.   end
  210.   if debug then sleep(2.0) end
  211. end
  212.  
  213. --Get info
  214. shell.run("clear")
  215. print("How far you you like me to go?")
  216. y_dist = read()
  217. print("How many rows would you like me to mine?")
  218. x_dist = read()
  219. print("Would you like me to use torches? (yes/no)")
  220. torchs = read()
  221. if torchs == "yes" then
  222.   print("Place torhes in slot 16")
  223.   repeat
  224.     sleep(1)
  225.   until turtle.getItemCount(16) > 0
  226.   torchs = true
  227.   max_slots = 15
  228. else
  229.   torchs = false
  230.   max_slots = 16
  231. end
  232.  
  233. y_dist = y_dist - 1
  234. x_dist = tonumber(x_dist)
  235. if x_dist > 0 then
  236.   x_dist = x_dist - 1
  237. else
  238.   x_dist = x_dist + 1
  239. end
  240. x=0
  241. y=0
  242. direction = 0
  243. work = true
  244.  
  245. if debug == true then
  246.   print("")
  247.   print("I'm mining " .. y_dist .. " blocks deep!")
  248.   print("I'm mining " .. x_dist .. " Row(s) over!")
  249.   print("Fuel level: " .. turtle.getFuelLevel())
  250.   if torchs == true then
  251.     print("I will use torches!")
  252.   else
  253.     print("I will not use torches!")
  254.   end
  255. end
  256.  
  257. -- Start Program
  258. repeat
  259.   cycle()
  260. until work == false
  261. print("All Done :D")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement