HeatedDZN

Marcus' OCM

May 18th, 2024 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.29 KB | None | 0 0
  1. -- Function to clear the screen
  2. function clearScreen()
  3.     term.clear()
  4.     term.setCursorPos(1, 1)
  5. end
  6.  
  7. -- Function to display the welcome screen
  8. function welcomeScreen()
  9.     clearScreen()
  10.     print("Welcome to the Strip Miner Setup")
  11.     print("================================")
  12.     print("This program will help you set up")
  13.     print("and manage an automated strip miner")
  14.     print("using ComputerCraft turtles.")
  15.     print("")
  16.     print("Press any key to continue...")
  17.     os.pullEvent("key")
  18. end
  19.  
  20. -- Function to get user input for setup
  21. function setupUI()
  22.     clearScreen()
  23.     print("Strip Miner Setup")
  24.     print("=================")
  25.     print("Instructions:")
  26.     print("1. Place the turtle over a chest.")
  27.     print("2. Ensure the chest contains the items you want to trash in the first few slots.")
  28.     print("3. Enter the number of strips:")
  29.     local numStrips = tonumber(read())
  30.     print("4. Enter the depth of each strip:")
  31.     local stripDepth = tonumber(read())
  32.     return numStrips, stripDepth
  33. end
  34.  
  35. -- Function to get trash items from the inventory
  36. function getTrashItems()
  37.     local trashItems = {}
  38.     for slot = 1, 16 do
  39.         local item = turtle.getItemDetail(slot)
  40.         if item then
  41.             table.insert(trashItems, item.name)
  42.         end
  43.     end
  44.     return trashItems
  45. end
  46.  
  47. -- Function to check if an item is trash
  48. function isTrash(itemName, trashItems)
  49.     for _, trashItem in ipairs(trashItems) do
  50.         if itemName == trashItem then
  51.             return true
  52.         end
  53.     end
  54.     return false
  55. end
  56.  
  57. -- Function to move the turtle forward by a specified number of blocks
  58. function moveForward(blocks)
  59.     for i = 1, blocks do
  60.         while not turtle.forward() do
  61.             turtle.dig()
  62.             turtle.attack()
  63.         end
  64.     end
  65. end
  66.  
  67. -- Function to move the turtle back by a specified number of blocks
  68. function moveBack(blocks)
  69.     for i = 1, blocks do
  70.         while not turtle.back() do
  71.             turtle.turnLeft()
  72.             turtle.turnLeft()
  73.             turtle.dig()
  74.             turtle.turnLeft()
  75.             turtle.turnLeft()
  76.             turtle.attack()
  77.         end
  78.     end
  79. end
  80.  
  81. -- Function to mine and move according to the specified pattern
  82. function mineAndMove(trashItems, foundItems, numStrips, stripDepth)
  83.     local totalDistance = 0
  84.     for i = 1, numStrips do
  85.         -- Mine the main tunnel
  86.         for j = 1, 4 do
  87.             turtle.dig()
  88.             moveForward(1)
  89.             totalDistance = totalDistance + 1
  90.             -- Check and mine blocks to the left and right
  91.             checkAndMineSides(trashItems, foundItems)
  92.             -- Mine the block above and move up
  93.             turtle.digUp()
  94.             turtle.up()
  95.             -- Check and mine blocks to the left and right on the upper layer
  96.             checkAndMineSides(trashItems, foundItems)
  97.             -- Move back down
  98.             turtle.down()
  99.         end
  100.  
  101.         -- Create left strip
  102.         turtle.turnLeft()
  103.         mineStrip(trashItems, foundItems, stripDepth)
  104.         turtle.turnRight()
  105.         turtle.turnRight()
  106.         mineStrip(trashItems, foundItems, stripDepth)
  107.         turtle.turnLeft()
  108.     end
  109.     return totalDistance
  110. end
  111.  
  112. -- Function to mine a strip
  113. function mineStrip(trashItems, foundItems, depth)
  114.     for i = 1, depth do
  115.         turtle.dig()
  116.         moveForward(1)
  117.         inspectAndMineSurroundings(trashItems, foundItems)
  118.     end
  119.     moveBack(depth)
  120. end
  121.  
  122. -- Function to check and mine blocks to the sides
  123. function checkAndMineSides(trashItems, foundItems)
  124.     -- Check and mine left
  125.     turtle.turnLeft()
  126.     if turtle.detect() then
  127.         local _, block = turtle.inspect()
  128.         if not isTrash(block.name, trashItems) then
  129.             turtle.dig()
  130.             moveForward(1)
  131.             updateFoundItems(block.name, foundItems)
  132.             inspectAndMineSurroundings(trashItems, foundItems)
  133.             moveBack(1)
  134.         end
  135.     end
  136.     turtle.turnRight()
  137.    
  138.     -- Check and mine right
  139.     turtle.turnRight()
  140.     if turtle.detect() then
  141.         local _, block = turtle.inspect()
  142.         if not isTrash(block.name, trashItems) then
  143.             turtle.dig()
  144.             moveForward(1)
  145.             updateFoundItems(block.name, foundItems)
  146.             inspectAndMineSurroundings(trashItems, foundItems)
  147.             moveBack(1)
  148.         end
  149.     end
  150.     turtle.turnLeft()
  151. end
  152.  
  153. -- Function to update found items count
  154. function updateFoundItems(itemName, foundItems)
  155.     if foundItems[itemName] then
  156.         foundItems[itemName] = foundItems[itemName] + 1
  157.     else
  158.         foundItems[itemName] = 1
  159.     end
  160.     print("Found " .. itemName .. ": " .. foundItems[itemName])
  161. end
  162.  
  163. -- Function to inspect and mine surroundings when a non-trash item is found
  164. function inspectAndMineSurroundings(trashItems, foundItems)
  165.     local directions = {"forward", "up", "down", "left", "right"}
  166.    
  167.     for _, direction in ipairs(directions) do
  168.         if direction == "forward" then
  169.             if turtle.detect() then
  170.                 local _, block = turtle.inspect()
  171.                 if not isTrash(block.name, trashItems) then
  172.                     turtle.dig()
  173.                     moveForward(1)
  174.                     updateFoundItems(block.name, foundItems)
  175.                     inspectAndMineSurroundings(trashItems, foundItems)
  176.                 end
  177.             end
  178.         elseif direction == "up" then
  179.             if turtle.detectUp() then
  180.                 local _, block = turtle.inspectUp()
  181.                 if not isTrash(block.name, trashItems) then
  182.                     turtle.digUp()
  183.                     turtle.up()
  184.                     updateFoundItems(block.name, foundItems)
  185.                     inspectAndMineSurroundings(trashItems, foundItems)
  186.                     turtle.down()
  187.                 end
  188.             end
  189.         elseif direction == "down" then
  190.             if turtle.detectDown() then
  191.                 local _, block = turtle.inspectDown()
  192.                 if not isTrash(block.name, trashItems) then
  193.                     turtle.digDown()
  194.                     turtle.down()
  195.                     updateFoundItems(block.name, foundItems)
  196.                     inspectAndMineSurroundings(trashItems, foundItems)
  197.                     turtle.up()
  198.                 end
  199.             end
  200.         elseif direction == "left" then
  201.             turtle.turnLeft()
  202.             if turtle.detect() then
  203.                 local _, block = turtle.inspect()
  204.                 if not isTrash(block.name, trashItems) then
  205.                     turtle.dig()
  206.                     moveForward(1)
  207.                     updateFoundItems(block.name, foundItems)
  208.                     inspectAndMineSurroundings(trashItems, foundItems)
  209.                     moveBack(1)
  210.                 end
  211.             end
  212.             turtle.turnRight()
  213.         elseif direction == "right" then
  214.             turtle.turnRight()
  215.             if turtle.detect() then
  216.                 local _, block = turtle.inspect()
  217.                 if not isTrash(block.name, trashItems) then
  218.                     turtle.dig()
  219.                     moveForward(1)
  220.                     updateFoundItems(block.name, foundItems)
  221.                     inspectAndMineSurroundings(trashItems, foundItems)
  222.                     moveBack(1)
  223.                 end
  224.             end
  225.             turtle.turnLeft()
  226.         end
  227.     end
  228. end
  229.  
  230. -- Function to return to the chest and dump items
  231. function returnToChestAndDump(travelLength)
  232.     -- Reverse direction
  233.     turtle.turnLeft()
  234.     turtle.turnLeft()
  235.  
  236.     -- Move back the specified number of blocks
  237.     moveBack(travelLength)
  238.  
  239.     -- Dump inventory
  240.     for slot = 1, 16 do
  241.         turtle.select(slot)
  242.         turtle.dropDown()
  243.     end
  244. end
  245.  
  246. -- Main function
  247. function main()
  248.     welcomeScreen()
  249.     local numStrips, stripDepth = setupUI()
  250.     local trashItems = getTrashItems()
  251.     local foundItems = {}
  252.     print("Trash items identified: ")
  253.     for _, item in ipairs(trashItems) do
  254.         print(item)
  255.     end
  256.     print("Starting mining operation...")
  257.     local travelLength = mineAndMove(trashItems, foundItems, numStrips, stripDepth)
  258.     print("Mining operation complete! Returning to chest and dumping inventory...")
  259.     returnToChestAndDump(travelLength)
  260.     print("Inventory dumped!")
  261. end
  262.  
  263. -- Start the mining process
  264. main()
  265.  
Add Comment
Please, Sign In to add comment