Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Feel free to use any code or functions in the following paste under the condition
- that credit is given where credit is due.
- All code has been made by minecraft:montana_1
- For comments or concerns, please email [email protected]
- Thanks!
- ]]--
- --[[
- Code to-do list:
- *Auto chest deposit
- *Auto torch placement
- *Rednet flags
- *Main tunnel function
- *Finish branch function (located in another paste "CMining Rev 2" currently)
- *Various others
- Newest Features:
- *Ore priorities
- ]]--
- -- Function to split string values into tables --
- function split(inputstr, sep)
- if(inputstr == nil or inputstr == "") then
- return nil
- end
- if sep == nil then
- sep = ","
- end
- local t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- -- Function to add elements to priorities list --
- function addList(name,mine,flag,drop,vein)
- if(fs.exists("listed")) then
- xlist = fs.open("listed","r")
- local x = split(xlist.readLine())
- while(x[1] ~= nil) do
- if(name == x[1]) then
- return false
- end
- s = xlist.readLine()
- if(s ~= nil) then
- x = split(s)
- else
- break
- end
- end
- xlist.close()
- xlist = fs.open("listed","a")
- else
- xlist = fs.open("listed","w")
- end
- xlist.writeLine(name..","..mine..","..flag..","..drop..","..vein)
- xlist.close()
- return true
- end
- -- Function to get priorities list in file format as a list --
- function lstToTable()
- if(fs.exists("listed")) then
- ifile = fs.open("listed","r")
- xlist = {}
- x = split(ifile.readLine())
- while(x ~= nil and x ~= "") do
- table.insert(xlist, x)
- x = split(ifile.readLine())
- end
- else
- -- name,mine,flag,drop,vein
- xlist = {
- {'iron_ore','1','0','0','1'},
- {'coal_ore','1','0','0','1'},
- {'gold_ore','1','0','0','1'},
- {'lapis_ore','1','0','0','1'},
- {'redstone_ore','1','0','0','1'},
- {'lit_redstone_ore','1','0','0','1'},
- {'diamond_ore','1','0','0','1'},
- {'quartz_ore','1','0','0','1'},
- {'emerald_ore','1','0','0','1'}
- }
- for k,v in pairs(xlist) do
- addList(v[1],v[2],v[3],v[4],v[5])
- end
- end
- return xlist
- end
- -- Function to clear turtle inventory according to priorities list --
- function clrInv(xtable)
- for i = 1, 16 do -- loop through the slots
- turtle.select(i)
- local x = turtle.getItemDetail(i)
- if(x ~= nil) then
- local istr = string.sub(x.name,string.find(x.name,":",0)+1)
- for key,value in pairs(xtable) do
- if(istr == value[1] and value[4] == "1") then
- turtle.drop()
- end
- end
- end
- end
- turtle.select(1)
- end
- -- Function to sort turtle inventory --
- function sortInv()
- for i = 1, 16 do -- loop through the slots
- turtle.select(i)
- if(turtle.getItemDetail(i) ~= nil) then
- for c = i, 16 do
- if(turtle.getItemDetail(c) ~= nil) then
- if(turtle.compareTo(c)) then
- turtle.select(c)
- turtle.transferTo(i)
- turtle.select(i)
- end
- end
- end
- end
- end
- turtle.select(1)
- end
- -- Function to check for turtle inventory space --
- function invSpace()
- for i = 1, 16 do
- turtle.select(i)
- local x = turtle.getItemDetail(i)
- if(x == nil or turtle.compare()) then
- turtle.select(1)
- return true
- end
- end
- turtle.select(1)
- return false
- end
- -- Function to check for sufficient turtle fuel --
- function sufficientFuel(vector1,vector2,fuel,remainder)
- local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z)
- if(fuel <= distance + remainder) then
- return false
- else
- return true
- end
- end
- -- Function to consume turtle fuel --
- function consumeFuel(maxFuel) -- Optionally add more fuel types and prioritise fuel types
- local refuel = false
- for i = 1, 16 do -- loop through the slots
- turtle.select(i) -- change to the slot
- local x = turtle.getItemDetail(i)
- if(x ~= nil) then
- local istr = string.sub(x.name,string.find(x.name,":",0)+1)
- if(istr == "planks" or istr == "stick" or istr == "log") then
- turtle.refuel()
- refuel = true
- end
- if(turtle.getFuelLevel() < maxFuel and istr == "coal") then
- turtle.refuel(1)
- refuel = true
- end
- end
- end
- turtle.select(1)
- --print(turtle.getFuelLevel())
- return refuel
- end
- -- Function to place torch --
- function placeTorch(direction) -- Optionally add functionality to place torch when no torch can be placed
- local torch = false
- for i = 1, 16 do -- loop through the slots
- turtle.select(i) -- change to the slot
- local x = turtle.getItemDetail(i)
- if(x ~= nil) then
- local istr = string.sub(x.name,string.find(x.name,":",0)+1)
- if(istr == "torch") then
- if(direction == "up") then
- if(turtle.placeUp()) then
- torch = true
- turtle.select(1)
- end
- elseif(direction == "down")then
- if(turtle.placeDown()) then
- torch = true
- turtle.select(1)
- end
- else
- if(turtle.place()) then
- torch = true
- turtle.select(1)
- end
- end
- end
- end
- end
- turtle.select(1)
- return torch
- end
- -- Function to get turtle heading --
- function getHeading()
- if(turtle.getFuelLevel() < 2) then
- if(not consumeFuel(400)) then
- error("Insufficient fuel")
- end
- end
- local start = vector.new(gps.locate())
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- local current = vector.new(gps.locate())
- turtle.back()
- if(start.z - current.z > 0) then
- heading = 'N'
- elseif (start.z - current.z < 0) then
- heading = 'S'
- end
- if(start.x - current.x > 0) then
- heading = 'W'
- elseif (start.x - current.x < 0) then
- heading = 'E'
- end
- return heading
- end
- -- Function to set turtle heading --
- function setHeading(heading,newHeading)
- if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
- if(turtle.getFuelLevel() < 2) then
- error("Insufficient fuel")
- end
- local start = vector.new(gps.locate())
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- local current = vector.new(gps.locate())
- turtle.back()
- if(start.z - current.z > 0) then
- heading = 'N'
- elseif (start.z - current.z < 0) then
- heading = 'S'
- end
- if(start.x - current.x > 0) then
- heading = 'W'
- elseif (start.x - current.x < 0) then
- heading = 'E'
- end
- end
- if(heading ~= newHeading) then
- if(newHeading == 'N' or newHeading == 'n') then
- if(heading == 'S' or heading == 's') then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- if(heading == 'E' or heading == 'e') then
- turtle.turnLeft()
- end
- if(heading == 'W' or heading == 'w') then
- turtle.turnRight()
- end
- end
- if(newHeading == 'E' or newHeading == 'e') then
- if(heading == 'S' or heading == 's') then
- turtle.turnLeft()
- end
- if(heading == 'N' or heading == 'n') then
- turtle.turnRight()
- end
- if(heading == 'W' or heading == 'w') then
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- if(newHeading == 'S' or newHeading == 's') then
- if(heading == 'N' or heading == 'n') then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- if(heading == 'E' or heading == 'e') then
- turtle.turnRight()
- end
- if(heading == 'W' or heading == 'w') then
- turtle.turnLeft()
- end
- end
- if(newHeading == 'W' or newHeading == 'w') then
- if(heading == 'S' or heading == 's') then
- turtle.turnRight()
- end
- if(heading == 'E' or heading == 'e') then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- if(heading == 'N' or heading == 'n') then
- turtle.turnLeft()
- end
- end
- end
- end
- -- Function to go to a specific location --
- function goToLocation(location,heading)
- if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
- heading = getHeading()
- end
- local current = vector.new(gps.locate())
- if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then
- if(turtle.getFuelLevel() < 2) then
- error("Insufficient fuel")
- end
- end
- while(location.y ~= current.y) do
- if(location.y > current.y) then
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- else
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- end
- current = vector.new(gps.locate())
- end
- while(location.z ~= current.z) do
- if(location.z > current.z) then
- setHeading(heading,'S')
- heading = 'S'
- elseif(location.z < current.z) then
- setHeading(heading,'N')
- heading = 'N'
- end
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- current = vector.new(gps.locate())
- end
- while(location.x ~= current.x) do
- if(location.x > current.x) then
- setHeading(heading,'E')
- heading = 'E'
- elseif(location.x < current.x) then
- setHeading(heading,'W')
- heading = 'W'
- end
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- current = vector.new(gps.locate())
- end
- return heading
- end
- -- Function to decide whether to mine whole ore vein based on priorities list --
- function shouldMineWhole(istr,xlist) -- must add functionality for flagging ores
- for key,value in pairs(xlist) do
- if(istr == value[1] and value[5] == "1") then
- return true
- end
- end
- return false
- end
- -- Function to mine entire ore pocket --
- function mineVein(start,moves,back,xtable) -- This function needs work on flagging ore, also could be significantly more efficient
- --Establish current GPS location--
- local current = vector.new(gps.locate())
- --Check for sufficient fuel, if not, try to refuel, if refuel fails, function return false and
- --recursion tree will collapse with turtle returning to where it started--
- if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
- if(not consumeFuel(400)) then
- return -2
- end
- end
- --Check for inventory space, if no inventory space, try to create some. if no space can be created,
- --function return false and recursion tree will collapse, with turtle returning to where it started.
- if(not invSpace()) then
- sortInv()
- clrInv(xtable)
- if(not invSpace()) then
- return -1
- end
- end
- --Check above turtle for ores--
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false,xtable)
- turtle.back()
- end
- end
- if(moves == 0) then
- if(current.y == start.y + 1) then
- local success,data = turtle.inspectUp()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digUp()
- turtle.up()
- mineVein(start,moves+1,true,xtable)
- turtle.down()
- end
- end
- end
- if(current.y == start.y) then
- local success,data = turtle.inspectDown()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digDown()
- turtle.down()
- mineVein(start,moves+1,true,xtable)
- turtle.up()
- end
- end
- end
- end
- --will ensure turtle does not check sides on start.
- if(moves < 1) then
- return 1
- end
- turtle.turnLeft()
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false,xtable)
- turtle.back()
- end
- end
- if(back) then
- turtle.turnLeft()
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false,xtable)
- turtle.back()
- end
- end
- turtle.turnLeft()
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false,xtable)
- turtle.back()
- end
- end
- turtle.turnLeft()
- local success,data = turtle.inspectUp()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digUp()
- turtle.up()
- mineVein(start,moves+1,true,xtable)
- turtle.down()
- end
- end
- local success,data = turtle.inspectDown()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digDown()
- turtle.down()
- mineVein(start,moves+1,true,xtable)
- turtle.up()
- end
- end
- else
- turtle.turnRight()
- turtle.turnRight()
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false,xtable)
- turtle.back()
- end
- end
- turtle.turnLeft()
- local success,data = turtle.inspectUp()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digUp()
- turtle.up()
- mineVein(start,moves+1,true,xtable)
- turtle.down()
- end
- end
- local success,data = turtle.inspectDown()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- --print(istr)
- if(shouldMineWhole(istr,xtable)) then
- turtle.digDown()
- turtle.down()
- mineVein(start,moves+1,true,xtable)
- turtle.up()
- end
- end
- end
- return 1
- end
- -- Function to mine branch --
- function mineBranch(branchStart,branchHeading,currentHeading,branchLimit,fuelRemainder,plist,torchLength) -- Still needs optimization and functions to replace repeated blocks
- -- Search for GPS signal --
- local current = vector.new(gps.locate())
- if(current.x == nil or current.y == nil or current.z == nil) then
- -- GPS signal could not be established --
- error("Could not establish GPS signal, please ensure GPS servers are running and try again")
- end
- -- GPS signal established --
- if(not sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
- if(not consumeFuel(4000)) then
- error("Insufficient fuel!")
- end
- end
- if(currentHeading ~= 'N' and currentHeading ~= 'n' and currentHeading ~= 'E' and currentHeading ~= 'e' and currentHeading ~= 'S' and currentHeading ~= 's' and currentHeading ~= 'W' and currentHeading ~= 'w') then
- currentHeading = getHeading()
- end
- --print("Heading: ", currentHeading)
- if(branchStart.y ~= current.y) then
- -- If y level is not the same --
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z), currentHeading)
- current = vector.new(gps.locate())
- end
- -- Ensure z level is the same as starting y level --
- if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's') then
- if(branchStart.z ~= current.z) then
- -- If z level is not the same --
- currentHeading = goToLocation(vector.new(branchStart.x,current.y,current.z), currentHeading)
- current = vector.new(gps.locate())
- end
- end
- -- Ensure x level is the same as starting y level --
- if(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
- if(branchStart.x ~= current.x) then
- -- If x level is not the same --
- currentHeading = goToLocation(vector.new(current.x,current.y,branchStart.z), currentHeading)
- current = vector.new(gps.locate())
- end
- end
- setHeading(currentHeading,branchHeading)
- currentHeading = branchHeading
- --[[
- Done
- 0 : Not done, still mining
- 1 : Is done, mining was successful
- -1 : Inventory full
- -2 : Insufficient fuel
- ]]--
- done = 0
- while(done == 0) do
- current = vector.new(gps.locate())
- if(current.x == nil or current.y == nil or current.z == nil) then
- -- GPS signal could not be established --
- error("Could not establish GPS signal, please ensure GPS servers are running and try again")
- else
- -- Ensure distance from branchStart is less than branchLimit --
- -- Along the North/South line --
- if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's')then
- -- Check if distance from branchStart is within branchLimit --
- if(math.abs(branchStart.z - current.z)>=branchLimit) then
- -- Distance from branchStart is not within branchLimit --
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = 1
- else
- -- Distance from branchStart is within branchLimit --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
- if(invSpace()) then
- if(current.y == branchStart.y) then
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- elseif(current.y - 1 == branchStart.y) then
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- else
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
- end
- else
- sortInv()
- clrInv(plist)
- if(not invSpace()) then
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = -1
- end
- end
- else
- if(not consumeFuel(4000)) then
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = -2
- end
- end
- end
- -- Along the East/West line --
- elseif(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
- -- Check if distance from branchStart is within branchLimit --
- if(math.abs(branchStart.x - current.x)>=branchLimit) then
- -- Distance from branchStart is not within branchLimit --
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = 1
- else
- -- Distance from branchStart is within branchLimit --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
- if(invSpace()) then
- if(current.y == branchStart.y) then
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- elseif(current.y - 1 == branchStart.y) then
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- turtle.turnLeft()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnRight()
- turtle.turnRight()
- if(done == 1) then
- done = mineVein(branchStart,0,false,plist)
- end
- turtle.turnLeft()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- else
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
- end
- else
- sortInv()
- clrInv(plist)
- if(not invSpace()) then
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = -1
- end
- end
- else
- if(not consumeFuel(4000)) then
- currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
- done = -2
- end
- end
- end
- end
- end
- end
- return done
- end
- x = lstToTable()
- print(mineBranch(vector.new(gps.locate()),getHeading(),"",5,10,x,0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement