Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --test miner
- currentFacing = 0
- currentlySelectedSlot = 1
- fuelLevelToRefuelAt = 5
- dirtBlockCount = 0
- minSlot = 1
- maxSlot = 12
- tunnelLengthMax = 8
- torchesEvery = 3
- baseY = 0
- currentY = 0
- currentLevel = 1
- mineName = "MineX"
- function main()
- -- check parameters
- --fuel
- while (turtle.getItemCount(16) < 2) do
- selectSlot(16)
- if turtle.refuel(1) then
- awaitFix("Too low fuel items")
- else
- awaitFix("No fuel in selected slot")
- end
- end
- --torches
- while (turtle.getItemCount(15) < 16) do
- selectSlot(15)
- awaitFix("Too low torches in selected slot")
- end
- --signs
- while (turtle.getItemCount(14) < 4) do
- selectSlot(14)
- awaitFix("Too low signs in selected slot")
- end
- --chests
- while (turtle.getItemCount(13) < 4) do
- selectSlot(13)
- awaitFix("Too small chests in selected slot")
- end
- -- dirt blocks
- getDirtBlock() --init
- while dirtBlockCount==0 do
- awaitFix("No dirt blocks in 1sts slots")
- getDirtBlock()
- end
- -- y coord
- while baseY==0 do
- print("Enter my Y coordinate: ")
- baseY = tonumber(io.read())
- if baseY < 16 then
- print(".. not valid")
- baseY = 0
- end
- end
- -- init some
- minSlot = dirtBlockCount + 1
- print("Go first level (11)")
- --go first level
- for y=baseY-1,11,-1 do
- go("down", true)
- end
- for l=11,baseY-5,5 do
- --level
- currentY = l
- print("Level: "..currentY)
- selectSlot(13)
- if not turtle.compare() then --if no chest here
- makeShaftHall(tunnelLengthMax)
- --minig process
- turnRight()
- go("forward", true)
- turnLeft() -- i'm on right side of shaftHall
- for tunPos=0,tunnelLengthMax-1,1 do
- go("forward", true) -- first go 0,1,2,3,4,5 .. pos
- if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
- --the best pos to tunneling
- turnRight()
- makeShaft(tunPos+3)
- turnRight() --i'm again on right Side
- if isNeedstoReturn() then
- print("OMG! I need to refuel some shit! returning..")
- --returning to fuel chest
- turnLeft()
- turnLeft()
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- turnRight()
- go("forward", true)
- turnLeft() --saw back
- for rY=currentY,baseY-1,1 do
- go("up", true)
- end
- --chest must be here
- selectSlot(13)
- while not turtle.compare() do
- awaitFix("There is no chest!")
- end
- --load
- reload("fwd")
- while isNeedstoReturn() do
- awaitFix("Place resources to chest!")
- reload("fwd")
- end
- --go back
- for rY=baseY-1,currentY,-1 do
- go("down",true)
- end
- turnLeft()
- go("forward", true)
- turnLeft() -- i'm on right side
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- -- yepp! returned!
- end --if isNeedstoReturn()
- end --digShaft
- end --for tunnel
- -- go("forward", true)
- turnLeft()
- go("forward", true)
- go("forward", true)
- turnLeft()
- for tunPos=tunnelLengthMax-1,0,-1 do
- if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
- --the best pos to tunneling
- turnRight()
- makeShaft(tunPos+3)
- turnRight() --i'm again on right Side
- if isNeedstoReturn() then
- print("OMG! I need to refuel some shit! returning..")
- --returning to fuel chest
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- turnLeft()
- go("forward", true)
- turnRight() --saw back
- for rY=currentY,baseY-1,1 do
- go("up", true)
- end
- --chest must be here
- selectSlot(13)
- while not turtle.compare() do
- awaitFix("There is no chest!")
- end
- --load
- reload("fwd")
- while isNeedstoReturn() do
- awaitFix("Place resources to chest!")
- reload("fwd")
- end
- --go back
- for rY=baseY-1,currentY,-1 do
- go("down",true)
- end
- turnRight()
- go("forward", true)
- turnRight() -- i'm on right side
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- turnLeft()
- turnLeft()
- -- yepp! returned!
- end --if isNeedstoReturn()
- end --digShaft
- go("forward", true) -- first go -63,-62,-61,-60 ..
- end --for tunnel
- turnLeft()
- go("forward", true)
- turnLeft() -- i'm on base position
- end --if chest, skipping level
- for i=1,5,1 do
- go("up", true)
- end
- --did it again
- currentLevel = currentLevel + 1
- end
- --small go up
- for y=currentY,baseY,1 do
- go("up", true)
- end
- print("Well Done!")
- end
- -- make shaft hall, return and unload to placed chest FWD
- function makeShaftHall(tunnelLength)
- local tunPos = 0
- while tunPos < tunnelLength do
- go('forward', true)
- --turn left and dig all in side & up
- turnLeft()
- for i=1,3,1 do
- digNE('forward', false)
- digNE('up', false)
- go('up',true)
- end
- digNE('forward', false)
- -- go right and again down
- turnRight()
- turnRight()
- digNE('forward', false)
- if math.floor((tunPos-2)/3 + 1) == ((tunPos-2)/3 + 1) then
- placeTorch() -- place torch on right side above tunnels
- end
- for i=1,3,1 do
- go('down',true)
- digNE('forward', false)
- end
- turnLeft()
- if tunPos < (tunnelLength-1) then
- digNE('forward', false)
- end
- if checkEmpty('down') then
- print("making bridge")
- putDirtBlock("down") -- for fucking peoples
- end
- tunPos = tunPos + 1
- if math.floor(tunPos/5) == (tunPos/5) then
- if getEmptySlots() < 2 then
- print("Repacking..")
- prettyRepack()
- end
- end
- end
- print("Ohh.. fin! I go back")
- turnRight()
- go('up', true) -- place Sign on right side
- local signText = mineName.."\nLevel: "..currentLevel.." ("..currentY..")\nLength: "..tunnelLength.."\nEXIT --->"
- placeSign(signText)
- go('down', true)
- turnRight()
- while tunPos > 0 do
- go('forward', true)
- tunPos = tunPos - 1
- end
- turnLeft()
- turnLeft()
- --we at start position
- while not placeChest() do
- awaitFix("Can't place chest")
- end
- prettyUnload("forward")
- end
- -- makes single shaft
- -- 1: turtle creates shaft fwd
- -- 2: digging..
- -- z: turtle behind shaft place chest if need and unload
- function makeShaft(tunnelLength)
- local tunPos = 0
- local lastTorch = 0
- local fromSlot = dirtBlockCount + 1
- while tunPos < tunnelLength do
- print("tunPos: "..tunPos)
- --first fwd
- go('forward', true)
- --turn left and look
- turnLeft()
- digNEND("fwd", true)
- go('up', true)
- digNEND("fwd", true)
- go('up', true)
- digNEND("fwd", true)
- if lastTorch >=torchesEvery then --torches
- turnLeft() --turn back
- placeTorch() --back
- turnRight()
- lastTorch = 0
- end
- digNEND("up", true) --look up
- turnRight()
- turnRight()
- digNEND("fwd", true)
- go('down', true)
- digNEND("fwd", true)
- go('down', true)
- digNEND("fwd", true)
- turnLeft()
- if not checkEmpty('down') then
- if not checkDirt('down') then
- dig("down")
- putDirtBlock("down")
- end
- else
- print("Making bridge...")
- putDirtBlock("down") -- for fucking peoples
- end
- --and repeat process
- tunPos = tunPos + 1
- lastTorch = lastTorch + 1
- end
- print("Ohh.. fin! I'll go back")
- turnLeft()
- turnLeft()
- while tunPos > 1 do
- go('forward', true)
- tunPos = tunPos - 1
- end
- putDirtBlock("up") --close tunnel
- go('forward', true)
- tunPos = tunPos - 1
- print("Returned from shaft!")
- if (getEmptySlots() < 3) then --place chest
- if not turtle.detect() then
- print("Placing chest...")
- placeChest("forward")
- end
- print("Unloading...")
- prettyUnload("forward")
- end
- end
- -- BOOL if too small resources
- function isNeedstoReturn()
- local result = (turtle.getItemCount(16) < 5) or (turtle.getItemCount(15) < 10) or (turtle.getItemCount(14) < 4) or (turtle.getItemCount(13) < 4)
- return result
- end
- -- INT place chest FWD
- function placeChest()
- ensureFuel()
- if turtle.getItemCount(13) <=1 then
- print("I have no chests...")
- return false
- else
- selectSlot(13)
- if not turtle.place() then
- print("Can't place chest!")
- return false
- end
- end
- return true
- end
- -- INT place sign FWD with label
- function placeSign(label)
- ensureFuel()
- if turtle.getItemCount(14) <=1 then
- print("I have no signs...")
- return false
- else
- selectSlot(14)
- if not turtle.place(label) then
- print("Can't place sign!")
- return false
- end
- end
- return true
- end
- -- INT get count of empty slots
- function getEmptySlots()
- local emptyslots = 0
- for slot=maxSlot,minSlot,-1 do
- if turtle.getItemCount(slot)==0 then
- emptyslots = emptyslots + 1
- end
- end
- return emptyslots
- end
- -- compress items in inventory and kick dirt
- function prettyRepack()
- local isDirt = true
- local emptySlots = 0
- local cnt = 0
- for slot=maxSlot,minSlot,-1 do
- if turtle.compareTo(16) then --this is fuel, okay!
- turtle.transferTo(16)
- end
- selectSlot(slot)
- isDirt = false
- for dirt=1,dirtBlockCount,1 do
- if turtle.compareTo(dirt) then
- isDirt = true
- end
- end
- --
- for slotB=minSlot,maxSlot,1 do
- if turtle.compareTo(slotB) then
- --try to fill B (move to upslot)
- turtle.transferTo(slotB)
- end
- end
- if (turtle.getItemCount(slot) ~= 0) and (emptySlots < 2) and not isDirt then
- turtle.drop() -- we have no two empty slots and drop all dirt in this slot
- end
- end
- for dirt=1,dirtBlockCount,1 do
- cnt = turtle.getItemCount(dirt)
- if cnt > 1 then
- selectSlot(dirt)
- turtle.drop(cnt-1)
- end
- end
- end
- --unload to side chest
- -- TODO: make unloading not dirt item first
- function prettyUnload(direction)
- ensureFuel()
- for slot=minSlot,maxSlot,1 do
- selectSlot(slot)
- if direction=="up" then
- turtle.dropUp()
- elseif direction=="down" then
- turtle.dropDown()
- else
- turtle.drop()
- end
- end
- for dirt=1,dirtBlockCount,1 do
- cnt = turtle.getItemCount(dirt)
- if cnt > 1 then
- selectSlot(dirt)
- turtle.drop(cnt-1)
- end
- end
- end
- --reload from side chest
- function reload(direction)
- ensureFuel()
- for slot=13,16,1 do
- selectSlot(slot)
- if direction=="up" then
- turtle.suckUp()
- elseif direction=="down" then
- turtle.suckDown()
- else
- turtle.suck()
- end
- for slot2=minSlot,maxSlot,1 do
- if turtle.compareTo(slot) then
- selectSlot(slot2)
- -- unload unused
- if direction=="up" then
- turtle.dropUp()
- elseif direction=="down" then
- turtle.dropDown()
- else
- turtle.drop()
- end
- end
- end
- end
- end
- -- dig if block ON side and put Dirt - crazy %)
- function digNE(direction, putDirt)
- if not checkEmpty(direction) then
- while not checkEmpty(direction) do --for gravel ore
- dig(direction)
- end
- if putDirt then
- putDirtBlock(direction)
- end
- end
- end
- -- dig if block ON side and this block NOT dirt, and put Dirt
- function digNEND(direction, putDirt)
- if not checkEmpty(direction) then
- if not checkDirt(direction) then
- while not checkEmpty(direction) do --for gravel ore
- dig(direction)
- end
- if putDirt then
- putDirtBlock(direction)
- end
- end
- end
- end
- -- BOOL place torch on side
- function placeTorch()
- ensureFuel()
- if turtle.getItemCount(15) <=1 then
- print("I have no torches...")
- return false
- else
- selectSlot(15)
- if not turtle.place() then
- print("Can't place torch!")
- return false
- end
- end
- return true
- end
- -- BOOL put dirt block on side
- function putDirtBlock(side)
- ensureFuel()
- local toSelect = 1
- toSelect = getDirtBlock()
- if toSelect==false then
- return false
- end
- selectSlot(toSelect)
- if side=="up" then
- return turtle.placeUp()
- elseif side=="down" then
- return turtle.placeDown()
- else
- return turtle.place()
- end
- return false
- end
- -- INT get first inventory id with dirt blocks
- function getDirtBlock()
- if dirtBlockCount==0 then
- selectSlot(1)
- while turtle.getItemCount(currentlySelectedSlot)~=0 do
- dirtBlockCount = dirtBlockCount + 1
- selectSlot(currentlySelectedSlot + 1)
- end
- end
- if dirtBlockCount==0 then
- return false
- end
- local fromSlot = dirtBlockCount + 1
- for i=fromSlot,11,1 do
- if turtle.getItemCount(i)>1 then
- selectSlot(i)
- for j=1,dirtBlockCount,1 do
- if turtle.compareTo(j) then
- return i
- end
- end
- end
- end
- return false
- end
- -- BOOL check if block on side is Dirt
- function checkDirt(side)
- local result = false
- if dirtBlockCount==0 then
- getDirtBlock()
- end
- for j=1,dirtBlockCount,1 do
- selectSlot(j)
- if side=="up" then
- result = turtle.compareUp()
- elseif side=="down" then
- result = turtle.compareDown()
- else
- result = turtle.compare()
- end
- if result then
- return result
- end
- end
- return result
- end
- -- BOOL check if block on side is not Exists
- function checkEmpty(side)
- local result = false
- if side=="up" then
- result = not turtle.detectUp()
- elseif side=="down" then
- result = not turtle.detectDown()
- else
- result = not turtle.detect()
- end
- return result
- end
- --select slot
- function selectSlot(x)
- if x<1 then
- x = 1
- elseif x > 16 then
- x = 16
- end
- turtle.select(x)
- currentlySelectedSlot = x
- end
- --Check fuel, refueling from inventory special slot or not
- function ensureFuel()
- -- Determine whether a refuel is required
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel ~= "unlimited") then
- if (fuelLevel < fuelLevelToRefuelAt) then
- -- Need to refuel
- selectSlot(16)
- local fuelItems = turtle.getItemCount(16)
- -- Do we need to impact the emergency fuel to continue? (always
- -- keep one fuel item in slot 16)
- if (fuelItems == 0) then
- awaitFix("I want more fuel!")
- elseif (fuelItems == 1) then
- --load fuel from another slot
- local fueled = false
- local fromSlot = dirtBlockCount + 1
- for i=fromSlot,15,1 do
- selectSlot(i)
- if (turtle.compareTo(16)) then
- print("Refueling from not fuel slot: "+i)
- turtle.refuel(1)
- turtle.transferTo(16)
- fueled = true
- end
- end
- if not fueled then
- print("completely out of fuel!")
- end
- else --fuelItems > 1
- print("Time to refueling!")
- turtle.refuel(1)
- end
- end
- end
- end
- --turns
- function turnFacing(facing)
- while facing ~= currentFacing do
- turnRight()
- end
- end
- --turns
- function turnLeft()
- ensureFuel()
- turtle.turnLeft()
- currentFacing = currentFacing - 1
- if currentFacing < 0 then
- currentFacing = 3
- end
- end
- --turns
- function turnRight()
- ensureFuel()
- turtle.turnRight()
- currentFacing = currentFacing + 1
- if currentFacing >= 4 then
- currentFacing = 0
- end
- end
- -- BOOL dig direction with retries
- function dig(direction)
- local result = false
- local tries = 0
- selectSlot(1)
- while (not result) and (tries < 10) do
- ensureFuel()
- if direction=="up" then
- result = turtle.digUp()
- elseif direction=="down" then
- result = turtle.digDown()
- else
- result = turtle.dig()
- end
- tries = tries + 1
- end
- if not result then
- print ("can't dig in "..direction)
- end
- return result
- end
- --go direction with breaking blocks and refueling
- function go(direction, allowDig)
- if direction=="right" then
- turnRight()
- direction="forward"
- end
- if direction=="left" then
- turnLeft()
- direction="forward"
- end
- if direction=="forward" then
- while turtle.detect() do
- if allowDig then
- ensureFuel()
- turtle.dig()
- else
- awaitFix("can't forward")
- end
- end
- while not turtle.forward() do
- ensureFuel()
- turtle.dig()
- end
- end
- if direction=="up" then
- while turtle.detectUp() do
- if allowDig then
- ensureFuel()
- turtle.digUp()
- else
- awaitFix("can't up")
- end
- end
- while not turtle.up() do
- ensureFuel()
- turtle.digUp()
- end
- end
- if direction=="down" then
- while turtle.detectDown() do
- if allowDig then
- ensureFuel()
- turtle.digDown()
- else
- awaitFix("can't down")
- end
- end
- while not turtle.down() do
- ensureFuel()
- turtle.digDown()
- end
- end
- end
- --wait for user action
- function awaitFix(message)
- print("I have problem: " .. message)
- print("Please fix it and press Enter to continue")
- io.read()
- end
- main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment