Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check if turtle has sufficient 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 refuel turtle appropriately --
- function consumeFuel(maxFuel)
- local refuel = false
- for i = 1, 16 do
- turtle.select(i)
- local x = turtle.getItemDetail(i)
- if(x ~= nil) then
- local istr = string.sub(x.name,string.find(x.name,":",0)+1)
- print(istr," t")
- 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 check for inv 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 sort 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 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 decide whether to mine whole ore vein based on priorities list --
- function shouldMineWhole(istr,xlist)
- for key,value in pairs(xlist) do
- if(istr == value[1] and value[5] == "1") then
- return true
- end
- end
- return false
- end
- function mineVein(start,moves,back,xtable)
- --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 false
- 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 false
- 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 true
- 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 true
- end
- gpsData = fs.open("GPS_DATA","r")
- local start = vector.new(gps.locate())
- 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
- print("Listed file does not exist, cannot continue")
- end
- mineVein(start,0,false,xlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement