Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- -- Initialising...
- local run = true -- if this is false, the program will terminate
- local blocksToMove = 0
- local infinite = false
- local noAsking = false
- local placeTorches = false
- local placeChests = false
- local enderChestMode = false
- local blocksMoved = 0
- local torchMoves = 0 -- number of blocks moved since last torch placement
- function printUsage()
- print("Usages:")
- print("goldMine <blocks> <placeTorches> <placeChests>")
- print("goldMine infinite <placeTorches> <placeBlocks>")
- end
- if tArgs[1] == "infinite" then
- infinite = true
- elseif type(tonumber(tArgs[1])) == "number" then -- if it's a valid number
- blocksToMove = tonumber(tArgs[1])
- else
- printUsage()
- run = false
- end
- if run then
- if tArgs[2] == "true" then
- placeTorches = true
- elseif tArgs[2] == "false" then
- placeTorches = false
- else
- printUsage()
- run = false
- end
- end
- if run then
- if tArgs[3] == "true" then
- placeChests = true
- elseif tArgs[3] == "false" then
- placeChests = false
- else
- printUsage()
- run = false
- end
- end
- if run then
- if tArgs[4] == "noAskingEnder" then -- i have some evil plans
- noAsking = true
- enderChestMode = true
- elseif tArgs[4] == "noAsking" then
- noAsking = true
- enderChestMode = false
- end
- end
- if run then -- printing a whole bunch of text
- if placeTorches and (noAsking == false) then
- print("----------------------------")
- print("Put torches in the top left slot and press any button to continue...")
- os.pullEvent("key")
- print("----------------------------")
- end
- if placeChests and (noAsking == false) then
- print("----------------------------")
- print("Put chests in the bottom right slot and press any button to continue...")
- os.pullEvent("key")
- print("----------------------------")
- local validInput = false
- print("----------------------------")
- print("Enable Ender Chest mode? (Will break the chest after dropping off) <y/n>")
- while validInput == false do
- event, key = os.pullEvent("char")
- if key == 'y' then
- enderChestMode = true
- print("----------------------------")
- validInput = true
- elseif key == 'n' then
- enderChestMode = false
- print("----------------------------")
- validInput = true
- else
- print("Invalid input")
- end
- end
- end
- if infinite then
- print("Mode: infinite")
- else
- print("Mode: finite. Blocks to break: "..blocksToMove)
- end
- print("Chest placing: "..tostring(placeChests))
- print("Torch placing: "..tostring(placeTorches))
- if placeChests then
- print("Ender Chest mode: "..tostring(enderChestMode))
- end
- end
- function terminate()
- print("Goodbye")
- os.reboot()
- end
- function refuelAdv(value)
- if turtle.getFuelLevel() == "unlimited" then
- return true -- If turtles don't need fuel
- elseif turtle.getFuelLevel() > value - 1 then
- return true
- else
- if value > turtle.getFuelLimit() then
- value = turtle.getFuelLimit()
- end
- for slot = 1, 16 do
- turtle.select(slot)
- while turtle.refuel(0) == true do
- turtle.refuel(1)
- print(turtle.getFuelLevel())
- if turtle.getFuelLevel() > value - 1 then
- print("Enough fuel! "..turtle.getFuelLevel().."/"..value)
- return true
- end
- end
- end
- end
- if (turtle.getFuelLevel() > value - 1) then
- print("Enough fuel! "..turtle.getFuelLevel().."/"..value)
- else
- print("Not enough fuel! "..turtle.getFuelLevel().."/"..value)
- end
- return false
- end
- function mineAllSides()
- if turtle.detectUp() then
- turtle.digUp()
- turtle.suckUp()
- end
- if turtle.detectDown() then
- turtle.digDown()
- turtle.suckDown()
- end
- if turtle.detect() then
- return turtle.dig() -- return false if there is an unbreakable block
- end
- return true
- end
- function tryBreaking()
- local success = false
- local tries = 0
- print("Unbreakable block?")
- while (success == false) and (tries < 10) do
- tries = tries + 1
- print("Trying to break it...")
- success = turtle.dig()
- if turtle.detect() == false then
- success = true
- end
- os.sleep(2)
- end
- return success
- end
- function tryAttacking()
- local success = false
- local tries = 0
- print("Can't move. Mob or gravel blocking the way?")
- while (success == false) and (tries < 20) do
- tries = tries + 1
- print("Trying to attack it...")
- turtle.attack()
- print("Trying to mine it...")
- if turtle.forward() then
- success = true
- break
- else
- os.sleep(2)
- end
- end
- return success
- end
- function placeTorch()
- turtle.select(1)
- if turtle.getItemCount() == 1 then -- if it's the only torch left then it's the last time we're placing them
- placeTorches = false
- end
- turtle.placeDown()
- end
- function placeChest()
- local firstSlot = 1
- local lastSlot = 15
- if placeTorches then
- firstSlot = 2 -- ignore torches in slot 1
- end
- turtle.select(16)
- if turtle.getItemCount() > 0 then
- if (turtle.getItemCount() == 1) and (enderChestMode == false) then
- placeChests = false -- if it's the last chest then it's the last time we're placing it
- end
- if turtle.placeDown() then
- for slot = firstSlot, lastSlot do
- turtle.select(slot)
- if turtle.refuel(0) == false then -- ignore fuel
- turtle.dropDown()
- end
- end
- if enderChestMode then
- turtle.select(16)
- turtle.digDown()
- end
- end
- end
- end
- -- Actual loop
- while run do -- similar to "while true do" but won't run if there was an error while initializing
- local senderId, message, protocol = rednet.receive(0.1) -- Using a timeout to check for messages periodically
- if message == "terminate" then
- run = false
- break
- end
- local complete = false
- local moved = false
- if infinite then
- print("Blocks moved: "..blocksMoved)
- else
- print("Blocks moved: "..blocksMoved.." out of "..blocksToMove)
- if blocksMoved == blocksToMove then
- run = false
- complete = true
- end
- end
- if (turtle.getFuelLevel() < 100) and run then
- refuelAdv(100)
- run = turtle.getFuelLevel() ~= 0
- end
- if (mineAllSides() == false) and run then
- run = tryBreaking()
- end
- if placeTorches and run then
- if torchMoves == 8 then
- placeTorch()
- torchMoves = 0
- else
- torchMoves = torchMoves + 1
- end
- end
- if placeChests and run then
- if turtle.getItemCount(15) > 0 then
- if turtle.getItemCount(16) > 0 then
- placeChest()
- end
- end
- end
- if (turtle.forward() == false) and run then
- if tryAttacking() then
- moved = true -- if moving is successful after attacking
- else
- run = false
- end
- elseif run then
- moved = true -- if moving is successful without doing anything
- end
- if moved then
- blocksMoved = blocksMoved + 1
- end
- if run == false then
- if complete then
- print("Mining complete!")
- else
- print("Program terminated. Sorry!")
- end
- end
- end
- terminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement