Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function saveFile()
- local file = fs.open("dataList","w")
- file.write(textutils.serialize(dataList))
- file.close()
- end
- function loadFile()
- local file = fs.open("dataList","r")
- local data = file.readAll()
- file.close()
- if string.len(data) > 0 and data ~= nil then
- return textutils.unserialize(data)
- end
- end
- local slot = 1
- dataList = {}
- dataList.x = 0
- dataList.y = 0
- dataList.z = 0
- dataList.side = 0
- dataList.ignore = 0
- dataList.total = 0
- local oldx = 0
- local oldy = 0
- local oldz = 0
- local oldside = 0
- dataList = loadFile()
- --parameter for hole (square)
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: miner <diameter>" )
- return
- end
- local size = tonumber( tArgs[1] )-1
- if size < 1 then
- print( "Miner diameter must be positive." )
- return
- end
- --takes all important start informations and gives infos
- function start()
- if dataList.total == 0 then
- print("Place a chest with fuel on the left of the turtle!")
- print("The turtle will also consume coal and other fuel resources, which it mines.")
- print(" ")
- print("Place blocks you don't want to get mined in the first few slots!")
- print("The 1st slot will be used to cover the holes.")
- print("Please place a cobble stone in the 2nd slot!")
- print("When you've done, press any key!")
- os.pullEvent("key")
- local i
- for i=1, 16, 1 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- dataList.ignore = dataList.ignore +1
- end
- end
- print("You want to ignore " ..dataList.ignore.." blocks.")
- turn(3)
- for i=0,2,1 do
- turtle.select(ignore+1)
- turtle.suck()
- turtle.refuel()
- turtle.drop()
- end
- local fuel = turtle.getFuelLevel()
- print("Fuel level: "..fuel)
- turn(1)
- dataList.total = dataList.total+1
- end
- end
- --drops all the items and counts them
- function drop()
- local slot
- home()
- turn(2)
- print("Full inventory, dropping items into chest.")
- for slot=1, ignore ,1 do
- turtle.select(slot)
- local items = math.floor(turtle.getItemCount(slot)/2)
- dataList.total = dataList.total + items
- turtle.drop(items)
- end
- for slot=dataList.ignore+1, 16 do
- turtle.select(slot)
- local items = turtle.getItemCount(slot)
- dataList.total = dataList.total + items
- turtle.drop()
- end
- while dataList.side ~= 0 do
- turn(1)
- end
- back()
- end
- --checks if theres enough fuel for the movement
- function fuelcheck(togo)
- local way = dataList.x+dataList.y+math.abs(dataList.z)+togo
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- if turtle.getFuelLevel() < way then
- local fueled = false
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < way do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= way then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- print("Fuel Level: " ..fuelLevel)
- return true
- end
- function refuel()
- print("refueling.")
- home()
- turn(3)
- for i=0,2,1 do
- turtle.select(dataList.ignore+1)
- turtle.suck()
- turtle.refuel()
- turtle.drop()
- end
- print("Fuel Level: " ..turtle.getFuelLevel())
- back()
- end
- function turn(times)
- local i
- for i=1, times, 1 do
- turtle.turnRight()
- dataList.side = (dataList.side +1)%4
- end
- end
- function forward(dist)
- local i
- for i=1, dist, 1 do
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- if dataList.side == 0 then
- dataList.x = dataList.x+1
- elseif dataList.side == 1 then
- dataList.y = dataList.y+1
- elseif dataList.side == 2 then
- dataList.x = dataList.x-1
- elseif dataList.side == 3 then
- dataList.y = dataList.y-1
- end
- end
- end
- function down(dist)
- local i
- for i=1, dist, 1 do
- if turtle.down() == false then
- turtle.digDown()
- if turtle.down() == false then
- return false
- end
- end
- dataList.z = dataList.z-1
- end
- return true
- end
- function up()
- local i
- local tempz = math.abs(z)
- for i=1, tempz, 1 do
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- end
- dataList.z = dataList.z+1
- end
- end
- function dropCobble()
- local i
- turtle.select(2)
- for i=3,16,1 do
- if turtle.compareTo(i) then
- turtle.select(i)
- turtle.dropDown()
- turtle.select(2)
- end
- end
- end
- function cover()
- turtle.select(1)
- down(1)
- dropCobble()
- local i
- for i=1,4,1 do
- if turtle.detect() == false then
- if turtle.getItemCount(1) == 0 then
- home()
- print("No blocks to cover the holes!")
- print("Place some blocks in the first slot")
- print("Press any key if you've done")
- os.pullEvent("key")
- back()
- end
- turtle.place()
- end
- turn(1)
- end
- turtle.up()
- dataList.z=dataList.z+1
- if turtle.getItemCount(1) == 0 then
- home()
- print("No blocks to cover the holes!")
- print("Place some blocks in the first slot")
- print("Press any key if you've done")
- os.pullEvent("key")
- back()
- end
- turtle.placeDown()
- print("Covered hole.")
- end
- function home()
- print("Going home.")
- oldz = dataList.z
- oldx = dataList.x
- oldy = dataList.y
- oldside = side
- up()
- while dataList.side ~= 2 do
- turn(1)
- end
- forward(x)
- turn(1)
- forward(y)
- while dataList.side ~= 0 do
- turn(1)
- end
- end
- function back()
- print("Going back to work.")
- if fuelcheck((oldx+oldy+oldz)*2) then
- while dataList.side ~= 0 do
- turn(1)
- end
- forward(oldx)
- turn(1)
- forward(oldy)
- down(oldz)
- while dataList.side ~= oldside do
- turn(1)
- end
- else
- print("Not enough fuel!")
- refuel()
- end
- end
- function count()
- local count = 0
- local slot
- for slot=1, 16, 1 do
- count = count + turtle.getItemCount(slot)
- end
- return count
- end
- function clean()
- local slot
- local i
- local items = 0
- for slot=1, 16, 1 do
- if turtle.getItemCount(slot) > 0 then
- items = items+1
- end
- end
- if items == 16 then
- for slot=1, 16, 1 do
- turtle.select(slot)
- if turtle.getItemCount(slot) > 0 then
- for i=slot+1, 16, 1 do
- if turtle.compareTo(i) then
- turtle.select(i)
- turtle.transferTo(slot)
- turtle.select(slot)
- end
- end
- end
- end
- end
- end
- --turns around and checks for blocks to mine
- function miner()
- local i
- for i=0, 3, 1 do
- local test = 0
- local slot
- if turtle.detect() then
- for slot=1, ignore ,1 do
- turtle.select(slot)
- if turtle.compare() then
- test = test+1
- end
- end
- if test == 0 then
- local ammount = count()
- clean()
- turtle.dig()
- if ammount == count() then
- drop()
- end
- end
- end
- turn(1)
- end
- end
- --digs hole
- function dig()
- if fuelcheck(140) then
- while down(1) do
- miner()
- end
- up()
- cover()
- else
- print("Not enough fuel!")
- refuel()
- end
- end
- --makes the pattern for the holes
- function mover()
- if fuelcheck(10) then
- if side == 0 then
- if dataList.x+5 < size then
- forward(5)
- elseif dataList.y+1 <= size then
- local dist = size-dataList.x
- forward(dist)
- turn(1)
- forward(1)
- turn(1)
- if (dataList.x-(dist+3)%5) > 0 then
- forward((dist+3)%5)
- else
- mover()
- end
- else
- return false
- end
- elseif dataList.side == 2 then
- if dataList.x-5 > 0 then
- forward(5)
- elseif dataList.y+1 <= size then
- local dist = dataList.x
- forward(x)
- turn(3)
- forward(1)
- turn(3)
- if (dataList.x-(dist+2)%5) < size then
- forward((dist+2)%5)
- else
- mover()
- end
- else
- return false
- end
- end
- else
- print("Not enough fuel!")
- refuel()
- end
- return true
- end
- --main process
- function process()
- start()
- dig()
- while mover() do
- dig()
- end
- home()
- drop()
- print("I've mined "..total.." items.")
- end
- process()
Advertisement
Add Comment
Please, Sign In to add comment