Advertisement
Guest User

stripmine.lua

a guest
Oct 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | None | 0 0
  1. local x = 0
  2. local y = 0
  3. local z = 0
  4. local dir = "north"
  5.  
  6. function sortFuel()
  7.     local data = turtle.getItemDetail(16)
  8.     if data ~= nil then
  9.         if data.name ~= "minecraft:coal" then
  10.             turtle.select(16)
  11.             sleep(0.25)
  12.             turtle.drop()
  13.             sleep(0.25)
  14.             turtle.select(1)
  15.             sleep(0.25)
  16.         end
  17.     end
  18.     for i = 1,15 do
  19.         data = turtle.getItemDetail(i)
  20.         if data ~= nil then
  21.             if data.name == "minecraft:coal" then
  22.                 turtle.select(i)
  23.                 turtle.transferTo(16)
  24.                 return
  25.             end
  26.         end
  27.     end
  28. end
  29.  
  30. function saveHere()
  31.     local file = fs.open("here","w")
  32.     file.writeLine(tostring(x).."\n"..tostring(y).."\n"..tostring(z).."\n"..dir)
  33.     file.close()
  34.     print("x:"..tostring(x)..", y:"..tostring(y)..", z:"..tostring(z))
  35. end
  36.  
  37. function right()
  38.     if dir == "north" then
  39.         dir = "east"
  40.     elseif dir == "east" then
  41.         dir = "south"
  42.     elseif dir == "south" then
  43.         dir = "west"
  44.     else
  45.         dir = "north"
  46.     end
  47.     turtle.turnRight()
  48.     saveHere()
  49. end
  50.  
  51. function left()
  52.     if dir == "north" then
  53.         dir = "west"
  54.     elseif dir == "west" then
  55.         dir = "south"
  56.     elseif dir == "south" then
  57.         dir = "east"
  58.     else
  59.         dir = "north"
  60.     end
  61.     turtle.turnLeft()
  62.     saveHere()
  63. end
  64.  
  65. function forward()
  66.     if turtle.getFuelLevel() < 500 then
  67.         sortFuel()
  68.         turtle.select(16)
  69.         sleep(0.25)
  70.         while turtle.getItemCount(16) > 2 and turtle.getFuelLevel() < 1000 do
  71.             if turtle.getItemCount(16) > 2 then
  72.                 turtle.refuel(1)
  73.             end
  74.         end
  75.         sleep(0.25)
  76.     end
  77.     if dir == "north" then
  78.         z = z - 1
  79.     elseif dir == "south" then
  80.         z = z + 1
  81.     elseif dir == "east" then
  82.         x = x + 1
  83.     else
  84.         x = x - 1
  85.     end
  86.     if turtle.detect() then
  87.         if string.find(block(),"turtle") then
  88.             if math.random(1,2) == 2 then
  89.                 down()
  90.                 forward()
  91.                 up()
  92.                 return
  93.             else
  94.                 up()
  95.                 forward()
  96.                 down()
  97.                 return
  98.             end    
  99.         else
  100.            repeat
  101.                dig()
  102.                sleep(0.5)
  103.            until not turtle.detect()
  104.         end
  105.    end
  106.    repeat
  107.        dig()
  108.    until turtle.forward()
  109.    saveHere()
  110. end
  111.  
  112. function back()
  113.     if dir == "north" then
  114.         z = z + 1
  115.     elseif dir == "south" then
  116.         z = z - 1
  117.     elseif dir == "east" then
  118.         x = x - 1
  119.     else
  120.         x = x + 1
  121.     end
  122.     repeat
  123.     until turtle.back()
  124.     saveHere()
  125. end
  126.  
  127. function down()
  128.     local tries = 250
  129.     repeat
  130.         digDown()
  131.         tries = tries-1
  132.     until turtle.down() or tries < 0
  133.     if not (tries < 0) then
  134.         y = y - 1
  135.     end
  136.     saveHere()
  137. end
  138.  
  139. function up()
  140.     repeat
  141.         digUp()
  142.     until turtle.up()
  143.     y = y+1
  144.     saveHere()
  145. end
  146.  
  147. if fs.exists("here") then
  148.     local file = fs.open("here","r")
  149.     x = tonumber(file.readLine())
  150.     y = tonumber(file.readLine())
  151.     z = tonumber(file.readLine())
  152.     dir = file.readLine()
  153.     file.close()
  154. else
  155.     local file = fs.open("here","w")
  156.     file.writeLine("0\n0\n0\nnorth")
  157.     file.close()
  158. end
  159.    
  160. function block(o)
  161.     local d,data = turtle.inspect()
  162.     if o ~= nil then
  163.         if o == "up" then
  164.             d,data = turtle.inspectUp()
  165.         else
  166.             d,data = turtle.inspectDown()
  167.         end
  168.     end
  169.     if not d then
  170.         return "air"
  171.     else
  172.         return data.name
  173.     end
  174. end
  175.  
  176. function dig()
  177.    
  178.     while string.find(block(),"turtle")
  179.         sleep(0.5)
  180.     end
  181.     turtle.dig()
  182. end
  183.  
  184. function digUp()
  185.     repeat
  186.     sleep(0.5)
  187.     until not string.find(block("up"),"turtle")
  188.     turtle.digUp()
  189. end
  190.  
  191. function digDown()
  192.     repeat
  193.     sleep(0.5)
  194.     until not string.find(block("down"),"turtle")
  195.     turtle.digDown()
  196. end
  197.            
  198. function vien()
  199.     if string.find(block(),"ore") then
  200.         dig()
  201.         forward()
  202.         vien()
  203.         back()
  204.     end    
  205.     right()
  206.     if string.find(block(),"ore") then
  207.         dig()
  208.         forward()
  209.         vien()
  210.         back()
  211.     end
  212.     right()
  213.     right()
  214.     if string.find(block(),"ore") then
  215.         dig()
  216.         forward()
  217.         vien()
  218.         back()
  219.     end
  220.     right()
  221.     if string.find(block("up"),"ore") then
  222.         digUp()
  223.         up()
  224.         vien()
  225.         down()
  226.     end
  227.    
  228.     if string.find(block("down"),"ore") then
  229.         digDown()
  230.         down()
  231.         vien()
  232.         up()
  233.     end
  234. end
  235.  
  236. function goHome()
  237.     if x ~= 0 or y ~= 0 or z ~= 0 or dir ~= "north" then
  238.         if x > 0 then
  239.             if dir ~= "west" then
  240.                 repeat
  241.                     right()
  242.                 until dir == "west"
  243.             end
  244.             forward()
  245.         elseif x < 0 then
  246.             if dir ~= "east" then
  247.                 repeat
  248.                     right()
  249.                 until dir == "east"
  250.             end
  251.             forward()
  252.         elseif z > 0 then
  253.             if dir ~= "north" then
  254.                 repeat
  255.                     right()
  256.                 until dir == "north"
  257.             end
  258.             forward()
  259.         elseif z < 0 then
  260.             if dir ~= "south" then
  261.                 repeat
  262.                     right()
  263.                 until dir == "south"
  264.             end
  265.         elseif y > 0 then
  266.             down()
  267.         elseif y < 0 then
  268.             up()
  269.         elseif dir ~= north then
  270.             right()
  271.         end
  272.         goHome()
  273.     else
  274.         print("home!")
  275.     end
  276. end
  277.  
  278. function dump()
  279.     for i = 1,15 do
  280.         turtle.select(i)
  281.         sleep(0.25)
  282.         turtle.drop()
  283.     end
  284.     turtle.select(1)
  285. end
  286.  
  287. function shouldGoHome()
  288.     local dist = math.abs(x)+math.abs(y)+math.abs(z)
  289.     if (dist*2) > turtle.getFuelLevel() or dist > 150 then
  290.         return true
  291.     end
  292.     for i = 1,15 do
  293.         if turtle.getItemCount(i) == 0 then
  294.             return false
  295.         end
  296.     end
  297.     return true
  298. end
  299.  
  300. goHome()
  301. right()
  302. right()
  303.  
  304. while turtle.getFuelLevel() > 100 do
  305.     turtle.dig()
  306.     forward()
  307.     left()
  308.     if turtle.detect() then
  309.         while not shouldGoHome() do
  310.             dig()
  311.             forward()
  312.             vien()
  313.         end
  314.         goHome()
  315.         dump()
  316.         right()
  317.         right()
  318.     else
  319.         right()
  320.         dig()
  321.         forward()
  322.         right()
  323.         if turtle.detect() then
  324.             while not shouldGoHome() do
  325.                 dig()
  326.                 forward()
  327.                 vien()
  328.             end
  329.             goHome()
  330.             dump()
  331.             right()
  332.             right()
  333.         else
  334.             left()
  335.         end        
  336.     end        
  337. end
  338.  
  339. print("no fuel!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement