Advertisement
Guest User

miner.lua

a guest
Jul 20th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.54 KB | None | 0 0
  1. Position = {0, 0, 0}
  2. Orientation = 0
  3. Stored_Position = {0, 0, 0}
  4.  
  5. Junk = {"minecraft:cobblestone", "minecraft:dirt", "minecraft:gravel"}
  6.  
  7.  
  8. function init()
  9.     Position = {0, 0, 0}    
  10. end
  11. init()    
  12. function dig_front()
  13.     if turtle.detect() then
  14.         return turtle.dig()    
  15.     else
  16.         return true
  17.     end
  18. end
  19.    
  20. function dig_up()
  21.     if turtle.detectUp() then
  22.         return turtle.digUp()
  23.     else
  24.         return true
  25.     end
  26. end
  27.  
  28. function dig_down()
  29.     if turtle.detectDown() then
  30.         return turtle.digDown()
  31.     else
  32.         return true
  33.     end
  34. end
  35.  
  36. function forward()
  37.     if turtle.forward() then
  38.         if Orientation == 0 then
  39.             Position[2] = Position[2] + 1
  40.         elseif Orientation == 1 then
  41.             Position[1] = Position[1] + 1
  42.         elseif Orientation == 2 then
  43.             Position[2] = Position[2] - 1
  44.         elseif Orientation == 3 then
  45.             Position[1] = Position[1] - 1
  46.         end
  47.         return true    
  48.     else
  49.         return false
  50.     end    
  51. end
  52.  
  53. function up()
  54.     local success = true
  55.     success = turtle.up()
  56.     if success == true then
  57.         Position[3] = Position[3] + 1
  58.         return true
  59.     else
  60.         return false
  61.     end
  62. end
  63.  
  64. function down()
  65.     local success = true
  66.     success = turtle.down()
  67.     if success == true then
  68.         Position[3] = Position[3] - 1
  69.         return true
  70.     else
  71.         return false
  72.     end
  73. end
  74.  
  75.  
  76. function mine_forward()
  77.     local success = true
  78.     success = dig_front()
  79.     if success == false then
  80.         return false
  81.     end
  82.     return forward()
  83. end
  84.  
  85. function mine_up()
  86.     local success = true
  87.     success = dig_up()
  88.     if success == false then
  89.         return false
  90.     end
  91.     return up()
  92. end    
  93. function mine_down()
  94.     local success = true
  95.     success = dig_down()
  96.     if success == false then
  97.         return false
  98.     end
  99.     return down()
  100. end
  101. function turn_right()
  102.     turtle.turnRight()
  103.     Orientation = (Orientation + 1) % 4
  104. end
  105.  
  106. function turn_left()
  107.     turtle.turnLeft()
  108.     Orientation = (Orientation - 1) % 4
  109. end
  110.  
  111. function set_orientation(o)
  112.     while o ~= Orientation do
  113.         turn_right()
  114.     end
  115. end
  116.  
  117.  
  118.  
  119. function navigate_to(x, y, z)
  120.     navigate_to_z(z)
  121.     navigate_to_x(x)
  122.     navigate_to_y(y)
  123. end
  124.  
  125. function navigate_to_xyz(x, y, z)
  126.     navigate_to_x(x)
  127.     navigate_to_y(y)
  128.     navigate_to_z(z)
  129. end
  130.  
  131. function navigate_to_x(x)
  132.     local offset = x - Position[1]
  133.     if offset > 0 then
  134.         set_orientation(1)
  135.         while Position[1] ~= x do
  136.             mine_forward()
  137.         end
  138.     elseif offset < 0 then
  139.         set_orientation(3)
  140.         while Position[1] ~= x do
  141.             mine_forward()
  142.         end
  143.     end
  144. end
  145.  
  146. function navigate_to_y(y)
  147.     local offset = y - Position[2]
  148.     if offset > 0 then
  149.         set_orientation(0)
  150.     elseif offset < 0 then
  151.         set_orientation(2)
  152.     end
  153.     while Position[2] ~= y do
  154.         mine_forward()
  155.     end
  156. end
  157.                                                
  158. function navigate_to_z(z)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  159.     local offset = z - Position[3]
  160.     print(offset)
  161.     if offset < 0 then
  162.         while Position[3] > z do
  163.             mine_down()
  164.         end    
  165.     elseif offset > 0 then
  166.         while Position[3] < z do
  167.             mine_up()
  168.         end
  169.     end
  170. end
  171.  
  172. function navigate_to_start()
  173.     navigate_to(0,0,0)
  174.     set_orientation(0)
  175. end
  176.  
  177. function distance_to_start()
  178.  
  179.     local distance = 0
  180.  
  181.  
  182.     local y_dist = math.abs(Position[1])
  183.     local x_dist = math.abs(Position[2])
  184.     local z_dist = math.abs(Position[3])
  185.     distance = y_dist + x_dist + y_dist
  186.     return distance
  187. end
  188.  
  189. function check_fuel_return(delta )
  190.     local fuel = turtle.getFuelLevel()
  191.     local distance = distance_to_start()
  192.     if distance + delta >= fuel then
  193.         print("Not enough fuel - ")
  194.         print("Returning to start")
  195.         navigate_to_start()
  196.     return true
  197.     end
  198.     return false
  199. end
  200.  
  201. Mine_Circ = 8
  202. Mine_Height = 40
  203. print("ayy lmao")
  204.  
  205. function do_mine_up()
  206.     init()
  207.     local returning
  208.     for i=0, Mine_Height, 1 do
  209.         returning = mine_layer()
  210.         if returning == true then
  211.             return
  212.         end
  213.         -- Check inventory
  214.         -- Dump if necessary
  215.         check_inv_dump()
  216.         navigate_to_x(0)
  217.         navigate_to_y(0)
  218.         set_orientation(0)
  219.         mine_up()
  220.     end
  221. end
  222.  
  223. function check_fuel_mine_up()
  224.     local returning = check_fuel_for_return(Mine_Circ*Mine_Circ)
  225.     return returning
  226. end    
  227.  
  228. function mine_layer()  
  229.     local returning = check_fuel_return(Mine_Circ * Mine_Circ)
  230.     if returning == true then
  231.         return true    
  232.     end
  233.     set_orientation(0)
  234.     for trips=1, Mine_Circ / 2, 1 do
  235.         mine_line()
  236.         turn_right()
  237.         while mine_forward() == false do
  238.         end
  239.         turn_right()    
  240.         mine_line()        
  241.         turn_left()        
  242.         while mine_forward() == false do
  243.         end
  244.         turn_left()
  245.     end
  246. end    
  247.  
  248. function mine_line()
  249.     local line_counter = 0
  250.     while line_counter < Mine_Circ do
  251.         while mine_forward() == false do
  252.         end
  253.         line_counter = line_counter + 1
  254.         check_inv_dump()
  255.     end
  256. end
  257.  
  258. function keep_item(item)
  259.     return false
  260. end
  261.  
  262. function dump_junk()
  263.     for i=1, 16, 1 do
  264.         turtle.select(i)
  265.         info = turtle.getItemDetail(i)
  266.         if info ~= nil  then      
  267.             for _, item in ipairs(Junk) do
  268.                 if info.name == item then
  269.                     turtle.dropDown()
  270.                 end
  271.             end
  272.         end
  273.     end
  274. end
  275.  
  276. function empty_inventory()
  277.     for i=1, 16, 1 do
  278.         turtle.select(i)
  279.         item = turtle.getItemDetail(i)
  280.         qty = turtle.getItemCount(i)
  281.         if keep_item(item) == false then
  282.             turtle.dropDown(qty)
  283.         end
  284.     end
  285. end        
  286.  
  287. function empty_slots()
  288.     empty = 0
  289.     for i=1, 16, 1 do        
  290.         qty = turtle.getItemCount(i)
  291.         if qty == 0 then
  292.             empty = empty + 1
  293.         end
  294.     end
  295.     return empty
  296. end
  297.  
  298. function is_inv_full()
  299.     if empty_slots() == 0 then
  300.         return true
  301.     end
  302.     return false
  303. end
  304. function check_inv_dump()
  305.     if is_inv_full() == true then
  306.         dump_junk()
  307.     end
  308.     if is_inv_full() == true then
  309.         print("Returning to start to")
  310.         print("Unload inventory")
  311.         Stored_Orientation = Orientation
  312.         Stored_Position[1] = Position[1]
  313.         Stored_Position[2] = Position[2]
  314.         Stored_Position[3] = Position[3]
  315.         navigate_to_start()
  316.         while is_inv_full() == true do
  317.             empty_inventory()
  318.         end
  319.         print("Resuming mining")
  320.         navigate_to_xyz(Stored_Position[1], Stored_Position[2], Stored_Position[3])
  321.         set_orientation(Stored_Orientation)
  322.         return true
  323.     end
  324.     return false
  325. end
  326.  
  327. function dig_stairs(width, height)
  328.     Mine_Circ = width
  329.     for i=1, height, 1 do
  330.         mine_down()
  331.         mine_layer()
  332.         navigate_to(0, i, -i)
  333.     end
  334. end
  335.  
  336. Mine_Height = 50
  337. Mine_Circ = 16
  338. print("Emptying inventory...")
  339. empty_inventory()
  340. print("Starting mining operation")
  341. do_mine_up()
  342. --dig_stairs(6, 68)
  343. --check_inv_dump()
  344. print("Mining op completed - returning home")
  345. navigate_to_start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement