Advertisement
Link712011

Mine opti v1.1

Apr 15th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.98 KB | None | 0 0
  1. function select_fuel()
  2.     local slot = 1
  3.     local data = turtle.getItemDetail(slot)
  4.  
  5.   if select_item("minecraft:lava_bucket") then
  6.     return true
  7.   end
  8.     while slot <= 16 do
  9.         if data and (data.name == "minecraft:coal" or data.name == "minecraft:planks" or data.name == "minecraft:log"or data.name == "minecraft:stick") then
  10.             break
  11.         end
  12.         slot = slot + 1
  13.         if slot <= 16 then
  14.             turtle.select(slot)
  15.             data = turtle.getItemDetail(slot)
  16.         end
  17.     end
  18.     if (slot > 16) then
  19.         return false
  20.     end
  21.     turtle.select(slot)
  22.   return true
  23. end
  24.  
  25. function check_fuel()
  26.     select_fuel()
  27.     if turtle.getFuelLevel() < 1000 then
  28.         while turtle.refuel(1) == false do
  29.             print("No fuel available. Retrying refuel in 10 seconds")
  30.             sleep(10)
  31.         end
  32.     end
  33. end
  34.  
  35. function avance(x)
  36.     while x > 0 do
  37.     if quick_inspect("front") == "minecraft:lava" or quick_inspect("front") == "minecraft:flowing_lava" then
  38.       search_and_fill_lava()
  39.     end
  40.         while turtle.forward() == false do
  41.             check_fuel()
  42.       search_and_mine_ore()
  43.             turtle.dig()
  44.         end
  45.         x = x - 1
  46.     end
  47. end
  48.  
  49. function line(maxx, maxz)
  50.     local z = 0
  51.     while z <= maxz do
  52.         avance(maxx)
  53.         turtle.turnLeft()
  54.         turtle.turnLeft()
  55.         avance(maxx)
  56.         turtle.turnRight()
  57.         turtle.turnRight()
  58.         while turtle.up() == false do
  59.             check_fuel()
  60.             turtle.digUp()
  61.         end
  62.         z = z + 1
  63.     end
  64.     while z > 0 do
  65.         while turtle.down() == false do
  66.             check_fuel()
  67.             turtle.digDown()
  68.         end
  69.         z = z - 1
  70.     end
  71. end
  72.  
  73. function select_item(item)
  74.     local slot = 1
  75.    
  76.     while slot <= 16 do
  77.         local data = turtle.getItemDetail(slot)
  78.         if data and data.name == item then
  79.             turtle.select(slot)
  80.             return true
  81.         end
  82.         slot = slot + 1
  83.     end
  84.     return false
  85. end
  86.  
  87. function search_bloc(bloc)
  88.   if quick_inspect("up") == bloc then return "up"
  89.   elseif quick_inspect("front") == bloc then return "front"
  90.   elseif quick_inspect("down") == bloc then return "down"
  91.   end
  92.   turtle.turnLeft()
  93.   if quick_inspect("front") == bloc then
  94.     turtle.turnRight()
  95.     return "left"
  96.   end
  97.   turtle.turnRight()
  98.   turtle.turnRight()
  99.   if quick_inspect("front") == bloc then
  100.     turtle.turnLeft()
  101.     return "right"
  102.   end
  103.   turtle.turnLeft()
  104.   return "none"
  105. end
  106.  
  107. function quick_inspect(direction)
  108.   local success = false
  109.   local data
  110.  
  111.   if direction == "up" then success, data = turtle.inspectUp()
  112.   elseif direction == "front" then success, data = turtle.inspect()
  113.   elseif direction == "down" then success, data = turtle.inspectDown()
  114.   end
  115.   if success then
  116.     return data.name
  117.   else
  118.     return "none"
  119.   end
  120. end
  121.  
  122.  
  123. function search_and_fill_lava()
  124.   local direction = search_bloc("minecraft:lava")
  125.  
  126.   if direction ~= "none" and select_item("minecraft:lava_bucket") then
  127.     turtle.refuel()
  128.   end
  129.   if direction ~= "none" and select_item("minecraft:bucket") then
  130.     if direction == "up" then turtle.placeUp()
  131.     elseif direction == "down" then turtle.placeDown()
  132.     elseif direction == "left" then
  133.       turtle.turnLeft()
  134.       turtle.place()
  135.       turtle.turnRight()
  136.     elseif direction == "right" then
  137.       turtle.turnRight()
  138.       turtle.place()
  139.       turtle.turnLeft()
  140.     elseif direction == "front" then
  141.       turtle.place()
  142.     end
  143.   end
  144. end
  145.  
  146.  
  147. function search_and_mine_ore()
  148.   if  quick_inspect("up") == "minecraft:iron_ore" or
  149.       quick_inspect("up") == "minecraft:gold_ore" or
  150.       quick_inspect("up") == "minecraft:diamond_ore" or
  151.       quick_inspect("up") == "minecraft:emerald_ore" or
  152.       quick_inspect("up") == "minecraft:lapis_ore" or
  153.       quick_inspect("up") == "minecraft:redstone_ore" or
  154.       quick_inspect("up") == "minecraft:coal_ore" then
  155.     turtle.digUp()
  156.     return "up"
  157.   elseif  quick_inspect("front") == "minecraft:iron_ore" or
  158.           quick_inspect("front") == "minecraft:gold_ore" or
  159.           quick_inspect("front") == "minecraft:diamond_ore" or
  160.           quick_inspect("front") == "minecraft:emerald_ore" or
  161.           quick_inspect("front") == "minecraft:lapis_ore" or
  162.           quick_inspect("front") == "minecraft:redstone_ore" or
  163.           quick_inspect("front") == "minecraft:coal_ore" then
  164.     turtle.dig()
  165.     return "front"
  166.   elseif  quick_inspect("") == "minecraft:iron_ore" or
  167.           quick_inspect("down") == "minecraft:gold_ore" or
  168.           quick_inspect("down") == "minecraft:diamond_ore" or
  169.           quick_inspect("down") == "minecraft:emerald_ore" or
  170.           quick_inspect("down") == "minecraft:lapis_ore" or
  171.           quick_inspect("down") == "minecraft:redstone_ore" or
  172.           quick_inspect("down") == "minecraft:coal_ore" then
  173.     turtle.digDown()
  174.     return "down"
  175.   end
  176.   turtle.turnLeft()
  177.   if  quick_inspect("front") == "minecraft:iron_ore" or
  178.       quick_inspect("front") == "minecraft:gold_ore" or
  179.       quick_inspect("front") == "minecraft:diamond_ore" or
  180.       quick_inspect("front") == "minecraft:emerald_ore" or
  181.       quick_inspect("front") == "minecraft:lapis_ore" or
  182.       quick_inspect("front") == "minecraft:redstone_ore" or
  183.       quick_inspect("front") == "minecraft:coal_ore" then
  184.     turtle.dig()
  185.     return "left"
  186.   end
  187.   turtle.turnRight()
  188.   turtle.turnRight()
  189.   if  quick_inspect("front") == "minecraft:iron_ore" or
  190.       quick_inspect("front") == "minecraft:gold_ore" or
  191.       quick_inspect("front") == "minecraft:diamond_ore" or
  192.       quick_inspect("front") == "minecraft:emerald_ore" or
  193.       quick_inspect("front") == "minecraft:lapis_ore" or
  194.       quick_inspect("front") == "minecraft:redstone_ore" or
  195.       quick_inspect("front") == "minecraft:coal_ore" then    
  196.     turtle.dig()
  197.     return "right"
  198.   end
  199.   turtle.turnLeft()
  200.   return "none"
  201. end
  202. function is_inventory_full()
  203.   local slot = 1
  204.  
  205.   while slot <= 16 do
  206.     if not turtle.getItemDetail(slot) then
  207.       return false
  208.     end
  209.     slot = slot + 1
  210.   end
  211.   return true
  212. end
  213.  
  214. function drop_all_shit()
  215.   local slot = 1
  216.   local data
  217.  
  218.   while slot <= 16 do
  219.     data = turtle.getItemDetail(slot)
  220.     if data and (data.name == "minecraft:cobblestone" or data.name == "minecraft:granite" or data.name == "minecraft:diorite" or data.name == "minecraft:andesite" or data.name == "minecraft:gravel" or data.name == "minecraft:dirt") then
  221.       turtle.select(slot)
  222.       turtle.drop()
  223.     end
  224.     slot = slot + 1
  225.   end
  226. end
  227. function go_drop_ores(xbase, ybase, zbase)
  228.   local x, y, z = secure_gps()
  229.  
  230.   goto_coordinates(xbase, ybase, zbase)
  231.   wait_chest()
  232.   drop_all_non_fuel()
  233.   goto_coordinates(x, y, z)
  234. end
  235. function drop_all_non_fuel()
  236.   local slot = 1
  237.   local data
  238.  
  239.   while slot <= 16 do
  240.     data = turtle.getItemDetail(slot)
  241.     if  data and data.name ~= "minecraft:coal" and
  242.         data.name ~= "minecraft:planks" and
  243.         data.name ~= "minecraft:log"and
  244.         data.name ~= "minecraft:stick" then
  245.       turtle.select(slot)
  246.       turtle.dropDown()
  247.     end
  248.     slot = slot + 1
  249.   end
  250. end
  251. function wait_chest()
  252.   while quick_inspect("down") ~= "minecraft:chest" do
  253.     print("Waiting for chest bottom me. Retrying in 20 secs")
  254.     sleep(20)
  255.   end
  256. end
  257. function secure_gps()
  258.   local x, y, z = gps.locate()
  259.   local i = 0
  260.  
  261.   while not x do
  262.     print("Can't communicate with GPS. Retrying in 10 seconds. I will shutdown after 50 try")
  263.     x, y, z = gps.locate()
  264.     i = i + 1
  265.     if i >= 50 then os.shutdown() end
  266.   end
  267.   return x, y, z
  268. end
  269.  
  270. function goto_coordinates(x, y, z)
  271.   local xn, yn, zn = secure_gps()
  272.  
  273.   look_at_north(xn, zn)
  274.   xn, yn, zn = secure_gps()
  275.   if (z - zn) < 0 then
  276.     turtle.turnLeft()
  277.     turtle.turnLeft()
  278.     avance(abs(z - zn))
  279.     turtle.turnRight()
  280.     turtle.turnRight()
  281.   else
  282.     avance(abs(z - zn))
  283.   end
  284.   if (x - xn) < 0 then
  285.     turtle.turnLeft()
  286.   elseif (x - xn) > 0 then
  287.     turtle.turnRight()
  288.   end
  289.   avance(abs(x - xn))
  290.   if (z - zn) < 0 then
  291.     descend(abs(z - zn))
  292.   elseif (z - zn) > 0 then
  293.     ascend(abs(z - zn))
  294.   end
  295. end
  296. function look_at_north(x, z)
  297.   local xn, yn, zn
  298.    
  299.     if turtle.detect() then
  300.         check_fuel()
  301.         while not turtle.back() do
  302.             check_fuel()
  303.         end
  304.         xn, yn, zn = get_coords()
  305.         if x > xn then turtle.turnRight()
  306.         elseif x < xn then turtle.turnLeft()
  307.         elseif z < zn then
  308.             turtle.turnLeft()
  309.             turtle.turnLeft()
  310.         end
  311.         return true
  312.     end
  313.     check_avance()
  314.     xn, yn, zn = get_coords()
  315.     if x < xn then turtle.turnRight()
  316.     elseif x > xn then turtle.turnLeft()
  317.     elseif z > zn then
  318.         turtle.turnLeft()
  319.         turtle.turnLeft()
  320.     end
  321.     return true
  322. end
  323. function abs(x)
  324.   if x < 0 then return -x end return x
  325. end
  326.  
  327.  
  328. function move_to_direction(direction)
  329.   if direction == "front" then avance(1)
  330.   elseif direction == "back" then
  331.     turtle.turnLeft()
  332.     turtle.turnLeft()
  333.     avance(1)
  334.     turtle.turnLeft()
  335.     turtle.turnLeft()
  336.   elseif direction == "left" then
  337.     turtle.turnLeft()
  338.     avance(1)
  339.     turtle.turnRight()
  340.   elseif direction == "right" then
  341.     turtle.turnRight()
  342.     avance(1)
  343.     turtle.turnLeft()
  344.   elseif direction == "down" then
  345.     descend(1)
  346.   elseif direction == "up" then
  347.     ascend(1)
  348.   end
  349. end
  350. function descend(max)
  351.   local i = 0
  352.  
  353.   while i < max do
  354.     if turtle.down() == false then
  355.       check_fuel()
  356.       turtle.digDown()
  357.     else
  358.       i = i + 1
  359.     end
  360.   end
  361. end
  362. function ascend(max)
  363.   local i = 0
  364.  
  365.   while i < max do
  366.     if turtle.up() == false then
  367.       check_fuel()
  368.       turtle.digUp()
  369.     else
  370.       i = i + 1
  371.     end
  372.   end
  373. end
  374.  
  375. function check_inventory(x, y, z)
  376.   if is_inventory_full() then
  377.     go_drop_ores(x, y, z)
  378.   end
  379. end
  380. local xhome, yhome, zhome = secure_gps()
  381.  
  382. print("Don't forget to put chest bottom me before I start !")
  383. io.write("Nbr of blocs to mine in front of: ")
  384. local maxx = tonumber(read())
  385. io.write("Nbr of blocs to mine at right: ")
  386. local maxz = tonumber(read())
  387. io.write("Nbr of blocs to mine at top: ")
  388. local maxy = tonumber(read())
  389. local z = 0
  390. while z < maxz do
  391.     line(maxx, maxy)
  392.   drop_all_shit()
  393.   check_inventory()
  394.     turtle.turnRight()
  395.     line(3, maxy)
  396.     avance(4)
  397.     turtle.turnLeft()
  398.     z = z + 3
  399. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement