Bmorr

strip_mine.lua

Dec 21st, 2020 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.05 KB | None | 0 0
  1. if not turtle then
  2.     printError("Requires a Turtle")
  3.     return
  4. end
  5. -- String Functions
  6. function currentTimeString()
  7.     return textutils.formatTime(os.time("local"))
  8. end
  9.  
  10. function startsWith(str, substr)
  11.     return str:find("^"..substr) ~= nil
  12. end
  13.  
  14. -- Process args
  15. local tArgs = { ... }
  16. if not (#tArgs >= 1) then
  17.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  18.     print("Usage: " .. programName .. " <length> Optional: <debugMode>")
  19.     return
  20. end
  21.  
  22. local additionalOreName = nil
  23. local length = tonumber(tArgs[1])
  24. if length == 0 then
  25.     local is_block, block = turtle.inspect()
  26.     if not is_block then
  27.         print("Turtle cannot dig a tunnel of length 0 unless there is ore directly ahead!")
  28.         return
  29.     end
  30.     print("Turtle will dig until it finds no more ore. Would you like to count \""..block.name.."\" as ore? (y/n)")
  31.     local response = read()
  32.     if response == "y" or response == "Y" then
  33.         additionalOreName = block.name
  34.         print("Turtle will now dig until it finds no more ore, including \""..additionalOreName.."\".")
  35.     end
  36.     length = 1
  37. elseif length < 1 then
  38.     print("Tunnel length must be positive")
  39.     return
  40. end
  41.  
  42. local debugMode = false
  43. if #tArgs == 2 then
  44.     print("DebugMode Active")
  45.     debugMode = true
  46. end
  47.  
  48. -- Load up config
  49. blacklist = {}
  50. config = io.open("config.txt", "r")
  51. for line in config:lines() do
  52.     if startsWith(line, "-") then
  53.         blacklist[#blacklist + 1] = string.sub(line, 2)
  54.     end
  55. end
  56.  
  57. -- Inventory Functions
  58. function checkIfSlotIsItem(slot, name)
  59.     local item = turtle.getItemDetail(slot)
  60.     if item ~= nil then
  61.         return item["name"] == name
  62.     end
  63.     return false
  64. end
  65.  
  66. function findItem(name)
  67.     for slot = 1, 16 do
  68.         if checkIfSlotIsItem(slot, name) then
  69.             return slot
  70.         end
  71.     end
  72.     return -1
  73. end
  74.  
  75. function checkIfHaveItem(name)
  76.     return findItem(name) ~= -1
  77. end
  78.  
  79. function findEmpty()
  80.     for index = 1, 16 do
  81.         if turtle.getItemCount(index) == 0 then
  82.             return index
  83.         end
  84.     end
  85.     return -1
  86. end
  87.  
  88. function countItems()
  89.     local total = 0
  90.     for index = 1, 16 do
  91.         total = turtle.getItemCount(index) + total
  92.     end
  93. end
  94.  
  95. function emptyInventory()
  96.     print("Dropping:")
  97.     for i = 1, 16 do
  98.         turtle.select(i)
  99.         local data = turtle.getItemDetail()
  100.         if data then
  101.             print(tostring(turtle.getItemCount()).."x "..data["name"])
  102.         end
  103.         turtle.dropDown()
  104.     end
  105. end
  106. -- Mining Functions
  107. function tryDig()
  108.     while turtle.detect() do
  109.         if turtle.dig() then
  110.             sleep(0.5)
  111.         else
  112.             return false
  113.         end
  114.     end
  115.     return true
  116. end
  117.  
  118. function tryDigUp()
  119.     while turtle.detectUp() do
  120.         if turtle.digUp() then
  121.             sleep(0.5)
  122.         else
  123.             return false
  124.         end
  125.     end
  126.     return true
  127. end
  128.  
  129. function tryDigDown()
  130.     while turtle.detectDown() do
  131.         if turtle.digDown() then
  132.             sleep(0.5)
  133.         else
  134.             return false
  135.         end
  136.     end
  137.     return true
  138. end
  139.  
  140.  
  141. -- Refueling
  142. function refuel()
  143.     local fuelLevel = turtle.getFuelLevel()
  144.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  145.         return
  146.     end
  147.  
  148.     function tryRefuel()
  149.         for n = 1, 16 do
  150.             if turtle.getItemCount(n) > 0 then
  151.                 turtle.select(n)
  152.                 if turtle.refuel(1) then
  153.                     turtle.select(1)
  154.                     return true
  155.                 end
  156.             end
  157.         end
  158.         turtle.select(1)
  159.         return false
  160.     end
  161.  
  162.     if not tryRefuel() then
  163.         print("Add more fuel to continue.")
  164.         while not tryRefuel() do
  165.             os.pullEvent("turtle_inventory")
  166.         end
  167.         print("Resuming Tunnel.")
  168.     end
  169. end
  170.  
  171. -- Movement Functions
  172. local x, y, z, dir = 0, 0, 0, 0
  173.  
  174. function tryUp()
  175.     refuel()
  176.     while not turtle.up() do
  177.         if turtle.detectUp() then
  178.             if not tryDigUp() then
  179.                 return false
  180.             end
  181.         else
  182.             sleep(0.5)
  183.         end
  184.     end
  185.     y = y + 1
  186.     print("Moved to", x, y, z)
  187.     return true
  188. end
  189.  
  190. function tryDown()
  191.     refuel()
  192.     while not turtle.down() do
  193.         if turtle.detectDown() then
  194.             if not tryDigDown() then
  195.                 return false
  196.             end
  197.         else
  198.             sleep(0.5)
  199.         end
  200.     end
  201.     y = y - 1
  202.     print("Moved to", x, y, z)
  203.     return true
  204. end
  205.  
  206. function tryForward(doprint)
  207.     doprint = doprint or true
  208.     refuel()
  209.     while not turtle.forward() do
  210.         if turtle.detect() then
  211.             if not tryDig() then
  212.                 return false
  213.             end
  214.         else
  215.             sleep(0.5)
  216.         end
  217.     end
  218.     if dir == 0 then
  219.         x = x + 1
  220.     elseif dir == 1 then
  221.         z = z + 1
  222.     elseif dir == 2 then
  223.         x = x - 1
  224.     elseif dir == 3 then
  225.         z = z - 1
  226.     end
  227.     if doprint then
  228.         print("Moved to", x, y, z)
  229.     end
  230.     return true
  231. end
  232.  
  233. function tryBack()
  234.     refuel()
  235.     if not turtle.back() then
  236.         turnLeft(2)
  237.         local ret = tryForward()
  238.         turnLeft(2)
  239.         return ret
  240.     else
  241.         if dir == 0 then
  242.             x = x - 1
  243.         elseif dir == 1 then
  244.             z = z - 1
  245.         elseif dir == 2 then
  246.             x = x + 1
  247.         elseif dir == 3 then
  248.             z = z + 1
  249.         end
  250.         print("Moved to", x, y, z)
  251.     end
  252.     return true
  253. end
  254.  
  255. function turnRight(count)
  256.     count = count or 1
  257.     turtle.turnRight()
  258.     dir = dir - 1
  259.     if dir < 0 then
  260.         dir = 3
  261.     end
  262.     if count > 1 then
  263.         turnRight(count - 1)
  264.     end
  265. end
  266.  
  267. function turnLeft(count)
  268.     count = count or 1
  269.     turtle.turnLeft()
  270.     dir = dir + 1
  271.     if dir > 3 then
  272.         dir = 0
  273.     end
  274.     if count > 1 then
  275.         turnLeft(count - 1)
  276.     end
  277. end
  278.  
  279. -- Mapping Functions
  280. local world_data = {}
  281.  
  282. function spotExists(fx, fy, fz)
  283.     local exists = world_data[fx] ~= nil
  284.     if exists then
  285.         exists = world_data[fx][fy] ~= nil
  286.     end
  287.     if exists then
  288.         exists = world_data[fx][fy][fz] ~= nil
  289.     end
  290.     return exists
  291. end
  292.  
  293. function getSpot(fx, fy, fz)
  294.     if spotexists(fx, fy, fz) then
  295.         return world_data[fx][fy][fz]
  296.     end
  297.     return "?"
  298. end
  299.  
  300. function enterSpot(fx, fy, fz, value)
  301.     local exists = world_data[fx] ~= nil
  302.     if not exists then
  303.         world_data[fx] = {}
  304.     end
  305.     exists = world_data[fx][fy] ~= nil
  306.     if not exists then
  307.         world_data[fx][fy] = {}
  308.     end
  309.     world_data[fx][fy][fz] = value
  310.     if debugMode then
  311.         print(fx.." "..fy.." "..fz..": "..value)
  312.     end
  313. end
  314.  
  315. function scanUp()
  316.     local isthere, info = turtle.inspectUp()
  317.     if isthere then
  318.         enterSpot(x, y + 1, z, info)
  319.         return info
  320.     else
  321.         enterSpot(x, y + 1, z, "empty")
  322.         return "empty"
  323.     end
  324. end
  325.  
  326. function scanFront()
  327.     lx, ly, lz = x, y, z
  328.     if dir == 0 then
  329.         lx = lx + 1
  330.     elseif dir == 1 then
  331.         lz = lz + 1
  332.     elseif dir == 2 then
  333.         lx = lx - 1
  334.     elseif dir == 3 then
  335.         lz = lz - 1
  336.     end
  337.     local isthere, info = turtle.inspect()
  338.     if isthere then
  339.         enterSpot(lx, ly, lz, info)
  340.         return info
  341.     else
  342.         enterSpot(lx, ly, lz, "empty")
  343.         return "empty"
  344.     end
  345. end
  346.  
  347. function scanDown()
  348.     local isthere, info = turtle.inspectDown()
  349.     if isthere then
  350.         enterSpot(x, y - 1, z, info)
  351.         return info
  352.     else
  353.         enterSpot(x, y - 1, z, "empty")
  354.         return "empty"
  355.     end
  356. end
  357.  
  358. function isOre(block)
  359.     if additionalOreName ~= nil then
  360.         -- Check if the block is the additional ore name
  361.         if block.name == additionalOreName then
  362.             return true
  363.         end
  364.     end
  365.     -- Check if the block has a "c:ores" tag and if it's true
  366.     return block.tags and block.tags["c:ores"] == true
  367. end
  368. -- Mining functions
  369. function followOre()
  370.     if isOre(scanFront()) then
  371.         tryDig()
  372.         tryForward()
  373.         followOre()
  374.         tryBack()
  375.         if checkIfHaveItem("minecraft:cobblestone") then
  376.             turtle.select(findItem("minecraft:cobblestone"))
  377.             turtle.place()
  378.             turtle.select(1)
  379.         end
  380.     end
  381.     if isOre(scanUp()) then
  382.         tryDigUp()
  383.         tryUp()
  384.         followOre()
  385.         tryDown()
  386.         if checkIfHaveItem("minecraft:cobblestone") then
  387.             turtle.select(findItem("minecraft:cobblestone"))
  388.             turtle.placeUp()
  389.             turtle.select(1)
  390.         end
  391.     end
  392.     if isOre(scanDown()) then
  393.         tryDigUp()
  394.         tryDown()
  395.         followOre()
  396.         tryUp()
  397.         if checkIfHaveItem("minecraft:cobblestone") then
  398.             turtle.select(findItem("minecraft:cobblestone"))
  399.             turtle.placeDown()
  400.             turtle.select(1)
  401.         end
  402.     end
  403.     turnLeft()
  404.     if isOre(scanFront()) then
  405.         tryDig()
  406.         tryForward()
  407.         followOre()
  408.         tryBack()
  409.         if checkIfHaveItem("minecraft:cobblestone") then
  410.             turtle.select(findItem("minecraft:cobblestone"))
  411.             turtle.place()
  412.             turtle.select(1)
  413.         end
  414.     end
  415.     turnLeft()
  416.     if isOre(scanFront()) then
  417.         tryDig()
  418.         tryForward()
  419.         followOre()
  420.         tryBack()
  421.         if checkIfHaveItem("minecraft:cobblestone") then
  422.             turtle.select(findItem("minecraft:cobblestone"))
  423.             turtle.place()
  424.             turtle.select(1)
  425.         end
  426.     end
  427.     turnLeft()
  428.     if isOre(scanFront()) then
  429.         tryDig()
  430.         tryForward()
  431.         followOre()
  432.         tryBack()
  433.         if checkIfHaveItem("minecraft:cobblestone") then
  434.             turtle.select(findItem("minecraft:cobblestone"))
  435.             turtle.place()
  436.             turtle.select(1)
  437.         end
  438.     end
  439.     turnLeft()
  440. end
  441.  
  442. function scanAround()
  443.     scanUp()
  444.     scanDown()
  445.     for index = 1, 3 do
  446.         scanFront()
  447.         turnLeft()
  448.     end
  449. end
  450.  
  451. local start_count = countItems()
  452. -- Return to where we started
  453. for dist = 1, length do
  454.     tryForward()
  455.     followOre()
  456. end
  457. for dist = 1, length do
  458.     tryBack()
  459. end
  460.  
  461. -- Moon walking to deposit items
  462. --[[
  463. local i, back = 0, 6
  464. local below = scanDown()
  465. while i < back and below ~= "minecraft:water" and below ~= "minecraft:spruce_sign" and below ~= "minecraft:oak_sign" and below ~= "minecraft:spruce_wall_sign" and below ~= "minecraft:oak_wall_sign" do
  466.     tryBack()
  467.     below = scanDown()
  468.     i = i + 1
  469. end
  470. if below == "minecraft:spruce_sign" or below == "minecraft:oak_sign" or below == "minecraft:spruce_wall_sign" or below == "minecraft:oak_wall_sign" then
  471.     turnLeft()
  472.     tryForward()
  473.     emptyInventory()
  474.     tryBack()
  475.     turnRight()
  476. elseif below == "minecraft:water" then
  477.     emptyInventory()
  478. else
  479.     print("Couldn't empty inventory!")
  480. end
  481. for lcv = 1, i do
  482.     tryForward(false)
  483. end
  484. --]]
  485. -- print("Found", (start_count - countItems()), "items during expedition.")
  486. print("Fuel is currently "..turtle.getFuelLevel().."!")
  487.  
Advertisement
Add Comment
Please, Sign In to add comment