Advertisement
Bmorr

strip_mine.lua

Dec 21st, 2020 (edited)
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.36 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["name"])
  305.         return info
  306.     else
  307.         enterSpot(x, y + 1, z, "empty")
  308.         return {}
  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["name"])
  326.         return info
  327.     else
  328.         enterSpot(lx, ly, lz, "empty")
  329.         return {}
  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["name"])
  337.         return info
  338.     else
  339.         enterSpot(x, y - 1, z, "empty")
  340.         return {}
  341.     end
  342. end
  343.  
  344. function isOre(item_data)
  345.     if item_data.name == "minecraft:obsidian" then
  346.         return true
  347.     end
  348.     local tags = item_data["tags"]
  349.     if tags ~= nil then
  350.         return tags["forge:ores"] == true
  351.     end
  352.     return false
  353. end
  354.  
  355. -- Mining functions
  356. function followOre()
  357.     if isOre(scanFront()) then
  358.         tryDig()
  359.         tryForward()
  360.         followOre()
  361.         tryBack()
  362.         if checkIfHaveItem("minecraft:cobblestone") then
  363.             turtle.select(findItem("minecraft:cobblestone"))
  364.             turtle.place()
  365.             turtle.select(1)
  366.         end
  367.     end
  368.     if isOre(scanUp()) then
  369.         tryDigUp()
  370.         tryUp()
  371.         followOre()
  372.         tryDown()
  373.         if checkIfHaveItem("minecraft:cobblestone") then
  374.             turtle.select(findItem("minecraft:cobblestone"))
  375.             turtle.placeUp()
  376.             turtle.select(1)
  377.         end
  378.     end
  379.     if isOre(scanDown()) then
  380.         tryDigUp()
  381.         tryDown()
  382.         followOre()
  383.         tryUp()
  384.         if checkIfHaveItem("minecraft:cobblestone") then
  385.             turtle.select(findItem("minecraft:cobblestone"))
  386.             turtle.placeDown()
  387.             turtle.select(1)
  388.         end
  389.     end
  390.     turnLeft()
  391.     if isOre(scanFront()) then
  392.         tryDig()
  393.         tryForward()
  394.         followOre()
  395.         tryBack()
  396.         if checkIfHaveItem("minecraft:cobblestone") then
  397.             turtle.select(findItem("minecraft:cobblestone"))
  398.             turtle.place()
  399.             turtle.select(1)
  400.         end
  401.     end
  402.     turnLeft()
  403.     if isOre(scanFront()) then
  404.         tryDig()
  405.         tryForward()
  406.         followOre()
  407.         tryBack()
  408.         if checkIfHaveItem("minecraft:cobblestone") then
  409.             turtle.select(findItem("minecraft:cobblestone"))
  410.             turtle.place()
  411.             turtle.select(1)
  412.         end
  413.     end
  414.     turnLeft()
  415.     if isOre(scanFront()) then
  416.         tryDig()
  417.         tryForward()
  418.         followOre()
  419.         tryBack()
  420.         if checkIfHaveItem("minecraft:cobblestone") then
  421.             turtle.select(findItem("minecraft:cobblestone"))
  422.             turtle.place()
  423.             turtle.select(1)
  424.         end
  425.     end
  426.     turnLeft()
  427. end
  428.  
  429. function scanAround()
  430.     scanUp()
  431.     scanDown()
  432.     for index = 1, 3 do
  433.         scanFront()
  434.         turnLeft()
  435.     end
  436. end
  437.  
  438. local start_count = countItems()
  439. -- Return to where we started
  440. for dist = 1, length do
  441.     tryForward()
  442.     followOre()
  443. end
  444. for dist = 1, length do
  445.     tryBack()
  446. end
  447.  
  448. -- Moon walking to deposit items
  449. --[[
  450. local i, back = 0, 6
  451. local below = scanDown()
  452. 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
  453.     tryBack()
  454.     below = scanDown()
  455.     i = i + 1
  456. end
  457. if below == "minecraft:spruce_sign" or below == "minecraft:oak_sign" or below == "minecraft:spruce_wall_sign" or below == "minecraft:oak_wall_sign" then
  458.     turnLeft()
  459.     tryForward()
  460.     emptyInventory()
  461.     tryBack()
  462.     turnRight()
  463. elseif below == "minecraft:water" then
  464.     emptyInventory()
  465. else
  466.     print("Couldn't empty inventory!")
  467. end
  468. for lcv = 1, i do
  469.     tryForward(false)
  470. end
  471. --]]
  472. -- print("Found", (start_count - countItems()), "items during expedition.")
  473. print("Fuel is currently "..turtle.getFuelLevel().."!")
  474.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement