Advertisement
Guest User

miner.lua

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