Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to clear the screen
- function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Function to display the welcome screen
- function welcomeScreen()
- clearScreen()
- print("Welcome to the Strip Miner Setup")
- print("================================")
- print("This program will help you set up")
- print("and manage an automated strip miner")
- print("using ComputerCraft turtles.")
- print("")
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- -- Function to get user input for setup
- function setupUI()
- clearScreen()
- print("Strip Miner Setup")
- print("=================")
- print("Instructions:")
- print("1. Place the turtle over a chest.")
- print("2. Ensure the chest contains the items you want to trash in the first few slots.")
- print("3. Enter the number of strips:")
- local numStrips = tonumber(read())
- print("4. Enter the depth of each strip:")
- local stripDepth = tonumber(read())
- return numStrips, stripDepth
- end
- -- Function to get trash items from the inventory
- function getTrashItems()
- local trashItems = {}
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item then
- table.insert(trashItems, item.name)
- end
- end
- return trashItems
- end
- -- Function to check if an item is trash
- function isTrash(itemName, trashItems)
- for _, trashItem in ipairs(trashItems) do
- if itemName == trashItem then
- return true
- end
- end
- return false
- end
- -- Function to move the turtle forward by a specified number of blocks
- function moveForward(blocks)
- for i = 1, blocks do
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- end
- end
- -- Function to move the turtle back by a specified number of blocks
- function moveBack(blocks)
- for i = 1, blocks do
- while not turtle.back() do
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.attack()
- end
- end
- end
- -- Function to mine and move according to the specified pattern
- function mineAndMove(trashItems, foundItems, numStrips, stripDepth)
- local totalDistance = 0
- for i = 1, numStrips do
- -- Mine the main tunnel
- for j = 1, 4 do
- turtle.dig()
- moveForward(1)
- totalDistance = totalDistance + 1
- -- Check and mine blocks to the left and right
- checkAndMineSides(trashItems, foundItems)
- -- Mine the block above and move up
- turtle.digUp()
- turtle.up()
- -- Check and mine blocks to the left and right on the upper layer
- checkAndMineSides(trashItems, foundItems)
- -- Move back down
- turtle.down()
- end
- -- Create left strip
- turtle.turnLeft()
- mineStrip(trashItems, foundItems, stripDepth)
- turtle.turnRight()
- turtle.turnRight()
- mineStrip(trashItems, foundItems, stripDepth)
- turtle.turnLeft()
- end
- return totalDistance
- end
- -- Function to mine a strip
- function mineStrip(trashItems, foundItems, depth)
- for i = 1, depth do
- turtle.dig()
- moveForward(1)
- inspectAndMineSurroundings(trashItems, foundItems)
- end
- moveBack(depth)
- end
- -- Function to check and mine blocks to the sides
- function checkAndMineSides(trashItems, foundItems)
- -- Check and mine left
- turtle.turnLeft()
- if turtle.detect() then
- local _, block = turtle.inspect()
- if not isTrash(block.name, trashItems) then
- turtle.dig()
- moveForward(1)
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- moveBack(1)
- end
- end
- turtle.turnRight()
- -- Check and mine right
- turtle.turnRight()
- if turtle.detect() then
- local _, block = turtle.inspect()
- if not isTrash(block.name, trashItems) then
- turtle.dig()
- moveForward(1)
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- moveBack(1)
- end
- end
- turtle.turnLeft()
- end
- -- Function to update found items count
- function updateFoundItems(itemName, foundItems)
- if foundItems[itemName] then
- foundItems[itemName] = foundItems[itemName] + 1
- else
- foundItems[itemName] = 1
- end
- print("Found " .. itemName .. ": " .. foundItems[itemName])
- end
- -- Function to inspect and mine surroundings when a non-trash item is found
- function inspectAndMineSurroundings(trashItems, foundItems)
- local directions = {"forward", "up", "down", "left", "right"}
- for _, direction in ipairs(directions) do
- if direction == "forward" then
- if turtle.detect() then
- local _, block = turtle.inspect()
- if not isTrash(block.name, trashItems) then
- turtle.dig()
- moveForward(1)
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- end
- end
- elseif direction == "up" then
- if turtle.detectUp() then
- local _, block = turtle.inspectUp()
- if not isTrash(block.name, trashItems) then
- turtle.digUp()
- turtle.up()
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- turtle.down()
- end
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- local _, block = turtle.inspectDown()
- if not isTrash(block.name, trashItems) then
- turtle.digDown()
- turtle.down()
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- turtle.up()
- end
- end
- elseif direction == "left" then
- turtle.turnLeft()
- if turtle.detect() then
- local _, block = turtle.inspect()
- if not isTrash(block.name, trashItems) then
- turtle.dig()
- moveForward(1)
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- moveBack(1)
- end
- end
- turtle.turnRight()
- elseif direction == "right" then
- turtle.turnRight()
- if turtle.detect() then
- local _, block = turtle.inspect()
- if not isTrash(block.name, trashItems) then
- turtle.dig()
- moveForward(1)
- updateFoundItems(block.name, foundItems)
- inspectAndMineSurroundings(trashItems, foundItems)
- moveBack(1)
- end
- end
- turtle.turnLeft()
- end
- end
- end
- -- Function to return to the chest and dump items
- function returnToChestAndDump(travelLength)
- -- Reverse direction
- turtle.turnLeft()
- turtle.turnLeft()
- -- Move back the specified number of blocks
- moveBack(travelLength)
- -- Dump inventory
- for slot = 1, 16 do
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- -- Main function
- function main()
- welcomeScreen()
- local numStrips, stripDepth = setupUI()
- local trashItems = getTrashItems()
- local foundItems = {}
- print("Trash items identified: ")
- for _, item in ipairs(trashItems) do
- print(item)
- end
- print("Starting mining operation...")
- local travelLength = mineAndMove(trashItems, foundItems, numStrips, stripDepth)
- print("Mining operation complete! Returning to chest and dumping inventory...")
- returnToChestAndDump(travelLength)
- print("Inventory dumped!")
- end
- -- Start the mining process
- main()
Add Comment
Please, Sign In to add comment