Bmorr

scan_mine.lua

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