Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mining turtle program for Create: Astral modpack
- -- Constants
- local FUEL_SLOT = 1 -- The slot where the turtle stores fuel (coal)
- local CHEST_SLOT = 2 -- The slot where the turtle stores chests
- local TRASH_SLOT = 3 -- The slot where the turtle stores trash blocks (cobblestone, dirt, etc.)
- local FUEL_LIMIT = 240 -- The minimum fuel level to start mining
- local FUEL_MAX = 2000 -- The maximum fuel level to refuel
- local COAL_LEVEL = 64 -- The y-coordinate of the coal level
- local ORE_LEVELS = { -- The y-coordinates of different ore levels
- ["iron"] = 64,
- ["gold"] = 32,
- ["redstone"] = 16,
- ["diamond"] = 12,
- ["emerald"] = 8,
- ["lapis"] = 8,
- ["quartz"] = 8,
- ["copper"] = 8,
- ["tin"] = 8,
- ["silver"] = 8,
- ["lead"] = 8,
- ["nickel"] = 8,
- ["uranium"] = 8,
- ["zinc"] = 8,
- ["osmium"] = 8
- }
- -- Variables
- local x, y, z -- The current coordinates of the turtle
- local dx, dy, dz -- The current direction of the turtle (unit vector)
- local ores -- The table of ores to mine at each level
- local trash -- The table of trash blocks to dump
- -- Functions
- -- Refuel the turtle from the fuel slot
- local function refuel()
- if turtle.getFuelLevel() < FUEL_MAX then
- turtle.select(FUEL_SLOT)
- while turtle.getFuelLevel() < FUEL_MAX and turtle.getItemCount() > 0 do
- turtle.refuel(1)
- end
- print("Refueled to " .. turtle.getFuelLevel())
- end
- end
- -- Check if the turtle needs to refuel and go to the coal level if necessary
- local function checkFuel()
- if turtle.getFuelLevel() < FUEL_LIMIT then
- print("Low on fuel, going to coal level")
- goTo(0, COAL_LEVEL, 0) -- Go to the origin at coal level
- local found = false
- for i = 1, 4 do -- Try four directions
- turnRight()
- if turtle.detect() then -- If there is a block in front
- turtle.dig() -- Dig it
- if turtle.inspect().name == "minecraft:coal_ore" then -- If it is coal ore
- found = true
- break
- else
- turtle.place() -- Place it back
- end
- end
- end
- if found then -- If coal ore is found
- print("Found coal ore, mining it")
- while turtle.inspect().name == "minecraft:coal_ore" do -- While there is coal ore in front
- turtle.dig() -- Dig it
- forward() -- Move forward
- end
- turnBack() -- Turn back
- while not turtle.detect() do -- While there is no block behind
- back() -- Move back
- end
- turnBack() -- Turn back again
- refuel() -- Refuel from the mined coal
- else -- If coal ore is not found
- print("No coal ore found, aborting")
- os.exit() -- Exit the program
- end
- end
- end
- -- Move the turtle forward and update its coordinates
- local function forward()
- checkFuel() -- Check fuel before moving
- while not turtle.forward() do -- While the move is unsuccessful
- if turtle.detect() then -- If there is a block in front
- turtle.dig() -- Dig it
- elseif turtle.attack() then -- If there is a mob in front
- print("Attacked a mob")
- else -- If there is some other obstacle
- print("Cannot move forward")
- os.sleep(1) -- Wait for a second and try again
- end
- end
- x = x + dx -- Update x-coordinate
- y = y + dy -- Update y-coordinate
- z = z + dz -- Update z-coordinate
- end
- -- Move the turtle backward and update its coordinates
- local function back()
- checkFuel() -- Check fuel before moving
- while not turtle.back() do -- While the move is unsuccessful
- if turtle.detectDown() then -- If there is a block below
- turtle.digDown() -- Dig it
- elseif turtle.attackDown() then -- If there is a mob below
- print("Attacked a mob")
- else -- If there is some other obstacle
- print("Cannot move backward")
- os.sleep(1) -- Wait for a second and try again
- end
- end
- x = x - dx -- Update x-coordinate
- y = y - dy -- Update y-coordinate
- z = z - dz -- Update z-coordinate
- end
- -- Move the turtle up and update its coordinates
- local function up()
- checkFuel() -- Check fuel before moving
- while not turtle.up() do -- While the move is unsuccessful
- if turtle.detectUp() then -- If there is a block above
- turtle.digUp() -- Dig it
- elseif turtle.attackUp() then -- If there is a mob above
- print("Attacked a mob")
- else -- If there is some other obstacle
- print("Cannot move up")
- os.sleep(1) -- Wait for a second and try again
- end
- end
- x = x + dx -- Update x-coordinate
- y = y + dy -- Update y-coordinate
- z = z + dz -- Update z-coordinate
- end
- -- Move the turtle down and update its coordinates
- local function down()
- checkFuel() -- Check fuel before moving
- while not turtle.down() do -- While the move is unsuccessful
- if turtle.detectDown() then -- If there is a block below
- turtle.digDown() -- Dig it
- elseif turtle.attackDown() then -- If there is a mob below
- print("Attacked a mob")
- else -- If there is some other obstacle
- print("Cannot move down")
- os.sleep(1) -- Wait for a second and try again
- end
- end
- x = x - dx -- Update x-coordinate
- y = y - dy -- Update y-coordinate
- z = z - dz -- Update z-coordinate
- end
- -- Turn the turtle right and update its direction
- local function turnRight()
- checkFuel() -- Check fuel before turning
- turtle.turnRight()
- dx, dy, dz = -dz, dy, dx -- Update direction vector
- end
- -- Turn the turtle left and update its direction
- local function turnLeft()
- checkFuel() -- Check fuel before turning
- turtle.turnLeft()
- dx, dy, dz = dz, dy, -dx -- Update direction vector
- end
- -- Turn the turtle back and update its direction
- local function turnBack()
- checkFuel() -- Check fuel before turning
- turtle.turnRight()
- turtle.turnRight()
- dx, dy, dz = -dx, dy, -dz -- Update direction vector
- end
- -- Go to a specific coordinate by the shortest path
- local function goTo(targetX, targetY, targetZ)
- while x ~= targetX do -- While not at the target x-coordinate
- if x < targetX then -- If need to go positive x-direction
- while dx ~= 1 do turnRight() end -- Turn to face positive x-direction
- forward() -- Move forward
- else -- If need to go negative x-direction
- while dx ~= -1 do turnRight() end -- Turn to face negative x-direction
- forward() -- Move forward
- end
- end
- while y ~= targetY do -- While not at the target y-coordinate
- if y < targetY then -- If need to go positive y-direction
- while dy ~= 1 do turnRight() end -- Turn to face positive y-direction
- forward() -- Move forward
- else -- If need to go negative y-direction
- while dy ~= -1 do turnRight() end -- Turn to face negative y-direction
- forward() -- Move forward
- end
- end
- while z ~= targetZ do -- While not at the target z-coordinate
- if z < targetZ then -- If need to go positive z-direction
- while dz ~= 0 do turnRight() end -- Turn to face positive z-direction
- forward() -- Move forward
- else -- If need to go negative z-direction
- while dz ~= 0 do turnLeft() end -- Turn to face negative z-direction
- forward() -- Move forward
- end
- end
- print("Reached " .. targetX .. ", " .. targetY .. ", " .. targetZ) -- Print the current coordinates
- end
- -- Place a chest behind the turtle and dump the trash blocks into it
- local function dumpTrash()
- print("Dumping trash blocks")
- turnBack() -- Turn back
- turtle.select(CHEST_SLOT) -- Select the chest slot
- if turtle.getItemCount() > 0 then -- If there is a chest in the slot
- turtle.place() -- Place the chest behind
- for i = 4, 16 do -- For each slot from 4 to 16
- turtle.select(i) -- Select the slot
- local name = turtle.getItemDetail().name -- Get the name of the item
- if trash[name] then -- If the item is in the trash table
- turtle.drop() -- Drop it into the chest
- end
- end
- turtle.select(CHEST_SLOT) -- Select the chest slot again
- turtle.dig() -- Break the chest and collect it
- else -- If there is no chest in the slot
- print("No chest available, aborting")
- os.exit() -- Exit the program
- end
- turnBack() -- Turn back again
- end
- -- Check if the block in front is an ore and mine it if necessary
- local function checkOre()
- if turtle.detect() then -- If there is a block in front
- local name = turtle.inspect().name -- Get the name of the block
- local level = y + dy -- Get the level of the block
- if ores[level] and ores[level][name] then -- If the block is an ore at this level
- print("Found " .. name .. " at level " .. level)
- turtle.dig() -- Dig it
- forward() -- Move forward
- checkOre() -- Check again recursively
- back() -- Move back
- end
- end
- end
- -- Scan and mine ores around the turtle at a given level
- local function scanOres(level)
- print("Scanning ores at level " .. level)
- for i = 1, 4 do -- For each direction
- checkOre() -- Check and mine ore in front
- turnRight() -- Turn right
- end
- checkOreUp = false -- Flag for checking ore above
- checkOreDown = false -- Flag for checking ore below
- if level < ORE_LEVELS["diamond"] then -- If below diamond level
- checkOreUp = true -- Check ore above
- elseif level > ORE_LEVELS["iron"] then -- If above iron level
- checkOreDown = true -- Check ore below
- end
- if checkOreUp then -- If need to check ore above
- up() -- Move up
- scanOres(level + 1) -- Scan ores at the next level recursively
- down() -- Move down
- end
- if checkOreDown then -- If need to check ore below
- down() -- Move down
- scanOres(level - 1) -- Scan ores at the next level recursively
- up() -- Move up
- end
- end
- -- Main program
- -- Initialize variables
- x, y, z = 0, 0, 0 -- Start at the origin
- dx, dy, dz = 0, 0, 1 -- Start facing positive z-direction
- ores = {} -- Create an empty table for ores
- trash = {} -- Create an empty table for trash
- -- Print a setup terminal
- print("Welcome to the mining turtle program")
- print("Please enter the ores you want to mine at each level, separated by commas")
- print("For example: diamond,emerald,lapis")
- print("Enter 'none' if you don't want to mine any ore at that level")
- print("Enter 'all' if you want to mine all ores at that level")
- print("Press enter to confirm your choice")
- -- Ask the user for the ores to mine at each level
- for k, v in pairs(ORE_LEVELS) do -- For each ore and its level
- print("Level " .. v .. ": " .. k) -- Print the level and the ore name
- local input = io.read() -- Read the user input
- local list = {} -- Create an empty list for the ores
- for word in input:gmatch("%w+") do -- For each word in the input
- word = word:lower() -- Convert the word to lower case
- if word == "none" then -- If the word is 'none'
- list = {} -- Set the list to empty
- break -- Break the loop
- elseif word == "all" then -- If the word is 'all'
- list = ORE_LEVELS -- Set the list to all ore levels
- break -- Break the loop
- elseif ORE_LEVELS[word] then -- If the word is a valid ore name
- list[word] = true -- Add the word to the list as a key
- else -- If the word is invalid
- print("Invalid ore name: " .. word) -- Print an error message
- os.exit() -- Exit the program
- end
- end
- ores[v] = list -- Add the list to the ores table with the level as a key
- end
- -- Ask the user for the trash blocks to dump
- print("Please enter the trash blocks you want to dump, separated by commas")
- print("For example: cobblestone,dirt,gravel")
- print("Enter 'none' if you don't want to dump any block")
- print("Enter 'all' if you want to dump all blocks except ores and chests")
- local input = io.read() -- Read the user input
- for word in input:gmatch("%w+") do -- For each word in the input
- word = word:lower() -- Convert the word to lower case
- if word == "none" then -- If the word is 'none'
- trash = {} -- Set the trash table to empty
- break -- Break the loop
- elseif word == "all" then -- If the word is 'all'
- trash = {["minecraft:cobblestone"] = true, ["minecraft:dirt"] = true, ["minecraft:gravel"] = true} -- Set the trash table to common blocks
- break -- Break the loop
- else -- If the word is a block name
- trash[word] = true -- Add the word to the trash table as a key
- end
- end
- -- Start mining from diamond level
- goTo(0, ORE_LEVELS["diamond"], 0) -- Go to the origin at diamond level
- scanOres(ORE_LEVELS["diamond"]) -- Scan and mine ores at diamond level
- -- Go back to the origin and dump the ores into a chest
- goTo(0, 0, 0) -- Go back to the origin
- turnBack() -- Turn back
- turtle.select(CHEST_SLOT) -- Select the chest slot
- if turtle.getItemCount() > 0 then -- If there is a chest in the slot
- turtle.place() -- Place the chest behind
- for i = 4, 16 do -- For each slot from 4 to 16
- turtle.select(i) -- Select the slot
- turtle.drop() -- Drop the item into the chest
- end
- print("Dumped all ores into the chest")
- else -- If there is no chest in the slot
- print("No chest available, aborting")
- os.exit() -- Exit the program
- end
- -- End of program
- print("Mining completed, thank you for using the program")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement