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 get turtle heading --
- function getHeading()
- 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
- 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 mine tunnel --
- function mTunnel(tunnelStart, tunnelHeading, currentHeading, tunnelLength, fuelRemainder, plist, torchDistance, branchSpacing, branchFunction)
- -- 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,tunnelStart,turtle.getFuelLevel(),fuelRemainder+10)) 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)
- -- Ensure y level is the same as starting y level --
- if(tunnelStart.y ~= current.y) then
- -- If y level is not the same --
- currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,current.z), currentHeading)
- current = vector.new(gps.locate())
- end
- -- Ensure z level is the same as starting z level if on N/S axis --
- if(tunnelHeading == 'N' or tunnelHeading == 'n' or tunnelHeading == 'S' or tunnelHeading == 's') then
- if(tunnelStart.z ~= current.z) then
- -- If z level is not the same --
- currentHeading = goToLocation(vector.new(tunnelStart.x,current.y,current.z), currentHeading)
- setHeading(currentHeading,tunnelHeading)
- currentHeading = tunnelHeading
- turtle.back()
- current = vector.new(gps.locate())
- end
- end
- -- Ensure x level is the same as starting x level if on E/W axis --
- if(tunnelHeading == 'E' or tunnelHeading == 'e' or tunnelHeading == 'W' or tunnelHeading == 'w') then
- if(tunnelStart.x ~= current.x) then
- -- If x level is not the same --
- currentHeading = goToLocation(vector.new(current.x,current.y,tunnelStart.z), currentHeading)
- setHeading(currentHeading,tunnelHeading)
- currentHeading = tunnelHeading
- turtle.back()
- current = vector.new(gps.locate())
- end
- end
- --[[
- 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 tunnelStart is less than tunnelLength --
- -- Along the North/South line --
- if(tunnelHeading == 'N' or tunnelHeading == 'n' or tunnelHeading == 'S' or tunnelHeading == 's')then
- -- Check if distance from tunnelStart is within tunnelLength --
- if(math.abs(tunnelStart.z - current.z)>=tunnelLength) then
- -- Distance from tunnelStart is not within tunnelLength --
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = 1
- else
- -- Distance from tunnelStart is within tunnelLength --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,tunnelStart,turtle.getFuelLevel(),fuelRemainder + 10)) then
- if(invSpace()) then
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- turtle.turnLeft()
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- turtle.back()
- turtle.turnLeft()
- else
- sortInv()
- clrInv(plist)
- if(not invSpace()) then
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = -1
- end
- end
- else
- if(not consumeFuel(4000)) then
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,current.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = -2
- end
- end
- end
- -- Along the East/West line --
- elseif(tunnelHeading == 'E' or tunnelHeading == 'e' or tunnelHeading == 'W' or tunnelHeading == 'w') then
- -- Check if distance from tunnelStart is within tunnelLength --
- if(math.abs(tunnelStart.x - current.x)>=tunnelLength) then
- -- Distance from tunnelStart is not within tunnelLength --
- currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = 1
- else
- -- Distance from tunnelStart is within tunnelLength --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,tunnelStart,turtle.getFuelLevel(),fuelRemainder + 10)) then
- if(invSpace()) then
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- turtle.turnLeft()
- turtle.turnLeft()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- turtle.back()
- turtle.turnLeft()
- else
- sortInv()
- clrInv(plist)
- if(not invSpace()) then
- currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = -1
- end
- end
- else
- if(not consumeFuel(4000)) then
- currentHeading = goToLocation(vector.new(current.x,tunnelStart.y,tunnelStart.z),currentHeading)
- currentHeading = goToLocation(vector.new(tunnelStart.x,tunnelStart.y,tunnelStart.z),currentHeading)
- done = -2
- end
- end
- end
- end
- end
- end
- return done
- end
- print("Enter length: ")
- c = tonumber(read())
- if(fs.exists("GPS_DATA")) then
- gpsData = fs.open("GPS_DATA","r")
- local start = vector.new(tonumber(gpsData.readLine()),tonumber(gpsData.readLine()),tonumber(gpsData.readLine()))
- sHeading = gpsData.readLine()
- if(start.x == nil or start.y == nil or start.z == nil) then
- -- GPS_DATA Invalid --
- term.clear()
- term.setCursorPos(1,1)
- error("Invalid GPS information")
- else
- -- GPS_DATA Valid --
- term.clear()
- term.setCursorPos(1,1)
- print("GPS_DATA exists, start: (",start.x,",",start.y,",",start.z,")")
- print("Heading: ", sHeading)
- -- 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")
- else
- -- GPS signal established --
- print("GPS locate, current: (",current.x,",",current.y,",",current.z,")")
- print(mTunnel(start, sHeading, "", c, 10, lstToTable(), 0, 0, 0))
- end
- end
- else
- -- GPS_DATA not found --
- term.clear()
- term.setCursorPos(1,1)
- error("File 'GPS_DATA' does not exist, please run program to initiate mining")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement