Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --What should this program accomplish?
- --Create a 2x1 Tunnel with user given length
- --Create roof against lava and depending on user input check also walls
- --Place torches
- --Automatically Refuel
- --Broadcast status on rednet
- --Automatically return to start location
- --Sand / Gravel support
- local DISTANCE_TO_MINE = 0
- local SHOULD_CHECK_WALLS = false
- local TORCH_SLOT = 1
- local FUEL_SLOT = 2
- local BLOCK_SLOT = 3
- local USES_FUEL = (turtle.getFuelLevel() == "unlimited")
- local DISTANCE_BETWEEN_TORCHES = 10
- local REDNET_ACCESS = false
- local MODEM_SIDE = ""
- local MINING_START_TIME = 0
- local MINING_FINISH_TIME = 0
- local MINING_BLOCK_COUNT = 0
- local function DigForward()
- if turtle.detect() then
- if turtle.dig() then
- MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
- end
- end
- end
- local function DigUp()
- if turtle.detectUp() then
- if turtle.digUp() then
- MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
- end
- end
- end
- local function DigDown()
- if turtle.detectDown() then
- if turtle.digDown() then
- MINING_BLOCK_COUNT = MINING_BLOCK_COUNT + 1
- end
- end
- end
- local function BroadcastMessage(text)
- if REDNET_ACCESS then
- rednet.broadcast(((os.getComputerLabel() or "nil") .. text), "miningturtle")
- end
- end
- --Returns true if a torch should be placed. False if not.
- local function ShouldPlaceTorch(distance)
- if (distance % DISTANCE_BETWEEN_TORCHES == 0) then
- return not (turtle.detectDown())
- end
- return false
- end
- --Places a torch
- local function PlaceTorch()
- turtle.select(TORCH_SLOT)
- if (turtle.getItemCount(TORCH_SLOT) > 0) then
- turtle.placeDown()
- end
- end
- --Checks if the turtle needs refuel
- local function NeedsRefuel()
- if USES_FUEL then
- return false
- else
- return (turtle.getFuelLevel() < 100)
- end
- end
- --Refuels the turtle
- local function Refuel()
- turtle.select(FUEL_SLOT)
- if (turtle.getItemCount(FUEL_SLOT) > 0) then
- turtle.refuel(1)
- else
- BroadcastMessage("nofuel")
- end
- end
- --Checks floor for existance.
- local function ShouldPlaceFloor()
- return not (turtle.detectDown())
- end
- --Places a block down
- local function PlaceFloor()
- turtle.select(BLOCK_SLOT)
- if (turtle.getItemCount(BLOCK_SLOT) > 0) then
- turtle.placeDown()
- end
- end
- --Checks the roof for existance.
- local function ShouldPlaceRoof()
- return not (turtle.detectUp())
- end
- --Places a block up
- local function PlaceRoof()
- turtle.select(BLOCK_SLOT)
- if (turtle.getItemCount(BLOCK_SLOT) > 0) then
- turtle.placeUp()
- end
- end
- --Checks walls left and right for existance. IF not, a block is placed.
- local function CheckWalls()
- turtle.turnLeft()
- turtle.select(BLOCK_SLOT)
- if not (turtle.detect()) then
- turtle.place()
- end
- turtle.turnRight()
- turtle.turnRight()
- if not (turtle.detect()) then
- turtle.place()
- end
- turtle.turnLeft()
- end
- --Moves forward, mines forward and up.
- local function MineForward(maxDistance)
- local currentDistance = 0
- repeat
- DigForward()
- DigUp()
- if ShouldPlaceFloor() then
- PlaceFloor()
- end
- if NeedsRefuel() then
- Refuel()
- end
- if turtle.forward() then
- currentDistance = currentDistance + 1
- end
- if SHOULD_CHECK_WALLS then
- CheckWalls()
- end
- until (currentDistance >= maxDistance)
- end
- --Turns the turtle 180° and makes it mine forward and move forward. Also moves the turtle up once at the beginning.
- local function ReturnToStart(distanceToStart)
- DigUp()
- turtle.up()
- turtle.turnLeft()
- turtle.turnLeft()
- local distanceLeft = distanceToStart
- repeat
- DigForward()
- DigDown()
- if ShouldPlaceRoof() then
- PlaceRoof()
- end
- if ShouldPlaceTorch(distanceLeft) then
- PlaceTorch()
- end
- if SHOULD_CHECK_WALLS then
- CheckWalls()
- end
- if NeedsRefuel() then
- Refuel()
- end
- if turtle.forward() then
- distanceLeft = distanceLeft - 1
- end
- until (distanceLeft <= 0)
- turtle.turnLeft()
- turtle.turnLeft()
- DigDown()
- turtle.down()
- end
- --Tries to open a modem on all sides and checks if a modem is open then, therefor returning whether the turtle has a modem or not.
- local function CheckIfModemExists()
- for n,m in ipairs(rs.getSides()) do
- if peripheral.isPresent(m) and peripheral.getType(m) == "modem" then
- rednet.open(m)
- MODEM_SIDE = m
- return true
- end
- end
- return false
- end
- --Converts a given user string (true/false OR y/n) to a boolean.
- local function UserStringToBoolean(text)
- local input = string.lower(text)
- if (input == "true") then
- return true
- end
- if (input == "y") then
- return true
- end
- return false
- end
- --A delayed calls of CheckInventory()
- local function DelayedRecheck(delay)
- local counter = 10
- repeat
- shell.run("clear")
- print("Please fill inventory with the required items.")
- print("Will recheck in " .. tostring(counter) .. " seconds.")
- sleep(1)
- counter = counter - 1
- until (counter == 0)
- end
- --Checks the inventory and the modem
- local function CheckInventory()
- local torchCount = turtle.getItemCount(TORCH_SLOT)
- local fuelCount = turtle.getItemCount(FUEL_SLOT)
- local blockCount = turtle.getItemCount(BLOCK_SLOT)
- local invError = false
- if (torchCount == 0) then
- print("There are no torches in the inventory! (Slot 1)")
- invError = true
- end
- if (fuelCount == 0) then
- print("There is no fuel in the inventory! (Slot 2)")
- invError = true
- end
- if (blockCount == 0) then
- print("There are no blocks in the inventory! (Slot 3)")
- invError = true
- end
- REDNET_ACCESS = CheckIfModemExists()
- if not (REDNET_ACCESS) then
- print("There is no connected modem!")
- invError = true
- end
- if (invError) then
- print("Continue with current status? y/n")
- local input = io.read()
- local continue = UserStringToBoolean(input)
- if (not continue) then
- DelayedRecheck()
- CheckInventory()
- return nil
- end
- end
- end
- --Called upon program start
- local function Startup()
- shell.run("clear")
- print("<== Stripmine Program ==>")
- print("Please enter tunnel length:")
- local input = io.read()
- DISTANCE_TO_MINE = tonumber(input)
- print("Should the turtle check the walls for holes? y/n")
- input = io.read()
- SHOULD_CHECK_WALLS = UserStringToBoolean(input)
- shell.run("clear")
- print("Tunnel length: " .. tostring(DISTANCE_TO_MINE))
- print("Check walls: " .. tostring(SHOULD_CHECK_WALLS))
- print("Continue with these settings? y/n")
- input = io.read()
- local continue = UserStringToBoolean(input)
- if (not continue) then
- Startup()
- return nil
- end
- CheckInventory()
- shell.run("clear")
- MINING_START_TIME = os.epoch("utc")
- BroadcastMessage("startmining")
- MineForward(DISTANCE_TO_MINE)
- ReturnToStart(DISTANCE_TO_MINE)
- MINING_FINISH_TIME = os.epoch("utc")
- local timeSpent = (MINING_FINISH_TIME - MINING_START_TIME) / 1000
- print("Time spent mining: " .. tostring(timeSpent) .. " seconds")
- print("Blocks mined: " .. tostring(MINING_BLOCK_COUNT))
- BroadcastMessage("endmining:" .. (tostring(timeSpent)) .. ":" .. (MINING_BLOCK_COUNT))
- end
- Startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement