Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --test miner by shaolin v 2
- currentFacing = 0
- currentlySelectedSlot = 1
- fuelLevelToRefuelAt = 5
- dirtBlockCount = 0
- minSlot = 1
- maxSlot = 14
- tunnelLengthMax = 0
- torchesEvery = 8
- baseY = 0
- currentY = 0
- currentLevel = 1
- mineName = ""
- args = { ... }
- function main()
- if #args > 1 then
- baseY = args[1]
- tunnelLengthMax = tonumber(args[2])
- else
- print("Usage: mine <Y Coord> <Length>")
- return false
- end
- -- check parameters
- print("[ mineX2 loaded ]")
- while tunnelLengthMax < 8 do
- print("Enter valid tunnel length: ")
- tunnelLengthMax = tonumber(io.read())
- end
- --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
- -- dirt blocks
- getDirtBlock() --init
- while dirtBlockCount==0 do
- awaitFix("No dirt blocks in first slots")
- getDirtBlock()
- end
- minSlot = dirtBlockCount + 1
- print("Go first level (7)")
- --go first level
- turnRight()
- for y=baseY-1,7,-1 do
- go("down", true)
- digNE("forward",false)
- end
- turnLeft()
- for l=7,baseY-5,5 do
- --level
- currentY = l
- print("Level: "..currentY)
- if not detectAlreadyMined(true) then
- makeShaftHall(tunnelLengthMax)
- turnRight()
- go("forward", true)
- turnLeft() -- i'm on right side
- tunPos = 0
- while tunPos < tunnelLengthMax-4 do
- go("forward", true) -- first go 0,1,2,3,4,5 .. pos
- if math.floor((tunPos-2)/6 + 1) == ((tunPos-2)/6 + 1) then --the best pos to tunneling
- turnRight()
- if not detectAlreadyMined(false) then
- makeShaftDouble(tunnelLengthMax - 1)
- tunPos = tunPos + 3
- --Go to unloading here
- if getEmptySlots() < 2 then
- go("forward", true)
- turnLeft()
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- for rY=currentY,baseY-1,1 do
- go("up", true)
- end
- prettyUnload("forward")
- for rY=baseY-1,currentY,-1 do
- go("down",true)
- end
- turnLeft()
- turnLeft()
- for rX=tunPos,0,-1 do
- go("forward", true)
- end
- turnRight()
- go("forward", true)
- turnLeft()
- turnLeft()
- end
- turnRight()
- end
- end
- tunPos = tunPos + 1
- end
- turnLeft()
- go("forward", true)
- turnLeft()
- for retunPos = tunPos-1,1,-1 do
- go("forward", true)
- end
- turnLeft()
- putDirtNE("fwd") --close mined tunnel
- go("up", true)
- putDirtNE("fwd")
- putDirtNE("up")
- turnLeft()
- turnLeft()
- putDirtNE("fwd")
- go("down", true)
- putDirtNE("fwd")
- turnLeft()
- go("forward", true)
- turnLeft()
- turnLeft() --i'm on start position
- else
- print("Skipping shaft..")
- end
- for i=1,5 do
- go("up", true)
- end
- end
- go("up", true)
- turnLeft()
- turnLeft()
- prettyUnload("forward")
- turnLeft()
- turnLeft()
- print("Well Done!")
- end
- function detectAlreadyMined(bigshaft)
- local da,db,dc
- if not turtle.detect() then
- if bigshaft then
- go('up', true)
- da = turtle.detect()
- go('up', true)
- db = turtle.detect()
- go('down', true)
- go('down', true)
- go('forward', true)
- turnRight()
- dc = turtle.detect()
- turnRight()
- go('forward', true)
- turnRight()
- turnRight()
- return (not da) and db and dc;
- else
- da = turtle.detect()
- go('up', true)
- db = turtle.detect()
- go('up', true)
- dc = turtle.detect()
- go('down', true)
- go('down', true)
- return (not da) and db and (not dc);
- end
- end
- return false;
- end
- function gidScanUpDown(fillDown,fillWalls,isplaceTorch)
- turnLeft()
- for i=1,2 do
- digNEND("fwd", true)
- if fillWalls then
- putDirtNE("fwd")
- end
- go('up', true)
- end
- digNEND("fwd", true)
- digNEND("up", true)
- if fillWalls then
- putDirtNE("up")
- end
- if isplaceTorch then --torches
- turnLeft() --turn back
- placeTorch("forward") --back
- turnRight()
- end
- turnRight()
- turnRight()
- digNEND("fwd", true)
- for i=1,2 do
- go('down', true)
- digNEND("fwd", true)
- if fillWalls then
- putDirtNE("fwd")
- end
- end
- digNEND("down", true)
- if fillDown then
- putDirtNE("down")
- end
- turnLeft()
- end
- function makeShaftHall(tunnelLength)
- print("Making hall..")
- tunPos = 0
- local lastTorch = torchesEvery - 2
- while tunPos < tunnelLengthMax-1 do
- go('forward', true)
- putDirtNE("down")
- turnLeft()
- go('forward', true)
- putDirtNE("down")
- turnLeft()
- turnLeft()
- go('up', true)
- go('up', true)
- if lastTorch >=torchesEvery then
- turnRight()
- placeTorch("forward") --back
- turnLeft() --turn back
- lastTorch = 0
- end
- go('forward', true)
- go('down', true)
- go('down', true)
- go('forward', true)
- putDirtNE("down")
- go('up', true)
- go('up', true)
- turnLeft()
- turnLeft()
- go('down', true)
- go('down', true)
- go('forward', true)
- turnRight()
- tunPos = tunPos + 1
- lastTorch = lastTorch + 1
- end
- turnRight()
- turnRight()
- for tunPos = tunnelLengthMax - 1,1,-1 do
- go('forward', true)
- end
- turnLeft()
- turnLeft()
- end
- -- makes double shaft
- function makeShaftDouble(tunnelLength)
- print("Making shaft: "..tunnelLength.." blocks")
- local tunPos = 0
- local lastTorch = 0
- local firstPos = true
- local torchNeed = true
- for i=1,2 do
- tunPos = 0
- lastTorch = 0
- while tunPos < tunnelLength do
- firstPos = ((tunPos == 0) and (i==1)) or ((tunPos == tunnelLength -1) and (i==2))
- torchNeed = lastTorch >=torchesEvery
- --first fwd
- go('forward', true)
- gidScanUpDown(true,firstPos,torchNeed)
- if firstPos then
- putDirtBlock("up") --close tunnel
- end
- if torchNeed then --torches
- lastTorch = 0
- end
- --and repeat process
- tunPos = tunPos + 1
- lastTorch = lastTorch + 1
- end
- if ( i==1) and (tunPos==tunnelLength) and (getEmptySlots() < 2) then
- print("Repacking..")
- prettyRepack()
- end
- if i==1 then
- turnLeft()
- go('forward', true)
- gidScanUpDown(true,false,false)
- go('forward', true)
- gidScanUpDown(true,false,false)
- go('forward', true)
- gidScanUpDown(true,false,false)
- turnLeft()
- print("Making shaft back")
- end
- tunnelLength = tunnelLength - 1
- end
- go('forward', true)
- print("Returned from shaft!")
- 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 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 (getEmptySlots() < 2) and isDirt then
- print("I'l drop this stuff cuz need more space!")
- 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 > 10 then
- selectSlot(dirt)
- turtle.drop(cnt-10) -- save 10 blocks to put in holes
- 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 turtle.compareTo(16) then
- --try to store fuel
- turtle.transferTo(16)
- print("Fuel! I'll take a little bit.")
- end
- 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 > 10 then
- selectSlot(dirt)
- turtle.drop(cnt-10) -- save 10 blocks to put in holes
- 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(direction)
- ensureFuel()
- if turtle.getItemCount(15) <=1 then
- print("I have no torches...")
- return false
- else
- selectSlot(15)
- if direction=="up" then
- return turtle.placeUp()
- elseif direction=="down" then
- return turtle.placeDown()
- else
- return turtle.place()
- end
- end
- return false
- end
- --
- function putDirtNE(direction)
- if checkEmpty(direction) then
- putDirtBlock(direction)
- end
- 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
- for i=minSlot,maxSlot,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
- for i=1,dirtBlockCount,1 do
- if turtle.getItemCount(i)>2 then --get from 1st slot
- return i
- 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.refuel(0)) then -- I can eat that
- 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