Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function splash() -- splash screen
- term.clear()
- term.setCursorPos(1,1)
- print("Deep Underground Mining Buddy")
- print(" v1.0 by mrkite\n\n")
- end
- function gety() -- get the current y position
- print("Stand next to me and tell me your feet position")
- return tonumber(io.read())
- end
- function getwidth() -- get the width of the quarry
- print("How large an area should I dig? (example: 30)")
- return tonumber(io.read())
- end
- function emptyInventory() -- dump all but slot 1 (fuel)
- for i=1,16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- function refuel() -- refuel all fuel we're carrying
- for i=1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- turtle.select(1)
- end
- function flip() -- do a 180
- left()
- left()
- end
- function up() -- move up and keep track of our position
- local moved=false
- while not moved do
- if turtle.detectUp() then
- turtle.digUp()
- end
- if turtle.up() then
- pos.y=pos.y+1
- fromHome=fromHome-1 -- up is always closer to home
- moved=true
- else
- turtle.attackUp()
- end
- end
- end
- function down() -- move down and keep track of our position
- local moved=false
- while not moved do
- if turtle.detectDown() then
- turtle.digDown()
- end
- if turtle.down() then
- pos.y=pos.y-1
- fromHome=fromHome+1 -- down is always away from home
- moved=true
- else
- turtle.attackDown()
- end
- end
- end
- function forward() -- more forward and keep track of our position
- local moved=false
- while not moved do
- if turtle.detect() then
- turtle.dig()
- end
- if turtle.forward() then
- if facing==0 then -- facing home
- pos.x=pos.x-1
- fromHome=fromHome-1
- elseif facing==1 then -- facing sidebranch
- pos.z=pos.z+1
- fromHome=fromHome+1
- elseif facing==2 then -- facing away from home
- pos.x=pos.x+1
- fromHome=fromHome+1
- else -- facing main tunnel
- pos.z=pos.z-1
- fromHome=fromHome-1
- end
- moved=true
- else
- turtle.attack() -- couldn't move, someone in the way?
- end
- end
- end
- function left() -- turn left and keep track of direction
- turtle.turnLeft()
- if facing==0 then
- facing=3
- else
- facing=facing-1
- end
- end
- function right() -- turn right and keep track of direction
- turtle.turnRight()
- if facing==3 then
- facing=0
- else
- facing=facing+1
- end
- end
- function faceMain()
- while facing~=3 do
- left()
- end
- end
- function faceHome()
- while facing~=0 do
- left()
- end
- end
- function returnHome(temporary) -- return to home, housekeep, and possibly return
- local oldX=pos.x
- local oldY=pos.y
- local oldZ=pos.z
- local oldFace=facing
- print("returning home")
- faceMain()
- while pos.z~=0 do -- head back to main tunnel if necessary
- forward()
- end
- faceHome()
- while pos.x~=0 do -- head back home if necessary
- forward()
- end
- while pos.y<home do -- we're at home, but below it
- up()
- end
- refuel() -- lets refuel before we empty our inventory
- emptyInventory()
- if turtle.getFuelLevel()==0 then -- failed to refuel
- print("Waiting for fuel")
- while turtle.getFuelLevel()==0 do
- sleep(1)
- refuel()
- end
- end
- if temporary then -- return to where we left off
- print("returning to duty")
- while pos.y>oldY do
- checkAll()
- down()
- end
- flip()
- while pos.x<oldX do
- checkAll()
- forward()
- end
- left()
- while pos.z<oldZ do
- checkAll()
- forward()
- end
- while facing~=oldFace do
- left()
- end
- end
- end
- function checkFuel() -- checks if we only have enough fuel to get home
- if turtle.getFuelLevel()==fromHome then
- print("refueling")
- refuel()
- if turtle.getFuelLevel()==fromHome then -- did we fail?
- returnHome(true) -- return home to refuel, but come back
- end
- end
- end
- function checkStorage() -- checks to see if we have any empty slots
- for i=1,16 do
- if turtle.getItemCount(i)==0 then -- we're okay as long as one is empty
- return
- end
- end
- returnHome(true) -- return home to unload, but come back
- end
- function checkAll() -- checks all reasons for returning home
- checkFuel()
- checkStorage()
- end
- function downToBottom() -- step 1.. tunnel down to 7
- while pos.y>7 do
- checkAll()
- down()
- end
- end
- function mainTunnel(cnt) -- dig the main tunnel
- while cnt>0 do
- checkAll()
- forward()
- cnt=cnt-1
- end
- end
- function sideTunnel(cnt) -- dig a side tunnel
- while cnt>0 do
- checkAll()
- forward()
- turtle.digUp()
- turtle.digDown()
- turtle.turnLeft() -- no need to keep track of position here
- turtle.dig()
- turtle.turnLeft() -- since we spin completely around
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft() -- and end up facing the original direction
- cnt=cnt-1
- end
- end
- function branch() -- do the branch mine from 7 to 15
- for y=7,14 do
- for m=0,mainLength do
- --serpentine
- if m%2==y%2 then
- right()
- else
- left()
- end
- sideTunnel(branchWidth)
- if m%2==y%2 then
- left()
- else
- right()
- end
- mainTunnel(5)
- end
- up()
- flip()
- if y%2==1 then
- mainTunnel(2)
- else
- mainTunnel(3)
- end
- end
- end
- -- main routine
- splash()
- pos={}
- pos.x=0 --home is always at 0,0
- pos.z=0
- pos.y=gety()
- if pos.y<16 then
- print("Error - I should be started at 16 or above")
- exit()
- end
- home=pos.y
- fromHome=0 -- we start 0 moves from home
- facing=2 -- we start facing 2
- branchWidth=getwidth()
- mainLength=math.floor(branchWidth/5)
- if mainLength%2==0 then -- must be an odd number or things will get screwed
- mainLength=mainLength+1
- end
- print("Make sure there's a chest behind me, then press any key to begin")
- a,b=os.pullEvent()
- while a~="key" do a,b=os.pullEvent() end
- flip() -- turn around to align with chest
- checkAll()
- downToBottom()
- flip() -- face the mine
- branch()
- returnHome(false) -- we're done, go home and stay there
- flip() -- face away from chest
- print("All done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement