Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- efficient mining program
- -- slot 1: stone
- -- slot 2: chest
- -- if enderchest
- -- slot 3: dump chest
- -- slot 4: refuel chest
- local sstone = 1
- local schest = 2
- local sdump = 3
- local sfuel = 4
- local yreset = 90
- local controlID = 871
- local args = {...} -- Xchunk, Zchunk
- x,y,z,f = 0,0,0,0
- rednet.close("right")
- rednet.open("right")
- -- set default values
- local yTravel = 90
- local ender = true
- -- some functions
- function dump()
- if ender then
- cx,cy,cz = loc()
- sendStatus("dumping inventory",cx,cy,cz)
- print("dumping inventory")
- -- put down dump chest
- turtle.select(sdump)
- while turtle.detectDown() do
- turtle.digDown()
- sleep(0.2)
- end
- while not turtle.placeDown() do
- turtle.attackDown()
- sleep(0.2)
- end
- -- drop stuff
- for s = 16,5,-1 do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- while not turtle.dropDown() do
- sleep(0.2)
- end
- end
- end
- -- pick up dump chest
- turtle.select(sdump)
- turtle.digDown()
- end
- end
- function refuel(level)
- if ender then
- -- free up inventory
- dump()
- -- put down fuel chest
- cx,cy,cz = gps.locate()
- sendStatus("refueling",cx,cy,cz)
- print("refueling")
- turtle.select(sfuel)
- while turtle.detectDown() do
- turtle.digDown()
- sleep(0.2)
- end
- while not turtle.placeDown() do
- turtle.attack()
- sleep(0.2)
- end
- -- get fuel from chest
- while turtle.getFuelLevel() < level do
- if turtle.getItemCount(sfuel) < 1 then
- turtle.suckDown()
- end
- turtle.refuel(1)
- print("fueled up to "..turtle.getFuelLevel().." / "..level)
- end
- -- put remaining fuel back in chest and move it out of the way if the chest is full
- turtle.dropDown()
- if turtle.getItemCount(sfuel) > 0 then
- for s = 16,5,-1 do
- if turtle.getItemCount(s) == 0 then
- turtle.transferTo(s)
- break
- end
- end
- end
- -- pick up fuel chest
- turtle.select(sfuel)
- turtle.digDown()
- turtle.select(schest) -- select normal chest in inventory
- sendStatus("digging / moving",cx,cy,cz)
- end
- end
- function left(turns)
- for i = 1,turns do
- turtle.turnLeft()
- f = f - 1
- if f < 0 then
- f = 3
- end
- end
- return f
- end
- function right(turns)
- for i = 1,turns do
- turtle.turnRight()
- f = f + 1
- if f > 3 then
- f = 0
- end
- end
- return f
- end
- function move(dir, dist, cf)
- turtle.select(schest)
- for d = 1, dist do
- if dir == "f" then
- while not turtle.forward() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.compare() then
- emptyChest(dir)
- else
- if not turtle.dig() then
- turtle.attack()
- sleep(0.2)
- end
- end
- end
- if cf == 0 then
- z = z + 1
- elseif f == 1 then
- x = x - 1
- elseif f == 2 then
- z = z - 1
- elseif f == 3 then
- x = x + 1
- end
- elseif dir == "u" then
- while not turtle.up() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.compareUp() then
- emptyChest(dir)
- else
- if not turtle.digUp() then
- turtle.attack()
- sleep(0.2)
- end
- end
- end
- y = y + 1
- elseif dir == "d" then
- while not turtle.down() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.compareDown() then
- emptyChest(dir)
- else
- if not turtle.digDown() then
- turtle.attack()
- sleep(0.2)
- end
- end
- end
- y = y - 1
- elseif dir == "b" then
- while not turtle.back() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- else
- left(2)
- if not turtle.dig() then
- turtle.attack()
- sleep(0.2)
- end
- right(2)
- end
- end
- if cf == 0 then
- z = z - 1
- elseif f == 1 then
- x = x + 1
- elseif f == 2 then
- z = z + 1
- elseif f == 3 then
- x = x - 1
- end
- end
- end
- return x,y,z
- end
- function findDir()
- x1,_,z1 = loc()
- local moved = false
- local dir = -1
- for d = 1,4 do
- if not turtle.detect() then
- while not turtle.forward() do
- if turtle.getFuelLevel() == 0 then
- refuel(2)
- else
- turtle.attack()
- sleep(0.2)
- end
- end
- moved = true
- break
- else
- left(1)
- end
- end
- if not moved then
- move("f",1,dir)
- end
- x2,_,z2 = loc()
- move("b",1)
- if x2 > x1 then
- dir = 3
- elseif x2 < x1 then
- dir = 1
- elseif z2 > z1 then
- dir = 0
- elseif z2 < z1 then
- dir = 2
- end
- return dir
- end
- function turnTo(tf, cf)
- if tf ~= cf then
- print("turning to "..tf)
- local deltaf = tf - cf
- if (deltaf == 1) or (deltaf == -3) then
- right(1)
- elseif (deltaf == -1) or (deltaf == 3) then
- left(1)
- elseif (deltaf == 2) or (deltaf == -2) then
- left(2)
- end
- end
- return tf
- end
- function goto(tx,ty,tz,cf)
- local cx,cy,cz = loc()
- sendStatus("going to "..tx.."/"..ty.."/"..tz,cx,cy,cz)
- print("going to "..tx.."/"..ty.."/"..tz)
- if (cx ~= tx) or (cz ~= tz) then
- -- goto y travel
- if yTravel > cy then
- local deltay = yTravel - cy
- cx,cy,cz = move("u",deltay,cf)
- elseif y > yTravel then
- local deltay = y - yTravel
- cx,cy,cz = move("d",deltay,cf)
- end
- -- move to tx
- if tx ~= cx then
- local deltax = math.abs(tx - cx)
- if tx > cx then
- cf = turnTo(3,cf)
- elseif tx < cx then
- cf = turnTo(1,cf)
- end
- cx,cy,cz = move("f",deltax,cf)
- end
- -- move to tz
- if tz ~= cz then
- local deltaz = math.abs(tz - cz)
- if tz > cz then
- cf = turnTo(0,cf)
- elseif tz < cz then
- cf = turnTo(2,cf)
- end
- cx,cy,cz = move("f",deltaz,cf)
- end
- end
- -- goto y target
- if ty > cy then
- local deltay = ty - cy
- cx,cy,cz = move("u",deltay,cf)
- elseif y > ty then
- local deltay = y - ty
- cx,cy,cz = move("d",deltay,cf)
- end
- return cx,cy,cz,cf
- end
- function compdig()
- if turtle.getItemCount(16) > 0 then
- dump()
- sendStatus("digging",cx,cy,cz)
- end
- turtle.select(schest)
- if turtle.detectUp() then
- if turtle.compareUp() then
- emptyChest("u")
- sendStatus("digging",cx,cy,cz)
- end
- end
- if turtle.detectDown() then
- if turtle.compareDown() then
- emptyChest("d")
- sendStatus("digging",cx,cy,cz)
- end
- end
- turtle.select(sstone)
- if turtle.getItemCount(16) > 0 then
- dump()
- sendStatus("digging",cx,cy,cz)
- end
- if not turtle.compareUp() then
- turtle.digUp()
- end
- if turtle.getItemCount(16) > 0 then
- dump()
- sendStatus("digging",cx,cy,cz)
- end
- if not turtle.compareDown() then
- turtle.digDown()
- end
- end
- function digLayer(cx,cy,cz,cf)
- --refuel(256)
- sendStatus("digging",cx,cy,cz)
- for dx = 1,16 do
- for dz = 1,16 do
- compdig()
- if dz < 16 then
- cx,cy,cz = move("f",1,cf)
- end
- end
- if dx < 16 then
- if dx % 2 == 1 then
- cf = left(1)
- cx,cy,cz = move("f",1,cf)
- cf = left(1)
- else
- cf = right(1)
- cx,cy,cz = move("f",1,cf)
- cf = right(1)
- end
- end
- end
- return cx,cy,cz,cf
- end
- function digLayers(cx,cy,cz,cf)
- -- get into correct layer
- while cy % 3 ~= 2 do
- cx,cy,cz = move("d",1,cf)
- end
- while cy > 5 do
- cx,cy,cz,cf = digLayer(cx,cy,cz,cf)
- if cy > 5 then
- cf = left(1)
- cx,cy,cz,cf = move("d",3,cf)
- end
- end
- return cx,cy,cz,cf
- end
- function emptyChest(dir)
- cx,cy,cz = loc()
- sendStatus("found Chest",cx,cy,cz)
- print("found a chest, retrieveing items")
- if ender then
- if dir ~= "d" then
- turtle.select(sdump)
- while turtle.detectDown() do
- turtle.digDown()
- sleep(0.2)
- end
- while not turtle.placeDown() do
- turtle.attackDown()
- sleep(0.2)
- end
- -- drop stuff
- for s = 16,5,-1 do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- while not turtle.dropDown() do
- sleep(0.2)
- end
- end
- end
- -- transfer stuff from chest into enderchest
- if dir == "u" then
- while turtle.suckUp() do
- if turtle.getItemCount(5) > 0 then
- while not turtle.dropDown() do
- sleep(1)
- end
- end
- end
- elseif dir == "f" then
- while turtle.suck() do
- if turtle.getItemCount(5) > 0 then
- while not turtle.dropDown() do
- sleep(1)
- end
- end
- end
- end
- -- pick up dump chest
- turtle.select(sdump)
- turtle.digDown()
- else
- turtle.select(sdump)
- while turtle.detect() do
- turtle.dig()
- sleep(0.2)
- end
- while not turtle.place() do
- turtle.attack()
- sleep(0.2)
- end
- -- drop stuff
- for s = 16,5,-1 do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- while not turtle.drop() do
- sleep(0.2)
- end
- end
- end
- -- transfer stuff from chest into enderchest
- while turtle.suckDown() do
- if turtle.getItemCount(5) > 0 then
- while not turtle.drop() do
- sleep(1)
- end
- end
- end
- -- pick up dump chest
- turtle.select(sdump)
- turtle.dig()
- end
- end
- end
- function sendStatus(status,cx,cy,cz)
- local chunkX = math.floor(cx/16)
- local chunkZ = math.floor(cz/16)
- rednet.send(controlID, chunkX.." / "..chunkZ.." "..cy.." "..status)
- end
- function loc()
- x = nil
- while x == nil do
- x,y,z = gps.locate(1)
- if x == nil then
- print("couldn't find position, waiting for weather to clear")
- sleep(60)
- end
- end
- return x,y,z
- end
- -- parse commandline
- if #args == 1 then
- local x,y,z = loc()
- local f = findDir()
- local chunkx = math.floor(x / 16)
- local chunkz = math.floor(z / 16)
- if args[1] == "getchunk" then
- print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
- elseif args[1] == "chunkstart" then
- local xstart = chunkx * 16
- local zstart = chunkz * 16
- goto(xstart,y,zstart)
- end
- end
- -- check if items are complete
- local complete = true
- for s = 4,1,-1 do
- if turtle.getItemCount(s) < 1 then
- complete = false
- break
- end
- end
- if complete then
- x,y,z = loc()
- local chunkx = math.floor(x / 16)
- local chunkz = math.floor(z / 16)
- local xstart = chunkx * 16
- local zstart = chunkz * 16
- local f = findDir()
- print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
- x,y,z,f = goto(xstart,y,zstart,f)
- f = turnTo(0,f)
- x,y,z,f = digLayers(x,y,z,f)
- x,y,z,f = goto(xstart,yreset,zstart,f)
- dump()
- --print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
- sendStatus("done",x,y,z)
- else
- x,y,z = loc()
- sendStatus("incomplete",x,y,z)
- print("gimme my stuff! (stone, chest, dumpchest, fuelchest)")
- end
Advertisement
Add Comment
Please, Sign In to add comment