Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to set the heading of the turtle --
- 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())
- local heading = ""
- if(turtle.detect()) then
- 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 get the heading of the turtle --
- function getHeading()
- if(turtle.getFuelLevel() < 2) then
- error("Insufficient fuel")
- end
- local start = vector.new(gps.locate())
- local heading = ""
- if(turtle.detect()) then
- 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 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
- local 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
- if(turtle.detectUp()) then
- turtle.digUp()
- end
- turtle.up()
- else
- if(turtle.detectDown()) then
- 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
- if(turtle.detect()) then
- 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
- if(turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- current = vector.new(gps.locate())
- end
- return heading
- end
- -- 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 mine entire ore veins --
- function mineVein(start,moves,back)
- local current = vector.new(gps.locate())
- if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
- if(not consumeFuel(400)) then
- return false
- end
- end
- if(not invSpace()) then
- sortInv()
- if(not invSpace()) then
- return false
- end
- end
- local success,data = turtle.inspect()
- if(success) then
- local istr = string.sub(data.name,string.find(data.name,":",0)+1)
- print(istr)
- if(istr == "iron_ore") then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false)
- turtle.back()
- end
- end
- 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(istr == "iron_ore") then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false)
- 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(istr == "iron_ore") then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false)
- 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(istr == "iron_ore") then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false)
- 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(istr == "iron_ore") then
- turtle.digUp()
- turtle.up()
- mineVein(start,moves+1,true)
- 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(istr == "iron_ore") then
- turtle.digDown()
- turtle.down()
- mineVein(start,moves+1,true)
- 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(istr == "iron_ore") then
- turtle.dig()
- turtle.forward()
- mineVein(start,moves+1,false)
- 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(istr == "iron_ore") then
- turtle.digUp()
- turtle.up()
- mineVein(start,moves+1,true)
- 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(istr == "iron_ore") then
- turtle.digDown()
- turtle.down()
- mineVein(start,moves+1,true)
- turtle.up()
- end
- end
- --
- end
- return true
- end
- -- Starting variables --
- -- (These are only temporary) --
- level = 12
- limit = 16
- --|-----------------------|--
- --| ** Main function ** |--
- --V-----------------------V--
- -- Check if GPS_DATA file exists --
- if(fs.exists("GPS_DATA")) then
- -- GPS_DATA exists --
- -- Check validity of GPS_DATA --
- 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,")")
- -- Ensure y level is the same as starting y level --
- if(not sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
- if(not consumeFuel(400)) then
- error("Insufficient fuel!")
- end
- end
- local cHeading = getHeading()
- print("Heading: ", cHeading)
- if(start.y ~= current.y) then
- -- if y level is not the same --
- cHeading = goToLocation(vector.new(current.x,start.y,current.z), cHeading)
- current = vector.new(gps.locate())
- end
- -- Ensure z level is the same as starting y level --
- if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's') then
- if(start.z ~= current.z) then
- -- if z level is not the same --
- cHeading = goToLocation(vector.new(start.x,current.y,current.z), cHeading)
- current = vector.new(gps.locate())
- end
- end
- -- Ensure x level is the same as starting y level --
- if(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then
- if(start.x ~= current.x) then
- -- if x level is not the same --
- cHeading = goToLocation(vector.new(current.x,current.y,start.z), cHeading)
- current = vector.new(gps.locate())
- end
- end
- setHeading(cHeading,sHeading)
- cHeading = sHeading
- done = false
- while(done == false) 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 --
- print("Could not establish GPS signal, please ensure GPS servers are running and try again")
- error()
- else
- -- Ensure distance from start is less than limit --
- -- Along the North/South line --
- if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's')then
- -- Check if distance from start is within limit --
- if(math.abs(start.z - current.z)>limit) then
- -- Distance from start is not within limit --
- cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- else
- -- Distance from start is within limit --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
- if(invSpace()) then
- if(current.y == start.y) then
- --checkSides()
- if(turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- --checkSides()
- if(turtle.detectUp()) then
- turtle.digUp()
- end
- turtle.up()
- elseif(current.y - 1 == start.y) then
- --checkSides()
- if(turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- --checkSides()
- if(turtle.detectDown()) then
- turtle.digDown()
- end
- turtle.down()
- else
- cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading)
- end
- else
- sortInv()
- if(not invSpace()) then
- cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- end
- end
- else
- if(not consumeFuel(400)) then
- cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- end
- end
- end
- -- Along the East/West line --
- elseif(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then
- -- Check if distance from start is within limit --
- if(math.abs(start.x - current.x)>limit) then
- -- Distance from start is not within limit --
- cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- else
- -- Distance from start is within limit --
- -- Check for sufficient fuel --
- if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
- if(invSpace()) then
- if(current.y == start.y) then
- --checkSides()
- if(turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- --checkSides()
- if(turtle.detectUp()) then
- turtle.digUp()
- end
- turtle.up()
- elseif(current.y - 1 == start.y) then
- --checkSides()
- if(turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- --checkSides()
- if(turtle.detectDown()) then
- turtle.digDown()
- end
- turtle.down()
- else
- cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading)
- end
- else
- sortInv()
- if(not invSpace()) then
- cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- end
- end
- else
- if(not consumeFuel(400)) then
- cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
- cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
- done = true
- end
- end
- end
- end
- end
- end
- print("Mining complete")
- end
- end
- else
- -- GPS_DATA not found --
- term.clear()
- term.setCursorPos(1,1)
- print("File 'GPS_DATA' does not exist, please run program to initiate mining.")
- error()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement